aGrUM  0.14.2
utils_string.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005 by Christophe GONZALES and Pierre-Henri WUILLEMIN *
3  * {prenom.nom}_at_lip6.fr *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19  ***************************************************************************/
27 #include <iterator>
28 #include <regex>
29 
31 
32 namespace gum {
33 
34  std::string getUniqueFileName() {
35 #ifdef HAVE_MKSTEMP
36  char _tmpFileName[] = "fileXXXXXX";
37  int fd = mkstemp(_tmpFileName);
38  close(fd);
39 #else // mainly Windows
40  char _tmpFileName[] = "fileXXXXXX";
41  _mktemp_s(_tmpFileName, strlen(_tmpFileName));
42 #endif
43 
44  return std::string(_tmpFileName);
45  }
46 
47  bool endsWith(std::string const& value, std::string const& ending) {
48  if (ending.size() > value.size()) return false;
49  return std::equal(ending.rbegin(), ending.rend(), value.rbegin());
50  }
51 
52  std::vector< std::string > split(const std::string& str,
53  const std::string& delim) {
54  std::vector< std::string > tokens;
55  size_t prev = 0, pos = 0;
56  do {
57  pos = str.find(delim, prev);
58  if (pos == std::string::npos) pos = str.length();
59  std::string token = str.substr(prev, pos - prev);
60  if (!token.empty()) tokens.push_back(token);
61  prev = pos + delim.length();
62  } while (pos < str.length() && prev < str.length());
63  return tokens;
64  }
65 
77  std::string replace(const std::string& s,
78  const std::string& val,
79  const std::string& new_val) {
80  auto retVal = s;
81  auto pos = retVal.find(val);
82  while (pos != std::string::npos) {
83  std::stringstream sBuff;
84  sBuff << s.substr(0, pos) << new_val
85  << s.substr(pos + val.size(), std::string::npos);
86  retVal = sBuff.str();
87  pos = retVal.find(val);
88  }
89  return retVal;
90  }
91 
92 } /* namespace gum */
93 
94 #ifdef GUM_NO_INLINE
96 #endif // GUM_NO_INLINE
std::string getUniqueFileName()
Returns a path to a unique file name.
Contains usefull methods for random stuff.
gum is the global namespace for all aGrUM entities
Definition: agrum.h:25
std::vector< std::string > split(const std::string &str, const std::string &delim)
Split str using the delimiter.
bool endsWith(std::string const &value, std::string const &ending)
Returns true if value ends with ending.
std::string replace(const std::string &s, const std::string &val, const std::string &new_val)
not usable for gcc 4.8 std::vector<std::string> split( const std::string& orig, const std::string& de...
Utilities for manipulating strings.