Class CppFile
#include <CppFile.h >
Super class: TextFile
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.
const string& filename |
(optional) filename of source code; if present, the file is loaded Default value: "" |
Constructor
Clear all data out of this instance
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
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
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
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
When inside a method definition in the form of class::name(){}, return the class.
Return value: the class name of the current method
(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
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
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
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
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
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
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
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
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
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
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.
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.
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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 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+
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)
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
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
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
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
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
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
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
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 |
Detect the start of the block that encapsulates the current position.
Sets block0_line_no and block0_no_lines.
const string& leader |
|
const string& follower |
|
const string& comment |
|
int& line_no |
Add a Javadoc comment line
Return value: true if
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