arg_parser

View: New views
2 Messages — Rating Filter:   Alert me  

arg_parser

by Horvath, Attila :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Do you have examples of arg_parser being used to parse from configuration file?

Thank you

Attila ? Horvath @ TSC ? COM
(301)576-2419


_______________________________________________
Bug-ocrad mailing list
Bug-ocrad@...
http://lists.gnu.org/mailman/listinfo/bug-ocrad

Re: arg_parser

by Bugzilla from ant_diaz@teleline.es :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Horvath, Attila wrote:
> Do you have examples of arg_parser being used to parse from configuration file?

Yes, see the the function 'RC::process_rcfile' in the file 'rc.cc' from
GNU Moe http://www.gnu.org/software/moe/moe.html

I copy it below for your convenience.

Regards,
Antonio.


     // Returns 0 for success, 1 for file not found, 2 for syntax error.
int RC::process_rcfile( const std::string & name ) throw()
   {
   FILE *f = std::fopen( name.c_str(), "r" );
   if( !f ) return 1;

   std::fprintf( stderr, "Processing `%s'... ", name.c_str() );
   std::fflush( stderr );

   const char delimiters[] = " \t\r\n";
   int line = 0, retval = 0;
   int regex_status = 0; // 1 = got regex name, 2 got regex option
   char *buf = 0;
   size_t size = 0;
   while( true )
     {
     int len = getline( &buf, &size, f );
     if( len <= 0 ) break;
     ++line;
     if( len <= 1 || std::isspace( buf[0] ) ) continue;
     if( buf[0] != '-' ) // Create new file name dependent options
       {
       std::string regex = strtok( buf, delimiters );
       if( regex_status != 1 )
         regex_options_vector.push_back( Regex_options() );
       regex_options_vector.back().regex_vector.push_back( regex );
       regex_status = 1;
       }
     else // Set an option
       {
       char *opt = strtok( buf, delimiters );
       char *arg = strtok( 0, delimiters );
       char msg[80];
       snprintf( msg, sizeof(msg), "\n%s %d: ", name.c_str(), line );
       Arg_parser parser( opt, arg, options );
       if( parser.error().size() || !parser.arguments() || !parser.code(
0 ) )
         { std::fprintf( stderr, "%s%s\n", msg, parser.error().c_str()
); retval = 2; }
       else if( regex_status == 0 )
         { if( !set_option_from_code( parser.code( 0 ), parser.argument(
0 ),
                                      &_editor_options,
&_default_buffer_options,
                                      1, msg ) ) retval = 2; }
       else
         {
         if( !set_option_from_code( parser.code( 0 ), parser.argument( 0
), 0,
 
®ex_options_vector.back().buffer_options,
                                    0, msg ) ) retval = 2;
         regex_status = 2;
         }
       }
     }
   if( buf ) std::free( buf );
   std::fclose( f );
   if( !retval ) std::fprintf( stderr, "done\n" );
   return retval;
   }


_______________________________________________
Bug-ocrad mailing list
Bug-ocrad@...
http://lists.gnu.org/mailman/listinfo/bug-ocrad