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-2018 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v2.0
6 // which accompanies this distribution, and is available at
7 // http://www.eclipse.org/legal/epl-v20.html
8 // SPDX-License-Identifier: EPL-2.0
9 /****************************************************************************/
15 // Some methods for check type of dates
16 /****************************************************************************/
17 #ifndef TplCheck_h
18 #define TplCheck_h
19 
20 
21 // ===========================================================================
22 // included modules
23 // ===========================================================================
24 #include <config.h>
25 
26 #include <string>
27 #include <algorithm>
28 
29 
30 // ===========================================================================
31 // class definitions
32 // ===========================================================================
37 class TplCheck {
38 public:
41  static bool _str2int(const std::string& data) {
42  // Data empty does't mean 0
43  if (data.size() == 0) {
44  return false;
45  }
46  for (int i = 0; i < (int)data.size(); i++) {
47  if (data.at(i) == '+' || data.at(i) == '-') {
48  if (i != 0) {
49  return false;
50  }
51  } else if (data.at(i) < '0' || data.at(i) > '9') {
52  return false;
53  }
54  }
55  return true;
56  }
57 
60  static bool _str2double(const std::string& data) {
61  bool dot = false;
62  if (data.size() == 0) {
63  return false;
64  }
65  for (int i = 0; i < (int)data.size(); i++) {
66  if (data.at(i) == '+' || data.at(i) == '-') {
67  if (i != 0) {
68  return false;
69  }
70  } else if (data.at(i) == '.') {
71  if (data.at(i) == '.' && !dot) {
72  dot = true;
73  } else {
74  return false;
75  }
76  } else if (data.at(i) < '0' || data.at(i) > '9') {
77  return false;
78  }
79  }
80  return true;
81  }
82 
84  static bool _str2bool(const std::string& data) {
85  std::string dataToLower = data;
86  std::transform(dataToLower.begin(), dataToLower.end(), dataToLower.begin(), ::tolower);
87  if (data == "1" || data == "yes" || data == "true" || data == "on" || data == "x" || data == "t" ||
88  data == "0" || data == "no" || data == "false" || data == "off" || data == "-" || data == "f") {
89  return true;
90  } else {
91  return false;
92  }
93  }
94 
97  static bool _str2SUMOTime(const std::string& data) {
98  // Data empty does't mean 0
99  if (data.size() == 0) {
100  return false;
101  }
102  for (int i = 0; i < (int)data.size(); i++) {
103  if (data.at(i) == '+') {
104  if (i != 0) {
105  return false;
106  }
107  } else if (data.at(i) < '0' || data.at(i) > '9') {
108  return false;
109  }
110  }
111  return true;
112  }
113 };
114 
115 
116 #endif
117 
118 /****************************************************************************/
static bool _str2int(const std::string &data)
check if a String can be parsed into a int check overflows
Definition: TplCheck.h:41
static bool _str2bool(const std::string &data)
check if a String can be parsed into a Bool
Definition: TplCheck.h:84
static bool _str2SUMOTime(const std::string &data)
check if a String can be parsed into a SUMOTime check overflows
Definition: TplCheck.h:97
static bool _str2double(const std::string &data)
check if a String can be parsed into a double check overflows
Definition: TplCheck.h:60