aGrUM  0.18.1
a C++ library for (probabilistic) graphical models
utils_string.cpp
Go to the documentation of this file.
1 
30 #include <iterator>
31 #include <regex>
32 
34 
35 namespace gum {
36 
37  std::string getUniqueFileName() {
38 #ifdef HAVE_MKSTEMP
39  char tmpFileName_[] = "fileXXXXXX";
40  int fd = mkstemp(tmpFileName_);
41  close(fd);
42 #else // mainly Windows
43  char tmpFileName_[] = "fileXXXXXX";
44  _mktemp_s(tmpFileName_, strlen(tmpFileName_));
45 #endif
46 
47  return std::string(tmpFileName_);
48  }
49 
50  bool endsWith(std::string const& value, std::string const& ending) {
51  if (ending.size() > value.size()) return false;
52  return std::equal(ending.rbegin(), ending.rend(), value.rbegin());
53  }
54 
55  std::vector< std::string > split(const std::string& str,
56  const std::string& delim) {
57  std::vector< std::string > tokens;
58  size_t prev = 0, pos = 0;
59  do {
60  pos = str.find(delim, prev);
61  if (pos == std::string::npos) pos = str.length();
62  std::string token = str.substr(prev, pos - prev);
63  if (!token.empty()) tokens.push_back(token);
64  prev = pos + delim.length();
65  } while (pos < str.length() && prev < str.length());
66  return tokens;
67  }
68 
80  std::string replace(const std::string& s,
81  const std::string& val,
82  const std::string& new_val) {
83  auto retVal = s;
84  auto pos = retVal.find(val);
85  while (pos != std::string::npos) {
86  std::stringstream sBuff;
87  sBuff << s.substr(0, pos) << new_val
88  << s.substr(pos + val.size(), std::string::npos);
89  retVal = sBuff.str();
90  pos = retVal.find(val);
91  }
92  return retVal;
93  }
94 
95 } /* namespace gum */
96 
97 #ifdef GUM_NO_INLINE
99 #endif // GUM_NO_INLINE
std::string getUniqueFileName()
Returns a path to a unique file name.
Copyright 2005-2020 Pierre-Henri WUILLEMIN() & Christophe GONZALES() info_at_agrum_dot_org.
Copyright 2005-2020 Pierre-Henri WUILLEMIN() & Christophe GONZALES() info_at_agrum_dot_org.
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...
Copyright 2005-2020 Pierre-Henri WUILLEMIN() & Christophe GONZALES() info_at_agrum_dot_org.