libMesh
Loading...
Searching...
No Matches
utility.C
Go to the documentation of this file.
1// The libMesh Finite Element Library.
2// Copyright (C) 2002-2026 Benjamin S. Kirk, John W. Peterson, Roy H. Stogner
3
4// This library is free software; you can redistribute it and/or
5// modify it under the terms of the GNU Lesser General Public
6// License as published by the Free Software Foundation; either
7// version 2.1 of the License, or (at your option) any later version.
8
9// This library is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12// Lesser General Public License for more details.
13
14// You should have received a copy of the GNU Lesser General Public
15// License along with this library; if not, write to the Free Software
16// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
18
19// library configuration
20#include "libmesh/libmesh_config.h"
21#include "libmesh/libmesh_logging.h"
22
23// System includes
24#ifdef LIBMESH_HAVE_SYS_STAT_H
25#include <sys/stat.h>
26#endif
27#ifdef LIBMESH_HAVE_SYS_TYPES_H
28#include <sys/types.h>
29#endif
30#ifdef LIBMESH_HAVE_UNISTD_H
31#include <unistd.h> // for getuid(), getpid()
32#endif
33#include <sstream>
34
35#ifdef LIBMESH_HAVE_SYS_UTSNAME_H
36#include <sys/utsname.h>
37#endif
38
39#ifdef LIBMESH_HAVE_PWD_H
40#include <pwd.h>
41#endif
42
43#ifdef LIBMESH_HAVE_DIRECT_H
44#include <direct.h>
45#endif
46
47// Local includes
48#include "libmesh/utility.h"
49#include "libmesh/timestamp.h"
50
51namespace libMesh
52{
53
54
55//-----------------------------------------------------------------------
56// Utility members
57
58
59// The system_info function duplicates some of the
60// functionality found in the perf_log function.
61// This way you can get information about a user's
62// system without creating a perf_log object.
64{
65 std::ostringstream oss;
66
67 std::string date = Utility::get_timestamp();
68
69#ifdef LIBMESH_HAVE_SYS_UTSNAME_H
70 // Get system information
71 struct utsname sysInfo;
72 uname(&sysInfo);
73#endif
74
75 // Get user information
76#ifdef LIBMESH_HAVE_GETPWUID
77 struct passwd * p = getpwuid(getuid());
78#endif
79
80
81 oss << '\n'
82 << " ---------------------------------------------------------------------\n"
83 << "| Time: " << date << '\n'
84#ifdef LIBMESH_HAVE_SYS_UTSNAME_H
85 << "| OS: " << sysInfo.sysname << '\n'
86 << "| HostName: " << sysInfo.nodename << '\n'
87 << "| OS Release " << sysInfo.release << '\n'
88 << "| OS Version: " << sysInfo.version << '\n'
89 << "| Machine: " << sysInfo.machine << '\n'
90#else
91 << "| OS: " << "Unknown" << '\n'
92 << "| HostName: " << "Unknown" << '\n'
93 << "| OS Release " << "Unknown" << '\n'
94 << "| OS Version: " << "Unknown" << '\n'
95 << "| Machine: " << "Unknown" << '\n'
96#endif
97#ifdef LIBMESH_HAVE_GETPWUID
98 << "| Username: " << p->pw_name << '\n'
99#else
100 << "| Username: " << "Unknown" << '\n'
101#endif
102 << " ---------------------------------------------------------------------\n";
103
104 return oss.str();
105}
106
107
108std::string_view Utility::basename_of(const std::string & fullname)
109{
110 std::size_t first_char = fullname.find_last_of("/\\");
111 if (first_char == std::string::npos)
112 first_char = 0;
113 return std::string_view(fullname).substr(first_char);
114}
115
116
117#ifdef LIBMESH_USE_COMPLEX_NUMBERS
118
119std::string Utility::complex_filename (std::string basename,
120 const unsigned int r_o_c)
121{
122 if (r_o_c == 0)
123 basename.append(".real");
124
125 else
126 basename.append(".imag");
127
128 return basename;
129}
130
131
132
133void Utility::prepare_complex_data(const std::vector<Complex> & source,
134 std::vector<Real> & real_part,
135 std::vector<Real> & imag_part)
136{
137 const unsigned int len = source.size();
138
139 real_part.resize(len);
140 imag_part.resize(len);
141
142 for (unsigned int i=0; i<len; i++)
143 {
144 real_part[i] = source[i].real();
145 imag_part[i] = source[i].imag();
146 }
147}
148
149#endif // #ifdef LIBMESH_USE_COMPLEX_NUMBERS
150
151
152int Utility::mkdir(const char* pathname) {
153#if defined(LIBMESH_HAVE_MKDIR)
154 return ::mkdir(pathname, 0755);
155#elif LIBMESH_HAVE_DECL__MKDIR
156 return _mkdir(pathname);
157#else
158 libmesh_error_msg("Function mkdir not available on this system.");
159#endif
160
161}
162
163
164std::string Utility::unzip_file (std::string_view name)
165{
166 std::ostringstream pid_suffix;
167 pid_suffix << '_' << getpid();
168
169 std::string new_name { name };
170 if (Utility::ends_with(name, ".bz2"))
171 {
172#ifdef LIBMESH_HAVE_BZIP
173 new_name.erase(new_name.end() - 4, new_name.end());
174 new_name += pid_suffix.str();
175 LOG_SCOPE("system(bunzip2)", "Utility");
176 std::string system_string = "bunzip2 -f -k -c ";
177 system_string.append(name);
178 system_string += " > " + new_name;
179 if (std::system(system_string.c_str()))
180 libmesh_file_error(system_string);
181#else
182 libmesh_error_msg("ERROR: need bzip2/bunzip2 to open .bz2 file " << name);
183#endif
184 }
185 else if (Utility::ends_with(name, ".xz"))
186 {
187#ifdef LIBMESH_HAVE_XZ
188 new_name.erase(new_name.end() - 3, new_name.end());
189 new_name += pid_suffix.str();
190 LOG_SCOPE("system(xz -d)", "Utility");
191 std::string system_string = "xz -f -d -k -c ";
192 system_string.append(name);
193 system_string += " > " + new_name;
194 if (std::system(system_string.c_str()))
195 libmesh_file_error(system_string);
196#else
197 libmesh_error_msg("ERROR: need xz to open .xz file " << name);
198#endif
199 }
200 return new_name;
201}
202
203
204
205bool Utility::contains(std::string_view superstring, std::string_view substring)
206{
207 // This can just be C++23 contains() someday
208 return superstring.find(substring) != std::string::npos;
209}
210
211
212
213bool Utility::ends_with(std::string_view superstring,
214 std::string_view suffix)
215{
216 // This can just be C++20 ends_with() someday
217 const auto sufsize = suffix.size();
218 const auto supsize = superstring.size();
219 if (sufsize > supsize)
220 return false;
221
222 return superstring.compare(supsize - sufsize, std::string::npos,
223 suffix) == 0;
224}
225
226
227
228} // namespace libMesh
std::string get_timestamp()
Definition timestamp.C:37
int mkdir(const char *pathname)
Create a directory.
Definition utility.C:152
bool contains(std::string_view superstring, std::string_view substring)
Look for a substring within a string.
Definition utility.C:205
std::string system_info()
Definition utility.C:63
std::string complex_filename(std::string basename, unsigned int r_o_c=0)
Definition utility.C:119
void prepare_complex_data(const std::vector< Complex > &source, std::vector< Real > &real_part, std::vector< Real > &imag_part)
Prepare complex data for writing.
Definition utility.C:133
bool ends_with(std::string_view superstring, std::string_view suffix)
Look for a substring at the very end of a string.
Definition utility.C:213
std::string_view basename_of(const std::string &fullname)
Definition utility.C:108
std::string unzip_file(std::string_view name)
Create an unzipped copy of a bz2 or xz file, returning the name of the now-unzipped file that can be ...
Definition utility.C:164
The libMesh namespace provides an interface to certain functionality in the library.