Class CppFile

Index Home MAE > MAE Architecture > MAE Data Types > Class Index > Class CppFile

Summary
Public Methods
CppFile(filename)
void clear()
inline bool load(filename)
int getBlockLine0() const
int getBlockSize() const
string getOutsideBlockName(includeClass)
string getBlockClass()
int getBlockDepth() const
bool findMethod(name, fromTop)
inline bool findMethod(fromTop)
string getLineWithoutComments(line_no) const
string getComment(line_no) const
string getEndComment(line_no) const
string getPriorComment(line_no) const
bool isCommentLine(line_no) const
static bool isCommentLine(text)
bool isJavadocComment(line_no) const
JavadocComments getJavadocComments(line_no, after) const
string getDeclComment(line_no, isMethod) const
static string getDeclKeyword(text)
static string getDeclType(text, withPointer, withPreModifiers)
static string getDeclValue(text)
bool getVarDecl(varType, varnames, withPointer, withPreModifiers) const
bool isMethodCall(line_no) const
static bool isMethodCall(text)
static inline bool isInline(text)
static inline bool isStatic(text)
static bool containsKeyword(text, keyword)
bool getMethodCall(line_no, fnClass, fnName, params) const
inline bool getMethodCall(fnClass, fnName, params) const
static bool getMethodCall(text, fnClass, fnName, params)
inline int getNetOpenParens(line_no) const
inline static int getNetOpenParens(text)
inline static int getNetOpenCurlyBraces(text)
CppFile& setLineNo(line_no, line_pos)
CppFile& setLinePos(line_pos)
int getcchar(reverse)
inline int peekcchar(reverse)
string getNextSyntaxUnit(reverse)
bool findSyntaxUnit(unit)
bool findSyntaxSequence(syntaxUnits)
int isMethodDeclaration(line_no, includeFile)
string getMethodDeclarationName(line_no)
bool getMethodDeclaration(line_no, fnType, fnClass, fnName, params, isConst) const
inline bool getMethodDeclaration(fnType, fnClass, fnName, params, isConst) const
static bool getMethodDeclaration(text, fnType, fnClass, fnName, params, isConst)
IntList getMethodDeclarations()
static bool isKeywordCommand(token)
bool insertJavadocComments(javadoc, line_no)
static bool isIdentifier(text)
Private Properties
Private Methods
void detectBlock()
bool addJavadocLine(leader, follower, comment, line_no)
static int getNetOpenPairs(text, open, close)

#include <CppFile.h >

Super class: TextFile

Summary

Reads and allows smart manipulation of a C++ source code.

It does not compile the source code to understand everything, but it contains a lot of convenience methods.

Public Methods

CppFile(filename)

const string& filename

(optional) filename of source code; if present, the file is loaded

Default value: ""


Constructor

void clear()

Clear all data out of this instance

inline bool load(filename)

const string& filename

(optional) filename of source code to load

Default value: ""


Load the C/C++ source code file from the filesystem.

Return value: true if file successfully loaded, false if not

int getBlockLine0() const

When inside multiple blocks, return the line number of the outermost block.

This does not include any comments that precede the block.

Return value: line number of start of outermost block; 0 if unknown

int getBlockSize() const

When inside a block, return the number of source lines that the block spans.

Return value: number of lines for the entirety of the block; 0 if block not known

string getOutsideBlockName(includeClass)

bool includeClass

Flag: whether the class name in the block name (true), or not (false)

Default value: false


When inside a block (method definition, class definition, etc), return the name of the outermost block.

Return value: the block name, null string if unknown

string getBlockClass()

When inside a method definition in the form of class::name(){}, return the class.

Return value: the class name of the current method

int getBlockDepth() const

(Not implemented yet.) Return the number of blocks deep we are at the cursor.

Return value: curly bracket depth at cursor; 0 if not inside block or unknown

bool findMethod(name, fromTop)

string name

name of the method

Default value: ""

bool fromTop

if true, search from the top of the file; if false, search from current position

Default value: false


Given a method's name, locate it inside the source code.

The cursor is moved to the start of the method declaration.

Return value: true if method found, false if not found

