Class with string parsing algorithms. More...
#include <utilities/string_utilities.h>
Public Member Functions | |
template<class Iter1 , class Iter2 > | |
int | FindSubSequence (const Iter1 &Begin, const Iter1 &End, const Iter2 &SequenceBegin, const Iter2 &SequenceEnd) |
Static Public Member Functions | |
template<class cont > | |
static void | ExplodeString (const std::string &Str, cont &StringSegments, char ch) |
template<class cont > | |
static void | ExplodeString (const std::string &Str, cont &StringSegments, std::set< char > &Chars) |
template<class cont > | |
static void | StringToSet (std::string &Str, cont &c) |
static void | GetCommandLineParameter (const char *CommandLine, const char *ParameterName, const char *DefaultValue, char *ParameterValue) |
template<class str_type > | |
static str_type | GetCommandLineParameter (const str_type &CommandLine, const str_type &ParameterName, const str_type &DefaultValue) |
template<class str_type > | |
static str_type | GetCommandLineParameter (int argc, const char **argv, const str_type &ParameterName, const str_type &DefaultValue) |
static void | GetCommandLineParameter (int argc, const char **argv, const char *ParameterName, const char *DefaultValue, char *ParameterValue) |
static bool | CommandLineParameterExists (const char *CommandLine, const char *ParameterName) |
template<class str_type > | |
static bool | CommandLineParameterExists (const str_type &CommandLine, const str_type &ParameterName) |
static bool | CommandLineParameterExists (int argc, const char **argv, const char *ParameterName) |
template<class str_type > | |
static bool | CommandLineParameterExists (int argc, const char **argv, const str_type &ParameterName) |
template<class str_type > | |
static bool | BeginsWith (const str_type &String, const str_type &Prefix) |
static bool | BeginsWith (const char *String, const char *Prefix) |
template<class str_type > | |
static bool | EndsWith (const str_type &String, const str_type &Suffix) |
static bool | EndsWith (const char *String, const char *Suffix) |
template<class Iter > | |
static std::string | GetCommonPrefix (const Iter &IterBegin, const Iter &IterEnd) |
Class with string parsing algorithms.
Definition at line 1047 of file string_utilities.h.
bool nitro::Parsers::BeginsWith | ( | const str_type & | String, | |
const str_type & | Prefix | |||
) | [static] |
Definition at line 1742 of file string_utilities.h.
References nitro::exception::code(), and nitro::exception::what().
Referenced by CommandLineParameterExists(), and GetCommandLineParameter().
{ try { return( BeginsWith( String.c_str() , Prefix.c_str() ) ); } catch( nitro::exception e ) { throw( nitro::exception( std::string( "Parsers::BeginsWith( const str_type & String , const str_type & Prefix )::" ) + e.what() , e.code() ) ); } catch( ... ) { throw( nitro::exception( std::string( "Parsers::BeginsWith( const str_type & String , const str_type & Prefix )::An error occured while validating prefix " ) + Prefix + " for string " + String , 0 ) ); } }
bool nitro::Parsers::BeginsWith | ( | const char * | String, | |
const char * | Prefix | |||
) | [static] |
Function for string structure validation.
String | - Validating string. | |
Prefix | - Validating prefix. |
nitro::exception | Throws exception with error description. |
Definition at line 953 of file string_utilities.cpp.
References nitro::exception::code(), and nitro::exception::what().
{ try { if( strlen( String ) < strlen( Prefix ) ) { return( false ); } std::size_t i( 0 ); for( ; i < strlen( Prefix ) && Prefix[ i ] == String[ i ] ; i++ ); if( i == strlen( Prefix ) ) { return( true ); } return( false ); } catch( nitro::exception e ) { throw( nitro::exception( std::string( "Parsers::BeginsWith( const char * String , const char * Prefix )::" ) + e.what() , e.code() ) ); } catch( ... ) { throw( nitro::exception( std::string( "Parsers::BeginsWith( const char * String , const char * Prefix )::An error occured" ) , 1 ) ); } }
bool nitro::Parsers::CommandLineParameterExists | ( | const str_type & | CommandLine, | |
const str_type & | ParameterName | |||
) | [static] |
Definition at line 1710 of file string_utilities.h.
References nitro::exception::code(), and nitro::exception::what().
{ try { GetCommandLineParameterExists( CommandLine.c_str() , ParameterName.c_str() ); } catch( nitro::exception e ) { throw( nitro::exception( std::string( "Parsers::CommandLineParameterExists( const str_type & CommandLine , const str_type & ParameterName )::" ) + e.what() , e.code() ) ); } catch( ... ) { throw( nitro::exception( std::string( "Parsers::CommandLineParameterExists( const str_type & CommandLine , const str_type & ParameterName )::An error occured" ) , 0 ) ); } }
bool nitro::Parsers::CommandLineParameterExists | ( | int | argc, | |
const char ** | argv, | |||
const char * | ParameterName | |||
) | [static] |
Function returns parameter from command line.
argc | - Count of tokens in the command line. | |
argv | - Tokens of the command line. | |
ParameterName | - Name of the parameter. |
nitro::exception | Throws exception with error description. |
Definition at line 798 of file string_utilities.cpp.
References nitro::exception::code(), and nitro::exception::what().
{ try { std::string tmpParamName( ParameterName ); for( std::size_t i( 0 ) ; i < ( std::size_t )argc ; i++ ) { if( tmpParamName == argv[ i ] ) { return( true ); } } return( false ); } catch( nitro::exception e ) { throw( nitro::exception( std::string( "Parsers::CommandLineParameterExists( int argc , const char * * argv , const str_type & ParameterName )::" ) + e.what() , e.code() ) ); } catch( ... ) { throw( nitro::exception( std::string( "Parsers::CommandLineParameterExists( int argc , const char * * argv , const str_type & ParameterName )::An error occured" ) , 1 ) ); } }
bool nitro::Parsers::CommandLineParameterExists | ( | int | argc, | |
const char ** | argv, | |||
const str_type & | ParameterName | |||
) | [static] |
Function returns parameter from command line.
argc | - Count of tokens in the command line. | |
argv | - Tokens of the command line. | |
ParameterName | - Name of the parameter. |
nitro::exception | Throws exception with error description. |
Definition at line 1726 of file string_utilities.h.
References nitro::exception::code(), and nitro::exception::what().
{ try { GetCommandLineParameterExists( ParameterName.c_str() ); } catch( nitro::exception e ) { throw( nitro::exception( std::string( "Parsers::CommandLineParameterExists( int argc , const char * * argv , const str_type & ParameterName )::" ) + e.what() , e.code() ) ); } catch( ... ) { throw( nitro::exception( std::string( "Parsers::CommandLineParameterExists( int argc , const char * * argv , const str_type & ParameterName )::An error occured" ) , 0 ) ); } }
bool nitro::Parsers::CommandLineParameterExists | ( | const char * | CommandLine, | |
const char * | ParameterName | |||
) | [static] |
Function for parameter's existance validation.
CommandLine | - Command line. | |
ParameterName | - Command line parameter name. |
nitro::exception | Throws exception with error description. |
Definition at line 757 of file string_utilities.cpp.
References BeginsWith(), nitro::exception::code(), and nitro::exception::what().
Referenced by GetCommandLineParameter().
{ try { for( std::size_t i( 0 ) ; i < strlen( CommandLine ) ; i++ ) { // ищем параметр if( Parsers::BeginsWith( CommandLine + i , ParameterName ) && ( CommandLine[ i + strlen( ParameterName ) ] == ' ' || CommandLine[ i + strlen( ParameterName ) ] == '=' || CommandLine[ i + strlen( ParameterName ) ] == '\0' ) ) { if( i == 0 ) { // типа это самый первый параметр // нашли нужный нам параметр return( true ); } else { // это не самый первый параметр // следовательно надо проверить есть ли перед ним пробел if( CommandLine[ i - 1 ] == ' ' ) { return( true ); } } } } return( false ); } catch( nitro::exception e ) { throw( nitro::exception( std::string( "Parsers::CommandLineParameterExists( const char * CommandLine , const char * ParameterName )::" ) + e.what() , e.code() ) ); } catch( ... ) { throw( nitro::exception( std::string( "Parsers::CommandLineParameterExists( const char * CommandLine , const char * ParameterName )::An error occured" ) , 1 ) ); } }
bool nitro::Parsers::EndsWith | ( | const char * | String, | |
const char * | Suffix | |||
) | [static] |
Function for string structure validation.
String | - Validating string. | |
Suffix | - Validating suffix. |
nitro::exception | Throws exception with error description. |
Definition at line 983 of file string_utilities.cpp.
References nitro::exception::code(), and nitro::exception::what().
{ try { std::size_t a( strlen( String ) ); std::size_t b( strlen( Suffix ) ); if( a < b ) { return( false ); } size_t i( 0 ); for( ; i < b && Suffix[ i ] == String[ a - b + i ] ; i++ ); if( i == strlen( Suffix ) ) { return( true ); } return( false ); } catch( nitro::exception e ) { throw( nitro::exception( std::string( "Parsers::EndsWith( const char * String , const char * Suffix )::" ) + e.what() , e.code() ) ); } catch( ... ) { throw( nitro::exception( std::string( "Parsers::EndsWith( const char * String , const char * Suffix )::An error occured" ) , 1 ) ); } }
bool nitro::Parsers::EndsWith | ( | const str_type & | String, | |
const str_type & | Suffix | |||
) | [static] |
Function for string structure validation.
String | - Validating string. | |
Suffix | - Validating suffix. |
nitro::exception | Throws exception with error description. |
Definition at line 1758 of file string_utilities.h.
References nitro::exception::code(), and nitro::exception::what().
Referenced by nitro::MVC::LoadModules().
{ try { return( EndsWith( String.c_str() , Suffix.c_str() ) ); } catch( nitro::exception e ) { throw( nitro::exception( std::string( "Parsers::EndsWith( const str_type & String , const str_type & Suffix )::" ) + e.what() , e.code() ) ); } catch( ... ) { throw( nitro::exception( std::string( "Parsers::EndsWith( const str_type & String , const str_type & Suffix )::An error occured" ) , 0 ) ); } }
void nitro::Parsers::ExplodeString | ( | const std::string & | Str, | |
cont & | StringSegments, | |||
char | ch | |||
) | [static] |
Function explodes string in several parts.
Str | - Exploding string. | |
StringSegments | - Segments of exploded string. | |
ch | - Simbol wich separates exploding string. |
nitro::exception | Throws exception with error description. |
Definition at line 1573 of file string_utilities.h.
References nitro::exception::code(), and nitro::exception::what().
Referenced by nitro::INIFile::GetString(), nitro::INIFile::LoadINIFile(), and nitro::INIFile::SetString().
{ try { std::set< char > Chars; Chars.insert( ch ); ExplodeString( Str , StrSegments , Chars ); } catch( nitro::exception e ) { throw( nitro::exception( std::string( "Parsers::ExplodeString( const std::string & Str , cont & StrSegments , char ch )::" ) + e.what() , e.code() ) ); } catch( ... ) { throw( nitro::exception( std::string( "Parsers::ExplodeString( const std::string & Str , cont & StrSegments , char ch )::An error occured" ) , 0 ) ); } }
void nitro::Parsers::ExplodeString | ( | const std::string & | Str, | |
cont & | StringSegments, | |||
std::set< char > & | Chars | |||
) | [static] |
Function explodes string in several parts.
Str | - Exploding string. | |
StringSegments | - Segments of exploded string. | |
Chars | - Simbols wich separate exploding string. |
nitro::exception | Throws exception with error description. |
Definition at line 1591 of file string_utilities.h.
References nitro::exception::code(), and nitro::exception::what().
{ try { std::string TmpStr( Str ); std::string::const_iterator IterOld( Str.begin() ); std::string::const_iterator IterNew; for( std::set< char >::iterator i( Chars.begin() ) ; i != Chars.end() && IterNew != Str.end() ; i++ ) { IterNew = std::find( IterOld , Str.end() , *i ); } std::string Segment; while( IterNew != Str.end() ) { Segment.resize( 0 ); std::copy( IterOld , IterNew , std::back_inserter< std::string >( Segment ) ); StringSegments.push_back( Segment ); IterOld = IterNew; IterOld++; for( std::set< char >::iterator i( Chars.begin() ) ; i != Chars.end() && IterNew != Str.end() ; i++ ) { IterNew = std::find( IterOld , Str.end() , *i ); } } Segment = ""; std::copy( IterOld , Str.end() , std::back_inserter< std::string >( Segment ) ); StringSegments.push_back( Segment ); } catch( nitro::exception e ) { throw( nitro::exception( std::string( "Parsers::ExplodeString( const std::string & Str , cont & StrSegments , std::set< char > & Chars )::" ) + e.what() , e.code() ) ); } catch( ... ) { throw( nitro::exception( std::string( "Parsers::ExplodeString( const std::string & Str , cont & StrSegments , std::set< char > & Chars )::An error occured" ) , 0 ) ); } }
int nitro::Parsers::FindSubSequence | ( | const Iter1 & | Begin, | |
const Iter1 & | End, | |||
const Iter2 & | SequenceBegin, | |||
const Iter2 & | SequenceEnd | |||
) |
Function finds subsequence in a sequence.
Begin | - Beginning of the sequence. | |
End | - Ending of the sequence. | |
SequenceBegin | - Beginning of the subsequence. | |
SequenceEnd | - Ending of the subsequence. |
nitro::exception | Throws exception with error description. |
Definition at line 1803 of file string_utilities.h.
References nitro::exception::code(), and nitro::exception::what().
{ try { for( Iter1 i( Begin ) ; i != End ; i++ ) { if( * i == * SequenceBegin ) { Iter2 j( SequenceBegin ); for( ; i != End && j != SequenceEnd && * i == * j ; j++ , i++ ); if( j == SequenceEnd ) { return( ( int )( i - Begin ) ); } } } return( -1 ); } catch( nitro::exception e ) { throw( nitro::exception( std::string( "Parsers::FindSubSequence( const Iter1 & Begin , const Iter1 & End , const Iter2 & SequenceBegin , const Iter2 & SequenceEnd )::" ) + e.what() , e.code() ) ); } catch( ... ) { throw( nitro::exception( std::string( "Parsers::FindSubSequence( const Iter1 & Begin , const Iter1 & End , const Iter2 & SequenceBegin , const Iter2 & SequenceEnd )::An error occured" ) , 1 ) ); } }
str_type nitro::Parsers::GetCommandLineParameter | ( | int | argc, | |
const char ** | argv, | |||
const str_type & | ParameterName, | |||
const str_type & | DefaultValue | |||
) | [static] |
Function returns parameter from command line.
argc | - Count of tokens in the command line. | |
argv | - Tokens of the command line. | |
ParameterName | - Name of the parameter. | |
DefaultValue | - Default value for parameter DefaultValue. |
nitro::exception | Throws exception with error description. |
Definition at line 1679 of file string_utilities.h.
References nitro::exception::code(), GetCommandLineParameter(), and nitro::exception::what().
{ try { std::size_t a( 0 ); for( int i( 0 ) ; i < argc ; i++ ) { a += strlen( argv[ i ] ); } std::size_t b( DefaultValue.size() ); char * ParameterValue( new char [ ( a > b ? a : b ) + 1 ] ); GetCommandLineParameter( argc , argv , ParameterName.c_str() , DefaultValue.c_str() , ParameterValue ); std::string Ret( ParameterValue ); delete [] ParameterValue; return( Ret ); } catch( nitro::exception e ) { throw( nitro::exception( std::string( "Parsers::GetCommandLineParameter( const str_type & CommandLine , const str_type & ParameterName )::" ) + e.what() , e.code() ) ); } catch( ... ) { throw( nitro::exception( std::string( "Parsers::GetCommandLineParameter( const str_type & CommandLine , const str_type & ParameterName )::An error occured" ) , 0 ) ); } }
str_type nitro::Parsers::GetCommandLineParameter | ( | const str_type & | CommandLine, | |
const str_type & | ParameterName, | |||
const str_type & | DefaultValue | |||
) | [static] |
Function returns parameter from command line.
CommandLine | - Command line. | |
ParameterName | - Name of the command line parameter. | |
DefaultValue | - Default value for parameter DefaultValue. |
nitro::exception | Throws exception with error description. |
Definition at line 1653 of file string_utilities.h.
References nitro::exception::code(), GetCommandLineParameter(), and nitro::exception::what().
{ try { std::size_t a( CommandLine.size() ); std::size_t b( DefaultValue.size() ); char * ParameterValue( new char [ ( a > b ? a : b ) + 1 ] ); GetCommandLineParameter( CommandLine.c_str() , ParameterName.c_str() , DefaultValue.c_str() , ParameterValue ); std::string Ret( ParameterValue ); delete [] ParameterValue; return( Ret ); } catch( nitro::exception e ) { throw( nitro::exception( std::string( "Parsers::GetCommandLineParameter( const str_type & CommandLine , const str_type & ParameterName , const str_type & DefaultValue )::" ) + e.what() , e.code() ) ); } catch( ... ) { throw( nitro::exception( std::string( "Parsers::GetCommandLineParameter( const str_type & CommandLine , const str_type & ParameterName , const str_type & DefaultValue )::An error occured" ) , 0 ) ); } }
void nitro::Parsers::GetCommandLineParameter | ( | int | argc, | |
const char ** | argv, | |||
const char * | ParameterName, | |||
const char * | DefaultValue, | |||
char * | ParameterValue | |||
) | [static] |
Function returns parameter from command line.
argc | - Count of tokens in the command line. | |
argv | - Tokens of the command line. | |
ParameterName | - Name of the parameter. | |
DefaultValue | - Default value for parameter DefaultValue. | |
ParameterValue | - Value of the parameter. |
nitro::exception | Throws exception with error description. |
Definition at line 921 of file string_utilities.cpp.
References nitro::exception::code(), and nitro::exception::what().
{ try { std::string tmpParamName( ParameterName ); for( std::size_t i( 0 ) ; i < ( std::size_t )( argc - 1 ) ; i++ ) { if( tmpParamName == argv[ i ] ) { ParameterValue[ 0 ] = '\0'; strcpy( ParameterValue , argv[ i + 1 ] ); return; } } ParameterValue[ 0 ] = '\0'; if( DefaultValue ) { strcpy( ParameterValue , DefaultValue ); } } catch( nitro::exception e ) { throw( nitro::exception( std::string( "Parsers::GetCommandLineParameter( int argc , const char * * argv , const char * ParameterName , const char * DefaultValue , const char * ParameterValue )::" ) + e.what() , e.code() ) ); } catch( ... ) { throw( nitro::exception( std::string( "Parsers::GetCommandLineParameter( int argc , const char * * argv , const char * ParameterName , const char * DefaultValue , const char * ParameterValue )::An error occured" ) , 1 ) ); } }
void nitro::Parsers::GetCommandLineParameter | ( | const char * | CommandLine, | |
const char * | ParameterName, | |||
const char * | DefaultValue, | |||
char * | ParameterValue | |||
) | [static] |
Function returns parameter from command line.
CommandLine | - Command line. | |
ParameterName | - Name of the command line parameter. | |
DefaultValue | - Default value for parameter DefaultValue. | |
ParameterValue | - Value of the parameter. |
nitro::exception | Throws exception with error description. |
Definition at line 823 of file string_utilities.cpp.
References BeginsWith(), nitro::exception::code(), CommandLineParameterExists(), and nitro::exception::what().
Referenced by nitro::PGDatabase::Connect(), nitro::MySQLDatabase::Connect(), and GetCommandLineParameter().
{ try { ParameterValue[ 0 ] = '\0'; if( CommandLineParameterExists( CommandLine , ParameterName ) == false ) { strcpy( ParameterValue , DefaultValue ); return; } bool SearchParameter( true ); bool SpaceSeparatedValue( false ); std::size_t StartValueCursor( 0 ); for( std::size_t i( 0 ) ; i < strlen( CommandLine ) ; i++ ) { if( SearchParameter ) { // ищем параметр if( Parsers::BeginsWith( CommandLine + i , ParameterName ) && ( CommandLine[ i + strlen( ParameterName ) ] == ' ' || CommandLine[ i + strlen( ParameterName ) ] == '=' ) ) { // нашли нужный нам параметр // соотв. теперь можно искать где начинаются данные SearchParameter = false; i += strlen( ParameterName ); } } else { // ищем значение параметра if( ( CommandLine[ i - 1 ] == ' ' || CommandLine[ i - 1 ] == '=' ) && CommandLine[ i ] != ' ' && CommandLine[ i ] != '=' && StartValueCursor == 0 ) { StartValueCursor = i; if( CommandLine[ i ] == '"' && SpaceSeparatedValue == false ) { // наткнулись на начало параметра SpaceSeparatedValue = true; StartValueCursor = i + 1; } continue; } if( StartValueCursor != 0 ) { if( ( CommandLine[ i ] == '"' && SpaceSeparatedValue == true ) || ( CommandLine[ i ] == ' ' && SpaceSeparatedValue == false ) || ( strlen( CommandLine ) == i + 1 && SpaceSeparatedValue == false ) ) { // наткнулись на конец параметра // т.е. конец определяем либо по " если была открывающая кавычка // либо по пробелу, если открывающей кавычки не было /*if( SpaceSeparatedValue ) { strncpy( ParameterValue , CommandLine + StartValueCursor , i - StartValueCursor ); ParameterValue[ i - StartValueCursor ] = '\0'; } else {*/ if( strlen( CommandLine ) == i + 1 && SpaceSeparatedValue == false ) { strncpy( ParameterValue , CommandLine + StartValueCursor , i - StartValueCursor + 1 ); ParameterValue[ i - StartValueCursor + 1 ] = '\0'; } else { strncpy( ParameterValue , CommandLine + StartValueCursor , i - StartValueCursor ); ParameterValue[ i - StartValueCursor ] = '\0'; } return; } } } } if( SpaceSeparatedValue ) { throw( nitro::exception( "Illegal sintax" , 0 ) ); } else { std::size_t StrLen( strlen( CommandLine ) ); strncpy( ParameterValue , CommandLine + StartValueCursor , StrLen - StartValueCursor + 1 ); ParameterValue[ StrLen - StartValueCursor + 1 ] = '\0'; } } catch( nitro::exception e ) { throw( nitro::exception( std::string( "Parsers::GetCommandLineParameter( const char * CommandLine , const char * ParameterName , const char * DefaultValue , char * ParameterValue )::" ) + e.what() , e.code() ) ); } catch( ... ) { throw( nitro::exception( std::string( "Parsers::GetCommandLineParameter( const char * CommandLine , const char * ParameterName , const char * DefaultValue , char * ParameterValue )::An error occured" ) , 1 ) ); } }
std::string nitro::Parsers::GetCommonPrefix | ( | const Iter & | IterBegin, | |
const Iter & | IterEnd | |||
) | [static] |
Function returns common prefix for a set of strings.
IterBegin | - Beginning if the sequence. | |
IterEnd | - Ending of the sequence. |
nitro::exception | Throws exception with error description. |
Definition at line 1774 of file string_utilities.h.
References nitro::exception::code(), and nitro::exception::what().
{ try { std::string Prefix( * IterBegin ); for( Iter i( IterBegin ) ; i != IterEnd ; i++ ) { for( std::size_t j( 0 ) ; j < i->size() && j < Prefix.size() ; j++ ) { if( Prefix[ j ] != ( * i )[ j ] ) { Prefix.resize( j ); } } } return( Prefix ); } catch( nitro::exception e ) { throw( nitro::exception( std::string( "Parsers::GetCommonPrefix( const Iter & IterBegin , const Iter & IterEnd )::" ) + e.what() , e.code() ) ); } catch( ... ) { throw( nitro::exception( std::string( "Parsers::GetCommonPrefix( const Iter & IterBegin , const Iter & IterEnd )::An error occured" ) , 1 ) ); } }
void nitro::Parsers::StringToSet | ( | std::string & | Str, | |
cont & | c | |||
) | [static] |
Function transforms string into character set.
Str | - Transforming string. | |
c | - Character set. |
nitro::exception | Throws exception with error description. |
Definition at line 1634 of file string_utilities.h.
References nitro::exception::code(), and nitro::exception::what().
{ try { for( std::size_t i( 0 ) ; i < Str.size() ; i++ ) { c.insert( Str[ i ] ); } } catch( nitro::exception e ) { throw( nitro::exception( std::string( "Parsers::StringToSet( std::string & Str , cont & c )::" ) + e.what() , e.code() ) ); } catch( ... ) { throw( nitro::exception( std::string( "Parsers::StringToSet( std::string & Str , cont & c )::An error occured" ) , 0 ) ); } }