cis_config: /root/cis_interface/cis_interface/cis_interface/serialize/FormatSerialize.h Source File

cis_config

FormatSerialize.h
1 #ifndef CISFORMATSERIALIZE_H_
2 #define CISFORMATSERIALIZE_H_
3
4 #include <../tools.h>
5 #include <SerializeBase.h>
6 #include <../dataio/AsciiTable.h>
7
8 #ifdef __cplusplus /* If this is a C++ compiler, use C linkage */
9 extern "C" {
10 #endif
11
22 static inline
23 int serialize_format(const seri_t s, char *buf, const size_t buf_siz,
24 int *args_used, va_list ap) {
25 args_used[0] = 0;
26 char *fmt = (char*)s.info;
27 int ret = vsnprintf(buf, buf_siz, fmt, ap);
28 cislog_debug("serialize_format: vsnprintf returns %d", ret);
29 if (ret < 0) {
30 cislog_error("serialize_format: vsnprintf encoding error");
31 ret = -1;
32 } else {
33 args_used[0] = count_formats(fmt);
34 }
35 return ret;
36 };
37
46 static inline
47 int deserialize_format(const seri_t s, const char *buf, const size_t buf_siz,
48 va_list ap) {
49 // Prevent C4100 warning on windows by referencing param
50 #ifdef _WIN32
51 buf_siz;
52 #endif
53 // Simplify format
54 char *fmt0 = (char*)s.info;
55 size_t fmt_siz = 2*strlen(fmt0) + 1;
56 char *fmt = (char*)malloc(sizeof(char) * fmt_siz);
57 if (fmt == NULL) {
58 cislog_error("deserialize_format: Failed to allocate buffer for simplified format.");
59 return -1;
60 }
61 strcpy(fmt, fmt0);
62 int sret = simplify_formats(fmt, fmt_siz);
63 if (sret < 0) {
64 cislog_error("deserialize_format: simplify_formats returned %d", sret);
65 free(fmt);
66 return -1;
67 }
68 cislog_debug("deserialize_format: simplify_formats returns %d", sret);
69 int nfmt = count_formats(fmt);
70 cislog_debug("deserialize_format: Simplified format contains %d fields", nfmt);
71 // Interpret message
72 sret = vsscanf(buf, fmt, ap);
73 if (sret != nfmt) {
74 cislog_error("deserialize_format: vsscanf filled %d variables, but there are %d formats",
75 sret, nfmt);
76 free(fmt);
77 return -1;
78 }
79 cislog_debug("deserialize_format: vsscanf returns %d", sret);
80 free(fmt);
81 return sret;
82 };
83
84
85 #ifdef __cplusplus /* If this is a C++ compiler, end C linkage */
86 }
87 #endif
88
89 #endif /*CISFORMATSERIALIZE_H_*/
seri_t
Serializer structure.
Definition: SerializeBase.h:19