#include <mcs.hh>
This class parses a MCS command line (very close to the one used in a unix like shell) into its tokens.
In a command line tokens are separated by one or more blanks or tabs. A token can also contains blanks, because everything enclosed in double quotes is taken as a single token, in this case the double quotes are removed from the token. Double quotes can be escaped by a backslash, this way becoming part of the token. Also backslashes can be escaped with another backslash. Tokens are available through the tokenc() and token() methods.
The first token is considered the command, returned by the cmd() method.
After being parsed the tokens are separated into options and arguments. A token is an options if it start with a minus "-" character like the options in a unix shell, and can have an optional value (following the "=" chatracter). Argument are all the other tokens. Options are available through the optc(), opt(), givenOpt() methods. Argument to options are available through the optArg() method. Arguments are available through the argc() and arg() methods.
This class can also be used to read a MCS batch file, or SQL file and perform parameter substitutions. In these scripts an instruction terminate with a semicolon (";") character. Comments are everything between the "#" character and a newline. Parameters are substituted using the $1, $2, etc... placeholders. The replPars_onFile() and replPars() to this job. Consider the following script:
#This is a comment line 1; line 2; #A line with a comment line $1; line $2; #A line with a comment ; line splitted in two $3; line splitted #with comment on the first line in two $3; #and on the second !"£$%&;/()='ì
And a command line:
script one two three
where "script" is the name of the script. A call to replPars_onFile() will return a vector of strings as follows:
line 1 line 2 line one line two line splitted in two three line splitted in two three
Definition at line 5200 of file mcs.hh.
Public Member Functions | |
string | allargs () |
Returns all arguments (not tokens!) as a string. | |
Data & | arg (int i) |
Return argument at i-th position. | |
int | argc () |
Return number of arguments. | |
string | cline () |
Returns the entire command line. | |
string | cmd () |
Returns the first token, that is the command. | |
bool | cmpCmd (string cmd) |
Check if the string in the parameter is the same as the command (case insensitive). | |
CommandParser () | |
Constructor. | |
CommandParser (const CommandParser &) | |
Declared to avoid using of default copy constructor. | |
bool | givenOpt (string opt, int &i) |
Check if a certain option has been given and return the index in the "i" parameter. | |
bool | givenOpt (string opt) |
Check if a certain option has been given. | |
string | line_afterToken (int i) |
Return the "rest" relative to token at i-th position. | |
CommandParser & | operator= (const CommandParser &) |
Declared to avoid using of default assignment operator. | |
string | opt (int i) |
Return option at i-th position. | |
Data & | optarg (int i) |
Return argument to option at i-th position. | |
int | optc () |
Return number of options. | |
void | parseCmd (int argc, char *argv[]) |
Parse a command line using standard argc and argv parameters. | |
void | parseCmd (string c) |
Parse a command line into tokens, arguments and options. | |
string | replPars (string str) |
Perform parameter substitutions. | |
vector< string > | replPars_onFile (string fn) |
Parse a MCS script. | |
string | sarg (int i) |
Return argument at i-th position as a string, see arg(int). | |
string | token (int i) |
Return token at i-th position. | |
int | tokenc () |
Return number of tokens. | |
~CommandParser () | |
Destructor, this destroy all Data objects containing arguments. | |
Private Member Functions | |
void | clearArgs () |
Destroy all Data objects containing arguments. | |
Static Private Member Functions | |
static string | clean (string c) |
Cleans a string. | |
Private Attributes | |
vector< string > | args |
vector of arguments. | |
string | lcmd |
First token, that is the command. | |
MCS_DEBUG_ALLOC | |
Data * | md_args [100] |
Array of arguments stored as Data objects. | |
Data * | md_optargs [100] |
Array of arguments to options stored as Data objects. | |
vector< string > | optargs |
vector of argument to options. | |
vector< string > | opts |
vector of options. | |
string | origcmdline |
Command line parsed. | |
vector< string > | rest |
vector of "rests". | |
vector< string > | tokens |
vector of tokens. |
mcs::CommandParser::CommandParser | ( | const CommandParser & | ) |
Declared to avoid using of default copy constructor.
mcs::CommandParser::CommandParser | ( | ) |
mcs::CommandParser::~CommandParser | ( | ) |
Destructor, this destroy all Data objects containing arguments.
Definition at line 39 of file CommandParser.cc.
string mcs::CommandParser::allargs | ( | ) |
Data & mcs::CommandParser::arg | ( | int | i | ) |
Return argument at i-th position.
The index must be in the allowed range (0 <= i < argc()). Note that i-th position, doesn't mean that the argument is at the i-th position in the command line. Those are tokens.
i | Index of the argument to be returned. |
ERROR | MSG_INDEX_LT_ZERO. | |
ERROR | MSG_INDEX_OUT_RANGE. |
Definition at line 136 of file CommandParser.cc.
int mcs::CommandParser::argc | ( | ) |
string mcs::CommandParser::clean | ( | string | c | ) | [static, private] |
Cleans a string.
Remove tabs, newline, carriage-return, multiple spaces and trailing ";".
Definition at line 46 of file CommandParser.cc.
void mcs::CommandParser::clearArgs | ( | ) | [private] |
string mcs::CommandParser::cline | ( | ) |
string mcs::CommandParser::cmd | ( | ) |
bool mcs::CommandParser::cmpCmd | ( | string | cmd | ) |
Check if the string in the parameter is the same as the command (case insensitive).
Definition at line 190 of file CommandParser.cc.
bool mcs::CommandParser::givenOpt | ( | string | opt, | |
int & | i | |||
) |
Check if a certain option has been given and return the index in the "i" parameter.
Definition at line 202 of file CommandParser.cc.
bool mcs::CommandParser::givenOpt | ( | string | opt | ) |
string mcs::CommandParser::line_afterToken | ( | int | i | ) |
Return the "rest" relative to token at i-th position.
The index must be in the allowed range (0 <= i < tokenc())
i | Index of the "rest" to be returned. |
ERROR | MSG_INDEX_LT_ZERO. | |
ERROR | MSG_INDEX_OUT_RANGE. |
Definition at line 122 of file CommandParser.cc.
CommandParser& mcs::CommandParser::operator= | ( | const CommandParser & | ) |
Declared to avoid using of default assignment operator.
string mcs::CommandParser::opt | ( | int | i | ) |
Return option at i-th position.
The index must be in the allowed range (0 <= i < optc()). Note that i-th position, doesn't mean that the option is at the i-th position in the command line. Those are tokens.
i | Index of the option to be returned. |
ERROR | MSG_INDEX_LT_ZERO. | |
ERROR | MSG_INDEX_OUT_RANGE. |
Definition at line 156 of file CommandParser.cc.
Data & mcs::CommandParser::optarg | ( | int | i | ) |
Return argument to option at i-th position.
Argument to options are the part of the option token after the equal "=" character. If the equal character is not present in the option the corresponding argument is an empty string.
The index must be in the allowed range (0 <= i < optc()). Note that i-th position, doesn't mean that the option is at the i-th position in the command line. Those are tokens.
i | Index of the option to be returned. |
ERROR | MSG_INDEX_LT_ZERO. | |
ERROR | MSG_INDEX_OUT_RANGE. |
Definition at line 176 of file CommandParser.cc.
int mcs::CommandParser::optc | ( | ) |
void mcs::CommandParser::parseCmd | ( | int | argc, | |
char * | argv[] | |||
) |
Parse a command line using standard argc and argv parameters.
Definition at line 222 of file CommandParser.cc.
void mcs::CommandParser::parseCmd | ( | string | c | ) |
Parse a command line into tokens, arguments and options.
Definition at line 233 of file CommandParser.cc.
string mcs::CommandParser::replPars | ( | string | str | ) |
Perform parameter substitutions.
After a command line have been parsed (with parseCmd()) we have at our disposal a number of argument. This method substitute every occurrence of a placeholder of the type $1, $2, etc..., in "str" parameter with the corresponding parameter. It also remove comments.
str | The string in which perform parameter substitutions. |
Definition at line 349 of file CommandParser.cc.
vector< string > mcs::CommandParser::replPars_onFile | ( | string | fn | ) |
Parse a MCS script.
This method reads the file given as parameter, and parse it into single instructions, that is string terminating with a semicolon ";" character. Then it passes the instruction to the replPars() method to substitute the parameters. The returned strings are stored in a vector that is returned after all the content of the file have been parsed.
fn | File to parse. |
ERROR | MSG_CANT_OPEN_FILE. |
Definition at line 318 of file CommandParser.cc.
string mcs::CommandParser::sarg | ( | int | i | ) |
Return argument at i-th position as a string, see arg(int).
Definition at line 150 of file CommandParser.cc.
string mcs::CommandParser::token | ( | int | i | ) |
Return token at i-th position.
The index must be in the allowed range (0 <= i < tokenc())
i | Index of the token to be returned. |
ERROR | MSG_INDEX_LT_ZERO. | |
ERROR | MSG_INDEX_OUT_RANGE. |
Definition at line 108 of file CommandParser.cc.
int mcs::CommandParser::tokenc | ( | ) |
vector<string> mcs::CommandParser::args [private] |
string mcs::CommandParser::lcmd [private] |
Data* mcs::CommandParser::md_args[100] [private] |
Data* mcs::CommandParser::md_optargs[100] [private] |
vector<string> mcs::CommandParser::optargs [private] |
vector<string> mcs::CommandParser::opts [private] |
string mcs::CommandParser::origcmdline [private] |
vector<string> mcs::CommandParser::rest [private] |
vector<string> mcs::CommandParser::tokens [private] |
![]() |
MCS (My Customizable Server) ver. 0.3.3-alpha3
|