PhoenixMock  1.8.7
Tools to split/merge/print mock used in Phoenix
string_filename.cpp
Go to the documentation of this file.
1 
2 #include <unistd.h> //for get_current_dir_name
3 #include <dirent.h> //for opendir
4 
5 #include <sys/stat.h>
6 #include <sys/types.h>
7 
8 #include "string_utils.h"
9 #include "string_system.h"
10 #include "string_filename.h"
11 
12 using namespace std;
13 
15 
18 bool isFileOrDirExist(const std::string & fileName){
19  if(fileName == "") return false;
20  return access(fileName.c_str(), F_OK) != -1;
21 }
22 
24 
27 bool isFileExist(const std::string & fileName){
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 }
35 
36 
38 
42 std::string getExistingFileName(const std::string & fileName, const std::vector<std::string> & vecDirectory){
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 }
52 
54 
57 bool isDirectoryExist(const std::string & dirName){
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 }
65 
67 
69 std::string getCurrentDirectory(){
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 }
79 
81 
84 bool isAbsolutePath(const std::string & path){
85  if(path == ""){return false;}
86  return path[0] == '/';
87 }
88 
90 
93 std::string makeAbsolutePath(const std::string & path){
94  if(path == ""){return getCurrentDirectory() + "/";}
95  else if(path[0] == '/'){return path;}
96  else{
97  return getCurrentDirectory() + "/" + path;
98  }
99 }
100 
102 
105 std::vector<std::string> makeAbsolutePath(const std::vector<std::string> & vecPath){
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 }
112 
114 
117 std::string removePathDots(const std::string path){
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 }
151 
153 
156 std::string getDirectory(const std::string & fileName){
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 }
170 
172 
176 std::string getUnderPath(const std::string & fileName, const std::string & pathPart){
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 }
193 
195 
199 std::vector<std::string> getUnderPath(const std::vector<std::string> & vecFileName, const std::string & pathPart){
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 }
206 
208 
212 bool checkFileBegning(const std::string & fileName, const std::string & expectedBegining){
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 }
226 
228 
231 std::string getFileName(const std::string & fileName){
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 }
241 
243 
246 std::string getDirName(const std::string & path){
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 }
263 
265 
268 std::string getFileContent(const std::string & filename){
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 }
278 
280 
283 std::string getFileContent(FILE* fp){
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 }
294 
296 
300 bool saveFileContent(const std::string & filename, const std::string & content){
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 }
310 
312 
316 bool saveFileContent(FILE* fp, const std::string & content){
317  if(fp == NULL) return false;
318  fprintf(fp, "%s", content.c_str());
319  return true;
320 }
321 
323 
326 std::string getExtention(const std::string & fileName){
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 }
338 
340 
343 std::string getLongestExtention(const std::string & fileName){
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 }
363 
365 
368 std::string eraseExtension(const std::string & fileName){
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 }
387 
389 
392 std::string eraseLongestExtension(const std::string & fileName){
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 }
400 
402 
405 bool createDirectoriesIfNotExist(const std::string & dirName){
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 }
426 
427 
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.
Definition: string_filename.cpp:42
createDirectoriesIfNotExist
bool createDirectoriesIfNotExist(const std::string &dirName)
Create the directory if not exists.
Definition: string_filename.cpp:405
string_utils.h
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
string_filename.h
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
eraseLongestExtension
std::string eraseLongestExtension(const std::string &fileName)
Erase longest extention of the given file.
Definition: string_filename.cpp:392
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
checkFileBegning
bool checkFileBegning(const std::string &fileName, const std::string &expectedBegining)
Check if the given file starts with the given begning.
Definition: string_filename.cpp:212
eraseExtension
std::string eraseExtension(const std::string &fileName)
Erase extention of the given file.
Definition: string_filename.cpp:368
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
removePathDots
std::string removePathDots(const std::string path)
Remove dots from the path.
Definition: string_filename.cpp:117
createReleaseCurl.str
str
Definition: createReleaseCurl.py:128
getFileName
std::string getFileName(const std::string &fileName)
fonction qui renvoie le nom du fichier du nom complet de fichier passé en paramètre
Definition: string_filename.cpp:231
string_system.h
getExtention
std::string getExtention(const std::string &fileName)
Get file extention.
Definition: string_filename.cpp:326
isAbsolutePath
bool isAbsolutePath(const std::string &path)
Tel if a path is absolute or not.
Definition: string_filename.cpp:84
isFileExist
bool isFileExist(const std::string &fileName)
Say if a file exsits or not.
Definition: string_filename.cpp:27
getDirectory
std::string getDirectory(const std::string &fileName)
fonction qui renvoie le dossier parent du fichier
Definition: string_filename.cpp:156
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
getDirName
std::string getDirName(const std::string &path)
Get the name of the deeper directory.
Definition: string_filename.cpp:246
isFileOrDirExist
bool isFileOrDirExist(const std::string &fileName)
Say if the given path name exsits or not.
Definition: string_filename.cpp:18