inline bool findMethod(fromTop)

bool fromTop

if true, search from the top of the file; if false, search from current position


Find the next instance of the last method searched for using other findMethod(). Handy when there are multiple methods with the same name (but different parameter signatures).

Return value: true if method found, false if not found

string getLineWithoutComments(line_no) const

int line_no

If present, use this source code line instead of the current line.

Default value: 0


Return the source code of the current line, but remove any comments. Comments are replaced with spaces.

Return value: source code at the current line, minus the comments

string getComment(line_no) const

int line_no

If present, use this source code line instead of the current line.

Default value: 0


Return the comment that starts on the current line. The start/stop comment delimeters themselves are not returned.

Return value: the text of the comment, null string if no comment

string getEndComment(line_no) const

int line_no

If present, use this source code line instead of the current line.

Default value: 0


Return the comment on the specified line that ends the line. Any comment interior to C tokens is ignored.

Return value: the text of the comment, null string if no comment

string getPriorComment(line_no) const

int line_no

If present, use this source code line instead of the current line.

Default value: 0


Get the whole comment prior to the file line. A comment block spanning multiple lines will be fetched.

Note that the end comment must be on the prior line; a blank prior line means no comment

Return value: the text of the comment, null string if no comment

bool isCommentLine(line_no) const

int line_no

If present, use this source code line instead of the current line.

Default value: 0


Determine if the file line is a comment line. It must start a new comment or continue a previous comment block.

Return value: true if the file line is a comment line, false if not

static bool isCommentLine(text)

string text

text to scan to check for a comment


Determine if the text provided is a comment line. It must start a new comment or continue a previous comment block.

Return value: true if the text is a comment line, false if not

bool isJavadocComment(line_no) const

int line_no

If present, use this source code line instead of the current line.

Default value: 0


Determine if the comment on the file line is a Javadoc comment. Javadoc comments start with a slash followed by two stars and a space.

Return value: true if a Javadoc comment is present, false if not

JavadocComments getJavadocComments(line_no, after) const

int line_no

If present, use this source code line instead of the current line.

Default value: 0

bool after

true if Javadoc comments follow the line provided, false if preceed

Default value: true


Extract Javadoc comments from the C++ file.  They are expected at line provided.

Return value: s A JavadocComments structure with the various comment parts.

string getDeclComment(line_no, isMethod) const

int line_no

If present, use this source code line instead of the current line.

Default value: 0

bool isMethod

true if declaration is method (so prefer prior comments to same line comments); false if declaration is for variable (so prefer current line comments to prior comments)

Default value: true


Get the comment allociated with the declaration on the line, whether the the declaration is a method or variable.

The strategy is: if there's a preceding comment line/block, use that, next take any comment on the current line.

static string getDeclKeyword(text)

const string& text

text to scan to check for a comment


Given a variable declaration like int a, return the variable name like a.

Also works for variables that start with static, const, struct, etc.

Return value: the variable name

static string getDeclType(text, withPointer, withPreModifiers)

const string& text

the text from which to extract the type

bool withPointer

true to include & or *, false if just the type

Default value: false

bool withPreModifiers

true to keep keywords like static, const, inline, or mutable; false if just the type

Default value: false


Given a variable declaration like int a, return the type like int.

Note that const, static, struct, and class are removed.

Types like "unsigned long" remain as two words.

Note that class and struct are stripped from a declaration, e.g. "class a" - > "a"

Return value: the declaration type

static string getDeclValue(text)

const string& text

the text from which to extract the value


Given a variable declaration like "int a= 5" or method declaration, reutrn the value assigned.

Return value: the value assigned, null string if not found

bool getVarDecl(varType, varnames, withPointer, withPreModifiers) const

string& varType

(output) The class of the method

vector <string >& varnames

(output) The name of the method

bool withPointer

true to include & or *, false if just the type

Default value: false

bool withPreModifiers

true to keep keywords like static, const, inline, or mutable; false if just the type

Default value: false


For the variable declaration that begins at the current line, return its type and name.

If multiple variables are declared (e.g. "int a, b, c;"), all variable names are returned.

