1 #include "xmlwriter.hpp"
5 XMLWriter::XMLWriter(bool schema_only
) :
6 indent_level(0), schema_only(schema_only
)
10 void XMLWriter::marshal_int8(const char *name
, int8_t *obj
)
12 if (this->schema_only
) {
13 this->emit("<int8%s/>", this->name(name
));
15 this->emit("<int8%s>%u</int8>", this->name(name
), *obj
);
20 void XMLWriter::marshal_int16(const char *name
, int16_t *obj
)
22 if (this->schema_only
) {
23 this->emit("<int16%s/>", this->name(name
));
25 this->emit("<int16%s>%u</int16>", this->name(name
), *obj
);
29 void XMLWriter::marshal_int32(const char *name
, int32_t *obj
)
31 if (this->schema_only
) {
32 this->emit("<int32%s/>", this->name(name
));
34 this->emit("<int32%s>%u</int32>", this->name(name
), *obj
);
38 void XMLWriter::marshal_int64(const char *name
, int64_t *obj
)
40 if (this->schema_only
) {
41 this->emit("<int64%s/>", this->name(name
));
43 this->emit("<int64%s>%lu</int64>", this->name(name
), *obj
);
47 void XMLWriter::marshal_uint8(const char *name
, uint8_t *obj
)
49 if (this->schema_only
) {
50 this->emit("<uint8%s/>", this->name(name
));
52 this->emit("<uint8%s>%u</uint8>", this->name(name
), *obj
);
56 void XMLWriter::marshal_uint16(const char *name
, uint16_t *obj
)
58 if (this->schema_only
) {
59 this->emit("<uint16%s/>", this->name(name
));
61 this->emit("<uint16%s>%u</uint16>", this->name(name
), *obj
);
65 void XMLWriter::marshal_uint32(const char *name
, uint32_t *obj
)
67 if (this->schema_only
) {
68 this->emit("<uint32%s/>", this->name(name
));
70 this->emit("<uint32%s>%u</uint32>", this->name(name
), *obj
);
74 void XMLWriter::marshal_uint64(const char *name
, uint64_t *obj
)
76 if (this->schema_only
) {
77 this->emit("<uint64%s/>", this->name(name
));
79 this->emit("<uint64%s>%lu</uint64>", this->name(name
), *obj
);
83 void XMLWriter::marshal_bool(const char *name
, bool *obj
)
85 if (this->schema_only
) {
86 this->emit("<bool%s/>", this->name(name
));
88 this->emit("<bool%s>%s</bool>", this->name(name
), *obj
? "true" : "false");
92 void XMLWriter::start_struct(const char *name
, const char *type
)
94 this->emit("<struct%s type=\"%s\">", this->name(name
), type
);
95 this->indent_level
+= 4;
98 void XMLWriter::end_struct(void)
100 this->indent_level
-= 4;
101 this->emit("</struct>");
104 void XMLWriter::start_array(const char *name
)
106 this->emit("<array%s>", this->name(name
));
107 this->indent_level
+= 4;
110 void XMLWriter::end_array(void)
112 this->indent_level
-= 4;
113 this->emit("</array>");
116 const std::string
&XMLWriter::as_string(void)
121 const char *XMLWriter::indent(void)
125 this->indent_buf
= "";
127 for (i
= 0; i
< indent_level
; i
++) {
128 this->indent_buf
+= " ";
131 return this->indent_buf
.c_str();
134 const char *XMLWriter::name(const char *str
)
137 this->name_buf
= " name=\"";
138 this->name_buf
+= str
;
139 this->name_buf
+= "\"";
144 return this->name_buf
.c_str();
147 void XMLWriter::emit(const char *fmt
, ...)
153 vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
156 this->buffer
+= this->indent();
158 this->buffer
+= "\n";