PhoenixMock  1.8.7
Tools to split/merge/print mock used in Phoenix
string_filename.h File Reference
#include <stdio.h>
#include <string>
#include <vector>
+ Include dependency graph for string_filename.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

bool checkFileBegning (const std::string &fileName, const std::string &expectedBegining)
 Check if the given file starts with the given begning. More...
 
bool createDirectoriesIfNotExist (const std::string &dirName)
 Create the directory if not exists. More...
 
std::string eraseExtension (const std::string &fileName)
 Erase extention of the given file. More...
 
std::string eraseLongestExtension (const std::string &fileName)
 Erase longest extention of the given file. More...
 
std::string getCurrentDirectory ()
 Returns the current directory. More...
 
std::string getDirectory (const std::string &fileName)
 fonction qui renvoie le dossier parent du fichier More...
 
std::string getDirName (const std::string &path)
 Get the name of the deeper directory. More...
 
std::string getExistingFileName (const std::string &fileName, const std::vector< std::string > &vecDirectory)
 Get the fileName with the directory to get a readable file. More...
 
std::string getExtention (const std::string &fileName)
 Get file extention. More...
 
std::string getFileContent (const std::string &filename)
 Get the file content in a string. More...
 
std::string getFileContent (FILE *fp)
 Get the file content in a string. More...
 
std::string getFileName (const std::string &fileName)
 fonction qui renvoie le nom du fichier du nom complet de fichier passé en paramètre More...
 
std::string getLongestExtention (const std::string &fileName)
 Get the longest file extention. More...
 
std::string getUnderPath (const std::string &fileName, const std::string &pathPart)
 Get path which is under the given pathPart ('some/dir/path' with 'dir' will return 'path') More...
 
std::vector< std::string > getUnderPath (const std::vector< std::string > &vecFileName, const std::string &pathPart)
 Get path which is under the given pathPart ('some/dir/path' with 'dir' will return 'path') More...
 
bool isAbsolutePath (const std::string &path)
 Tel if a path is absolute or not. More...
 
bool isDirectoryExist (const std::string &dirName)
 Says if the given direcotry exists. More...
 
bool isFileExist (const std::string &fileName)
 Say if a file exsits or not. More...
 
bool isFileOrDirExist (const std::string &fileName)
 Say if the given path name exsits or not. More...
 
std::string makeAbsolutePath (const std::string &path)
 Make an absolute path of the given path. More...
 
std::vector< std::string > makeAbsolutePath (const std::vector< std::string > &vecPath)
 Make an absolute path of the vector of given paths. More...
 
std::string removePathDots (const std::string path)
 Remove dots from the path. More...
 
bool saveFileContent (const std::string &filename, const std::string &content)
 Save a string in a file. More...
 
bool saveFileContent (FILE *fp, const std::string &content)
 Save a string in a file. More...
 

Function Documentation

◆ checkFileBegning()

bool checkFileBegning ( const std::string &  fileName,
const std::string &  expectedBegining 
)

Check if the given file starts with the given begning.

Parameters
fileName: name of the file to be checked
expectedBegining: expected begening of the file
Returns
true if the file starts with the expected begning, false otherwise

Definition at line 212 of file string_filename.cpp.

212  {
213  FILE* fp = fopen(fileName.c_str(), "r");
214  if(fp == NULL || expectedBegining.size() == 0lu){return false;}
215  int val(0);
216  size_t i(0lu);
217  bool isMatch(true);
218  while(isMatch && !feof(fp) && i < expectedBegining.size()){
219  val = fgetc(fp);
220  isMatch = (char)val == expectedBegining[i];
221  ++i;
222  }
223  fclose(fp);
224  return isMatch;
225 }

Referenced by testFileBegning().

+ Here is the caller graph for this function:

◆ createDirectoriesIfNotExist()

bool createDirectoriesIfNotExist ( const std::string &  dirName)

Create the directory if not exists.

Parameters
dirName: name of the directory
Returns
true on success, false otherwise

Definition at line 405 of file string_filename.cpp.

405  {
406  std::list<std::string> listDir(cutStringList(dirName, '/'));
407  std::string tmpDirName("");
408  bool isNotFirst(false);
409  for(std::list<std::string>::iterator it(listDir.begin()); it != listDir.end(); ++it){
410  if(isNotFirst){
411  tmpDirName += "/";
412  }
413  tmpDirName += *it;
414  if(tmpDirName != ""){
415  if(!isDirectoryExist(tmpDirName)){
416  if(mkdir(tmpDirName.c_str(), 0755) != 0){
417  cerr << "createDirectoryIfNotExist : can't create directory '" << tmpDirName << "'" << endl;
418  return false;
419  }
420  }
421  }
422  isNotFirst = true;
423  }
424  return true;
425 }

