PICCANTE  0.4
The hottest HDR imaging library!
string.hpp
Go to the documentation of this file.
1 /*
2 
3 PICCANTE
4 The hottest HDR imaging library!
5 http://vcg.isti.cnr.it/piccante
6 
7 Copyright (C) 2014
8 Visual Computing Laboratory - ISTI CNR
9 http://vcg.isti.cnr.it
10 First author: Francesco Banterle
11 
12 This Source Code Form is subject to the terms of the Mozilla Public
13 License, v. 2.0. If a copy of the MPL was not distributed with this
14 file, You can obtain one at http://mozilla.org/MPL/2.0/.
15 
16 */
17 
18 #ifndef PIC_UTIL_STRING_HPP
19 #define PIC_UTIL_STRING_HPP
20 
21 #include <vector>
22 #include <string>
23 #include <sstream>
24 #include <iostream>
25 #include <fstream>
26 
27 #include "../base.hpp"
28 
29 
30 #ifdef PIC_WIN32
31  #include <direct.h>
32 #endif
33 
34 #ifndef PIC_WIN32
35  #include <unistd.h>
36 #endif
37 
38 
39 namespace pic {
40 
44 #define MAKE_STRING(input_string) #input_string
45 
49 typedef std::vector<std::string > StringVec;
50 
58 inline std::string stdStringRep(std::string str, std::string strSub,
59  std::string strRep)
60 {
61  std::string ret = str;
62 
63  size_t found = ret.find(strSub);
64 
65  if(found != std::string::npos) {
66  ret.replace(found, strRep.length(), strRep);
67  }
68 
69  return ret;
70 }
71 
79 inline std::string stdStringRepAll(std::string str, std::string strSub,
80  std::string strRep)
81 {
82  auto n_sub = strSub.size();
83  auto n_rep = strRep.size();
84 
85  std::string ret = str;
86  std::string::size_type pos = ret.find(strSub);
87 
88  while(pos != std::string::npos) {
89  ret.replace(pos, n_sub, strRep);
90 
91  pos = ret.find(strSub, pos + 1 - n_sub + n_rep);
92  }
93 
94  return ret;
95 }
96 
101 template<class T>
102 inline std::string fromNumberToString(T num)
103 {
104  std::ostringstream convert;
105  convert << num;
106  return convert.str();
107 }
108 
114 inline char getSeparatorChar(std::string path)
115 {
116  if(path.find("/") != std::string::npos) {
117  return '/';
118  } else {
119  if(path.find("\\") != std::string::npos) {
120  return '\\';
121  } else {
122  return '/';
123  }
124  }
125 }
126 
132 inline std::string removeExtension(std::string name)
133 {
134  std::string tmp(name);
135  std::reverse(tmp.begin(), tmp.end());
136 
137  size_t pos = tmp.find(".");
138 
139  if(pos != std::string::npos) {
140  name.erase(name.end() - pos - 1, name.end());
141  }
142 
143  return name;
144 }
145 
151 inline std::string removeLocalPath(std::string name)
152 {
153  std::string toFind(1, getSeparatorChar(name));
154 
155  if(toFind.empty()) {
156  return name;
157  } else {
158  size_t oldPos;
159  size_t pos = 0;
160  do{
161  oldPos = pos;
162  pos = name.find(toFind, pos + 1);
163  } while(pos != std::string::npos);
164 
165  name.erase(0, oldPos + 1);
166  }
167 
168  return name;
169 }
170 
176 inline std::string getFileNameOnly(std::string name)
177 {
178  return removeLocalPath(removeExtension(name));
179 }
180 
186 inline std::string getExtension(std::string name)
187 {
188  std::string tmp(name);
189  std::reverse(tmp.begin(), tmp.end());
190 
191  size_t pos = tmp.find(".");
192  std::string ext = "";
193 
194  if(pos != std::string::npos) {
195  auto n = name.length() - pos;
196  ext = name.substr(n, n);
197  }
198 
199  return ext;
200 }
201 
208 inline std::string addSuffix(std::string name, std::string suffix)
209 {
210  std::string tmp = removeExtension(name);
211  std::string tmpExt = getExtension(name);
212  return tmp + suffix + "." + tmpExt;
213 }
214 
222 inline std::string replaceExtension(std::string nameOut, std::string fmtIn,
223  std::string fmtOut)
224 {
225  size_t found = nameOut.find(fmtIn);
226 
227  if(found != std::string::npos) {
228  nameOut.replace(nameOut.begin() + found, nameOut.end(), fmtOut);
229  }
230 
231  return nameOut;
232 }
233 
240 inline int countSubString(std::string str, std::string subStr)
241 {
242  int count = 0;
243 
244  std::string::size_type pos = str.find(subStr);
245 
246  while(pos != std::string::npos) {
247  count++;
248  pos = str.find(subStr, pos + 1);
249  }
250 
251  return count;
252 }
253 
259 inline std::string getLocaDirectory(std::string path)
260 {
261  std::string ret = path;
262 
263  std::string toFind(1, getSeparatorChar(path));
264 
265  if(toFind.empty()) {
266  return ret;
267  }
268 
269  size_t pos1 = path.rfind(toFind);
270 
271  if(pos1 != std::string::npos) {
272  ret = path.substr(0, pos1);
273  size_t pos2 = ret.rfind(toFind);
274 
275  if(pos2 != std::string::npos) {
276  return ret.substr(pos2 + 1, ret.length());
277  }
278  }
279 
280  return ret;
281 }
282 
288 inline std::string getSeparator(std::string path)
289 {
290  char sepChar = getSeparatorChar(path);
291  std::string strOut;
292  return strOut + sepChar;
293 }
294 
300 inline std::string getFolderName(std::string path)
301 {
302  size_t found = path.find_last_of(getSeparator(path));
303 
304  if(found != std::string::npos) {
305  return path.substr(0, found);
306  } else {
307  return "./";
308  }
309 }
310 
316 inline std::string getFileName(std::string path)
317 {
318  std::string toFind;
319  std::string ret = path;
320 
321  if(path.find("/") != std::string::npos) {
322  toFind = "/";
323  } else {
324  if(path.find("\\") != std::string::npos) {
325  toFind = "\\";
326  } else {
327  return ret;
328  }
329  }
330 
331  size_t pos = path.rfind(toFind);
332 
333  if(pos != std::string::npos) {
334  ret = path.substr(pos + 1, path.length());
335  return ret;
336  }
337 
338  return ret;
339 }
340 
347 inline void parseStringToStdVector(std::string str, char delim,
348  StringVec *str_vec)
349 {
350  std::stringstream ss(str);
351 
352  while(!ss.eof()) {
353  std::string tmpStr;
354  std::getline(ss, tmpStr, delim);
355  str_vec->push_back(tmpStr);
356  }
357 }
358 
366 inline std::string genBilString(std::string type, float sigma_s,
367  float sigma_r)
368 {
369  std::string ret = type +
370  "_Ss_" + fromNumberToString(sigma_s) +
371  "_Sr_" + fromNumberToString(sigma_r);
372  return ret;
373 }
374 
380 inline std::string fromFileToStdString(std::string nameFile)
381 {
382  std::ifstream infile;
383  infile.open(nameFile.c_str(), std::ios::in);
384 
385  std::string ret;
386 
387  if((!infile.is_open()) || (!infile.good())) {
388  return ret;
389  }
390 
391  int c = infile.get();
392  while (infile.good()) {
393  ret += c;
394  c = infile.get();
395  }
396 
397  infile.close();
398 
399  return ret;
400 }
401 
407 inline bool checkAbsolutePath(std::string path)
408 {
409  //win32 absolute path
410  if(path.find(":\\") != std::string::npos) {
411  return true;
412  }
413 
414  if(path.find(":/") != std::string::npos) {
415  return true;
416  }
417 
418  if(path.find("\\\\\"") != std::string::npos) {
419  return true;
420  }
421 
422  //unix/mac path
423  return (path.at(0) == '/');
424 }
425 
431 inline char *fromStdStringToChar(std::string str)
432 {
433  char *cstr = new char [str.size() + 1];
434  strcpy (cstr, str.c_str());
435  return cstr;
436 }
437 
443 inline std::string checkPath(std::string name)
444 {
445  if(name.length() < 3) {
446  return "";
447  }
448 
449  if((name.at(0) == '.') && (name.at(0) == '.')) {
450  #ifdef PIC_WIN32
451  char *path = _getcwd(NULL, 0);
452  #endif
453 
454  #ifndef PIC_WIN32
455  char *path = getcwd(NULL, 0);
456  #endif
457 
458  std::string dsepName = getSeparator(name);
459  std::string dsepPath = getSeparator(path);
460 
461  name = stdStringRepAll(name, dsepName, dsepPath);
462  if(name.at(2) == '\\' || name.at(2) == '/') {
463  name = name.substr(3);
464  } else {
465  name = name.substr(2);
466  }
467 
468  std::string newPath = path + dsepPath + name;
469  return newPath;
470  } else {
471  return "";
472  }
473 }
474 
481 std::string adjustPath(std::string nameFile, std::string pathFolder)
482 {
483  if(!checkAbsolutePath(nameFile)) {
484  std::string fullPath = checkPath(nameFile);
485 
486  if(fullPath.empty()) {
487  std::string ret = pathFolder + getSeparator(pathFolder) + nameFile;
488  return ret;
489  } else {
490  return fullPath;
491  }
492  } else {
493  return nameFile;
494  }
495 }
496 
502 inline std::string removeInitialSpaces(char name[])
503 {
504  size_t pos;
505  std::string ret = name;
506 
507  pos = ret.find(' ');
508  ret.erase(pos, 1);
509 
510  pos = ret.find('\n');
511  ret.erase(pos, 1);
512 
513  return ret;
514 }
515 
516 } // end namespace pic
517 
518 #endif /* PIC_UTIL_STRING_HPP */
519 
std::vector< std::string > StringVec
StringVec is an std::vector of std::string.
Definition: string.hpp:49
std::string getLocaDirectory(std::string path)
getLocaDirectory gets local path.
Definition: string.hpp:259
std::string removeInitialSpaces(char name[])
removeInitialSpaces removes spaces at the beginning of a string.
Definition: string.hpp:502
int countSubString(std::string str, std::string subStr)
countSubString counts how many subStr are in str.
Definition: string.hpp:240
std::string fromNumberToString(T num)
fromNumberToString converts a number into a string.
Definition: string.hpp:102
char getSeparatorChar(std::string path)
getSeparatorChar returns the folder separator in path as a char.
Definition: string.hpp:114
std::string getFolderName(std::string path)
getFolderName gets the folder name from the path.
Definition: string.hpp:300
std::string fromFileToStdString(std::string nameFile)
fromFileToStdString writes a file into a std::string.
Definition: string.hpp:380
std::string addSuffix(std::string name, std::string suffix)
addSuffix adds a suffix to a file name.
Definition: string.hpp:208
void parseStringToStdVector(std::string str, char delim, StringVec *str_vec)
parseStringToStdVector
Definition: string.hpp:347
std::string getSeparator(std::string path)
getSeparator returns the folder separator in path as a string
Definition: string.hpp:288
std::string getFileName(std::string path)
getFileName gets the file name.
Definition: string.hpp:316
std::string checkPath(std::string name)
checkPath
Definition: string.hpp:443
std::string getFileNameOnly(std::string name)
getFileNameOnly
Definition: string.hpp:176
std::string getExtension(std::string name)
getExtension gets the extension of a file name.
Definition: string.hpp:186
std::string adjustPath(std::string nameFile, std::string pathFolder)
adjustPath modifies the path if it is not global.
Definition: string.hpp:481
std::string genBilString(std::string type, float sigma_s, float sigma_r)
genBilString
Definition: string.hpp:366
std::string stdStringRepAll(std::string str, std::string strSub, std::string strRep)
stdStringRepAll replaces all strSub in str with strRep.
Definition: string.hpp:79
bool checkAbsolutePath(std::string path)
checkAbsolutePath checks if the path is absolute or not.
Definition: string.hpp:407
Definition: bilateral_separation.hpp:25
std::string removeExtension(std::string name)
RemoveExtension removes the extension of a string.
Definition: string.hpp:132
std::string removeLocalPath(std::string name)
removeLocalPath removes the local path of a string.
Definition: string.hpp:151
std::string stdStringRep(std::string str, std::string strSub, std::string strRep)
stdStringRep replaces strSub in str with strRep just once.
Definition: string.hpp:58
std::string replaceExtension(std::string nameOut, std::string fmtIn, std::string fmtOut)
replaceExtension changes .format in a file name.
Definition: string.hpp:222
char * fromStdStringToChar(std::string str)
fromStdStringToChar converts from a std::string to a char*.
Definition: string.hpp:431