Darwin  1.10(beta)
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
drwnStrUtils.h
Go to the documentation of this file.
1 /*****************************************************************************
2 ** DARWIN: A FRAMEWORK FOR MACHINE LEARNING RESEARCH AND DEVELOPMENT
3 ** Distributed under the terms of the BSD license (see the LICENSE file)
4 ** Copyright (c) 2007-2015, Stephen Gould
5 ** All rights reserved.
6 **
7 ******************************************************************************
8 ** FILENAME: drwnStrUtils.h
9 ** AUTHOR(S): Stephen Gould <stephen.gould@anu.edu.au>
10 **
11 *****************************************************************************/
12 
19 #pragma once
20 
21 #include <string>
22 #include <vector>
23 #include <list>
24 #include <set>
25 #include <iostream>
26 #include <sstream>
27 #include <map>
28 #include <deque>
29 #include <stdlib.h>
30 #include <limits>
31 
32 using namespace std;
33 
34 // constants and globals ----------------------------------------------------
35 
36 extern std::string DRWN_COL_SEP;
37 extern std::string DRWN_ROW_BEG;
38 extern std::string DRWN_ROW_END;
39 
40 // functions ----------------------------------------------------------------
41 
45 template<typename T>
46 std::string toString(const T& v);
47 
48 template<typename T>
49 std::string toString(const std::vector<T>& v);
50 
51 template<typename T>
52 std::string toString(const std::list<T>& v);
53 
54 template<typename T>
55 std::string toString(const std::set<T>& v);
56 
57 template<typename T>
58 std::string toString(const std::deque<T>& q);
59 
60 template<typename T, typename U>
61 std::string toString(const std::pair<T, U>& p);
62 
64 std::string toString(const map<std::string, std::string>& m);
65 
66 namespace drwn {
67 
69  int strNoCaseCompare(const std::string& A, const std::string& B);
70 
72  template<typename T>
73  int parseString(const std::string& str, std::vector<T>& v);
74 };
75 
76  //@cond
77  template<typename T, bool B>
78  struct parseInfToken {
79  static bool apply(const std::string& token, T& value);
80  };
81  //@endcond
82 
83 namespace drwn {
84 
86  map<string, string> parseNameValueString(std::string str);
87 
89  bool trueString(const std::string& str);
90 
92  std::string padString(const std::string& str, int padLength,
93  unsigned char padChar = '0');
94 
96  std::list<string> breakString(const std::string& str, unsigned lineLength);
97 
99  std::string& trim(std::string& str);
100 
102  string strReplaceSubstr(const string& str, const string& substr, const string& rep);
103 
106  string strSpacifyCamelCase(const string& str);
107 
109  string bytesToString(unsigned b);
111  string millisecondsToString(unsigned ms);
112 
114  string strBaseName(const string &fullPath);
116  string strFilename(const string &fullPath);
118  string strDirectory(const string &fullPath);
120  string strExtension(const string &fullPath);
122  string strReplaceExt(const string &fullPath, const string &ext);
124  string strWithoutExt(const string &fullPath);
126  string strWithoutEndSlashes(const string &fullPath);
128  int strFileIndex(const string &fullPath);
129 };
130 
131 // Implementation -----------------------------------------------------------
132 
133 template<typename T>
134 std::string toString(const T& v)
135 {
136  std::stringstream s;
137  s << v;
138  return s.str();
139 }
140 
141 template<typename T>
142 std::string toString(const std::vector<T>& v)
143 {
144  std::stringstream s;
145  for (unsigned i = 0; i < v.size(); i++) {
146  s << " " << v[i];
147  }
148  return s.str();
149 }
150 
151 template<typename T>
152 std::string toString(const std::list<T>& v)
153 {
154  std::stringstream s;
155  for (typename std::list<T>::const_iterator it = v.begin(); it != v.end(); ++it) {
156  if (it != v.begin()) s << " ";
157  s << *it;
158  }
159  return s.str();
160 }
161 
162 template<typename T>
163 std::string toString(const std::set<T>& v)
164 {
165  std::stringstream s;
166  s << "{";
167  for (typename std::set<T>::const_iterator it = v.begin(); it != v.end(); ++it) {
168  s << " " << *it;
169  }
170  s << " }";
171  return s.str();
172 }
173 
174 template<typename T>
175 std::string toString(const std::deque<T>& q)
176 {
177  std::stringstream s;
178  for (typename std::deque<T>::const_iterator it = q.begin(); it != q.end(); ++it) {
179  s << " " << *it;
180  }
181  return s.str();
182 }
183 
184 template<typename T, typename U>
185 std::string toString(const std::pair<T, U>& p)
186 {
187  std::stringstream s;
188  s << "(" << p.first << ", " << p.second << ")";
189  return s.str();
190 }
191 
192 // Conversion from a string
193 template<typename T>
194 int drwn::parseString(const std::string& str, std::vector<T>& v)
195 {
196  std::stringstream buffer;
197  T data;
198  int count;
199 
200  buffer << str;
201 
202  count = 0;
203  while (1) {
204  int lastPosition = buffer.tellg();
205  buffer >> data;
206  if (buffer.fail()) {
207  // try to parse special token
208  buffer.clear();
209  buffer.seekg(lastPosition, ios::beg);
210  string token;
211  buffer >> token;
212  if (!parseInfToken<T, numeric_limits<T>::has_infinity>::apply(token, data)) {
213  break;
214  }
215  }
216  v.push_back(data);
217  count++;
218 
219  if (buffer.eof()) break;
220  }
221 
222  return count;
223 }
224 
225 //@cond
226 template<typename T>
227 struct parseInfToken<T, true> {
228  static bool apply(const std::string& token, T& value) {
229  if (token.compare("-inf") == 0) {
230  value = -numeric_limits<T>::infinity();
231  } else if (token.compare("inf") == 0) {
232  value = numeric_limits<T>::infinity();
233  } else {
234  return false;
235  }
236 
237  return true;
238  }
239 };
240 
241 template<typename T>
242 struct parseInfToken<T, false> {
243  static bool apply(const std::string& token, T& value) {
244  return false;
245  }
246 };
247 //@condend
std::string DRWN_ROW_BEG
row beginning when printing tables
std::string DRWN_COL_SEP
column separator when printing tables
std::string DRWN_ROW_END
row ending when printing tables
std::string toString(const T &v)
Templated function to make conversion from simple data types like int and double to strings easy for ...
Definition: drwnStrUtils.h:134