PGROUTING  2.4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Utils.h
Go to the documentation of this file.
1 /*PGR
2 
3 Copyright (c) 2013 Khondoker Md. Razequl Islam
4 ziboncsedu@gmail.com
5 
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10 
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15 
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 
20 */
21 
22 #ifndef SRC_VRP_BASIC_SRC_UTILS_H_
23 #define SRC_VRP_BASIC_SRC_UTILS_H_
24 
25 #include <string>
26 #include <map>
27 #include <vector>
28 #include <queue>
29 #include <iostream>
30 
31 
32 
33 typedef std::vector<int> IntVector;
34 typedef std::vector<double> DoubleVector;
35 typedef std::vector<std::string> StringVector;
36 
37 class tokenizer {
38  std::string s, delim;
39  std::string::size_type pos;
40 
41  public:
42  tokenizer(std::string xs, std::string xdelim) : s(xs), delim(xdelim) {
43  pos = s.find_first_not_of(delim);
44  }
45 
46  bool has_more_tokens() {
47  return pos != std::string::npos;
48  }
49 
50  std::string next_token() {
51  std::string::size_type end_pos = s.find_first_of(delim, pos);
52  std::string token = s.substr(pos, end_pos-pos);
53  pos = s.find_first_not_of(delim, end_pos);
54  return token;
55  }
56 };
57 
58 class StringOperation {
59  public:
60  static std::string TRIMWHITESPACE(std::string strInput) {
61  std::string strOut = strInput;
62  std::string strWht = " \f\n\r\t\v";
63 
64  strOut.erase(0, strOut.find_first_not_of(strWht));
65  strOut.erase(strOut.find_last_not_of(strWht) + 1);
66  return strOut;
67  };
68 };
69 
70 class StringTokenizer {
71  public:
73  reset();
74  }
75 
76  void reset() {
77  vecTokens.clear();
78  strInput = "";
79  }
80 
81  bool parse(std::string strInput, std::string chDelim) {
82  tokenizer t(strInput, chDelim);
83  while (t.has_more_tokens()) {
84  vecTokens.push_back(t.next_token());
85  }
86  return true;
87  }
88 
89  size_t getTokenCount() {
90  return vecTokens.size();
91  }
92 
93  bool getToken(std::string& strToken, int64_t iTokenIndex) {
94  int64_t lTokenCount = vecTokens.size();
95  if (iTokenIndex < 0 || iTokenIndex < lTokenCount) {
96  strToken = vecTokens[iTokenIndex];
97  return true;
98  }
99  return false;
100  }
101 
102  bool getTokens(StringVector& vecTokensRef) {
103  vecTokensRef = vecTokens;
104  return true;
105  }
106 
107  private:
108  std::string strInput;
110 };
111 
112 #endif // SRC_VRP_BASIC_SRC_UTILS_H_
std::string s
Definition: Utils.h:38
bool parse(std::string strInput, std::string chDelim)
Definition: Utils.h:81
bool getToken(std::string &strToken, int64_t iTokenIndex)
Definition: Utils.h:93
tokenizer(std::string xs, std::string xdelim)
Definition: Utils.h:42
std::string next_token()
Definition: Utils.h:50
StringTokenizer()
Definition: Utils.h:72
std::string delim
Definition: Utils.h:38
std::vector< int > IntVector
Definition: Utils.h:33
bool getTokens(StringVector &vecTokensRef)
Definition: Utils.h:102
std::vector< std::string > StringVector
std::vector< double > DoubleVector
Definition: Utils.h:34
std::vector< std::string > StringVector
Definition: Utils.h:35
static std::string TRIMWHITESPACE(std::string strInput)
Definition: Utils.h:60
size_t getTokenCount()
Definition: Utils.h:89
std::string::size_type pos
Definition: Utils.h:39
bool has_more_tokens()
Definition: Utils.h:46