Geant4 11.4.0
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
LUPI::FileInfo Namespace Reference

Classes

class  FileStat

Functions

std::string realPath (std::string const &a_path)
std::string _basename (std::string const &a_path)
std::string basenameWithoutExtension (std::string const &a_path)
std::string _dirname (std::string const &a_path)
bool exists (std::string const &a_path)
bool isDirectory (std::string const &a_path)
bool createDirectories (std::string const &a_path)

Function Documentation

◆ _basename()

std::string LUPI::FileInfo::_basename ( std::string const & a_path)

Returns the base name of a path.

Parameters
a_path[in] The path whose base name is returned.

Definition at line 82 of file LUPI_file.cc.

82 {
83
84 char *path = new char[a_path.size( ) + 1];
85 strcpy( path, a_path.c_str( ) );
86 std::string basename1( basename( path ) );
87
88 delete[] path;
89
90 return( basename1 );
91}

◆ _dirname()

std::string LUPI::FileInfo::_dirname ( std::string const & a_path)

Returns the directory name of a path. This is, it removes the base name.

Parameters
a_path[in] The path whose base name is returned.

Definition at line 112 of file LUPI_file.cc.

112 {
113
114 char *path = new char[a_path.size( ) + 1];
115 strcpy( path, a_path.c_str( ) );
116 std::string dirname1( dirname( (char *) path ) );
117
118 delete[] path;
119
120 return( dirname1 );
121}

Referenced by createDirectories().

◆ basenameWithoutExtension()

std::string LUPI::FileInfo::basenameWithoutExtension ( std::string const & a_path)

Same as basename but removes, if a period (i.e., ".") exists in the string, the last "." and all following characters.

Parameters
a_path[in] The path whose base name is returned without its extension.

Definition at line 99 of file LUPI_file.cc.

99 {
100
101 std::size_t found = a_path.rfind( '.' );
102
103 return( a_path.substr( 0, found ) );
104}

Referenced by LUPI::ArgumentParser::parse().

◆ createDirectories()

bool LUPI::FileInfo::createDirectories ( std::string const & a_path)

Adds all needed directories to complete a_path.

Parameters
a_path[in] The path that is checked for existence.
Returns
Returns true if the path exists (e.g., created if it does not exists) and false otherwise.

Definition at line 165 of file LUPI_file.cc.

165 {
166
167 if( isDirectory( a_path ) ) return( true );
168 if( ( a_path == LUPI_FILE_SEPARATOR ) || ( a_path == "." ) || ( a_path == "" ) ) return( true );
169
170 std::string dirname1( _dirname( a_path ) );
171 if( createDirectories( dirname1 ) ) {
172#ifdef _WIN32
173 int status = _mkdir( a_path.c_str( ) );
174#else
175 int status = mkdir( a_path.c_str( ), S_IRWXU | S_IRWXG | S_IRWXG );
176#endif
177 if( status == 0 ) return( true );
178 switch( errno ) {
179 case EEXIST :
180 return( true );
181 default :
182 return( false );
183 }
184 }
185
186 return( false );
187}
#define LUPI_FILE_SEPARATOR
Definition LUPI.hpp:45
bool createDirectories(std::string const &a_path)
Definition LUPI_file.cc:165
std::string _dirname(std::string const &a_path)
Definition LUPI_file.cc:112
bool isDirectory(std::string const &a_path)
Definition LUPI_file.cc:146

Referenced by createDirectories().

◆ exists()

bool LUPI::FileInfo::exists ( std::string const & a_path)

Returns true if the path exists and false otherwise.

Parameters
a_path[in] The path that is checked for existence.

Definition at line 129 of file LUPI_file.cc.

129 {
130
131#ifdef _WIN32
132 return std::filesystem::exists( std::filesystem::path( a_path ) );
133#else
134 return( access( a_path.c_str( ), F_OK ) == 0 );
135#endif
136}

Referenced by GIDI::Map::Map::RIS_fileExist().

◆ isDirectory()

bool LUPI::FileInfo::isDirectory ( std::string const & a_path)

Returns true if path is a direction that exists and false otherwise.

Parameters
a_path[in] The path that is checked for existence and is it a directory.
Returns
Returns true if the path exists (e.g., created if it does not exists) and false otherwise.

Definition at line 146 of file LUPI_file.cc.

146 {
147
148 try {
149 FileStat fileStat( a_path );
150 return( fileStat.isDirectory( ) ); }
151 catch (...) {
152 }
153
154 return( false );
155}

Referenced by createDirectories().

◆ realPath()

std::string LUPI::FileInfo::realPath ( std::string const & a_path)

This function takes a file path and returns its real path. On a Unix system, the system function realpath is called.

Parameters
a_path[in] The path whose real path is to be determined.
Returns
The real path.

Definition at line 63 of file LUPI_file.cc.

63 {
64
65 char *p1 = realpath( a_path.c_str( ), nullptr );
66
67 if( p1 == nullptr ) {
68 std::string errMsg( "realPath: file does not exist: " );
69 throw Exception( errMsg + a_path );
70 }
71 std::string basePath( p1 );
72 free( p1 );
73 return( basePath );
74}
void free(voidpf ptr)

Referenced by GIDI::Map::BaseEntry::path().