Ticket #2957: ticket-2957-generic-option-parser.patch

File ticket-2957-generic-option-parser.patch, 2.2 KB (added by wereHamster, 12 years ago)
  • daemon/daemon.c

    From d05a8bada51954db1d92465db5057f72df29ce4c Mon Sep 17 00:00:00 2001
    From: Tomas Carnecky <tom@dbservice.com>
    Date: Sat, 30 May 2009 00:57:33 +0200
    Subject: [PATCH] Add a generic option parser to the daemon
    
    ---
     daemon/daemon.c |   39 +++++++++++++++++++++++++++++++++++++++
     1 files changed, 39 insertions(+), 0 deletions(-)
    
    diff --git a/daemon/daemon.c b/daemon/daemon.c
    index 6745a57..56dc571 100644
    a b static const struct tr_option options[] = 
    9797    { 'r', "rpc-bind-address", "Where to listen for RPC connections", "r", 1, "<ipv4 address>" },
    9898    { 953, "global-seedratio", "All torrents, unless overridden by a per-torrent setting, should seed until a specific ratio", "gsr", 1, "ratio" },
    9999    { 954, "no-global-seedratio", "All torrents, unless overridden by a per-torrent setting, should seed regardless of ratio", "GSR", 0, NULL },
     100    { 999, "option", "Set the raw option in the settings file", "opt", 1, "<option>=<type>:<value>" },
    100101    { 0, NULL, NULL, NULL, 0, NULL }
    101102};
    102103
    pumpLogMessages( FILE * logfile ) 
    274275    tr_freeMessageList( list );
    275276}
    276277
     278/* generic option parser */
     279static void
     280parseOption( tr_benc * settings, const char * optarg )
     281{
     282        char option[strlen(optarg) + 1];
     283        char * sep, type, * value;
     284
     285        sep = strchr( optarg, '=' );
     286        if( !sep || strlen( sep ) < 4 || sep[2] != ':' ) {
     287                printf( "Malformed option: %s\n", optarg );
     288                return;
     289        }
     290
     291        strcpy(option, optarg);
     292        option[sep - optarg] = 0;
     293
     294        type = sep[1];
     295        value = sep + 3;
     296
     297        switch( type ) {
     298        case 'b':
     299                tr_bencDictAddBool( settings, option, strcmp( value, "true" ) == 0 );
     300                break;
     301        case 'i':
     302                tr_bencDictAddInt( settings, option, atoi( value ) );
     303                break;
     304        case 's':
     305                tr_bencDictAddStr( settings, option, value );
     306                break;
     307        default:
     308                printf( "GenOp: Unknown type '%c'\n", type );
     309                break;
     310        }
     311}
     312
    277313int
    278314main( int argc, char ** argv )
    279315{
    main( int argc, char ** argv ) 
    387423            case 954:
    388424                      tr_bencDictAddBool( &settings, TR_PREFS_KEY_RATIO_ENABLED, FALSE );
    389425                      break;
     426            case 999:
     427                      parseOption( &settings, optarg );
     428                      break;
    390429            default:  showUsage( );
    391430                      break;
    392431        }