static ttstr TVPParseCommandLineOne(const ttstr &i) { // value is specified const tjs_char *p, *o; p = o = i.c_str(); p = TJS_strchr(p, '='); if(p == NULL) { return i + TJS_W("=yes"); } p++; ttstr optname(o, p - o); if(*p == TJS_W('\'') || *p == TJS_W('\"')) { // as an escaped string tTJSVariant v; TJSParseString(v, &p); return optname + ttstr(v); } else { // as a string return optname + p; } }
int main( const int argc, const char * const argv[] ) { char verbose = 0; const struct ap_Option options[] = { { 'H', "hidden", ap_no }, { 'V', "version", ap_no }, { 'a', "append", ap_no }, { 'b', "block", ap_yes }, { 'c', "casual", ap_maybe }, { 'h', "help", ap_no }, { 'o', 0, ap_yes }, { 'q', "quiet", ap_no }, { 'u', "uncaught", ap_no }, { 'v', "verbose", ap_no }, { 256, "orphan", ap_no }, { 0, 0, ap_no } }; struct Arg_parser parser; int argind; invocation_name = argv[0]; if( !ap_init( &parser, argc, argv, options, 0 ) ) { show_error( "Memory exhausted.", 0, 0 ); return 1; } if( ap_error( &parser ) ) /* bad option */ { show_error( ap_error( &parser ), 0, 1 ); return 1; } for( argind = 0; argind < ap_arguments( &parser ); ++argind ) { const int code = ap_code( &parser, argind ); if( !code ) break; /* no more options */ switch( code ) { case 'H': break; /* example, do nothing */ case 'V': show_version(); return 0; case 'a': break; /* example, do nothing */ case 'b': break; /* example, do nothing */ case 'c': break; /* example, do nothing */ case 'h': show_help( verbose ); return 0; case 'o': break; /* example, do nothing */ case 'q': verbose = 0; break; /* case 'u': break; */ /* intentionally not caught */ case 'v': verbose = 1; break; case 256: break; /* example, do nothing */ default : internal_error( "uncaught option" ); } } /* end process options */ for( argind = 0; argind < ap_arguments( &parser ); ++argind ) { const int code = ap_code( &parser, argind ); const char * const arg = ap_argument( &parser, argind ); if( code ) /* option */ { const char * const name = optname( code, options ); if( !name[1] ) printf( "option '-%c'", name[0] ); else printf( "option '--%s'", name ); if( arg[0] ) printf( " with argument '%s'", arg ); } else /* non-option */ printf( "non-option argument '%s'", arg ); printf( "\n" ); } if( !ap_arguments( &parser ) ) printf( "Hello, world!\n" ); return 0; }
// read input file from stream void ConfigSvcImplFile::readStream(std::istream& in, const std::string& name) { // read all the lines from the file std::string line ; std::string section ; unsigned int nlines = 0 ; while ( std::getline ( in, line ) ) { nlines ++ ; // skip comments std::string::size_type fchar = line.find_first_not_of(" \t") ; if ( fchar == std::string::npos ) { // empty line //std::cout << "line " << nlines << ": empty\n" ; continue ; } else if ( line[fchar] == '#' ) { // comment //std::cout << "line " << nlines << ": comment\n" ; continue ; } if ( line[fchar] == '[' ) { // must be section name, whole string ends with ']' and we take // everything between as section name std::string::size_type lchar = line.find_last_not_of(" \t\r") ; if ( lchar == std::string::npos or line[lchar] != ']' ) { throw ExceptionSyntax(name, nlines, "illegal section name format"); } // don't need to keep whitespace fchar = line.find_first_not_of(" \t", fchar+1) ; lchar = line.find_last_not_of(" \t", lchar-1) ; section = line.substr( fchar, lchar-fchar+1 ); m_config[section]; //std::cout << "line " << nlines << ": section [" << section << "]\n" ; continue; } // we get an option line, check that section name is defined if ( section.empty() ) { throw ExceptionSyntax(name, nlines, "parameter outside of section"); } // must be option name followed by equal sign std::string::size_type eqpos = line.find( "=", fchar ) ; if ( eqpos == std::string::npos ) { throw ExceptionSyntax(name, nlines, "equal sign missing after option name"); } if ( eqpos == fchar ) { throw ExceptionSyntax(name, nlines, "option name is missing"); } std::string::size_type optend = line.find_last_not_of( " \t", eqpos-1 ) ; std::string optname ( line, fchar, optend-fchar+1 ) ; //std::cout << "line " << nlines << ": option '" << optname << "'\n" ; // get option value std::string optval ; std::string::size_type pos1 = line.find_first_not_of(" \t",eqpos+1) ; //std::cout << "line " << nlines << ": pos1 = " << pos1 << "\n" ; if ( pos1 != std::string::npos ) { std::string::size_type pos2 = line.find_last_not_of( " \t" ) ; //std::cout << "line " << nlines << ": pos2 = " << pos2 << "\n" ; if ( pos2 != std::string::npos ) { optval = std::string ( line, pos1, pos2-pos1+1 ) ; } else { optval = std::string ( line, pos1 ) ; } //std::cout << "line " << nlines << ": value '" << optval << "'\n" ; } // set the option m_config[section][optname] = boost::shared_ptr<std::string>(new std::string(optval)); //std::cout << "line " << nlines << ": '" << optname << "' = '" << optval << "'\n" ; } // check the status of the file, must be at EOF if ( not in.eof() ) { throw ExceptionFileRead(name) ; } }