References cutStringList(), and isDirectoryExist().

Referenced by testStringFilename().

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

◆ eraseExtension()

std::string eraseExtension ( const std::string &  fileName)

Erase extention of the given file.

Parameters
fileName: input file name
Returns
filen name without extention

Definition at line 368 of file string_filename.cpp.

368  {
369  int nbPoint(countNbChar(fileName, '.'));
370  if(nbPoint == 0) return fileName;
371  std::string buffer("");
372  int findNbPoint(0);
373  std::string::const_iterator it = fileName.begin();
374  while(it != fileName.end() && findNbPoint < nbPoint){
375  if(*it == '.'){
376  findNbPoint++;
377  if(findNbPoint < nbPoint){
378  buffer += *it;
379  }
380  }else{
381  buffer += *it;
382  }
383  it++;
384  }
385  return buffer;
386 }

References countNbChar().

Referenced by PLog::resize(), splitMock(), and testStringFilename().

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

◆ eraseLongestExtension()

std::string eraseLongestExtension ( const std::string &  fileName)

Erase longest extention of the given file.

Parameters
fileName: input file name
Returns
filen name without longest extention

Definition at line 392 of file string_filename.cpp.

392  {
393  std::string longestExtention(getLongestExtention(fileName));
394  if(longestExtention != ""){
395  return copyStr(fileName, 0lu, fileName.size() - (longestExtention.size() + 1lu));
396  }else{
397  return fileName;
398  }
399 }

References copyStr(), and getLongestExtention().

Referenced by testStringFilename().

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

◆ getCurrentDirectory()

std::string getCurrentDirectory ( )

Returns the current directory.

Returns
current directory

Definition at line 69 of file string_filename.cpp.

69  {
70 #ifndef __APPLE__
71  char* ptr = get_current_dir_name();
72  std::string str(ptr);
73  free(ptr);
74  return str;
75 #else
76  return phoenix_getenv("PWD");
77 #endif
78 }

References phoenix_getenv(), and createReleaseCurl::str.

Referenced by makeAbsolutePath(), and testStringFilename().

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

◆ getDirectory()

std::string getDirectory ( const std::string &  fileName)

fonction qui renvoie le dossier parent du fichier

Parameters
fileName: nom du fichier dont on veut le dossier
Returns
nom du dossier parent du fichier passé en paramètre

Definition at line 156 of file string_filename.cpp.

156  {
157  std::string buffer("");
158  bool find(false);
159  std::string::const_reverse_iterator rit = fileName.rbegin();
160  while(rit != fileName.rend()){
161  if(find){
162  buffer = *rit + buffer;
163  }else{
164  find = (*rit == '/');
165  }
166  rit++;
167  }
168  return buffer;
169 }

Referenced by getProgramDirectory(), getProgramPrefix(), path_completion_all(), path_completion_dirOnly(), and testStringFilename().

+ Here is the caller graph for this function:

◆ getDirName()

std::string getDirName ( const std::string &  path)

Get the name of the deeper directory.

Parameters
path: input path
Returns
name of the deeper directory

Definition at line 246 of file string_filename.cpp.

246  {
247  std::string buffer("");
248  std::string::const_reverse_iterator rit = path.rbegin();
249  while(rit != path.rend()){
250  if(*rit == '/'){
251  if(buffer != ""){
252  break;
253  }else{
254  rit++;
255  continue;
256  }
257  }
258  buffer = *rit + buffer;
259  rit++;
260  }
261  return buffer;
262 }

Referenced by testStringFilename().

+ Here is the caller graph for this function:

◆ getExistingFileName()

std::string getExistingFileName ( const std::string &  fileName,
const std::vector< std::string > &  vecDirectory 
)

Get the fileName with the directory to get a readable file.

Parameters
fileName: file name to be opened
vecDirectory: vector of possible directories to look at
Returns
readable file name with appropriate directory, or empty string if the file is not found

Definition at line 42 of file string_filename.cpp.

