Line data Source code
1 : /*
2 : * Copyright (c) 2017 Juniper Networks, Inc. All rights reserved.
3 : */
4 : #ifndef AUTOGEN_UTIL_H_
5 : #define AUTOGEN_UTIL_H_
6 :
7 : #include <stdint.h>
8 : #include <time.h>
9 :
10 : #include <sstream>
11 : #include <string>
12 :
13 : #include <boost/algorithm/string/trim.hpp>
14 : #include <pugixml/pugixml.hpp>
15 : #include "rapidjson/rapidjson.h"
16 : #include "rapidjson/document.h"
17 :
18 : using namespace contrail_rapidjson;
19 : using namespace pugi;
20 : using namespace std;
21 :
22 : #include "base/compiler.h"
23 : #if defined(__GNUC__) && (__GCC_HAS_PRAGMA > 0)
24 : #pragma GCC diagnostic ignored "-Wunused-function"
25 : #endif
26 :
27 : namespace autogen {
28 :
29 : // Json Parse routines
30 :
31 44419 : static bool ParseString(const contrail_rapidjson::Value &node, std::string *s) {
32 44419 : if (node.IsString()) {
33 44419 : *s = node.GetString();
34 44419 : return true;
35 : }
36 :
37 0 : std::stringstream ss;
38 0 : switch (node.GetType()) {
39 0 : case contrail_rapidjson::kNullType:
40 0 : *s = "null";
41 0 : break;
42 0 : case contrail_rapidjson::kTrueType:
43 0 : *s = "true";
44 0 : break;
45 0 : case contrail_rapidjson::kFalseType:
46 0 : *s = "false";
47 0 : break;
48 0 : case contrail_rapidjson::kStringType:
49 0 : *s = node.GetString();
50 0 : break;
51 0 : case contrail_rapidjson::kNumberType:
52 0 : if (node.IsUint())
53 0 : ss << node.GetUint();
54 0 : else if (node.IsInt())
55 0 : ss << node.GetInt();
56 0 : else if (node.IsUint64())
57 0 : ss << node.GetUint64();
58 0 : else if (node.IsInt64())
59 0 : ss << node.GetInt64();
60 0 : else if (node.IsDouble())
61 0 : ss << node.GetDouble();
62 0 : *s = ss.str();
63 0 : break;
64 0 : case contrail_rapidjson::kObjectType:
65 0 : return false;
66 0 : case contrail_rapidjson::kArrayType:
67 0 : return false;
68 : }
69 0 : return true;
70 0 : }
71 :
72 17936787 : static bool ParseInteger(const char *nptr, int *valuep) {
73 : char *endp;
74 17936787 : *valuep = strtoul(nptr, &endp, 10);
75 17939963 : while (isspace(*endp))
76 3159 : endp++;
77 17936804 : return (endp[0] == '\0');
78 : }
79 :
80 50158 : static bool ParseUnsignedLong(const char *nptr, uint64_t *valuep) {
81 : char *endp;
82 50158 : *valuep = strtoull(nptr, &endp, 10);
83 50158 : while (isspace(*endp))
84 0 : endp++;
85 50158 : return (endp[0] == '\0');
86 : }
87 :
88 3218080 : static bool ParseBoolean(const char *bptr, bool *valuep) {
89 3218080 : if (strcmp(bptr, "true") ==0)
90 1549995 : *valuep = true;
91 : else
92 1668085 : *valuep = false;
93 3218080 : return true;
94 : }
95 :
96 1550834 : static bool ParseInteger(const pugi::xml_attribute &attr, int *valuep) {
97 1550834 : return ParseInteger(attr.value(), valuep);
98 : }
99 :
100 0 : static bool ParseUnsignedLong(const pugi::xml_attribute &attr,
101 : uint64_t *valuep) {
102 0 : return ParseUnsignedLong(attr.value(), valuep);
103 : }
104 :
105 1550833 : static bool ParseBoolean(const pugi::xml_attribute &attr, bool *valuep) {
106 1550833 : return ParseBoolean(attr.value(), valuep);
107 : }
108 :
109 16380723 : static bool ParseInteger(const pugi::xml_node &node, int *valuep) {
110 16380723 : return ParseInteger(node.child_value(), valuep);
111 : }
112 :
113 46360 : static bool ParseUnsignedLong(const pugi::xml_node &node, uint64_t *valuep) {
114 46360 : return ParseUnsignedLong(node.child_value(), valuep);
115 : }
116 :
117 1665644 : static bool ParseBoolean(const pugi::xml_node &node, bool *valuep) {
118 1665644 : return ParseBoolean(node.child_value(), valuep);
119 : }
120 :
121 646 : static bool ParseDateTime(const pugi::xml_node &node, time_t *valuep) {
122 646 : string value(node.child_value());
123 646 : boost::trim(value);
124 : struct tm tm;
125 : char *endp;
126 646 : memset(&tm, 0, sizeof(tm));
127 646 : if (value.size() == 0) return true;
128 646 : endp = strptime(value.c_str(), "%FT%T", &tm);
129 646 : if (!endp) return false;
130 646 : *valuep = timegm(&tm);
131 646 : return true;
132 646 : }
133 0 : static bool ParseTime(const pugi::xml_node &node, time_t *valuep) {
134 0 : string value(node.child_value());
135 0 : boost::trim(value);
136 : struct tm tm;
137 : char *endp;
138 0 : endp = strptime(value.c_str(), "%T", &tm);
139 0 : if (!endp) return false;
140 0 : *valuep = timegm(&tm);
141 0 : return true;
142 0 : }
143 1296 : static std::string FormatDateTime(const time_t *valuep) {
144 : struct tm tm;
145 : char result[100];
146 1296 : gmtime_r(valuep, &tm);
147 1296 : strftime(result, sizeof(result), "%FT%T", &tm);
148 1296 : return std::string(result);
149 : }
150 0 : static std::string FormatTime(const time_t *valuep) {
151 : struct tm tm;
152 : char result[100];
153 0 : gmtime_r(valuep, &tm);
154 0 : strftime(result, sizeof(result), "%T", &tm);
155 0 : return std::string(result);
156 : }
157 :
158 : // Json Parse routines
159 8128 : static bool ParseInteger(const contrail_rapidjson::Value &node, int *valuep) {
160 8128 : if (node.IsString())
161 5264 : return ParseInteger(node.GetString(), valuep);
162 2864 : if (node.IsInt()) {
163 2864 : *valuep = node.GetInt();
164 2864 : return true;
165 : }
166 : // We need to check for Uint also because Int value becomes Uint in json
167 : // if MSB is set
168 0 : if (node.IsUint()) {
169 0 : *valuep = node.GetUint();
170 0 : return true;
171 : }
172 0 : return false;
173 : }
174 :
175 4997 : static bool ParseUnsignedLong(const contrail_rapidjson::Value &node, uint64_t *valuep) {
176 4997 : if (node.IsString())
177 3798 : return ParseUnsignedLong(node.GetString(), valuep);
178 1199 : if (!node.IsUint64())
179 0 : return false;
180 1199 : *valuep = node.GetUint64();
181 1199 : return true;
182 : }
183 :
184 3972 : static bool ParseBoolean(const contrail_rapidjson::Value &node, bool *valuep) {
185 3972 : if (node.IsString())
186 1604 : return ParseBoolean(node.GetString(), valuep);
187 2368 : if (!node.IsBool())
188 0 : return false;
189 2368 : *valuep = node.GetBool();
190 2368 : return true;
191 : }
192 :
193 1200 : static bool ParseDateTime(const contrail_rapidjson::Value &node, time_t *valuep) {
194 1200 : if (!node.IsString())
195 0 : return false;
196 1200 : string value(node.GetString());
197 1200 : boost::trim(value);
198 : struct tm tm;
199 : char *endp;
200 1198 : memset(&tm, 0, sizeof(tm));
201 1198 : if (value.size() == 0) return true;
202 1198 : endp = strptime(value.c_str(), "%FT%T", &tm);
203 1200 : if (!endp) return false;
204 1200 : *valuep = timegm(&tm);
205 1200 : return true;
206 1200 : }
207 :
208 0 : static bool ParseTime(const contrail_rapidjson::Value &node, time_t *valuep) {
209 0 : if (!node.IsString())
210 0 : return false;
211 0 : string value(node.GetString());
212 0 : boost::trim(value);
213 : struct tm tm;
214 : char *endp;
215 0 : endp = strptime(value.c_str(), "%T", &tm);
216 0 : if (!endp) return false;
217 0 : *valuep = timegm(&tm);
218 0 : return true;
219 0 : }
220 :
221 : } // namespace autogen
222 :
223 : #endif // AUTOGEN_UTIL_H_
|