5 #ifndef AUTOGEN_UTIL_H_
6 #define AUTOGEN_UTIL_H_
14 #include <boost/algorithm/string/trim.hpp>
15 #include <pugixml/pugixml.hpp>
16 #include "rapidjson/rapidjson.h"
17 #include "rapidjson/document.h"
19 using namespace contrail_rapidjson;
24 #if defined(__GNUC__) && (__GCC_HAS_PRAGMA > 0)
25 #pragma GCC diagnostic ignored "-Wunused-function"
32 static bool ParseString(
const contrail_rapidjson::Value &node, std::string *s) {
33 if (node.IsString()) {
34 *s = node.GetString();
39 switch (node.GetType()) {
40 case contrail_rapidjson::kNullType:
43 case contrail_rapidjson::kTrueType:
46 case contrail_rapidjson::kFalseType:
49 case contrail_rapidjson::kStringType:
50 *s = node.GetString();
52 case contrail_rapidjson::kNumberType:
55 else if (node.IsInt())
57 else if (node.IsUint64())
58 ss << node.GetUint64();
59 else if (node.IsInt64())
60 ss << node.GetInt64();
61 else if (node.IsDouble())
62 ss << node.GetDouble();
65 case contrail_rapidjson::kObjectType:
67 case contrail_rapidjson::kArrayType:
75 *valuep = strtoul(nptr, &endp, 10);
76 while (isspace(*endp))
78 return (endp[0] ==
'\0');
83 *valuep = strtoull(nptr, &endp, 10);
84 while (isspace(*endp))
86 return (endp[0] ==
'\0');
90 if (strcmp(bptr,
"true") ==0)
97 static bool ParseInteger(
const pugi::xml_attribute &attr,
int *valuep) {
106 static bool ParseBoolean(
const pugi::xml_attribute &attr,
bool *valuep) {
123 string value(node.child_value());
127 memset(&tm, 0,
sizeof(tm));
128 if (value.size() == 0)
return true;
129 endp = strptime(value.c_str(),
"%FT%T", &tm);
130 if (!endp)
return false;
131 *valuep = timegm(&tm);
134 static bool ParseTime(
const pugi::xml_node &node, time_t *valuep) {
135 string value(node.child_value());
139 endp = strptime(value.c_str(),
"%T", &tm);
140 if (!endp)
return false;
141 *valuep = timegm(&tm);
147 gmtime_r(valuep, &tm);
148 strftime(result,
sizeof(result),
"%FT%T", &tm);
149 return std::string(result);
154 gmtime_r(valuep, &tm);
155 strftime(result,
sizeof(result),
"%T", &tm);
156 return std::string(result);
160 static bool ParseInteger(
const contrail_rapidjson::Value &node,
int *valuep) {
164 *valuep = node.GetInt();
170 *valuep = node.GetUint();
179 if (!node.IsUint64())
181 *valuep = node.GetUint64();
185 static bool ParseBoolean(
const contrail_rapidjson::Value &node,
bool *valuep) {
190 *valuep = node.GetBool();
194 static bool ParseDateTime(
const contrail_rapidjson::Value &node, time_t *valuep) {
195 if (!node.IsString())
197 string value(node.GetString());
201 memset(&tm, 0,
sizeof(tm));
202 if (value.size() == 0)
return true;
203 endp = strptime(value.c_str(),
"%FT%T", &tm);
204 if (!endp)
return false;
205 *valuep = timegm(&tm);
209 static bool ParseTime(
const contrail_rapidjson::Value &node, time_t *valuep) {
210 if (!node.IsString())
212 string value(node.GetString());
216 endp = strptime(value.c_str(),
"%T", &tm);
217 if (!endp)
return false;
218 *valuep = timegm(&tm);
static bool ParseString(const contrail_rapidjson::Value &node, std::string *s)
static std::string FormatDateTime(const time_t *valuep)
static bool ParseTime(const contrail_rapidjson::Value &node, time_t *valuep)
static bool ParseBoolean(const contrail_rapidjson::Value &node, bool *valuep)
static bool ParseDateTime(const contrail_rapidjson::Value &node, time_t *valuep)
static std::string FormatTime(const time_t *valuep)
static bool ParseUnsignedLong(const pugi::xml_node &node, uint64_t *valuep)
static bool ParseInteger(const pugi::xml_node &node, int *valuep)