Classes | Functions
definition.h File Reference
#include <qlist.h>
#include <qdict.h>
#include "types.h"

Go to the source code of this file.

Classes

struct  DocInfo
 
struct  BriefInfo
 
struct  BodyInfo
 
class  DefinitionIntf
 
class  Definition
 
struct  Definition::Cookie
 
class  DefinitionList
 
class  DefinitionListIterator
 

Functions

bool readCodeFragment (const char *fileName, int &startLine, int &endLine, QCString &result)
 

Function Documentation

bool readCodeFragment ( const char *  fileName,
int &  startLine,
int &  endLine,
QCString result 
)

Reads a fragment from file fileName starting with line startLine and ending with line endLine. The result is returned as a string via result. The function returns TRUE if successful and FALSE in case of an error.

Reads a fragment of code from file fileName starting at line startLine and ending at line endLine (inclusive). The fragment is stored in result. If FALSE is returned the code fragment could not be found.

The file is scanned for a opening bracket ('{') from startLine onward The line actually containing the bracket is returned via startLine. The file is scanned for a closing bracket ('}') from endLine backward. The line actually containing the bracket is returned via endLine. Note that for VHDL code the bracket search is not done.

Definition at line 728 of file definition.cpp.

730 {
731  static bool filterSourceFiles = Config_getBool("FILTER_SOURCE_FILES");
732  static int tabSize = Config_getInt("TAB_SIZE");
733  //printf("readCodeFragment(%s,%d,%d)\n",fileName,startLine,endLine);
734  if (fileName==0 || fileName[0]==0) return FALSE; // not a valid file name
736  FILE *f=0;
737  bool usePipe = !filter.isEmpty() && filterSourceFiles;
739  if (!usePipe) // no filter given or wanted
740  {
741  f = portable_fopen(fileName,"r");
742  }
743  else // use filter
744  {
745  QCString cmd=filter+" \""+fileName+"\"";
746  Debug::print(Debug::ExtCmd,0,"Executing popen(`%s`)\n",qPrint(cmd));
747  f = portable_popen(cmd,"r");
748  }
749  bool found = lang==SrcLangExt_VHDL ||
750  lang==SrcLangExt_Tcl ||
751  lang==SrcLangExt_Python ||
752  lang==SrcLangExt_Fortran;
753  // for VHDL, TCL, Python, and Fortran no bracket search is possible
754  if (f)
755  {
756  int c=0;
757  int col=0;
758  int lineNr=1;
759  // skip until the startLine has reached
760  while (lineNr<startLine && !feof(f))
761  {
762  while ((c=fgetc(f))!='\n' && c!=EOF) /* skip */;
763  lineNr++;
764  if (found && c == '\n') c = '\0';
765  }
766  if (!feof(f))
767  {
768  // skip until the opening bracket or lonely : is found
769  char cn=0;
770  while (lineNr<=endLine && !feof(f) && !found)
771  {
772  int pc=0;
773  while ((c=fgetc(f))!='{' && c!=':' && c!=EOF) // } so vi matching brackets has no problem
774  {
775  //printf("parsing char `%c'\n",c);
776  if (c=='\n')
777  {
778  lineNr++,col=0;
779  }
780  else if (c=='\t')
781  {
782  col+=tabSize - (col%tabSize);
783  }
784  else if (pc=='/' && c=='/') // skip single line comment
785  {
786  while ((c=fgetc(f))!='\n' && c!=EOF) pc=c;
787  if (c=='\n') lineNr++,col=0;
788  }
789  else if (pc=='/' && c=='*') // skip C style comment
790  {
791  while (((c=fgetc(f))!='/' || pc!='*') && c!=EOF)
792  {
793  if (c=='\n') lineNr++,col=0;
794  pc=c;
795  }
796  }
797  else
798  {
799  col++;
800  }
801  pc = c;
802  }
803  if (c==':')
804  {
805  cn=fgetc(f);
806  if (cn!=':') found=TRUE;
807  }
808  else if (c=='{') // } so vi matching brackets has no problem
809  {
810  found=TRUE;
811  }
812  }
813  //printf(" -> readCodeFragment(%s,%d,%d) lineNr=%d\n",fileName,startLine,endLine,lineNr);
814  if (found)
815  {
816  // For code with more than one line,
817  // fill the line with spaces until we are at the right column
818  // so that the opening brace lines up with the closing brace
819  if (endLine!=startLine)
820  {
821  QCString spaces;
822  spaces.fill(' ',col);
823  result+=spaces;
824  }
825  // copy until end of line
826  if (c) result+=c;
827  startLine=lineNr;
828  if (c==':')
829  {
830  result+=cn;
831  if (cn=='\n') lineNr++;
832  }
833  const int maxLineLength=4096;
834  char lineStr[maxLineLength];
835  do
836  {
837  //printf("reading line %d in range %d-%d\n",lineNr,startLine,endLine);
838  int size_read;
839  do
840  {
841  // read up to maxLineLength-1 bytes, the last byte being zero
842  char *p = fgets(lineStr, maxLineLength,f);
843  //printf(" read %s",p);
844  if (p)
845  {
846  size_read=qstrlen(p);
847  }
848  else // nothing read
849  {
850  size_read=-1;
851  lineStr[0]='\0';
852  }
853  result+=lineStr;
854  } while (size_read == (maxLineLength-1));
855 
856  lineNr++;
857  } while (lineNr<=endLine && !feof(f));
858 
859  // strip stuff after closing bracket
860  int newLineIndex = result.findRev('\n');
861  int braceIndex = result.findRev('}');
862  if (braceIndex > newLineIndex)
863  {
864  result.truncate(braceIndex+1);
865  }
866  endLine=lineNr-1;
867  }
868  }
869  if (usePipe)
870  {
871  portable_pclose(f);
872  Debug::print(Debug::FilterOutput, 0, "Filter output\n");
873  Debug::print(Debug::FilterOutput,0,"-------------\n%s\n-------------\n",qPrint(result));
874  }
875  else
876  {
877  fclose(f);
878  }
879  }
880  result = transcodeCharacterStringToUTF8(result);
881  //fprintf(stderr,"readCodeFragement(%d-%d)=%s\n",startLine,endLine,result.data());
882  return found;
883 }
bool isEmpty() const
Definition: qcstring.h:189
FILE * portable_popen(const char *name, const char *type)
Definition: portable.cpp:400
const bool FALSE
Definition: qglobal.h:370
SrcLangExt
Definition: types.h:41
int findRev(char c, int index=-1, bool cs=TRUE) const
Definition: qcstring.cpp:95
QCString getFileFilter(const char *name, bool isSourceCode)
Definition: util.cpp:2345
QCString transcodeCharacterStringToUTF8(const QCString &input)
Definition: util.cpp:2374
#define Config_getInt(val)
Definition: config.cpp:661
Q_EXPORT uint qstrlen(const char *str)
Definition: qcstring.h:81
fileName
Definition: dumpTree.py:9
static void print(DebugMask mask, int prio, const char *fmt,...)
Definition: debug.cpp:84
p
Definition: test.py:223
#define Config_getBool(val)
Definition: config.cpp:664
SrcLangExt getLanguageFromFileName(const QCString fileName)
Definition: util.cpp:7061
int portable_pclose(FILE *stream)
Definition: portable.cpp:405
bool fill(char c, int len=-1)
Definition: qcstring.h:243
bool truncate(uint pos)
Definition: qcstring.h:232
list cmd
Definition: getreco.py:22
FILE * portable_fopen(const char *fileName, const char *mode)
Definition: portable.cpp:344
const char * qPrint(const char *s)
Definition: qcstring.h:797
const bool TRUE
Definition: qglobal.h:371