If a variable is an array, it's array size part is returned as well.

Return value: true if a valid variable , false it not

bool isMethodCall(line_no) const

int line_no

If present, use this source code line instead of the current line.

Default value: 0


Determine if the current file line contains a method declaration.

Return value: true if a valid method declaration was found, false it not

static bool isMethodCall(text)

const string& text

text to check


Determine if the provided text contains a method declaration.

Return value: true if a valid method declaration was found, false it not

static inline bool isInline(text)

const string& text

text to check


Check if the inline keyword is present on this line

Return value: true if inline keyword present, false otherwise

static inline bool isStatic(text)

const string& text

text to check


Check if the static keyword is present on this line

Return value: true if static keyword present, false otherwise

static bool containsKeyword(text, keyword)

string text

text to scan

const string& keyword

keyword to seek


Check if the provided keyword is present on this line

Return value: true if keyword present, false otherwise

bool getMethodCall(line_no, fnClass, fnName, params) const

int line_no

If present, use this source code line instead of the current line.

string& fnClass

(output) The class of the method

string& fnName

(output) The name of the method

vector <string >& params

(output) The ordered parameters of the method


For the method at the current provided line position, extract the method declaration parts.

Return value: true if a valid method declaration was found, false it not

inline bool getMethodCall(fnClass, fnName, params) const

string& fnClass

(output) The class of the method

string& fnName

(output) The name of the method

vector <string >& params

(output) The ordered parameters of the method


For the method at the current file position, extract the method declaration parts.

Return value: true if a valid method invocation was found, false it not

static bool getMethodCall(text, fnClass, fnName, params)

const string& text

The text to parse

string& fnClass

(output) The class of the method

string& fnName

(output) The name of the method

vector <string >& params

(output) The ordered parameters of the method


For the method in the provided text, extract full method declaration parts.

Return value: true if a valid method invocation was found, false it not

inline int getNetOpenParens(line_no) const

int line_no

If present, use this source code line instead of the current line.

Default value: 0


Scan across the file line and determine how many parenthesis are still open after scanning the last character. Only open and close parenthesis are counted - ( and ).  The characters inside double quotes are ignored.

Return value: the count; positive if more open than close parens; negative if more close than open parens

inline static int getNetOpenParens(text)

const string& text

If present, use this source code line instead of the current line.


Scan across the provided text and determine how many parenthesis are still open after scanning the last character. Only open and close parenthesis are counted - ( and ).  The characters inside double quotes are ignored.

Return value: the count; positive if more open than close parens; negative if more close than open parens

inline static int getNetOpenCurlyBraces(text)

const string& text

If present, use this source code line instead of the current line.


Scan across the provided text and determine how many curly braces are still open after scanning the last character. Only open and close curly braces are counted - { and }.  The characters inside double quotes are ignored.

Return value: the count; positive if more open than close curly braces; negative if more close than open curly braces

CppFile& setLineNo(line_no, line_pos)

int line_no

the file line numuber, 1+

int line_pos

the character position, 1+

Default value: 0


Move the current file position to the line number and character position provided.

Return value: the current instance of CppFile, so another method can be performed on it

CppFile& setLinePos(line_pos)

int line_pos

the character position, 1+


Move the current file position to the character position provided on the current line.

Return value: the current instance of CppFile, so another method can be performed on it

int getcchar(reverse)

bool reverse

Flag: true for previous character, false for next character

Default value: false


Get the next character in the file and advance the current file position.

Return value: the character

inline int peekcchar(reverse)

bool reverse

Flag: true for previous character, false for next character

Default value: false


Look at the next character in the file, but do not update the file position.

Return value: the character

string getNextSyntaxUnit(reverse)

bool reverse

true to search the file backwards from the current point

Default value: false


Continue the last syntax search using findSyntaxUnit() to find the next instance of unit provided then.

The current file position is advanced to the syntax unit.

Return value: the syntax unit

bool findSyntaxUnit(unit)

const string& unit

a single C token to find


Starting at the current file position, return the next C syntax unit (e.g. identifier or logical token).

The current file position is advanced to the syntax unit.

Return value: the syntax unit

