void FileDef::acquireFileVersion() { QCString vercmd = Config_getString("FILE_VERSION_FILTER"); if (!vercmd.isEmpty() && filepath!="generated") { msg("Version of %s : ",filepath.data()); QCString cmd = vercmd+" \""+filepath+"\""; Debug::print(Debug::ExtCmd,0,"Executing popen(`%s`)\n",cmd.data()); FILE *f=portable_popen(cmd,"r"); if (!f) { err("error: could not execute %s\n",vercmd.data()); return; } const int bufSize=1024; char buf[bufSize]; int numRead = fread(buf,1,bufSize,f); portable_pclose(f); if (numRead > 0) { fileVersion = QCString(buf,numRead).stripWhiteSpace(); msg("%s\n",fileVersion.data()); } else { msg("no version available\n"); } } }
//! read a file name \a fileName and optionally filter and transcode it bool readInputFile(const char *fileName,BufStr &inBuf,bool filter=TRUE,bool isSourceCode=FALSE) { // try to open file int size=0; //uint oldPos = dest.curPos(); //printf(".......oldPos=%d\n",oldPos); QFileInfo fi(fileName); if (!fi.exists()) return FALSE; QCString filterName = getFileFilter(fileName,isSourceCode); if (filterName.isEmpty() || !filter) { QFile f(fileName); if (!f.open(IO_ReadOnly)) { return FALSE; } size=fi.size(); // read the file inBuf.skip(size); if (f.readBlock(inBuf.data()/*+oldPos*/,size)!=size) { return FALSE; } } else { QCString cmd=filterName+" \""+fileName+"\""; FILE *f=portable_popen(cmd,"r"); if (!f) { return FALSE; } const int bufSize=1024; char buf[bufSize]; int numRead; while ((numRead=(int)fread(buf,1,bufSize,f))>0) { //printf(">>>>>>>>Reading %d bytes\n",numRead); inBuf.addArray(buf,numRead),size+=numRead; } portable_pclose(f); inBuf.at(inBuf.curPos()) ='\0'; } int start=0; //inBuf.addChar('\n'); /* to prevent problems under Windows ? */ // and translate CR's size=inBuf.curPos()-start; int newSize=filterCRLF(inBuf.data()+start,size); //printf("filter char at %p size=%d newSize=%d\n",dest.data()+oldPos,size,newSize); if (newSize!=size) // we removed chars { inBuf.shrink(newSize); // resize the array //printf(".......resizing from %d to %d result=[%s]\n",oldPos+size,oldPos+newSize,dest.data()); } inBuf.addChar(0); return TRUE; }
/*! Reads a fragment of code from file \a fileName starting at * line \a startLine and ending at line \a endLine (inclusive). The fragment is * stored in \a result. If FALSE is returned the code fragment could not be * found. * * The file is scanned for a opening bracket ('{') from \a startLine onward * The line actually containing the bracket is returned via startLine. * The file is scanned for a closing bracket ('}') from \a endLine backward. * The line actually containing the bracket is returned via endLine. * Note that for VHDL code the bracket search is not done. */ static bool readCodeFragment(const char *fileName, int &startLine,int &endLine,QCString &result) { static bool vhdlOpt = Config_getBool("OPTIMIZE_OUTPUT_VHDL"); static bool filterSourceFiles = Config_getBool("FILTER_SOURCE_FILES"); //printf("readCodeFragment(%s,%d,%d)\n",fileName,startLine,endLine); if (fileName==0 || fileName[0]==0) return FALSE; // not a valid file name QCString filter = getFileFilter(fileName,TRUE); FILE *f=0; bool usePipe = !filter.isEmpty() && filterSourceFiles; if (!usePipe) // no filter given or wanted { f = portable_fopen(fileName,"r"); } else // use filter { QCString cmd=filter+" \""+fileName+"\""; Debug::print(Debug::ExtCmd,0,"Executing popen(`%s`)\n",cmd.data()); f = portable_popen(cmd,"r"); } bool found=vhdlOpt; // for VHDL no bracket search is possible if (f) { int c=0; int col=0; int lineNr=1; // skip until the startLine has reached while (lineNr<startLine && !feof(f)) { while ((c=fgetc(f))!='\n' && c!=EOF) /* skip */; lineNr++; } if (!feof(f)) { // skip until the opening bracket or lonely : is found char cn=0; while (lineNr<=endLine && !feof(f) && !found) { int pc=0; while ((c=fgetc(f))!='{' && c!=':' && c!=EOF) { //printf("parsing char `%c'\n",c); if (c=='\n') { lineNr++,col=0; } else if (c=='\t') { col+=Config_getInt("TAB_SIZE") - (col%Config_getInt("TAB_SIZE")); } else if (pc=='/' && c=='/') // skip single line comment { while ((c=fgetc(f))!='\n' && c!=EOF) pc=c; if (c=='\n') lineNr++,col=0; } else if (pc=='/' && c=='*') // skip C style comment { while (((c=fgetc(f))!='/' || pc!='*') && c!=EOF) { if (c=='\n') lineNr++,col=0; pc=c; } } else { col++; } pc = c; } if (c==':') { cn=fgetc(f); if (cn!=':') found=TRUE; } else if (c=='{') { found=TRUE; } } //printf(" -> readCodeFragment(%s,%d,%d) lineNr=%d\n",fileName,startLine,endLine,lineNr); if (found) { // For code with more than one line, // fill the line with spaces until we are at the right column // so that the opening brace lines up with the closing brace if (endLine!=startLine) { QCString spaces; spaces.fill(' ',col); result+=spaces; } // copy until end of line result+=c; if (c==':') { result+=cn; if (cn=='\n') lineNr++; } startLine=lineNr; const int maxLineLength=4096; char lineStr[maxLineLength]; do { //printf("reading line %d in range %d-%d\n",lineNr,startLine,endLine); int size_read; do { // read up to maxLineLength-1 bytes, the last byte being zero char *p = fgets(lineStr, maxLineLength,f); //printf(" read %s",p); if (p) { size_read=qstrlen(p); } else // nothing read { size_read=-1; lineStr[0]='\0'; } result+=lineStr; } while (size_read == (maxLineLength-1)); lineNr++; } while (lineNr<=endLine && !feof(f)); // strip stuff after closing bracket int newLineIndex = result.findRev('\n'); int braceIndex = result.findRev('}'); if (braceIndex > newLineIndex) { result.truncate(braceIndex+1); } endLine=lineNr-1; } } if (usePipe) { portable_pclose(f); } else { fclose(f); } } result = transcodeCharacterStringToUTF8(result); return found; }