SUMO - Simulation of Urban MObility
TplCheck.h
Go to the documentation of this file.
1 /****************************************************************************/
7 // Some methods for check type of dates
8 /****************************************************************************/
9 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
10 // Copyright (C) 2001-2017 DLR (http://www.dlr.de/) and contributors
11 /****************************************************************************/
12 //
13 // This file is part of SUMO.
14 // SUMO is free software: you can redistribute it and/or modify
15 // it under the terms of the GNU General Public License as published by
16 // the Free Software Foundation, either version 3 of the License, or
17 // (at your option) any later version.
18 //
19 /****************************************************************************/
20 #ifndef TplCheck_h
21 #define TplCheck_h
22 
23 
24 // ===========================================================================
25 // included modules
26 // ===========================================================================
27 #ifdef _MSC_VER
28 #include <windows_config.h>
29 #else
30 #include <config.h>
31 #endif
32 
33 #include <string>
34 #include <algorithm>
35 
36 
37 // ===========================================================================
38 // class definitions
39 // ===========================================================================
44 class TplCheck {
45 public:
48  static bool _str2int(const std::string& data) {
49  // Data empty does't mean 0
50  if (data.size() == 0) {
51  return false;
52  }
53  for (int i = 0; i < (int)data.size(); i++) {
54  if (data.at(i) == '+' || data.at(i) == '-') {
55  if (i != 0) {
56  return false;
57  }
58  } else if (data.at(i) < '0' || data.at(i) > '9') {
59  return false;
60  }
61  }
62  return true;
63  }
64 
67  static bool _str2double(const std::string& data) {
68  bool dot = false;
69  if (data.size() == 0) {
70  return false;
71  }
72  for (int i = 0; i < (int)data.size(); i++) {
73  if (data.at(i) == '+' || data.at(i) == '-') {
74  if (i != 0) {
75  return false;
76  }
77  } else if (data.at(i) == '.') {
78  if (data.at(i) == '.' && !dot) {
79  dot = true;
80  } else {
81  return false;
82  }
83  } else if (data.at(i) < '0' || data.at(i) > '9') {
84  return false;
85  }
86  }
87  return true;
88  }
89 
91  static bool _str2bool(const std::string& data) {
92  std::string dataToLower = data;
93  std::transform(dataToLower.begin(), dataToLower.end(), dataToLower.begin(), ::tolower);
94  if (data == "1" || data == "yes" || data == "true" || data == "on" || data == "x" || data == "t" ||
95  data == "0" || data == "no" || data == "false" || data == "off" || data == "-" || data == "f") {
96  return true;
97  } else {
98  return false;
99  }
100  }
101 
104  static bool _str2SUMOTime(const std::string& data) {
105  // Data empty does't mean 0
106  if (data.size() == 0) {
107  return false;
108  }
109  for (int i = 0; i < (int)data.size(); i++) {
110  if (data.at(i) == '+') {
111  if (i != 0) {
112  return false;
113  }
114  } else if (data.at(i) < '0' || data.at(i) > '9') {
115  return false;
116  }
117  }
118  return true;
119  }
120 };
121 
122 
123 #endif
124 
125 /****************************************************************************/
static bool _str2int(const std::string &data)
check if a String can be parsed into a int check overflows
Definition: TplCheck.h:48
static bool _str2bool(const std::string &data)
check if a String can be parsed into a Bool
Definition: TplCheck.h:91
static bool _str2SUMOTime(const std::string &data)
check if a String can be parsed into a SUMOTime check overflows
Definition: TplCheck.h:104
static bool _str2double(const std::string &data)
check if a String can be parsed into a double check overflows
Definition: TplCheck.h:67