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