42  {
43  if(vecDirectory.size() == 0lu || fileName == ""){return "";}
44  std::vector<std::string>::const_iterator it(vecDirectory.begin());
45  while(it != vecDirectory.end()){
46  std::string tmpFile(*it + "/" + fileName);
47  if(isFileExist(tmpFile)){return tmpFile;}
48  ++it;
49  }
50  return "";
51 }

References isFileExist().

Referenced by testStringFilename().

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

◆ getExtention()

std::string getExtention ( const std::string &  fileName)

Get file extention.

Parameters
fileName: input file name
Returns
file extention

Definition at line 326 of file string_filename.cpp.

326  {
327  if(!findInString(fileName, '.')) return "";
328  std::string extention("");
329  bool run(true);
330  std::string::const_reverse_iterator rit(fileName.rbegin());
331  while(run && rit != fileName.rend()){
332  if(*rit == '.' || *rit == '/') run = false;
333  else extention = *rit + extention;
334  rit++;
335  }
336  return extention;
337 }

References findInString().

Referenced by PLog::resize(), splitMock(), and testStringFilename().

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

◆ getFileContent() [1/2]

std::string getFileContent ( const std::string &  filename)

Get the file content in a string.

Parameters
filename: file name
Returns
string content of the file

Definition at line 268 of file string_filename.cpp.

268  {
269  FILE * fp = fopen(filename.c_str(), "r");
270  if(fp == NULL){
271  std::cerr << "getFileContent : cannot open file '" << filename << "'" << std::endl;
272  return "";
273  }
274  std::string tmp(getFileContent(fp));
275  fclose(fp);
276  return tmp;
277 }

References getFileContent().

Referenced by getFileContent(), phoenix_check_fileContent(), phoenix_popen(), and testStringFilename().

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

◆ getFileContent() [2/2]

std::string getFileContent ( FILE *  fp)

Get the file content in a string.

Parameters
fp: pointer to the file
Returns
string content of the file

Definition at line 283 of file string_filename.cpp.

283  {
284  if(fp == NULL) return "";
285  std::string bufferAllFile("");
286  int buffer;
287  while(!feof(fp)){
288  buffer = fgetc(fp);
289  if(buffer != EOF) bufferAllFile += (char)buffer;
290  else break;
291  }
292  return bufferAllFile;
293 }

◆ getFileName()

std::string getFileName ( const std::string &  fileName)

fonction qui renvoie le nom du fichier du nom complet de fichier passé en paramètre

Parameters
fileName: nom complet de fichier dont on veut le nom (/nom/du/fichier -> fichier)
Returns
nom du fichier passé en paramètre

Definition at line 231 of file string_filename.cpp.

231  {
232  std::string buffer("");
233  std::string::const_reverse_iterator rit = fileName.rbegin();
234  while(rit != fileName.rend()){
235  if(*rit == '/') break;
236  buffer = *rit + buffer;
237  rit++;
238  }
239  return buffer;
240 }

Referenced by path_completion_all(), path_completion_dirOnly(), and testStringFilename().

+ Here is the caller graph for this function:

◆ getLongestExtention()

std::string getLongestExtention ( const std::string &  fileName)

Get the longest file extention.

Parameters
fileName: input file name
Returns
longest file extention

Definition at line 343 of file string_filename.cpp.

343  {
344  size_t nbPoint(countNbChar(fileName, '.'));
345  if(nbPoint == 0lu) return "";
346  std::string extention(""), tmpExtention(""), middleDot("");
347  bool run(true);
348  std::string::const_reverse_iterator rit(fileName.rbegin());
349  while(run && rit != fileName.rend()){
350  if(*rit == '/'){run = false;}
351  else if(*rit == '.'){
352  extention = tmpExtention + middleDot + extention;
353  tmpExtention = "";
354  middleDot = ".";
355  }else{
356  tmpExtention = *rit + tmpExtention;
357  }
358 
359  rit++;
360  }
361  return extention;
362 }

References countNbChar().

Referenced by eraseLongestExtension(), and testStringFilename().

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

◆ getUnderPath() [1/2]

std::string getUnderPath ( const std::string &  fileName,
const std::string &  pathPart 
)

Get path which is under the given pathPart ('some/dir/path' with 'dir' will return 'path')

Parameters
fileName: path to be used
pathPart: directory in the fileName we are looking for
Returns
rest of the path under the pathPart

Definition at line 176 of file string_filename.cpp.

