#include <File.h>
Static Public Member Functions | |
static QStringList | expand (const QStringList &fileNames) |
static QStringList | expand (const QString &fileName) |
static qint32 | fromBigEndian (qint32 v) |
static qint16 | fromBigEndian (qint16 v) |
static qint32 | fromLittleEndian (qint32 v) |
static qint16 | fromLittleEndian (qint16 v) |
static void | getKeyValue (const char *fileNAme, const char *varName, QString &value) |
static QStringList | getLibList (QStringList pathList) |
static char * | nextField (char *&buf, const char *sep) |
static char * | readAll (const char *fileName) |
static void | readBreakLine (char *&buf, int &bufLen, FILE *f) |
static bool | readCleanUp (char *buf, bool returnValue) |
static bool | readLine (char *&buf, int &bufLen, gzFile f, bool delEOL=false) |
static bool | readLine (char *&buf, int &bufLen, FILE *f, bool delEOL=false) |
static QString | readLine (FILE *f, bool delEOL=false) |
static QString | readLine (bool delEOL) |
static void | readLineNoComments (char *buf, int &bufLen, FILE *f, QString *comments) |
static QString | readLineNoComments (QTextStream &s, QString *comments) |
static char * | stripWhiteSpace (char *buf) |
static const char * | stripWhiteSpace (const char *buf, int &len) |
static QString | uniqueName (QString fileName, const QDir &d) |
QStringList QGpCoreTools::File::expand | ( | const QStringList & | fileNames | ) | [static] |
Expands all wildcards contained in fileNames
Referenced by expand().
{ // Look for wildcards in file names bool hasWildCards[fileNames.count()]; bool hasOneWildCard=false; for (int i=fileNames.count()-1; i>=0; i--) { //QTextStream(stdout) << fileNames.at(i) << ::endl; hasWildCards[i]=fileNames.at(i).contains(QRegExp("[*?]")); if(hasWildCards[i]) hasOneWildCard=true; } if(!hasOneWildCard) { return fileNames; } QStringList expandedFileNames; int n=fileNames.count(); for (int i=0; i<n; i++) { if(hasWildCards[i]) { expandedFileNames+=expand(fileNames.at(i)); } else { expandedFileNames.append(fileNames.at(i)); } } return expandedFileNames; }
QStringList QGpCoreTools::File::expand | ( | const QString & | fileName | ) | [static] |
Expands all wildcards contained in fileName
References expand().
qint32 QGpCoreTools::File::fromBigEndian | ( | qint32 | v | ) | [static] |
Swap the number v only if the current platform is not Big Endian
References TRACE.
{ TRACE; #if Q_BYTE_ORDER==Q_LITTLE_ENDIAN union Swaper { qint32 i; struct { char b[4]; }; }; Swaper s1, s2; s1.i=v; s2.b[0]=s1.b[3]; s2.b[1]=s1.b[2]; s2.b[2]=s1.b[1]; s2.b[3]=s1.b[0]; return s2.i; #else return v; #endif }
qint16 QGpCoreTools::File::fromBigEndian | ( | qint16 | v | ) | [static] |
qint32 QGpCoreTools::File::fromLittleEndian | ( | qint32 | v | ) | [static] |
Swap the number v only if the current platform is not Lttle Endian
References TRACE.
{ TRACE; #if Q_BYTE_ORDER==Q_BIG_ENDIAN union Swaper { qint32 i; struct { char b[4]; }; }; Swaper s1, s2; s1.i=v; s2.b[0]=s1.b[3]; s2.b[1]=s1.b[2]; s2.b[2]=s1.b[1]; s2.b[3]=s1.b[0]; return s2.i; #else return v; #endif }
qint16 QGpCoreTools::File::fromLittleEndian | ( | qint16 | v | ) | [static] |
void QGpCoreTools::File::getKeyValue | ( | const char * | fileName, |
const char * | varName, | ||
QString & | value | ||
) | [static] |
Extract value from any file like a Makefile or a Qt .pro file, i.e. made of format: KEYWORD=VALUE
Lines can broken by \.
References readBreakLine(), stripWhiteSpace(), and TRACE.
{ TRACE; FILE * f=fopen(fileName,"r"); if(!f) return; int varNameLen=strlen(varName); int buflen=512; char * buf=new char [buflen]; while(!feof(f)) { readBreakLine(buf,buflen,f); char * ptr=stripWhiteSpace(buf); if(strncmp(varName,ptr,varNameLen)==0) { ptr+=varNameLen; ptr=stripWhiteSpace(ptr); if(*ptr=='=') { ptr++; ptr=stripWhiteSpace(ptr); value=ptr; } else if(*ptr=='+' && *(ptr+1)=='=') { ptr+=2; ptr=stripWhiteSpace(ptr); value+=ptr; } } } if(value[0]=='\"' && value[value.length()-1]=='\"') { value=value.mid(1,value.length()-2); } delete [] buf; fclose(f); }
QStringList QGpCoreTools::File::getLibList | ( | QStringList | pathList | ) | [static] |
Return the list of all dynamic libraries available in pathList
References TRACE.
{ TRACE; QStringList libFilter; #if defined(Q_WS_X11) libFilter << "lib*.so"; #elif defined(Q_WS_MAC) libFilter << "*.dylib"; #elif defined(Q_WS_WIN) libFilter << "*.dll"; #endif QStringList absLibs; for(QStringList::iterator itPath=pathList.begin(); itPath!=pathList.end(); ++itPath) { QDir d(*itPath); QStringList libs=d.entryList(libFilter); for(QStringList::iterator itLib=libs.begin(); itLib!=libs.end(); ++itLib) { QFileInfo fi(d.absoluteFilePath(*itLib)); if(!fi.isSymLink()) { absLibs << fi.absoluteFilePath(); } } } return absLibs; }
char * QGpCoreTools::File::nextField | ( | char *& | buf, |
const char * | sep | ||
) | [static] |
Split buf into fields separated with one of more separators sep. Return 0 if no more fields are found.
References TRACE.
{ TRACE; while( *buf!=0 && strchr(sep, *buf)!=NULL) { buf++; } if( *buf!=0) { char * ptr=buf; while( *buf!=0 && strchr(sep, *buf)==NULL) { buf++; } if( *buf!=0) { *buf=0; buf++; } return ptr; } else return 0; }
char * QGpCoreTools::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.
References TRACE.
{ TRACE; QFileInfo fi(fileName); unsigned int fileLen=(unsigned int)fi.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; } else { return 0; } } else { return 0; } }
void QGpCoreTools::File::readBreakLine | ( | char *& | buf, |
int & | bufLen, | ||
FILE * | f | ||
) | [static] |
Returns the line read from file f. If the line ends with \ the next lines are concatenated until a line without \ at the end.
References TRACE.
Referenced by getKeyValue().
{ TRACE; buf[0]='\0'; if(!fgets(buf, bufLen, f)) { return; } int n=strlen(buf); while(true) { while(n==bufLen-1 && buf[n-1]!='\n') { int nbufLen=bufLen << 1; char * nbuf=new char [nbufLen]; memmove(nbuf,buf,n); delete [] buf; buf=nbuf; if(!fgets(buf+n, nbufLen-n, f)) { return; } n=strlen(buf); bufLen=nbufLen; } char * ptr=buf+n-1; while(ptr>buf && isspace(*ptr)) ptr--; if(*ptr!='\\') break; n=ptr-buf; if(!fgets (buf+n, bufLen-n, f)) { return; } n=strlen(buf); //printf("%i,%i:%s---\n",n,bufLen,buf); } }
bool QGpCoreTools::File::readCleanUp | ( | char * | buf, |
bool | returnValue | ||
) | [static] |
Delete buf and return returnValue. Handy for parsers based on readLine().
References TRACE.
{ TRACE; delete [] buf; return returnValue; }
bool QGpCoreTools::File::readLine | ( | char *& | buf, |
int & | bufLen, | ||
gzFile | f, | ||
bool | delEOL = false |
||
) | [static] |
Overload function for convenience. Returns the line read from compressed (gzip) file f.
References TRACE.
Referenced by QGpCoreTools::Message::getExistingDirectoryInternal(), QGpCoreTools::Message::getOpenFileNameInternal(), QGpCoreTools::Message::getOpenFileNamesInternal(), QGpCoreTools::Message::getSaveFileNameInternal(), QGpCoreTools::CoreApplicationPrivate::getStdin(), QGpCoreTools::Message::message(), readLine(), readLineNoComments(), and QGpCoreTools::Message::wrongTextFormat().
{ TRACE; buf[0]='\0'; if(!gzgets (f, buf, bufLen)) 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; gzgets (f, buf+bufLen-1, bufLen+1); 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; }
bool QGpCoreTools::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.
References TRACE.
{ TRACE; 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)) { return false; } 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; }
QString QGpCoreTools::File::readLine | ( | FILE * | f, |
bool | delEOL = false |
||
) | [static] |
Overload function for convenience. Returns the line read from f in a QString.
References readLine(), and TRACE.
{ TRACE; int bufLen=256; char * buf=new char[bufLen]; File::readLine(buf, bufLen, f, delEOL); QString line(buf); delete [] buf; return line; }
QString QGpCoreTools::File::readLine | ( | bool | delEOL | ) | [static] |
Overload function for convenience. Returns the line read from stdin in a QString. User interrupt debug facility is stop while user is typing.
References QGpCoreTools::CoreApplicationPrivate::debugUserInterrupts(), QGpCoreTools::CoreApplication::instance(), readLine(), and TRACE.
{ TRACE; int bufLen=256; char * buf=new char[bufLen]; CoreApplication::instance()->debugUserInterrupts(false); File::readLine(buf, bufLen, stdin, delEOL); CoreApplication::instance()->debugUserInterrupts(true); QString line(buf); delete [] buf; return line; }
void QGpCoreTools::File::readLineNoComments | ( | char * | buf, |
int & | bufLen, | ||
FILE * | f, | ||
QString * | comments | ||
) | [static] |
Read next line on stream f stripping out comments (lines starting with '#'). Comments are eventually stored to comments if it is not null.
References readLine(), and TRACE.
QString QGpCoreTools::File::readLineNoComments | ( | QTextStream & | s, |
QString * | comments | ||
) | [static] |
Read next line on stream s stripping out comments (lines starting with '#'). Comments are eventually stored to comments if it is not null.
References TRACE.
{ TRACE; QString buf; while(true) { buf=s.readLine(); if(buf[0]=='#') { if(comments) { comments->append(buf); comments->append("\n"); } } else break; }; return buf; }
char * QGpCoreTools::File::stripWhiteSpace | ( | char * | buf | ) | [static] |
Return a pointer to a string without initial and end blanks (isspace()). No new buffer is allocated. The intput buffer buf is eventually modified to add a '\0' to end the string before the last blanks. The returned pointer cannot be deleted with free or delete.
References TRACE.
Referenced by getKeyValue().
{ TRACE; if(!buf) return 0; char * ptrE=buf+strlen(buf); // Remove white space at the end and at the beginning while(buf<ptrE && isspace(*buf)) buf++; ptrE--; while(buf<ptrE && isspace(*ptrE)) ptrE--; if(buf<ptrE) { ptrE++; *ptrE='\0'; } return buf; }
const char * QGpCoreTools::File::stripWhiteSpace | ( | const char * | buf, |
int & | len | ||
) | [static] |
Overload function for convenience. By contrast with stripWhiteSpace(char * buf) the input buffer is not touched but len is eventually decreased.
References TRACE.
{ TRACE; if(!buf) return 0; const char * ptrE=buf+len; // Remove white space at the end and at the beginning while(buf<ptrE && isspace(*buf)) buf++; ptrE--; while(buf<ptrE && isspace(*ptrE)) ptrE--; if(buf<ptrE) len=ptrE-buf+1; else len=0; return buf; }
QString QGpCoreTools::File::uniqueName | ( | QString | fileName, |
const QDir & | d | ||
) | [static] |
Returns a unique absolute file name in d forged on fileName and eventually _nnnn if the basename is already used. If fileName already contains "_nnnn", the value nnnn is taken as a starting point before incrementing.
References TRACE.
{ TRACE; if(!d.exists(fileName)) return d.absoluteFilePath(fileName); QFileInfo fi(fileName); QString suffix=fi.completeSuffix(); if(!suffix.isEmpty()) suffix="."+suffix; QString baseName=fi.baseName(); // Get a unique file name, that does not exist in d int index; int i=baseName.lastIndexOf( "_" ); if(i==-1) index=-1; else { bool ok; index=baseName.right(baseName.length() - i - 1).toInt(&ok); if( !ok) index=-1; } if(index==-1) { baseName += "_%1"; index=1; } else { baseName=baseName.left(i) + "_%1"; index++; } QString indexStr; indexStr.sprintf( "%04i", index); while(d.exists(baseName.arg(indexStr)+suffix)) { index++; indexStr.sprintf( "%04i", index); } return d.absoluteFilePath(baseName.arg(indexStr)+suffix); }