bug-ocrad
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Bug-ocrad] arg_parser


From: Antonio Diaz Diaz
Subject: Re: [Bug-ocrad] arg_parser
Date: Fri, 09 Mar 2007 16:57:33 +0100
User-agent: Mozilla/5.0 (X11; U; Linux i586; en-US; rv:1.7.11) Gecko/20050905

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,

&regex_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;
  }




reply via email to

[Prev in Thread] Current Thread [Next in Thread]