PhoenixInkscape  2.0.0
Generate multiple png files with svg inkscape files
PPath.cpp
Go to the documentation of this file.
1 /***************************************
2  Auteur : Pierre Aubert
3  Mail : pierre.aubert@lapp.in2p3.fr
4  Licence : CeCILL-C
5 ****************************************/
6 
7 #include <errno.h>
8 #include <stdio.h>
9 #include <regex.h>
10 #include <fcntl.h> //Definition of AT_* constants
11 
12 #include <unistd.h> //for get_current_dir_name
13 #include <dirent.h> //for opendir
14 
15 #include <sys/stat.h>
16 #include <sys/types.h>
17 #include <stdlib.h>
18 #include <unistd.h>
19 
20 #include "phoenix_env.h"
21 #include "phoenix_system.h"
22 #include "PPath.h"
23 
25 
29  if(fp == NULL){return "";}
30  //Let's get the size of the file
31  fseek(fp, 0, SEEK_END);
32  long fileOffset(ftell(fp));
33  fseek(fp, 0, SEEK_SET);
34  //Is the file is empty we quit
35  if(fileOffset == 0l){
36  return "";
37  }
38  //If the size of the file is -1lu this should be a stream which has no defined end yet, such as the popen output
39  if(fileOffset == -1l){
40  //So we have to use the old method which consist to read the stream character per character until EOF
41  //We have to use a std::string instead of PString to have smarter resize strategy
42  std::string strBuffer("");
43  int buffer;
44  while(!feof(fp)){
45  buffer = fgetc(fp);
46  if(buffer != EOF) strBuffer += (char)buffer;
47  else break;
48  }
49  return strBuffer;
50  }else{
51  size_t fileSize(fileOffset);
52  //If we know the size of the file, we can resize our PString once for all and load the content with fread
53  PString bufferAllFile;
54  bufferAllFile.resize(fileSize);
55  if(fread(bufferAllFile.data(), sizeof(char), fileSize, fp) == fileSize){
56 
57  }
58  return bufferAllFile;
59  }
60 }
61 
62 
65  :PString()
66 {
68 }
69 
71 
73 PPath::PPath(const PString & other)
74  :PString(other)
75 {
77 }
78 
80 
82 PPath::PPath(const PPath & other)
83  :PString(other)
84 {
85  copyPPath(other);
86 }
87 
90 
91 }
92 
94 
97 PPath & PPath::operator = (const PPath & other){
98  copyPString(other);
99  copyPPath(other);
100  return *this;
101 }
102 
104 
108 PPath operator + (const PPath & other1, const PPath & other2){
109  PPath out(other1);
110  out += other2;
111  return out;
112 }
113 
115 
119 PPath operator / (const PPath & other1, const PPath & other2){
120  PPath out(other1);
121  if(other1.size() != 0lu){
122  out += PString("/");
123  }
124  out += other2;
125  return out;
126 }
127 
129 
131 bool PPath::isExist() const{
132  if(size() == 0lu) return false;
133  return access(c_str(), F_OK) != -1;
134 }
135 
137 
139 bool PPath::isFileExist() const{
140  if(size() == 0lu) return false;
141  struct stat path_stat;
142  if(stat(c_str(), &path_stat) != 0){
143  return false;
144  }
145  return S_ISREG(path_stat.st_mode) != 0;
146 }
147 
149 
152  if(size() == 0lu) return false;
153  struct stat path_stat;
154  if(stat(c_str(), &path_stat) != 0){
155  return false;
156  }
157  return S_ISDIR(path_stat.st_mode) != 0;
158 }
159 
161 
165  if(size() == 0lu) return false;
166  return front() == '/';
167 }
168 
170 
173  std::string buffer("");
174  std::string::const_reverse_iterator rit = rbegin();
175  while(rit != rend()){
176  if(*rit == '/') break;
177  buffer = *rit + buffer;
178  rit++;
179  }
180  return PPath(buffer);
181 }
182 
184 
187  std::string buffer("");
188  std::string::const_reverse_iterator rit = rbegin();
189  while(rit != rend()){
190  if(*rit == '/'){
191  if(buffer != ""){
192  break;
193  }else{
194  rit++;
195  continue;
196  }
197  }
198  buffer = *rit + buffer;
199  rit++;
200  }
201  return PPath(buffer);
202 }
203 
205 
208  if(size() == 0lu){return PString("");}
209  std::string buffer("");
210  bool find(false), isLastSlash(back() == '/');
211  std::string::const_reverse_iterator rit = rbegin();
212  while(rit != rend()){
213  if(find){
214  buffer = *rit + buffer;
215  }else{
216  find = (*rit == '/' && !isLastSlash);
217  isLastSlash = *rit == '/';
218  }
219  rit++;
220  }
221  return PPath(buffer);
222 }
223 
225 
228 PPath PPath::getParentDirectory(const PPath & subDirName) const{
229  if(size() == 0lu || !isDirectoryExist()){return *this;}
230  PPath curDir(*this);
231  std::string tmp(curDir / subDirName);
232  DIR * dp = opendir(tmp.c_str());
233  while(NULL == dp && curDir.size() > 1lu){
234  curDir = curDir.getParentDirectory();
235  tmp = curDir / subDirName;
236  dp = opendir(tmp.c_str());
237  }
238  return curDir;
239 
240 
241 // PPath curDir(*this);
242 // PPath tmpDir(curDir / subDirName);
243 // while(!curDir.isDirectoryExist() && curDir.size() > subDirName.size()){
244 // curDir = curDir.getParentDirectory() / subDirName;
245 // }
246 // return curDir;
247 }
248 
250 
253  if(!find('.')) return "";
254  std::string extension("");
255  bool run(true);
256  std::string::const_reverse_iterator rit(rbegin());
257  while(run && rit != rend()){
258  if(*rit == '.' || *rit == '/') run = false;
259  else extension = *rit + extension;
260  rit++;
261  }
262  return PString(extension);
263 }
264 
266 
269  size_t nbPoint(count('.'));
270  if(nbPoint == 0lu) return "";
271  PString extension(""), tmpExtension(""), middleDot("");
272  bool run(true);
273  PString::const_reverse_iterator rit(rbegin());
274  while(run && rit != rend()){
275  if(*rit == '/'){run = false;}
276  else if(*rit == '.'){
277  extension = tmpExtension + middleDot + extension;
278  tmpExtension = "";
279  middleDot = ".";
280  }else{
281  tmpExtension = *rit + tmpExtension;
282  }
283 
284  rit++;
285  }
286  return extension;
287 }
288 
290 
293  if(size() <= 1lu){return *this;}
294  PString extension(getExtension());
295  if(extension.size() == 0lu){return *this;}
296  resize(size() - extension.size() - 1lu);
297  return *this;
298 }
299 
301 
304  if(size() <= 1lu){return *this;}
305  PString extension(getLongestExtension());
306  if(extension.size() == 0lu){return *this;}
307  resize(size() - extension.size() - 1lu);
308  return *this;
309 }
310 
312 
315  PPath out(*this);
316  return out.eraseExtension();
317 }
318 
320 
323  PPath out(*this);
324  return out.eraseLongestExtension();
325 }
326 
328 
331 bool PPath::createDirectory(mode_t mode) const{
332  if(size() == 0lu){return false;}
333  else if(isDirectoryExist()){return true;}
334 
335  std::vector<PString> listDir(split('/'));
336  PPath tmpDirName;
337  bool isNotFirst(false);
338  for(std::vector<PString>::iterator it(listDir.begin()); it != listDir.end(); ++it){
339  if(isNotFirst){
340  tmpDirName += "/";
341  }
342  tmpDirName += *it;
343  if(tmpDirName != ""){
344  if(!tmpDirName.isDirectoryExist()){
345  if(mkdir(tmpDirName.c_str(), mode) != 0){
346  return false;
347  }
348  }
349  }
350  isNotFirst = true;
351  }
352  return true;
353 // return mkdir(c_str(), 0755) == 0;
354 }
355 
357 
360 PPath PPath::getUnderPath(const PString & pathPart) const{
361  if(pathPart.size() == 0lu){return *this;}
362  std::vector<PString> listDir(split('/'));
363  std::vector<PString>::iterator it(listDir.begin());
364  bool isSearch(true);
365  while(isSearch && it != listDir.end()){
366  isSearch = *it != pathPart;
367  ++it;
368  }
369  PPath out;
370  PString separator("");
371  while(it != listDir.end()){
372  out += separator + (*it);
373  separator = "/";
374  ++it;
375  }
376  return out;
377 }
378 
380 
383  if(!isFileExist()){return "";}
384  FILE * fp = fopen(c_str(), "r");
385  if(fp == NULL){return "";}
386  PString bufferAllFile(phoenix_getFileContent(fp));
387  fclose(fp);
388  return bufferAllFile;
389 }
390 
392 
395 bool PPath::saveFileContent(const PString & content) const{
396  FILE * fp = fopen(c_str(), "w");
397  if(fp == NULL){return false;}
398  bool result(true);
399  fprintf(fp, "%s", content.c_str());
400  fclose(fp);
401  return result;
402 }
403 
405 
407 size_t PPath::getFileSize() const{
408  FILE* fs = fopen(c_str(), "r");
409  if(fs == NULL){return 0lu;}
410  fseek(fs, 0, SEEK_END);
411  size_t fileSize(ftell(fs));
412  fseek(fs, 0, SEEK_SET);
413  fclose(fs);
414  return fileSize;
415 }
416 
418 
421 bool PPath::checkFileBegning(const PString & expectedBegining) const{
422  if(size() == 0lu){return false;}
423  FILE* fp = fopen(c_str(), "r");
424  if(fp == NULL || expectedBegining.size() == 0lu){return false;}
425  int val(0);
426  size_t i(0lu);
427  bool isMatch(true);
428  while(isMatch && !feof(fp) && i < expectedBegining.size()){
429  val = fgetc(fp);
430  isMatch = (char)val == expectedBegining[i];
431  ++i;
432  }
433  fclose(fp);
434  return isMatch;
435 }
436 
438 
442  struct stat attr;
443  if(stat(c_str(), &attr) == 0){
444 #ifdef __APPLE__
445  return attr.st_mtimespec.tv_sec;
446 #else
447  return attr.st_mtim.tv_sec;
448 #endif
449  }else{
450  return -1l;
451  }
452 }
453 
455 
457 std::vector<PPath> PPath::getAllFileInDir() const{
458  std::vector<PPath> listFile;
459  // Open the current directory
460  DIR * dp = opendir(c_str());
461  if(NULL == dp){
462  return listFile;
463  }
464  dirent * dptr = readdir(dp);
465  while(NULL != dptr){
466  if(dptr->d_type == DT_REG){
467  PPath fileName(dptr->d_name);
468  listFile.push_back(fileName);
469  }
470  dptr = readdir(dp);
471  }
472  closedir(dp);
473  return listFile;
474 }
475 
477 
479 std::vector<PPath> PPath::getAllDirectoryInDir() const{
480  std::vector<PPath> listFile;
481  // Open the current directory
482  DIR * dp = opendir(c_str());
483  if(NULL == dp){
484  return listFile;
485  }
486  dirent * dptr = readdir(dp);
487  while(NULL != dptr){
488  if(dptr->d_type == DT_DIR){
489  PPath dirName(dptr->d_name);
490  if(dirName != ".." && dirName != "."){
491  listFile.push_back(dirName);
492  }
493  }
494  dptr = readdir(dp);
495  }
496  closedir(dp);
497  return listFile;
498 }
499 
501 
503 std::vector<PPath> PPath::getAllElementInDir() const{
504  std::vector<PPath> listFile;
505  // Open the current directory
506  DIR * dp = opendir(c_str());
507  if(NULL == dp){
508  return listFile;
509  }
510  dirent * dptr = readdir(dp);
511  while(NULL != dptr){
512  PPath fileName(dptr->d_name);
513  if(fileName != ".." && fileName != "."){
514  listFile.push_back(fileName);
515  }
516  dptr = readdir(dp);
517  }
518  closedir(dp);
519  return listFile;
520 }
521 
523 
526 bool PPath::changeMode(mode_t __mode) const{
527  return chmod(c_str(), __mode) >= 0;
528 }
529 
531 
534  if(size() == 0lu){return *this;}
535  if(front() == '.'){return *this;}
536  std::vector<PString> listDir(split('/'));
537  std::vector<PString>::reverse_iterator rit = listDir.rbegin();
538  while(rit != listDir.rend()){
539  if(*rit == "." || *rit == ""){
540 // rit = listDir.erase(rit); //No erase in reverse_iterator, so anoying
541  *rit = "";
542  }else if(*rit == ".."){
543 // rit = listDir.erase(rit); //No erase in reverse_iterator, so anoying
544  *rit = "";
545  ++rit;
546  if(rit != listDir.rend()){
547 // rit = listDir.erase(rit); //No erase in reverse_iterator, so anoying
548  *rit = "";
549  }
550  }
551 // else{
552 // ++rit; //No erase in reverse_iterator, so anoying
553 // }
554  ++rit;
555  }
556  PPath out;
557  PString separator("");
558  if(front() == '/'){
559  out = PString("/");
560  }
561  for(std::vector<PString>::iterator it(listDir.begin()); it != listDir.end(); ++it){
562  if(*it == ""){continue;}
563  out += separator + (*it);
564  separator = "/";
565  }
566  return out;
567 }
568 
570 
573  if(size() == 0lu){
574  return PPath::getCurrentDirectory().add("/");
575  }else if(isAbsolutePath()){
576  return *this;
577  }else{
578  return PPath::getCurrentDirectory().add("/").add(*this);
579  }
580 }
581 
583 
586  char buffer[4096];
587  ssize_t nbChar = readlink("/proc/self/exe", buffer, 2048);
588 // readlink("/proc/self/exe", buf, bufsize) (Linux)
589 // readlink("/proc/curproc/file", buf, bufsize) (FreeBSD)
590 // readlink("/proc/self/path/a.out", buf, bufsize) (Solaris)
591 
592  if(nbChar > 0l){
593  PString outputBuf("");
594  for(ssize_t i(0l); i < nbChar; ++i){
595  outputBuf += buffer[i];
596  }
597  return outputBuf;
598  }else{
599  return PString("");
600  }
601 }
602 
604 
607  PPath progLoc(getProgramLocation());
608  if(progLoc != ""){
609  return progLoc.getParentDirectory();
610  }else{
611 #ifdef CMAKE_INSTALL_PREFIX
612  return PString(CMAKE_INSTALL_PREFIX "/bin/");
613 #else
614  return PString("/usr/bin/");
615 #endif
616  }
617 }
618 
620 
624 }
625 
627 
630  return phoenix_getenv("HOME");
631 }
632 
634 
637 #ifndef __APPLE__
638  char* ptr = get_current_dir_name();
639  PString str(phoenix_charToString(ptr));
640  free(ptr);
641  return str;
642 #else
643  return phoenix_getenv("PWD");
644 #endif
645 }
646 
648 
651  return phoenix_popen("uname -n").eraseChar(" \n\t");
652 }
653 
655 
657 void PPath::copyPPath(const PPath & other){
658 
659 }
660 
663 
664 }
665 
666 
667 
668 
669 
PPath operator+(const PPath &other1, const PPath &other2)
Operator + for PPath.
Definition: PPath.cpp:108
PPath operator/(const PPath &other1, const PPath &other2)
Operator / for PPath to concatenate PPath.
Definition: PPath.cpp:119
PString phoenix_getFileContent(FILE *fp)
Get the content of a file.
Definition: PPath.cpp:28
PString phoenix_charToString(const char *ch)
Convert a char pointer into a string (event if the char pointer is NULL)
Definition: PString.cpp:14
Path of a directory or a file.
Definition: PPath.h:17
bool changeMode(mode_t __mode=S_IRWXU|S_IRWXG|S_IROTH|S_IXOTH) const
Change the mode of a file or directory.
Definition: PPath.cpp:526
bool checkFileBegning(const PString &expectedBegining) const
Check if the given file starts with the given begning.
Definition: PPath.cpp:421
PPath & eraseLongestExtension()
Erase the longest extension of the PPath.
Definition: PPath.cpp:303
std::vector< PPath > getAllDirectoryInDir() const
Get the list of files in a directory.
Definition: PPath.cpp:479
bool saveFileContent(const PString &content) const
Save a PString in a file.
Definition: PPath.cpp:395
PPath & eraseExtension()
Erase the extension of the PPath.
Definition: PPath.cpp:292
bool createDirectory(mode_t mode=0755) const
Create the current directory.
Definition: PPath.cpp:331
PPath getUnderPath(const PString &pathPart) const
Get path which is under the given pathPart ('some/dir/path' with 'dir' will return 'path')
Definition: PPath.cpp:360
static PPath getCurrentDirectory()
Returns the current directory.
Definition: PPath.cpp:636
static PString getCurrentNodeName()
Get the name of the current node on which the program is running.
Definition: PPath.cpp:650
PPath makeAbsolute() const
Make an absolute path of the given path.
Definition: PPath.cpp:572
PString getExtension() const
Get file extension.
Definition: PPath.cpp:252
PPath getParentDirectory() const
Get path of parent directory of current path.
Definition: PPath.cpp:207
PPath getDirectoryName() const
Get the name of the directory, from last char to / (if the last char is not a /, otherwise it takes t...
Definition: PPath.cpp:186
void copyPPath(const PPath &other)
Copy function of PPath.
Definition: PPath.cpp:657
static PPath getHomeDir()
Gets the $HOME directory.
Definition: PPath.cpp:629
size_t getFileSize() const
Get the size of the current file.
Definition: PPath.cpp:407
static PPath getProgramPrefix()
Get the program prefix (installation directory without /bin)
Definition: PPath.cpp:622
bool isDirectoryExist() const
Say if the current directory path does exist.
Definition: PPath.cpp:151
bool isAbsolutePath() const
Tel if a path is absolute or not.
Definition: PPath.cpp:164
time_t getFileModificationTime() const
Get the last modification time of the given file.
Definition: PPath.cpp:441
static PPath getProgramLocation()
Get the program location.
Definition: PPath.cpp:585
PPath simplify() const
Remove extra dots from the path.
Definition: PPath.cpp:533
bool isFileExist() const
Say if the current file path does exist.
Definition: PPath.cpp:139
PString loadFileContent() const
Get the file content in a PString.
Definition: PPath.cpp:382
std::vector< PPath > getAllFileInDir() const
Get the list of files in a directory.
Definition: PPath.cpp:457
PString getLongestExtension() const
Get the longest file extension.
Definition: PPath.cpp:268
virtual ~PPath()
Destructor of PPath.
Definition: PPath.cpp:89
bool isExist() const
Say if the current path does exist.
Definition: PPath.cpp:131
std::vector< PPath > getAllElementInDir() const
Get the list of all elements in a directory.
Definition: PPath.cpp:503
PPath & operator=(const PPath &other)
Definition of equal operator of PPath.
Definition: PPath.cpp:97
PPath getFileName() const
Get the name of the file, from last char to /.
Definition: PPath.cpp:172
static PPath getProgramDirectory()
Get the program directory.
Definition: PPath.cpp:606
void initialisationPPath()
Initialisation function of the class PPath.
Definition: PPath.cpp:662
PPath()
Default constructor of PPath.
Definition: PPath.cpp:64
Extends the std::string.
Definition: PString.h:16
PString & add(const PString &other)
Add a PString to an other.
Definition: PString.cpp:154
PString eraseChar(char ch) const
Erase char ch of current string.
Definition: PString.cpp:478
std::vector< PString > split(char separator) const
Cut a PString on the given separator char.
Definition: PString.cpp:420
bool find(char ch) const
Find a char in a string.
Definition: PString.cpp:371
size_t count(char ch) const
Count the number of char ch in the current PString.
Definition: PString.cpp:323
void copyPString(const PString &other)
Copy function of PString.
Definition: PString.cpp:706
PString()
Default constructor of PString.
Definition: PString.cpp:70
PString phoenix_getenv(const PString &varName)
Get the value of the given environment variable.
Definition: phoenix_env.cpp:15
PString phoenix_popen(const PString &command)
Execute the given command and returns the output of this command.