176  {
177  if(pathPart == ""){return fileName;}
178  std::list<std::string> listDir(cutStringList(fileName, '/'));
179  std::list<std::string>::iterator it(listDir.begin());
180  bool isSearch(true);
181  while(isSearch && it != listDir.end()){
182  isSearch = *it != pathPart;
183  ++it;
184  }
185  std::string out(""), separator("");
186  while(it != listDir.end()){
187  out += separator + (*it);
188  separator = "/";
189  ++it;
190  }
191  return out;
192 }

References cutStringList().

Referenced by getUnderPath(), and testStringFilename().

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

◆ getUnderPath() [2/2]

std::vector<std::string> getUnderPath ( const std::vector< std::string > &  vecFileName,
const std::string &  pathPart 
)

Get path which is under the given pathPart ('some/dir/path' with 'dir' will return 'path')

Parameters
vecFileName: vector of paths to be used
pathPart: directory in the fileName we are looking for
Returns
vector of the rest of the path under the pathPart

Definition at line 199 of file string_filename.cpp.

199  {
200  std::vector<std::string> vecOut;
201  for(std::vector<std::string>::const_iterator it(vecFileName.begin()); it != vecFileName.end(); ++it){
202  vecOut.push_back(getUnderPath(*it, pathPart));
203  }
204  return vecOut;
205 }

References getUnderPath().

+ Here is the call graph for this function:

◆ isAbsolutePath()

bool isAbsolutePath ( const std::string &  path)

Tel if a path is absolute or not.

Parameters
path: path to be checked
Returns
true if the path is absolute, false otherwise

Definition at line 84 of file string_filename.cpp.

84  {
85  if(path == ""){return false;}
86  return path[0] == '/';
87 }

Referenced by testStringFilename().

+ Here is the caller graph for this function:

◆ isDirectoryExist()

bool isDirectoryExist ( const std::string &  dirName)

Says if the given direcotry exists.

Parameters
dirName: name of the directory
Returns
true if the directory exists, false if not

Definition at line 57 of file string_filename.cpp.

57  {
58  if(dirName == ""){return false;}
59  struct stat path_stat;
60  if(stat(dirName.c_str(), &path_stat) != 0){
61  return false;
62  }
63  return S_ISDIR(path_stat.st_mode) != 0;
64 }

Referenced by createDirectoriesIfNotExist(), createDirIfNotExist(), path_completion_all(), path_completion_dirOnly(), and testStringFilename().

+ Here is the caller graph for this function:

◆ isFileExist()

bool isFileExist ( const std::string &  fileName)

Say if a file exsits or not.

Parameters
fileName: name of the file to be checked
Returns
true if the file exsits, false otherwise

Definition at line 27 of file string_filename.cpp.

27  {
28  if(fileName == ""){return false;}
29  struct stat path_stat;
30  if(stat(fileName.c_str(), &path_stat) != 0){
31  return false;
32  }
33  return S_ISREG(path_stat.st_mode) != 0;
34 }

Referenced by getExistingFileName(), path_completion_all(), and testStringFilename().

+ Here is the caller graph for this function:

◆ isFileOrDirExist()

bool isFileOrDirExist ( const std::string &  fileName)

Say if the given path name exsits or not.

Parameters
fileName: name of the file or dir to be checked
Returns
if the file exsits, false otherwise

Definition at line 18 of file string_filename.cpp.

18  {
19  if(fileName == "") return false;
20  return access(fileName.c_str(), F_OK) != -1;
21 }

Referenced by testStringFilename().

+ Here is the caller graph for this function:

◆ makeAbsolutePath() [1/2]

std::string makeAbsolutePath ( const std::string &  path)

Make an absolute path of the given path.

Parameters
path: relative or absolut path (file or dir)
Returns
corresponding absolute path

Definition at line 93 of file string_filename.cpp.

93  {
94  if(path == ""){return getCurrentDirectory() + "/";}
95  else if(path[0] == '/'){return path;}
96  else{
97  return getCurrentDirectory() + "/" + path;
98  }
99 }

References getCurrentDirectory().

Referenced by makeAbsolutePath(), and testStringFilename().

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

◆ makeAbsolutePath() [2/2]

std::vector<std::string> makeAbsolutePath ( const std::vector< std::string > &  vecPath)

Make an absolute path of the vector of given paths.

Parameters
vecPath: vector of relative or absolute path (file or dir)
Returns
vector of corresponding absolute path

