aGrUM 2.3.2
a C++ library for (probabilistic) graphical models
utils_string.cpp
Go to the documentation of this file.
1/****************************************************************************
2 * This file is part of the aGrUM/pyAgrum library. *
3 * *
4 * Copyright (c) 2005-2025 by *
5 * - Pierre-Henri WUILLEMIN(_at_LIP6) *
6 * - Christophe GONZALES(_at_AMU) *
7 * *
8 * The aGrUM/pyAgrum library is free software; you can redistribute it *
9 * and/or modify it under the terms of either : *
10 * *
11 * - the GNU Lesser General Public License as published by *
12 * the Free Software Foundation, either version 3 of the License, *
13 * or (at your option) any later version, *
14 * - the MIT license (MIT), *
15 * - or both in dual license, as here. *
16 * *
17 * (see https://agrum.gitlab.io/articles/dual-licenses-lgplv3mit.html) *
18 * *
19 * This aGrUM/pyAgrum library is distributed in the hope that it will be *
20 * useful, but WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, *
21 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES MERCHANTABILITY or FITNESS *
22 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, *
25 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR *
26 * OTHER DEALINGS IN THE SOFTWARE. *
27 * *
28 * See LICENCES for more details. *
29 * *
30 * SPDX-FileCopyrightText: Copyright 2005-2025 *
31 * - Pierre-Henri WUILLEMIN(_at_LIP6) *
32 * - Christophe GONZALES(_at_AMU) *
33 * SPDX-License-Identifier: LGPL-3.0-or-later OR MIT *
34 * *
35 * Contact : info_at_agrum_dot_org *
36 * homepage : http://agrum.gitlab.io *
37 * gitlab : https://gitlab.com/agrumery/agrum *
38 * *
39 ****************************************************************************/
40
41
49#include <chrono>
50#include <filesystem>
51#include <iostream>
52#include <iterator>
53#include <regex>
54#include <string>
55
58
59namespace gum {
60
61 // get a unique file name using std::filesystem
62
63 std::string getUniqueFileName() {
64 // Get the temporary directory path
65 std::filesystem::path temp_dir = std::filesystem::temp_directory_path();
66
67 // Generate a unique file name using a timestamp and a random number
68 auto now = std::chrono::system_clock::now();
69 auto timestamp
70 = std::chrono::duration_cast< std::chrono::milliseconds >(now.time_since_epoch()).count();
71
72 auto dis = gum::randomValue(1000);
73
74 std::string filename
75 = "tempfile_" + std::to_string(timestamp) + "_" + std::to_string(dis) + ".tmp";
76
77 // Combine the directory path and the file name
78 std::filesystem::path temp_file_path = temp_dir / filename;
79
80 return temp_file_path.string();
81 }
82
83 bool endsWith(std::string const& value, std::string const& ending) {
84 if (ending.size() > value.size()) return false;
85 return std::equal(ending.rbegin(), ending.rend(), value.rbegin());
86 }
87
88 std::vector< std::string > split(const std::string& str, const std::string& delim) {
89 std::vector< std::string > tokens;
90 size_t prev = 0, pos = 0;
91 do {
92 pos = str.find(delim, prev);
93 if (pos == std::string::npos) pos = str.length();
94 std::string token = str.substr(prev, pos - prev);
95 if (!token.empty()) tokens.push_back(token);
96 prev = pos + delim.length();
97 } while (pos < str.length() && prev < str.length());
98 return tokens;
99 }
100
111
112 std::string replace(const std::string& s, const std::string& val, const std::string& new_val) {
113 auto retVal = s;
114 auto pos = retVal.find(val);
115 while (pos != std::string::npos) {
116 std::stringstream sBuff;
117 sBuff << s.substr(0, pos) << new_val << s.substr(pos + val.size(), std::string::npos);
118 retVal = sBuff.str();
119 pos = retVal.find(val);
120 }
121 return retVal;
122 }
123
124 bool isIntegerWithResult(const std::string& val, int* res) {
125 if (val.empty()) return false;
126 std::size_t pos = 0;
127 if ((val[0] == '+') || (val[0] == '-')) { pos = 1; }
128
129 if (val.find_first_not_of("0123456789", pos) != std::string::npos) return false;
130
131 if (res != nullptr) {
132 const char* p = (val[0] == '+') ? 1 + val.c_str() : val.c_str();
133 *res = std::stoi(p);
134 }
135
136 return true;
137 }
138
139 bool isNumericalWithResult(const std::string& val, double* res) {
140 char* endptr = nullptr;
141 const char* str = val.c_str();
142 double d = std::strtod(str, &endptr);
143
144 if (*endptr != '\0' || endptr == str) return false;
145 if (res != nullptr) *res = d;
146
147 return true;
148 }
149
150
151} /* namespace gum */
152
153#ifdef GUM_NO_INLINE
155#endif // GUM_NO_INLINE
std::string getUniqueFileName()
Returns a path to a unique file name.
Idx randomValue(const Size max=2)
Returns a random Idx between 0 and max-1 included.
bool isNumericalWithResult(const std::string &val, double *res)
return true is a string contains a numerical (double) value
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, ...
bool isIntegerWithResult(const std::string &val, int *res)
return true is a string contains an integer value
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.
gum is the global namespace for all aGrUM entities
Definition agrum.h:46
Contains useful methods for random stuff.
Utilities for manipulating strings.
Contains usefull methods for random stuff.