PhoenixMock  1.8.7
Tools to split/merge/print mock used in Phoenix
string_system.h File Reference
#include <sys/stat.h>
#include <sys/types.h>
#include "string_function.h"
+ Include dependency graph for string_system.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

bool createDirIfNotExist (const std::string &directoryName)
 Creates a directory if it does not exist. More...
 
std::string getCurrentNodeName ()
 Get the name of the current node on which the program is running. More...
 
time_t getFileInDirPerTime (std::vector< std::string > &vecFile, const std::string &dirName, time_t mostRecentTime=0lu)
 Get the list of most recent files in a directory. More...
 
time_t getFileModificationTime (const std::string &fileName)
 Get the last modification time of the given file. More...
 
std::string getHomeDir ()
 Gets the $HOME directory. More...
 
bool getListAllFileInDir (std::list< std::string > &listFile, const std::string &dirName)
 Get the list of files in a directory. More...
 
void getListFileInCurrentDir (std::list< std::string > &listFile, const std::string &expr)
 Function like a ls in shell. More...
 
bool getListFileInDir (std::list< std::string > &listFile, const std::string &dirName, const std::string &expr)
 Get the list of files in a directory. More...
 
std::string getProgramDirectory ()
 Get the program directory. More...
 
std::string getProgramLocation ()
 Get the program location. More...
 
std::string getProgramPrefix ()
 Get the program prefix (installation directory without /bin) More...
 
bool isStringMatchRegex (const std::string &str, const std::string &expression)
 Fonction qui dit si une chaine de caractère correspond à une expression régulière de regex. More...
 
void makeListArgument (std::list< std::string > &listArgument, int argc, char **argv)
 Makes the argument list of a program. More...
 
bool phoenix_chmod (const std::string &fileName, mode_t __mode=S_IRWXU|S_IRWXG|S_IROTH|S_IXOTH)
 Change the mode of a file or directory. More...
 
