pgRouting  2.2
pgRouting extends the PostGIS / PostgreSQL geospatial database to provide geospatial routing functionality.
 All Classes Functions Variables Pages
Utils.h
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 UTILS_H
23 #define 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 {
39  std::string s, delim;
40  std::string::size_type pos;
41 
42 public:
43 
44  tokenizer( std::string xs, std::string xdelim ) : s(xs), delim(xdelim)
45  {
46  pos = s.find_first_not_of( delim );
47  }
48 
49  bool has_more_tokens()
50  {
51  return pos != std::string::npos;
52  }
53 
54  std::string next_token()
55  {
56  std::string::size_type end_pos = s.find_first_of( delim, pos );
57  std::string token = s.substr( pos, end_pos-pos );
58  pos = s.find_first_not_of( delim, end_pos );
59  return token;
60  }
61 };
62 
63 class StringOperation
64 {
65 
66 public:
67 
68  static std::string TRIMWHITESPACE(std::string strInput)
69  {
70  std::string strOut = strInput;
71  std::string strWht = " \f\n\r\t\v";
72 
73  strOut.erase(0,strOut.find_first_not_of(strWht));
74  strOut.erase(strOut.find_last_not_of(strWht) + 1);
75  return strOut;
76  };
77 };
78 
79 class StringTokenizer
80 {
81 public:
83  {
84  reset();
85  }
86 
87  void reset()
88  {
89  vecTokens.clear();
90  strInput = "";
91  }
92 
93  bool parse(std::string strInput, std::string chDelim)
94  {
95  tokenizer t( strInput, chDelim );
96  while(t.has_more_tokens())
97  {
98  vecTokens.push_back(t.next_token());
99  }
100  return true;
101  }
102 
103  size_t getTokenCount()
104  {
105  return vecTokens.size();
106  }
107 
108  bool getToken(std::string& strToken, long iTokenIndex)
109  {
110  long lTokenCount = vecTokens.size();
111  if(iTokenIndex < 0 || iTokenIndex < lTokenCount)
112  {
113  strToken = vecTokens[iTokenIndex];
114  return true;
115  }
116  return false;
117  }
118 
119  bool getTokens(StringVector& vecTokensRef)
120  {
121  vecTokensRef = vecTokens;
122  return true;
123  }
124 
125 private:
126  std::string strInput;
127  StringVector vecTokens;
128 
129 };
130