LCOV - code coverage report
Current view: top level - root/contrail/src/contrail-common/base - autogen_util.h (source / functions) Hit Total Coverage
Test: OpenSDN C/C++ coverage (all TARGET_SET jobs) Lines: 71 136 52.2 %
Date: 2026-11-09 02:10:49 Functions: 15 19 78.9 %
Legend: Lines: hit not hit

          Line data    Source code
       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       44777 : static bool ParseString(const contrail_rapidjson::Value &node, std::string *s) {
      33       44777 :     if (node.IsString()) {
      34       44775 :         *s = node.GetString();
      35       44774 :         return true;
      36             :     }
      37             : 
      38           0 :     std::stringstream ss;
      39           0 :     switch (node.GetType()) {
      40           0 :     case contrail_rapidjson::kNullType:
      41           0 :         *s = "null";
      42           0 :         break;
      43           0 :     case contrail_rapidjson::kTrueType:
      44           0 :         *s = "true";
      45           0 :         break;
      46           0 :     case contrail_rapidjson::kFalseType:
      47           0 :         *s = "false";
      48           0 :         break;
      49           0 :     case contrail_rapidjson::kStringType:
      50           0 :         *s = node.GetString();
      51           0 :         break;
      52           0 :     case contrail_rapidjson::kNumberType:
      53           0 :         if (node.IsUint())
      54           0 :             ss << node.GetUint();
      55           0 :         else if (node.IsInt())
      56           0 :             ss << node.GetInt();
      57           0 :         else if (node.IsUint64())
      58           0 :             ss << node.GetUint64();
      59           0 :         else if (node.IsInt64())
      60           0 :             ss << node.GetInt64();
      61           0 :         else if (node.IsDouble())
      62           0 :             ss << node.GetDouble();
      63           0 :         *s = ss.str();
      64           0 :         break;
      65           0 :     case contrail_rapidjson::kObjectType:
      66           0 :         return false;
      67           0 :     case contrail_rapidjson::kArrayType:
      68           0 :         return false;
      69             :     }
      70           0 :     return true;
      71           0 : }
      72             : 
      73    17953834 : static bool ParseInteger(const char *nptr, int *valuep) {
      74             :     char *endp;
      75    17953834 :     *valuep = strtoul(nptr, &endp, 10);
      76    17957010 :     while (isspace(*endp))
      77        3159 :         endp++;
      78    17953851 :     return (endp[0] == '\0');
      79             : }
      80             : 
      81       53338 : static bool ParseUnsignedLong(const char *nptr, uint64_t *valuep) {
      82             :     char *endp;
      83       53338 :     *valuep = strtoull(nptr, &endp, 10);
      84       53338 :     while (isspace(*endp))
      85           0 :         endp++;
      86       53338 :     return (endp[0] == '\0');
      87             : }
      88             : 
      89     3222947 : static bool ParseBoolean(const char *bptr, bool *valuep) {
      90     3222947 :     if (strcmp(bptr, "true") ==0)
      91     1552758 :         *valuep = true;
      92             :     else
      93     1670189 :         *valuep = false;
      94     3222947 :     return true;
      95             : }
      96             : 
      97     1552109 : static bool ParseInteger(const pugi::xml_attribute &attr, int *valuep) {
      98     1552109 :     return ParseInteger(attr.value(), valuep);
      99             : }
     100             : 
     101           0 : static bool ParseUnsignedLong(const pugi::xml_attribute &attr,
     102             :                               uint64_t *valuep) {
     103           0 :     return ParseUnsignedLong(attr.value(), valuep);
     104             : }
     105             : 
     106     1552108 : static bool ParseBoolean(const pugi::xml_attribute &attr, bool *valuep) {
     107     1552108 :     return ParseBoolean(attr.value(), valuep);
     108             : }
     109             : 
     110    16396392 : static bool ParseInteger(const pugi::xml_node &node, int *valuep) {
     111    16396392 :     return ParseInteger(node.child_value(), valuep);
     112             : }
     113             : 
     114       49538 : static bool ParseUnsignedLong(const pugi::xml_node &node, uint64_t *valuep) {
     115       49538 :     return ParseUnsignedLong(node.child_value(), valuep);
     116             : }
     117             : 
     118     1669203 : static bool ParseBoolean(const pugi::xml_node &node, bool *valuep) {
     119     1669203 :     return ParseBoolean(node.child_value(), valuep);
     120             : }
     121             : 
     122         646 : static bool ParseDateTime(const pugi::xml_node &node, time_t *valuep) {
     123         646 :     string value(node.child_value());
     124         646 :     boost::trim(value);
     125             :     struct tm tm;
     126             :     char *endp;
     127         646 :     memset(&tm, 0, sizeof(tm));
     128         646 :     if (value.size() == 0) return true;
     129         646 :     endp = strptime(value.c_str(), "%FT%T", &tm);
     130         646 :     if (!endp) return false;
     131         646 :     *valuep = timegm(&tm);
     132         646 :     return true;
     133         646 : }
     134           0 : static bool ParseTime(const pugi::xml_node &node, time_t *valuep) {
     135           0 :     string value(node.child_value());
     136           0 :     boost::trim(value);
     137             :     struct tm tm;
     138             :     char *endp;
     139           0 :     endp = strptime(value.c_str(), "%T", &tm);
     140           0 :     if (!endp) return false;
     141           0 :     *valuep = timegm(&tm);
     142           0 :     return true;
     143           0 : }
     144        1286 : static std::string FormatDateTime(const time_t *valuep) {
     145             :     struct tm tm;
     146             :     char result[100];
     147        1286 :     gmtime_r(valuep, &tm);
     148        1286 :     strftime(result, sizeof(result), "%FT%T", &tm);
     149        1286 :     return std::string(result);
     150             : }
     151           0 : static std::string FormatTime(const time_t *valuep) {
     152             :     struct tm tm;
     153             :     char result[100];
     154           0 :     gmtime_r(valuep, &tm);
     155           0 :     strftime(result, sizeof(result), "%T", &tm);
     156           0 :     return std::string(result);
     157             : }
     158             : 
     159             : // Json Parse routines
     160        8230 : static bool ParseInteger(const contrail_rapidjson::Value &node, int *valuep) {
     161        8230 :     if (node.IsString())
     162        5366 :         return ParseInteger(node.GetString(), valuep);
     163        2864 :     if (node.IsInt()) {
     164        2864 :         *valuep = node.GetInt();
     165        2864 :         return true;
     166             :     }
     167             :     // We need to check for Uint also because Int value becomes Uint in json
     168             :     // if MSB is set
     169           0 :     if (node.IsUint()) {
     170           0 :         *valuep = node.GetUint();
     171           0 :         return true;
     172             :     }
     173           0 :     return false;
     174             : }
     175             : 
     176        5000 : static bool ParseUnsignedLong(const contrail_rapidjson::Value &node, uint64_t *valuep) {
     177        5000 :     if (node.IsString())
     178        3800 :         return ParseUnsignedLong(node.GetString(), valuep);
     179        1200 :     if (!node.IsUint64())
     180           0 :         return false;
     181        1200 :     *valuep = node.GetUint64();
     182        1200 :     return true;
     183             : }
     184             : 
     185        4006 : static bool ParseBoolean(const contrail_rapidjson::Value &node, bool *valuep) {
     186        4006 :     if (node.IsString())
     187        1638 :         return ParseBoolean(node.GetString(), valuep);
     188        2368 :     if (!node.IsBool())
     189           0 :         return false;
     190        2368 :     *valuep = node.GetBool();
     191        2368 :     return true;
     192             : }
     193             : 
     194        1200 : static bool ParseDateTime(const contrail_rapidjson::Value &node, time_t *valuep) {
     195        1200 :     if (!node.IsString())
     196           0 :         return false;
     197        1200 :     string value(node.GetString());
     198        1200 :     boost::trim(value);
     199             :     struct tm tm;
     200             :     char *endp;
     201        1200 :     memset(&tm, 0, sizeof(tm));
     202        1200 :     if (value.size() == 0) return true;
     203        1200 :     endp = strptime(value.c_str(), "%FT%T", &tm);
     204        1200 :     if (!endp) return false;
     205        1200 :     *valuep = timegm(&tm);
     206        1200 :     return true;
     207        1200 : }
     208             : 
     209           0 : static bool ParseTime(const contrail_rapidjson::Value &node, time_t *valuep) {
     210           0 :     if (!node.IsString())
     211           0 :         return false;
     212           0 :     string value(node.GetString());
     213           0 :     boost::trim(value);
     214             :     struct tm tm;
     215             :     char *endp;
     216           0 :     endp = strptime(value.c_str(), "%T", &tm);
     217           0 :     if (!endp) return false;
     218           0 :     *valuep = timegm(&tm);
     219           0 :     return true;
     220           0 : }
     221             : 
     222             : }  // namespace autogen
     223             : 
     224             : #endif  // AUTOGEN_UTIL_H_

Generated by: LCOV version 1.14