std::vector< std::string > phoenix_find (const std::string &path)
 Find all files which matches the path expression (example : /path/*.cpp) More...
 
void phoenix_find (std::vector< std::string > &vecFile, const std::string &path)
 Find all files which matches the path expression (example : /path/*.cpp) More...
 
time_t phoenix_getClock ()
 Get current time. More...
 
double phoenix_getClockSec ()
 Get current time. More...
 
std::string phoenix_getDate ()
 Get the current date. More...
 
std::string phoenix_getDateCompact ()
 Get the current date. More...
 
std::string phoenix_getenv (const std::string &varName)
 Get the value of the given environment variable. More...
 
time_t phoenix_getTime ()
 Get the current time of the program. More...
 
std::string phoenix_popen (const std::string &command)
 Execute the given command and returns the output of this command. More...
 
bool phoenix_popen (const std::string &executionLogFile, const std::string &command, bool onlyLogOnFail)
 Execute the given command and returns the output of this command. More...
 
int phoenix_popen (std::string &executionLog, const std::string &command)
 Execute the given command and returns the output of this command. More...
 
bool phoenix_setenv (const std::string &name, const std::string &value, int overwrite=1)
 Set a environment variable. More...
 
bool phoenix_unsetenv (const std::string &name)
 Unset a environment variable. More...
 

Function Documentation

◆ createDirIfNotExist()

bool createDirIfNotExist ( const std::string &  directoryName)

Creates a directory if it does not exist.

Parameters
directoryName: name of the directory we want to create
Returns
true on success, false otherwise

Definition at line 242 of file string_system.cpp.

242  {
243  if(directoryName == "") return false;
244  if(isDirectoryExist(directoryName)){
245  return true;
246  }
247  int res = mkdir(directoryName.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
248  return res == 0 || res == EEXIST;
249 }

References isDirectoryExist().

Referenced by testStringSystem().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ getCurrentNodeName()

std::string getCurrentNodeName ( )

Get the name of the current node on which the program is running.

Returns
name of the current node on which the program is running

Definition at line 406 of file string_system.cpp.

406  {
407  return eraseCharsInStr(phoenix_popen("uname -n"), " \n\t");
408 }

References eraseCharsInStr(), and phoenix_popen().

Referenced by testStringSystem().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ getFileInDirPerTime()

time_t getFileInDirPerTime ( std::vector< std::string > &  vecFile,
const std::string &  dirName,
time_t  mostRecentTime 
)

Get the list of most recent files in a directory.

Parameters
[out]vecFile: vector of found files (without directory name)
dirName: name of the directory to be scanned
mostRecentTime: threshold time to select only most recent files
Returns
time of the most recent file found in the directory which is newer than the input mostRecentTime

Definition at line 274 of file string_system.cpp.

274  {
275  DIR * dp = opendir(dirName.c_str());
276  if(dp == NULL){return mostRecentTime;}
277  dirent * dptr = readdir(dp);
278  while(NULL != dptr){
279  if(dptr->d_type == DT_REG){ //We search for directory only
280  std::string pathName(dptr->d_name);
281  time_t fileTime = getFileModificationTime(dirName + "/" + pathName);
282  if(fileTime > mostRecentTime){
283  mostRecentTime = fileTime;
284  vecFile.push_back(pathName);
285  }
286  }
287  dptr = readdir(dp);
288  }
289  return mostRecentTime;
290 }

References getFileModificationTime().

Referenced by testStringSystem().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ getFileModificationTime()

time_t getFileModificationTime ( const std::string &  fileName)

Get the last modification time of the given file.

Parameters
fileName: name of the file we want the last modification time
Returns
last modification time of the given file

Definition at line 255 of file string_system.cpp.

255  {
256  struct stat attr;
257  if(stat(fileName.c_str(), &attr) == 0){
258 #ifdef __APPLE__
259  return attr.st_mtimespec.tv_sec;
260 #else
261  return attr.st_mtim.tv_sec;
262 #endif
263  }else{
264  return -1l;
265  }
266 }

Referenced by getFileInDirPerTime(), and testStringSystem().

+ Here is the caller graph for this function:

◆ getHomeDir()

std::string getHomeDir ( )

Gets the $HOME directory.

Returns
$HOME directory

Definition at line 234 of file string_system.cpp.

234  {
235  return phoenix_getenv("HOME");
236 }

References phoenix_getenv().

Referenced by testStringSystem().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ getListAllFileInDir()

bool getListAllFileInDir ( std::list< std::string > &  listFile,
const std::string &  dirName 
)

Get the list of files in a directory.

Parameters
[out]listFile: list of files in the current directory
dirName: name of the directory to look in

Definition at line 171 of file string_system.cpp.

171  {
172  // Open the current directory
173  DIR * dp = opendir(dirName.c_str());
174  if(NULL == dp){
175  return false;
176  }
177  dirent * dptr = readdir(dp);
178  while(NULL != dptr){
179  std::string fileName(dptr->d_name);
180  if(fileName != ".." && fileName != "."){
181  listFile.push_back(fileName);
182  }
183  dptr = readdir(dp);
184  }
185  closedir(dp);
186  return true;
187 }

Referenced by testStringSystem().

+ Here is the caller graph for this function:

◆ getListFileInCurrentDir()

void getListFileInCurrentDir ( std::list< std::string > &  listFile,
const std::string &  expr 
)

Function like a ls in shell.

Parameters
listFile: list of the files witch match with expr
expr: expression like "name*.txt" or "*.dat" or "name_*_something_*.ext"

Definition at line 126 of file string_system.cpp.

126  {
127  char * curr_dir = getenv("PWD");
128  if(NULL == curr_dir){
129  printf("getListFileInCurrentDir : Could not get the working directory\n");
130  return;
131  }
132  // Open the current directory
133  DIR * dp = opendir((const char*)curr_dir);
134  if(NULL == dp){
135  printf("getListFileInCurrentDir : Could not open the working directory\n");
136  return;
137  }
138  dirent * dptr = readdir(dp);
139  while(NULL != dptr){
140  if(isStringMatchRegex(std::string(dptr->d_name), expr)) listFile.push_back(std::string(dptr->d_name));
141  dptr = readdir(dp);
142  }
143 }

References isStringMatchRegex().

Referenced by testStringSystem().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ getListFileInDir()

bool getListFileInDir ( std::list< std::string > &  listFile,
const std::string &  dirName,
const std::string &  expr 
)

Get the list of files in a directory.

Parameters
[out]listFile: list of files in the current directory
dirName: name of the directory to look in
expr: regular expression like *.txt or *

Definition at line 150 of file string_system.cpp.

150  {
151  // Open the current directory
152  DIR * dp = opendir(dirName.c_str());
153  if(NULL == dp){
154 // printf("getListFileInDir : Could not open the working directory\n");
155  return false;
156  }
157  dirent * dptr = readdir(dp);
158  while(NULL != dptr){
159  if(isStringMatchRegex(std::string(dptr->d_name), expr)) listFile.push_back(std::string(dptr->d_name));
160  std::cout << "getListFileInDir : '" << std::string(dptr->d_name) << "'"<< std::endl;
161  dptr = readdir(dp);
162  }
163  closedir(dp);
164  return true;
165 }

References isStringMatchRegex().

Referenced by testStringSystem().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ getProgramDirectory()

std::string getProgramDirectory ( )

Get the program directory.

Returns
directory of the program

Definition at line 317 of file string_system.cpp.

317  {
318  std::string progLoc(getProgramLocation());
319  if(progLoc != ""){
320  return getDirectory(progLoc);
321  }else{
322 #ifdef CMAKE_INSTALL_PREFIX
323  return CMAKE_INSTALL_PREFIX "/bin/";
324 #else
325  return "/usr/bin/";
326 #endif
327  }
328 }

References getDirectory(), and getProgramLocation().

Referenced by getProgramPrefix(), and testStringSystem().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ getProgramLocation()

std::string getProgramLocation ( )

Get the program location.

Returns
location of the current program

Definition at line 296 of file string_system.cpp.

296  {
297  char buffer[4096];
298  ssize_t nbChar = readlink("/proc/self/exe", buffer, 2048);
299 // readlink("/proc/self/exe", buf, bufsize) (Linux)
300 // readlink("/proc/curproc/file", buf, bufsize) (FreeBSD)
301 // readlink("/proc/self/path/a.out", buf, bufsize) (Solaris)
302 
303  if(nbChar > 0l){
304  std::string outputBuf("");
305  for(ssize_t i(0l); i < nbChar; ++i){
306  outputBuf += buffer[i];
307  }
308  return outputBuf;
309  }else{
310  return "";
311  }
312 }

Referenced by getProgramDirectory(), and testStringSystem().

+ Here is the caller graph for this function:

◆ getProgramPrefix()

std::string getProgramPrefix ( )

Get the program prefix (installation directory without /bin)

Returns
prefix of the program (installation directory without /bin)

Definition at line 333 of file string_system.cpp.

333  {
335 }

References getDirectory(), and getProgramDirectory().

Referenced by testStringSystem().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ isStringMatchRegex()

bool isStringMatchRegex ( const std::string &  str,
const std::string &  expression 
)

Fonction qui dit si une chaine de caractère correspond à une expression régulière de regex.

Parameters
str: string dont on veut savoir si elle correspond à une expression régulière
expression: expression régulière regex
Returns
true si str correspond à une expression régulière, false sinon

Definition at line 94 of file string_system.cpp.

94  {
95  if(str.size() == 0lu || expression.size() == 0lu){return false;}
96  int err;
97  regex_t preg;
98  const char *str_request = str.c_str();
99  const char *str_regex = expression.c_str();
100  err = regcomp(&preg, str_regex, 0); //REG_NOSUB | REG_EXTENDED
101  if(err != 0) return false;
102  int match = regexec(&preg, str_request, 0, NULL, 0);
103  regfree (&preg);
104  if(match == 0){return true;}
105  else{return false;}//if (match == REG_NOMATCH){return false;}
106 // else{
107 // char *text;
108 // long unsigned int size;
109 // size = regerror (err, &preg, NULL, 0);
110 // text = new char[size];
111 // if (text){
112 // regerror (err, &preg, text, size);
113 // fprintf (stderr, "isStringMatchRegex : %s\n", text);
114 // delete [] text;
115 // }else{
116 // fprintf (stderr, "isStringMatchRegex : Memoire insuffisante\n");
117 // }
118 // return false;
119 // }
120 }

References createReleaseCurl::str.

Referenced by getListFileInCurrentDir(), getListFileInDir(), testRegExpr(), and testStringSystem().

+ Here is the caller graph for this function:

◆ makeListArgument()

void makeListArgument ( std::list< std::string > &  listArgument,
int  argc,
char **  argv 
)

Makes the argument list of a program.

Parameters
[out]listArgument: list of the program arguments
argc: number of arguments passed to the program
argv: array of the passed arguments to the program

Definition at line 193 of file string_system.cpp.

193  {
194  if(argc <= 0) return;
195  for(int i(0); i < argc; ++i){
196  listArgument.push_back(argv[i]);
197  }
198 }

Referenced by main().

+ Here is the caller graph for this function:

◆ phoenix_chmod()

bool phoenix_chmod ( const std::string &  fileName,
mode_t  __mode 
)

Change the mode of a file or directory.

Parameters
fileName: name of the file to be changed
__mode: mode to be applied to the given file (Default value makes files executable S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)
Returns
true on success, false otherwise

Definition at line 395 of file string_system.cpp.

395  {
396  bool b(chmod(fileName.c_str(), __mode) >= 0);
397  if(!b){
398  std::cerr << "phoenix_chmod : Cannot set mode of file '"<<fileName<<"'" << std::endl;
399  }
400  return b;
401 }

Referenced by testStringSystem().

+ Here is the caller graph for this function:

◆ phoenix_find() [1/2]

std::vector<std::string> phoenix_find ( const std::string &  path)

Find all files which matches the path expression (example : /path/*.cpp)

Parameters
path: path which can matches several files
Returns
vector of found files

Definition at line 422 of file string_system.cpp.

422  {
423  std::vector<std::string> vecFile;
424  phoenix_find(vecFile, path);
425  return vecFile;
426 }

References phoenix_find().

+ Here is the call graph for this function:

◆ phoenix_find() [2/2]

void phoenix_find ( std::vector< std::string > &  vecFile,
const std::string &  path 
)

Find all files which matches the path expression (example : /path/*.cpp)

Parameters
[out]vecFile: vector of found files
path: path which can matches several files

Definition at line 414 of file string_system.cpp.

414  {
415  vecFile = cutStringVector(phoenix_popen("ls " + path), '\n');
416 }

References cutStringVector(), and phoenix_popen().

Referenced by phoenix_find(), and testStringSystem().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ phoenix_getClock()

time_t phoenix_getClock ( )

Get current time.

Returns
current time

Definition at line 431 of file string_system.cpp.

431  {
432  return clock();
433 }

Referenced by phoenix_getClockSec().

+ Here is the caller graph for this function:

◆ phoenix_getClockSec()

double phoenix_getClockSec ( )

Get current time.

Returns
current time

Definition at line 438 of file string_system.cpp.

438  {
439  return ((double)phoenix_getClock())/((double)CLOCKS_PER_SEC);
440 }

References phoenix_getClock().

+ Here is the call graph for this function:

◆ phoenix_getDate()

std::string phoenix_getDate ( )

Get the current date.

Returns
current date

Definition at line 452 of file string_system.cpp.

452  {
453  std::time_t currentTime = phoenix_getTime();
454  std::tm* now_tm = std::gmtime(&currentTime);
455  char buf[42];
456  std::strftime(buf, 42, "%Y/%m/%d : %X", now_tm);
457  return buf;
458 }

References phoenix_getTime().

Referenced by PLog::close(), and PLog::open().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ phoenix_getDateCompact()

std::string phoenix_getDateCompact ( )

Get the current date.

Returns
current date

Definition at line 463 of file string_system.cpp.

463  {
464  std::time_t currentTime = phoenix_getTime();
465  std::tm* now_tm = std::gmtime(&currentTime);
466  char buf[42];
467  std::strftime(buf, 42, "%Y/%m/%d-%X", now_tm);
468  return buf;
469 }

References phoenix_getTime().

Referenced by PLog::getLog().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ phoenix_getenv()

std::string phoenix_getenv ( const std::string &  varName)

Get the value of the given environment variable.

Parameters
varName: name of the environment variable to be used
Returns
value of the variable, or empty string of the variable does not exist

Definition at line 204 of file string_system.cpp.

204  {
205  char * curr_var = getenv(varName.c_str());
206  if(NULL == curr_var){
207  return "";
208  }else{
209  return std::string(curr_var);
210  }
211 }

Referenced by getCurrentDirectory(), getHomeDir(), and testStringSystem().

+ Here is the caller graph for this function:

◆ phoenix_getTime()

time_t phoenix_getTime ( )

Get the current time of the program.

Returns
current time of the program

Definition at line 445 of file string_system.cpp.

445  {
446  return std::time(0);
447 }

Referenced by PLog::getLog(), phoenix_getDate(), and phoenix_getDateCompact().

+ Here is the caller graph for this function:

◆ phoenix_popen() [1/3]

std::string phoenix_popen ( const std::string &  command)

Execute the given command and returns the output of this command.

Parameters
command: command to be executed
Returns
output of the given command, empty string if the command is empty or null character on fail

Definition at line 341 of file string_system.cpp.

341  {
342  if(command == ""){return "";}
343  FILE * fp = popen(command.c_str(), "r");
344  if(fp == NULL){
345  std::cerr << "phoenix_popen : cannot get result of command '"<<command<<"'" << std::endl;
346  return "";
347  }
348  std::string resultCommand(getFileContent(fp));
349  pclose(fp);
350  return resultCommand;
351 }

References getFileContent().

Referenced by checkOptionCompletion(), getCurrentNodeName(), phoenix_find(), phoenix_popen(), and testPoenixPOpen().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ phoenix_popen() [2/3]

bool phoenix_popen ( const std::string &  executionLogFile,
const std::string &  command,
bool  onlyLogOnFail 
)

Execute the given command and returns the output of this command.

Parameters
[out]executionLogFile: file which will get output of the given command, empty string if the command is empty or full log on fail
command: command to be executed
onlyLogOnFail: true to log only if the command fails
Returns
true if the command was successful, false otherwise (in this case, log file will be created)

Definition at line 376 of file string_system.cpp.

376  {
377  std::string executionLog("");
378  bool b(phoenix_popen(executionLog, command) == 0);
379  if(!b){
380  std::cerr << "phoenix_popen : command '"<<command<<"' failed. To get more information see log '"<<executionLogFile<<"'" << std::endl;
381  }
382  if((onlyLogOnFail && !b) || !onlyLogOnFail){
383  if(!saveFileContent(executionLogFile, executionLog)){
384  std::cerr << "phoenix_popen : cannot create log file '"<<executionLogFile<<"'" << std::endl;
385  }
386  }
387  return b;
388 }

References phoenix_popen(), and saveFileContent().

+ Here is the call graph for this function:

◆ phoenix_popen() [3/3]

int phoenix_popen ( std::string &  executionLog,
const std::string &  command 
)

Execute the given command and returns the output of this command.

Parameters
[out]executionLog: output of the given command, empty string if the command is empty or null character on fail
command: command to be executed
Returns
exit status of the command

Definition at line 358 of file string_system.cpp.

358  {
359  executionLog = "";
360  if(command == ""){return -1;}
361  FILE * fp = popen(command.c_str(), "r");
362  if(fp == NULL){
363  std::cerr << "phoenix_popen : cannot get result of command '"<<command<<"'" << std::endl;
364  return -1;
365  }
366  executionLog = getFileContent(fp);
367  return pclose(fp);
368 }

References getFileContent().

+ Here is the call graph for this function:

◆ phoenix_setenv()

bool phoenix_setenv ( const std::string &  name,
const std::string &  value,
int  overwrite 
)

Set a environment variable.

Parameters
name: name of the variable to be created
value: value of the variable to be created
overwrite: 1 to overwrite an existing variable, 0 to not to
Returns
true on success, false otherwise

Definition at line 219 of file string_system.cpp.

219  {
220  return setenv(name.c_str(), value.c_str(), overwrite) == 0;
221 }

Referenced by testStringSystem().

+ Here is the caller graph for this function:

◆ phoenix_unsetenv()

bool phoenix_unsetenv ( const std::string &  name)

Unset a environment variable.

Parameters
name: name of the variable to be unset
Returns
true on success, false otherwise

Definition at line 227 of file string_system.cpp.

227  {
228  return unsetenv(name.c_str()) == 0;
229 }

Referenced by testStringSystem().

+ Here is the caller graph for this function:
getFileModificationTime
time_t getFileModificationTime(const std::string &fileName)
Get the last modification time of the given file.
Definition: string_system.cpp:255
phoenix_getenv
std::string phoenix_getenv(const std::string &varName)
Get the value of the given environment variable.
Definition: string_system.cpp:204
saveFileContent
bool saveFileContent(const std::string &filename, const std::string &content)
Save a string in a file.
Definition: string_filename.cpp:300
isDirectoryExist
bool isDirectoryExist(const std::string &dirName)
Says if the given direcotry exists.
Definition: string_filename.cpp:57
getFileContent
std::string getFileContent(const std::string &filename)
Get the file content in a string.
Definition: string_filename.cpp:268
isStringMatchRegex
bool isStringMatchRegex(const std::string &str, const std::string &expression)
Fonction qui dit si une chaine de caractère correspond à une expression régulière de regex.
Definition: string_system.cpp:94
phoenix_popen
std::string phoenix_popen(const std::string &command)
Execute the given command and returns the output of this command.
Definition: string_system.cpp:341
phoenix_getClock
time_t phoenix_getClock()
Get current time.
Definition: string_system.cpp:431
getProgramDirectory
std::string getProgramDirectory()
Get the program directory.
Definition: string_system.cpp:317
eraseCharsInStr
std::string eraseCharsInStr(const std::string &str, const std::string &rmchs)
copie la string str en effaçant les caractères rmchs
Definition: string_function.cpp:173
phoenix_getTime
time_t phoenix_getTime()
Get the current time of the program.
Definition: string_system.cpp:445
createReleaseCurl.str
str
Definition: createReleaseCurl.py:128
getProgramLocation
std::string getProgramLocation()
Get the program location.
Definition: string_system.cpp:296
phoenix_find
void phoenix_find(std::vector< std::string > &vecFile, const std::string &path)
Find all files which matches the path expression (example : /path/*.cpp)
Definition: string_system.cpp:414
cutStringVector
std::vector< std::string > cutStringVector(const std::string &str, char separator)
Cut a string the the given separator char.
Definition: string_function.cpp:448
getDirectory
std::string getDirectory(const std::string &fileName)
fonction qui renvoie le dossier parent du fichier
Definition: string_filename.cpp:156