Definition at line 105 of file string_filename.cpp.

105  {
106  std::vector<std::string> vecOut;
107  for(std::vector<std::string>::const_iterator it(vecPath.begin()); it != vecPath.end(); ++it){
108  vecOut.push_back(makeAbsolutePath(*it));
109  }
110  return vecOut;
111 }

References makeAbsolutePath().

+ Here is the call graph for this function:

◆ removePathDots()

std::string removePathDots ( const std::string  path)

Remove dots from the path.

Parameters
path: path to be cleaned
Returns
path wthout dots

Definition at line 117 of file string_filename.cpp.

117  {
118  if(path == ""){return path;}
119  if(path.front() == '.'){return path;}
120  std::list<std::string> listDir(cutStringList(path, '/'));
121  std::list<std::string>::reverse_iterator rit = listDir.rbegin();
122  while(rit != listDir.rend()){
123  if(*rit == "." || *rit == ""){
124 // rit = listDir.erase(rit); //No erase in reverse_iterator, so anoying
125  *rit = "";
126  }else if(*rit == ".."){
127 // rit = listDir.erase(rit); //No erase in reverse_iterator, so anoying
128  *rit = "";
129  ++rit;
130  if(rit != listDir.rend()){
131 // rit = listDir.erase(rit); //No erase in reverse_iterator, so anoying
132  *rit = "";
133  }
134  }
135 // else{
136 // ++rit; //No erase in reverse_iterator, so anoying
137 // }
138  ++rit;
139  }
140  std::string out(""), separator("");
141  if(path[0] == '/'){
142  out = "/";
143  }
144  for(std::list<std::string>::iterator it(listDir.begin()); it != listDir.end(); ++it){
145  if(*it == ""){continue;}
146  out += separator + (*it);
147  separator = "/";
148  }
149  return out;
150 }

References cutStringList().

Referenced by testStringFilename().

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

◆ saveFileContent() [1/2]

bool saveFileContent ( const std::string &  filename,
const std::string &  content 
)

Save a string in a file.

Parameters
filename: name of the file to be written
content: file content
Returns
true on success, false otherwise

Definition at line 300 of file string_filename.cpp.

300  {
301  FILE * fp = fopen(filename.c_str(), "w");
302  if(fp == NULL){
303  std::cerr << "saveFileContent : cannot open file '" << filename << "'" << std::endl;
304  return false;
305  }
306  bool result(saveFileContent(fp, content));
307  fclose(fp);
308  return result;
309 }

References saveFileContent().

Referenced by phoenix_popen(), saveFileContent(), testStringFilename(), and testStringSystem().

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

◆ saveFileContent() [2/2]

bool saveFileContent ( FILE *  fp,
const std::string &  content 
)

Save a string in a file.

Parameters
fp: pointer to the file to be written
content: file content
Returns
true on success, false otherwise

Definition at line 316 of file string_filename.cpp.

316  {
317  if(fp == NULL) return false;
318  fprintf(fp, "%s", content.c_str());
319  return true;
320 }
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
findInString
bool findInString(const std::string &str, char ch)
Find a char in a string.
Definition: string_function.cpp:15
getLongestExtention
std::string getLongestExtention(const std::string &fileName)
Get the longest file extention.
Definition: string_filename.cpp:343
makeAbsolutePath
std::string makeAbsolutePath(const std::string &path)
Make an absolute path of the given path.
Definition: string_filename.cpp:93
cutStringList
std::list< std::string > cutStringList(const std::string &str, char separator)
Cut a string the the given separator char.
Definition: string_function.cpp:428
copyStr
std::string copyStr(const std::string &str, long unsigned int begin, long unsigned int nbCh)
Copy a string of nbCh starting from begin char.
Definition: string_function.cpp:515
createReleaseCurl.str
str
Definition: createReleaseCurl.py:128
isFileExist
bool isFileExist(const std::string &fileName)
Say if a file exsits or not.
Definition: string_filename.cpp:27
countNbChar
size_t countNbChar(const std::string &str, char ch)
Count number of chararacters ch in string.
Definition: string_function.cpp:145
getCurrentDirectory
std::string getCurrentDirectory()
Returns the current directory.
Definition: string_filename.cpp:69
getUnderPath
std::string getUnderPath(const std::string &fileName, const std::string &pathPart)
Get path which is under the given pathPart ('some/dir/path' with 'dir' will return 'path')
Definition: string_filename.cpp:176