SUMO - Simulation of Urban MObility
TplCheck.h
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2001-2017 German Aerospace Center (DLR) and others.
4 /****************************************************************************/
5 //
6 // This program and the accompanying materials
7 // are made available under the terms of the Eclipse Public License v2.0
8 // which accompanies this distribution, and is available at
9 // http://www.eclipse.org/legal/epl-v20.html
10 //
11 /****************************************************************************/
17 // Some methods for check type of dates
18 /****************************************************************************/
19 #ifndef TplCheck_h
20 #define TplCheck_h
21 
22 
23 // ===========================================================================
24 // included modules
25 // ===========================================================================
26 #ifdef _MSC_VER
27 #include <windows_config.h>
28 #else
29 #include <config.h>
30 #endif
31 
32 #include <string>
33 #include <algorithm>
34 
35 
36 // ===========================================================================
37 // class definitions
38 // ===========================================================================
43 class TplCheck {
44 public:
47  static bool _str2int(const std::string& data) {
48  // Data empty does't mean 0
49  if (data.size() == 0) {
50  return false;
51  }
52  for (int i = 0; i < (int)data.size(); i++) {
53  if (data.at(i) == '+' || data.at(i) == '-') {
54  if (i != 0) {
55  return false;
56  }
57  } else if (data.at(i) < '0' || data.at(i) > '9') {
58  return false;
59  }
60  }
61  return true;
62  }
63 
66  static bool _str2double(const std::string& data) {
67  bool dot = false;
68  if (data.size() == 0) {
69  return false;
70  }
71  for (int i = 0; i < (int)data.size(); i++) {
72  if (data.at(i) == '+' || data.at(i) == '-') {
73  if (i != 0) {
74  return false;
75  }
76  } else if (data.at(i) == '.') {
77  if (data.at(i) == '.' && !dot) {
78  dot = true;
79  } else {
80  return false;
81  }
82  } else if (data.at(i) < '0' || data.at(i) > '9') {
83  return false;
84  }
85  }
86  return true;
87  }
88 
90  static bool _str2bool(const std::string& data) {
91  std::string dataToLower = data;
92  std::transform(dataToLower.begin(), dataToLower.end(), dataToLower.begin(), ::tolower);
93  if (data == "1" || data == "yes" || data == "true" || data == "on" || data == "x" || data == "t" ||
94  data == "0" || data == "no" || data == "false" || data == "off" || data == "-" || data == "f") {
95  return true;
96  } else {
97  return false;
98  }
99  }
100 
103  static bool _str2SUMOTime(const std::string& data) {
104  // Data empty does't mean 0
105  if (data.size() == 0) {
106  return false;
107  }
108  for (int i = 0; i < (int)data.size(); i++) {
109  if (data.at(i) == '+') {
110  if (i != 0) {
111  return false;
112  }
113  } else if (data.at(i) < '0' || data.at(i) > '9') {
114  return false;
115  }
116  }
117  return true;
118  }
119 };
120 
121 
122 #endif
123 
124 /****************************************************************************/
static bool _str2int(const std::string &data)
check if a String can be parsed into a int check overflows
Definition: TplCheck.h:47
static bool _str2bool(const std::string &data)
check if a String can be parsed into a Bool
Definition: TplCheck.h:90
static bool _str2SUMOTime(const std::string &data)
check if a String can be parsed into a SUMOTime check overflows
Definition: TplCheck.h:103
static bool _str2double(const std::string &data)
check if a String can be parsed into a double check overflows
Definition: TplCheck.h:66