16 #include <sys/types.h>
29 if(fp == NULL){
return "";}
31 fseek(fp, 0, SEEK_END);
32 long fileOffset(ftell(fp));
33 fseek(fp, 0, SEEK_SET);
39 if(fileOffset == -1l){
42 std::string strBuffer(
"");
46 if(buffer != EOF) strBuffer += (char)buffer;
51 size_t fileSize(fileOffset);
54 bufferAllFile.resize(fileSize);
55 if(fread(bufferAllFile.data(),
sizeof(
char), fileSize, fp) == fileSize){
121 if(other1.size() != 0lu){
132 if(size() == 0lu)
return false;
133 return access(c_str(), F_OK) != -1;
140 if(size() == 0lu)
return false;
141 struct stat path_stat;
142 if(stat(c_str(), &path_stat) != 0){
145 return S_ISREG(path_stat.st_mode) != 0;
152 if(size() == 0lu)
return false;
153 struct stat path_stat;
154 if(stat(c_str(), &path_stat) != 0){
157 return S_ISDIR(path_stat.st_mode) != 0;
165 if(size() == 0lu)
return false;
166 return front() ==
'/';
173 std::string buffer(
"");
174 std::string::const_reverse_iterator rit = rbegin();
175 while(rit != rend()){
176 if(*rit ==
'/')
break;
177 buffer = *rit + buffer;
180 return PPath(buffer);
187 std::string buffer(
"");
188 std::string::const_reverse_iterator rit = rbegin();
189 while(rit != rend()){
198 buffer = *rit + buffer;
201 return PPath(buffer);
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()){
214 buffer = *rit + buffer;
216 find = (*rit ==
'/' && !isLastSlash);
217 isLastSlash = *rit ==
'/';
221 return PPath(buffer);
231 std::string tmp(curDir / subDirName);
232 DIR * dp = opendir(tmp.c_str());
233 while(NULL == dp && curDir.size() > 1lu){
235 tmp = curDir / subDirName;
236 dp = opendir(tmp.c_str());
253 if(!
find(
'.'))
return "";
254 std::string extension(
"");
256 std::string::const_reverse_iterator rit(rbegin());
257 while(run && rit != rend()){
258 if(*rit ==
'.' || *rit ==
'/') run =
false;
259 else extension = *rit + extension;
269 size_t nbPoint(
count(
'.'));
270 if(nbPoint == 0lu)
return "";
271 PString extension(
""), tmpExtension(
""), middleDot(
"");
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;
281 tmpExtension = *rit + tmpExtension;
293 if(size() <= 1lu){
return *
this;}
295 if(extension.size() == 0lu){
return *
this;}
296 resize(size() - extension.size() - 1lu);
304 if(size() <= 1lu){
return *
this;}
306 if(extension.size() == 0lu){
return *
this;}
307 resize(size() - extension.size() - 1lu);
332 if(size() == 0lu){
return false;}
335 std::vector<PString> listDir(
split(
'/'));
337 bool isNotFirst(
false);
338 for(std::vector<PString>::iterator it(listDir.begin()); it != listDir.end(); ++it){
343 if(tmpDirName !=
""){
345 if(mkdir(tmpDirName.c_str(), mode) != 0){
361 if(pathPart.size() == 0lu){
return *
this;}
362 std::vector<PString> listDir(
split(
'/'));
363 std::vector<PString>::iterator it(listDir.begin());
365 while(isSearch && it != listDir.end()){
366 isSearch = *it != pathPart;
371 while(it != listDir.end()){
372 out += separator + (*it);
384 FILE * fp = fopen(c_str(),
"r");
385 if(fp == NULL){
return "";}
388 return bufferAllFile;
396 FILE * fp = fopen(c_str(),
"w");
397 if(fp == NULL){
return false;}
399 fprintf(fp,
"%s", content.c_str());
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);
422 if(size() == 0lu){
return false;}
423 FILE* fp = fopen(c_str(),
"r");
424 if(fp == NULL || expectedBegining.size() == 0lu){
return false;}
428 while(isMatch && !feof(fp) && i < expectedBegining.size()){
430 isMatch = (char)val == expectedBegining[i];
443 if(stat(c_str(), &attr) == 0){
445 return attr.st_mtimespec.tv_sec;
447 return attr.st_mtim.tv_sec;
458 std::vector<PPath> listFile;
460 DIR * dp = opendir(c_str());
464 dirent * dptr = readdir(dp);
466 if(dptr->d_type == DT_REG){
467 PPath fileName(dptr->d_name);
468 listFile.push_back(fileName);
480 std::vector<PPath> listFile;
482 DIR * dp = opendir(c_str());
486 dirent * dptr = readdir(dp);
488 if(dptr->d_type == DT_DIR){
489 PPath dirName(dptr->d_name);
490 if(dirName !=
".." && dirName !=
"."){
491 listFile.push_back(dirName);
504 std::vector<PPath> listFile;
506 DIR * dp = opendir(c_str());
510 dirent * dptr = readdir(dp);
512 PPath fileName(dptr->d_name);
513 if(fileName !=
".." && fileName !=
"."){
514 listFile.push_back(fileName);
527 return chmod(c_str(), __mode) >= 0;
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 ==
""){
542 }
else if(*rit ==
".."){
546 if(rit != listDir.rend()){
561 for(std::vector<PString>::iterator it(listDir.begin()); it != listDir.end(); ++it){
562 if(*it ==
""){
continue;}
563 out += separator + (*it);
587 ssize_t nbChar = readlink(
"/proc/self/exe", buffer, 2048);
594 for(ssize_t i(0l); i < nbChar; ++i){
595 outputBuf += buffer[i];
611 #ifdef CMAKE_INSTALL_PREFIX
612 return PString(CMAKE_INSTALL_PREFIX
"/bin/");
638 char* ptr = get_current_dir_name();
PPath operator+(const PPath &other1, const PPath &other2)
Operator + for PPath.
PPath operator/(const PPath &other1, const PPath &other2)
Operator / for PPath to concatenate PPath.
PString phoenix_getFileContent(FILE *fp)
Get the content of a file.
PString phoenix_charToString(const char *ch)
Convert a char pointer into a string (event if the char pointer is NULL)
Path of a directory or a file.
bool changeMode(mode_t __mode=S_IRWXU|S_IRWXG|S_IROTH|S_IXOTH) const
Change the mode of a file or directory.
bool checkFileBegning(const PString &expectedBegining) const
Check if the given file starts with the given begning.
PPath & eraseLongestExtension()
Erase the longest extension of the PPath.
std::vector< PPath > getAllDirectoryInDir() const
Get the list of files in a directory.
bool saveFileContent(const PString &content) const
Save a PString in a file.
PPath & eraseExtension()
Erase the extension of the PPath.
bool createDirectory(mode_t mode=0755) const
Create the current directory.
PPath getUnderPath(const PString &pathPart) const
Get path which is under the given pathPart ('some/dir/path' with 'dir' will return 'path')
static PPath getCurrentDirectory()
Returns the current directory.
static PString getCurrentNodeName()
Get the name of the current node on which the program is running.
PPath makeAbsolute() const
Make an absolute path of the given path.
PString getExtension() const
Get file extension.
PPath getParentDirectory() const
Get path of parent directory of current path.
PPath getDirectoryName() const
Get the name of the directory, from last char to / (if the last char is not a /, otherwise it takes t...
void copyPPath(const PPath &other)
Copy function of PPath.
static PPath getHomeDir()
Gets the $HOME directory.
size_t getFileSize() const
Get the size of the current file.
static PPath getProgramPrefix()
Get the program prefix (installation directory without /bin)
bool isDirectoryExist() const
Say if the current directory path does exist.
bool isAbsolutePath() const
Tel if a path is absolute or not.
time_t getFileModificationTime() const
Get the last modification time of the given file.
static PPath getProgramLocation()
Get the program location.
PPath simplify() const
Remove extra dots from the path.
bool isFileExist() const
Say if the current file path does exist.
PString loadFileContent() const
Get the file content in a PString.
std::vector< PPath > getAllFileInDir() const
Get the list of files in a directory.
PString getLongestExtension() const
Get the longest file extension.
virtual ~PPath()
Destructor of PPath.
bool isExist() const
Say if the current path does exist.
std::vector< PPath > getAllElementInDir() const
Get the list of all elements in a directory.
PPath & operator=(const PPath &other)
Definition of equal operator of PPath.
PPath getFileName() const
Get the name of the file, from last char to /.
static PPath getProgramDirectory()
Get the program directory.
void initialisationPPath()
Initialisation function of the class PPath.
PPath()
Default constructor of PPath.
PString & add(const PString &other)
Add a PString to an other.
PString eraseChar(char ch) const
Erase char ch of current string.
std::vector< PString > split(char separator) const
Cut a PString on the given separator char.
bool find(char ch) const
Find a char in a string.
size_t count(char ch) const
Count the number of char ch in the current PString.
void copyPString(const PString &other)
Copy function of PString.
PString()
Default constructor of PString.
PString phoenix_getenv(const PString &varName)
Get the value of the given environment variable.
PString phoenix_popen(const PString &command)
Execute the given command and returns the output of this command.