File manipulation tools. More...
#include <File.h>
Static Public Member Functions | |
static std::vector< std::string > * | complete (const std::string &beginFilePath) |
static bool | match (const char *buffer, int &bytesRead, int bytesCount, const char *string) |
static char * | readAll (const char *fileName) |
static double | readDouble (const char *buffer, int &bytesRead, int bytesCount, bool &ok) |
static int | readInteger (const char *buffer, int &bytesRead, int bytesCount, bool &ok) |
static bool | readLine (char *&buf, int &bufLen, FILE *f, bool delEOL=false) |
static long int | readLongInteger (const char *buffer, int &bytesRead, int bytesCount, bool &ok) |
File manipulation tools.
Full description of class still missing
std::vector< std::string > * GpCoreTools::File::complete | ( | const std::string & | beginFilePath | ) | [static] |
For instance, beginFilePath can be "/dev/serial/by-id/usb-LEAS_DASCUBE". "/dev/serial/by-id/usb-LEAS_DASCUBE_DS011105-if00-port0" is returned, if this file exists.
A pointer to a vector is returned. In all cases, you must delete this pointer.
{ std::vector<std::string> * files=new std::vector<std::string>; // Breaks path and file base name const char * baseName=strrchr(beginFilePath.data(), '/'); int pathLength=baseName-beginFilePath.data(); char * pathTemp=new char[pathLength+2]; strncpy(pathTemp, beginFilePath.data(), pathLength); pathTemp[pathLength]='/'; pathTemp[pathLength+1]='\0'; std::string path(pathTemp); delete [] pathTemp; baseName++; int baseNameLength=strlen(baseName); struct dirent *dirp; DIR *dp; if(!(dp=opendir(path.data()))) { return files; } while((dirp=readdir(dp))) { if(strncmp(baseName, dirp->d_name, baseNameLength)==0) { files->push_back(path+std::string(dirp->d_name)); } } closedir(dp); return files; }
bool GpCoreTools::File::match | ( | const char * | buffer, |
int & | bytesRead, | ||
int | bytesCount, | ||
const char * | string | ||
) | [static] |
Returns true if buffer at bytesRead index starts with string.
{ int n=bytesCount-bytesRead; int sn=strlen(string); if(n>=sn && strncmp(buffer+bytesRead, string, sn)==0) { bytesRead+=sn; return true; } else { return false; } }
char * GpCoreTools::File::readAll | ( | const char * | fileName | ) | [static] |
Reads all file and returns its contents in a newly allocated buffer. It returns 0 if an error occured or if file is empty.
{ struct stat sInfo; int sErr=stat(fileName, &sInfo); if(sErr==0) { unsigned int fileLen=sInfo.st_size; if(fileLen>0) { FILE * f=fopen(fileName,"rb"); if(f) { char * buf=new char [fileLen+1]; if(fread(buf,sizeof(char),fileLen,f)==fileLen) { buf[fileLen]='\0'; } else { delete [] buf; buf=0; } fclose(f); return buf; } } } return 0; }
double GpCoreTools::File::readDouble | ( | const char * | buffer, |
int & | bytesRead, | ||
int | bytesCount, | ||
bool & | ok | ||
) | [static] |
Returns -1 if value is truncated and bytesRead is not modified.
{ int i; double value; for(i=bytesRead; i<bytesCount && !isspace(buffer[i]); i++) {} int n=i-bytesRead; if(n>0 && i<bytesCount) { char * temp=new char[n+1]; strncpy(temp, buffer+bytesRead, n); temp[n]='\0'; value=atof(temp); bytesRead=i; ok=true; delete [] temp; } else { value=-1; ok=false; } return value; }
int GpCoreTools::File::readInteger | ( | const char * | buffer, |
int & | bytesRead, | ||
int | bytesCount, | ||
bool & | ok | ||
) | [static] |
Returns -1 if value is truncated and bytesRead is not modified.
{ int i; int value; for(i=bytesRead; i<bytesCount && !isspace(buffer[i]); i++) {} int n=i-bytesRead; if(n>0 && i<bytesCount) { char * temp=new char[n+1]; strncpy(temp, buffer+bytesRead, n); temp[n]='\0'; value=atoi(temp); bytesRead=i; ok=true; delete [] temp; } else { value=-1; ok=false; } return value; }
bool GpCoreTools::File::readLine | ( | char *& | buf, |
int & | bufLen, | ||
FILE * | f, | ||
bool | delEOL = false |
||
) | [static] |
Returns the line read from f in a dynamic string buffer buf of initial length of bufLen.. If the line read from f is longer than initial bufLen, buffer is re-allocated and expanded. bufLen contains the final length. If delEOL is true remaining end of line characters (LF and/or CR) at the end are removed.
Referenced by GpCoreTools::CoreApplication::getStdin().
{ buf[0]='\0'; if(!fgets (buf, bufLen, f)) { return false; } while((int)strlen(buf)==bufLen-1) { int nbufLen=bufLen << 1; char * nbuf=new char [nbufLen]; memmove(nbuf,buf,bufLen-1); delete [] buf; buf=nbuf; if(!fgets(buf+bufLen-1, bufLen+1, f)) { break; } bufLen=nbufLen; } if(delEOL) { int n=strlen(buf)-1; if(buf[n]=='\n') { if(buf[n-1]=='\r') { buf[n-1]='\0'; } else { buf[n]='\0'; } } } return true; }
long int GpCoreTools::File::readLongInteger | ( | const char * | buffer, |
int & | bytesRead, | ||
int | bytesCount, | ||
bool & | ok | ||
) | [static] |
Returns -1 if value is truncated and bytesRead is not modified.
{ int i; long int value; for(i=bytesRead; i<bytesCount && !isspace(buffer[i]); i++) {} int n=i-bytesRead; if(n>0 && i<bytesCount) { char * temp=new char[n+1]; strncpy(temp, buffer+bytesRead, n); temp[n]='\0'; value=atol(temp); bytesRead=i; ok=true; delete [] temp; } else { value=-1; ok=false; } return value; }