bool findSyntaxSequence(syntaxUnits)

const vector <string >& syntaxUnits

an array of C syntax units to find in series; these are not abstract, these are literal


Find the provided C token. A unit is an identifier or logical token (such as !=).

If you are looking for a sequence of tokens, use findSyntaxSequence().

The current file position is advanced to the syntax unit.

Return value: true if token count (current file position updated), false if not

int isMethodDeclaration(line_no, includeFile)

int line_no

If present, use this source code line instead of the current line.

Default value: 0

bool includeFile

true if the source code is an include file, false if the source code is C methods

Default value: false


Determine if the current line is a method declaration.

A method declaration is flush left, contains "(", a token before it and no semicolon in a cpp file, but possibly a semicolon in an include file.

Return value: 0 if it's not a method declaration, otherwise return number of lines in declaration, e.g. 1+

string getMethodDeclarationName(line_no)

int line_no

If present, use this source code line instead of the current line.

Default value: 0


Get the name of the method on the line in question.

Return value: the name of the method (not including the class)

bool getMethodDeclaration(line_no, fnType, fnClass, fnName, params, isConst) const

int line_no

If present, use this source code line instead of the current line.

string& fnType

(output) The return value type of the method

string& fnClass

(output) The class of the method

string& fnName

(output) The name of the method

vector <string >& params

(output) The ordered parameters of the method

bool& isConst

(output) Set to true if const tails the declaration


For the method at the current provided line position, extract the method declaration parts.

Return value: true if a valid method declaration was found, false it not

inline bool getMethodDeclaration(fnType, fnClass, fnName, params, isConst) const

string& fnType

(output) The return value type of the method

string& fnClass

(output) The class of the method

string& fnName

(output) The name of the method

vector <string >& params

(output) The ordered parameters of the method

bool& isConst

(output) Set to true if const tails the declaration


For the method at the current file position, extract the method declaration parts.

Return value: true if a valid method invocation was found, false it not

static bool getMethodDeclaration(text, fnType, fnClass, fnName, params, isConst)

const string& text

The text to parse

string& fnType

(output) The return value type of the method

string& fnClass

(output) The class of the method

string& fnName

(output) The name of the method

vector <string >& params

(output) The ordered parameters of the method

bool& isConst

(output) Set to true if const tails the declaration


For the method in the provided text, extract full method declaration parts.

Return value: true if a valid method invocation was found, false it not

IntList getMethodDeclarations()

Locate all the method declarations in the file and return an array of line numbers for those declarations.

Return value: a list of line numbers with method declarations

static bool isKeywordCommand(token)

const string& token

the tocken to check


Determine if the given toekn is a C language statement keyword, e.g. if, while, return, switch, case, for, continue, break, elase, or do.

Return value: true if the token is a keyword command

bool insertJavadocComments(javadoc, line_no)

JavadocComments javadoc

summary comments, parameter comments, etc.

int line_no

If present, use this source code line instead of the current line.

Default value: 0


Given comments in a JavadocComments structure, insert them into the file at the ilne provided

static bool isIdentifier(text)

const string& text

the text to check


Determine if the provided text is a valid C/C++ identifier.

No spaces, punctuation, or non-alphanumeric characters are allowed if it's an identifier.

Return value: true if the text is an identifier, false if it is not

Private Properties

unsigned int block0_line_no

the starting line number of the outermost block.

int block0_no_lines

the number of lines in the block starting at block0


Private Methods

void detectBlock()

Detect the start of the block that encapsulates the current position.

Sets block0_line_no and block0_no_lines.

bool addJavadocLine(leader, follower, comment, line_no)

const string& leader

const string& follower

const string& comment

int& line_no


Add a Javadoc comment line

Return value: true if

static int getNetOpenPairs(text, open, close)

const string& text

If present, use this source code line instead of the current line.

char open

the character that opens/starts the pair

char close

the character that closes/stops the pair


Scan across the provided text and determine how many pairs are still open after scanning the last character. Only open and close characters are counted as provided. The characters inside double quotes are ignored.

Return value: the count; positive if more open than close pairs; negative if more close than open pairs