My Project
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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.

References Config_getBool, Config_getInt, Debug::ExtCmd, filter(), Debug::FilterOutput, getFileFilter(), getLanguageFromFileName(), portable_fopen(), portable_pclose(), portable_popen(), Debug::print(), SrcLangExt_Fortran, SrcLangExt_Python, SrcLangExt_Tcl, SrcLangExt_VHDL, and transcodeCharacterStringToUTF8().

Referenced by VhdlDocGen::createFlowChart(), MemberContext::Private::sourceCode(), and Definition::writeInlineCode().

{
static bool filterSourceFiles = Config_getBool(FILTER_SOURCE_FILES);
static int tabSize = Config_getInt(TAB_SIZE);
//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",qPrint(cmd));
f = portable_popen(cmd,"r");
}
bool found = lang==SrcLangExt_VHDL ||
lang==SrcLangExt_Tcl ||
// for VHDL, TCL, Python, and Fortran 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 (found && c == '\n') c = '\0';
}
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) // } so vi matching brackets has no problem
{
//printf("parsing char `%c'\n",c);
if (c=='\n')
{
lineNr++,col=0;
}
else if (c=='\t')
{
col+=tabSize - (col%tabSize);
}
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=='{') // } so vi matching brackets has no problem
{
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
if (c) result+=c;
startLine=lineNr;
if (c==':')
{
result+=cn;
if (cn=='\n') 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)
{
Debug::print(Debug::FilterOutput, 0, "Filter output\n");
Debug::print(Debug::FilterOutput,0,"-------------\n%s\n-------------\n",qPrint(result));
}
else
{
fclose(f);
}
}
if (!result.isEmpty() && result.at(result.length()-1)!='\n') result += "\n";
//fprintf(stderr,"readCodeFragement(%d-%d)=%s\n",startLine,endLine,result.data());
return found;
}