LCOV - code coverage report
Current view: top level - src/utils - utility.C (source / functions) Hit Total Coverage
Test: libMesh/libmesh: #4229 (6a9aeb) with base 727f46 Lines: 24 62 38.7 %
Date: 2025-08-19 19:27:09 Functions: 5 8 62.5 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : // The libMesh Finite Element Library.
       2             : // Copyright (C) 2002-2025 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             : 
      51             : namespace 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.
      63           0 : std::string Utility::system_info()
      64             : {
      65           0 :   std::ostringstream oss;
      66             : 
      67           0 :   std::string date = Utility::get_timestamp();
      68             : 
      69             : #ifdef LIBMESH_HAVE_SYS_UTSNAME_H
      70             :   // Get system information
      71             :   struct utsname sysInfo;
      72           0 :   uname(&sysInfo);
      73             : #endif
      74             : 
      75             :   // Get user information
      76             : #ifdef LIBMESH_HAVE_GETPWUID
      77           0 :   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           0 :       << "| Username:       " << p->pw_name       << '\n'
      99             : #else
     100             :       << "| Username:       " << "Unknown"        << '\n'
     101             : #endif
     102           0 :       << " ---------------------------------------------------------------------\n";
     103             : 
     104           0 :   return oss.str();
     105           0 : }
     106             : 
     107             : 
     108        9138 : std::string_view Utility::basename_of(const std::string & fullname)
     109             : {
     110        8348 :   std::size_t first_char = fullname.find_last_of("/\\");
     111        9138 :   if (first_char == std::string::npos)
     112         728 :     first_char = 0;
     113        9138 :   return std::string_view(fullname).substr(first_char);
     114             : }
     115             : 
     116             : 
     117             : #ifdef LIBMESH_USE_COMPLEX_NUMBERS
     118             : 
     119           0 : std::string Utility::complex_filename (std::string basename,
     120             :                                        const unsigned int r_o_c)
     121             : {
     122           0 :   if (r_o_c == 0)
     123           0 :     basename.append(".real");
     124             : 
     125             :   else
     126           0 :     basename.append(".imag");
     127             : 
     128           0 :   return basename;
     129             : }
     130             : 
     131             : 
     132             : 
     133           0 : void Utility::prepare_complex_data(const std::vector<Complex> & source,
     134             :                                    std::vector<Real> & real_part,
     135             :                                    std::vector<Real> & imag_part)
     136             : {
     137           0 :   const unsigned int len = source.size();
     138             : 
     139           0 :   real_part.resize(len);
     140           0 :   imag_part.resize(len);
     141             : 
     142           0 :   for (unsigned int i=0; i<len; i++)
     143             :     {
     144           0 :       real_part[i] = source[i].real();
     145           0 :       imag_part[i] = source[i].imag();
     146             :     }
     147           0 : }
     148             : 
     149             : #endif // #ifdef LIBMESH_USE_COMPLEX_NUMBERS
     150             : 
     151             : 
     152        1516 : int Utility::mkdir(const char* pathname) {
     153             : #if defined(LIBMESH_HAVE_MKDIR)
     154        1516 :   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             : 
     164        3120 : std::string Utility::unzip_file (std::string_view name)
     165             : {
     166        3634 :   std::ostringstream pid_suffix;
     167        3120 :   pid_suffix << '_' << getpid();
     168             : 
     169        2863 :   std::string new_name { name };
     170        3120 :   if (Utility::ends_with(name, ".bz2"))
     171             :     {
     172             : #ifdef LIBMESH_HAVE_BZIP
     173           0 :       new_name.erase(new_name.end() - 4, new_name.end());
     174           0 :       new_name += pid_suffix.str();
     175           0 :       LOG_SCOPE("system(bunzip2)", "Utility");
     176           0 :       std::string system_string = "bunzip2 -f -k -c ";
     177           0 :       system_string.append(name);
     178           0 :       system_string += " > " + new_name;
     179           0 :       if (std::system(system_string.c_str()))
     180           0 :         libmesh_file_error(system_string);
     181             : #else
     182             :       libmesh_error_msg("ERROR: need bzip2/bunzip2 to open .bz2 file " << name);
     183             : #endif
     184             :     }
     185        3120 :   else if (Utility::ends_with(name, ".xz"))
     186             :     {
     187             : #ifdef LIBMESH_HAVE_XZ
     188           0 :       new_name.erase(new_name.end() - 3, new_name.end());
     189           0 :       new_name += pid_suffix.str();
     190           0 :       LOG_SCOPE("system(xz -d)", "Utility");
     191           0 :       std::string system_string = "xz -f -d -k -c ";
     192           0 :       system_string.append(name);
     193           0 :       system_string += " > " + new_name;
     194           0 :       if (std::system(system_string.c_str()))
     195           0 :         libmesh_file_error(system_string);
     196             : #else
     197             :       libmesh_error_msg("ERROR: need xz to open .xz file " << name);
     198             : #endif
     199             :     }
     200        3377 :   return new_name;
     201        2606 : }
     202             : 
     203             : 
     204             : 
     205       90716 : bool Utility::contains(std::string_view superstring, std::string_view substring)
     206             : {
     207             :   // This can just be C++23 contains() someday
     208       90716 :   return superstring.find(substring) != std::string::npos;
     209             : }
     210             : 
     211             : 
     212             : 
     213       46385 : bool Utility::ends_with(std::string_view superstring,
     214             :                         std::string_view suffix)
     215             : {
     216             :   // This can just be C++20 ends_with() someday
     217        5307 :   const auto sufsize = suffix.size();
     218       10614 :   const auto supsize = superstring.size();
     219       46385 :   if (sufsize > supsize)
     220          11 :     return false;
     221             : 
     222       46156 :   return superstring.compare(supsize - sufsize, std::string::npos,
     223       46156 :                              suffix) == 0;
     224             : }
     225             : 
     226             : 
     227             : 
     228             : } // namespace libMesh

Generated by: LCOV version 1.14