1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
/*
 * MOAB, a Mesh-Oriented datABase, is a software component for creating,
 * storing and accessing finite element mesh data.
 *
 * Copyright 2004 Sandia Corporation.  Under the terms of Contract
 * DE-AC04-94AL85000 with Sandia Corporation, the U.S. Government
 * retains certain rights in this software.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 */

/**\file FileOptions.cpp
 *\author Jason Kraftcheck ([email protected])
 *\date 2007-08-21
 */

#include "moab/FileOptions.hpp"

#include <cctype>
#include <cstdlib>
#include <cstring>
#include <algorithm>

namespace moab
{

const char DEFAULT_SEPARATOR = ';';

static inline bool strempty( const char* s )
{
    return !*s;
}

FileOptions::FileOptions( const char* str ) : mData( 0 )
{
    // if option string is null, just return
    if( !str ) return;

    // check if alternate separator is specified
    char separator[2] = { DEFAULT_SEPARATOR, '\0' };
    if( *str == DEFAULT_SEPARATOR )
    {
        ++str;
        if( strempty( str ) ) return;
        separator[0] = *str;
        ++str;
    }

    // don't bother allocating copy of input string if
    // input string is empty.
    if( !strempty( str ) )
    {
        // tokenize at separator character
        mData = strdup( str );
        for( char* i = strtok( mData, separator ); i; i = strtok( 0, separator ) )
            if( !strempty( i ) )  // skip empty strings
                mOptions.push_back( i );
    }

    mSeen.resize( mOptions.size(), false );
}

FileOptions::FileOptions( const FileOptions& copy ) : mData( 0 ), mOptions( copy.mOptions.size() )
{
    if( !copy.mOptions.empty() )
    {
        const char* last   = copy.mOptions.back();
        const char* endptr = last + strlen( last ) + 1;
        size_t len         = endptr - copy.mData;
        mData              = (char*)malloc( len );
        memcpy( mData, copy.mData, len );
        for( size_t i = 0; i < mOptions.size(); ++i )
            mOptions[i] = mData + ( copy.mOptions[i] - copy.mData );
    }
    mSeen = copy.mSeen;
}

FileOptions& FileOptions::operator=( const FileOptions& copy )
{
    // Check for self-assignment
    if( this == &copy ) return *this;

    free( mData );
    mData = 0;
    mOptions.resize( copy.mOptions.size() );

    if( !copy.mOptions.empty() )
    {
        const char* last   = copy.mOptions.back();
        const char* endptr = last + strlen( last ) + 1;
        size_t len         = endptr - copy.mData;
        mData              = (char*)malloc( len );
        memcpy( mData, copy.mData, len );
        for( size_t i = 0; i < mOptions.size(); ++i )
            mOptions[i] = mData + ( copy.mOptions[i] - copy.mData );
    }

    mSeen = copy.mSeen;
    return *this;
}

FileOptions::~FileOptions()
{
    free( mData );
}

ErrorCode FileOptions::get_null_option( const char* name ) const
{
    const char* s;
    ErrorCode rval = get_option( name, s );
    if( MB_SUCCESS != rval ) return rval;
    return strempty( s ) ? MB_SUCCESS : MB_TYPE_OUT_OF_RANGE;
}

ErrorCode FileOptions::get_int_option( const char* name, int& value ) const
{
    const char* s;
    ErrorCode rval = get_option( name, s );
    if( MB_SUCCESS != rval ) return rval;

    // empty string
    if( strempty( s ) ) return MB_TYPE_OUT_OF_RANGE;

    // parse value
    char* endptr;
    long int pval = strtol( s, &endptr, 0 );
    if( !strempty( endptr ) )  // syntax error
        return MB_TYPE_OUT_OF_RANGE;

    // check for overflow (parsing long int, returning int)
    value = pval;
    if( pval != (long int)value ) return MB_TYPE_OUT_OF_RANGE;

    return MB_SUCCESS;
}

ErrorCode FileOptions::get_int_option( const char* name, int default_val, int& value ) const
{
    const char* s;
    ErrorCode rval = get_option( name, s );
    if( MB_SUCCESS != rval ) return rval;

    // empty string
    if( strempty( s ) )
    {
        value = default_val;
        return MB_SUCCESS;
    }

    // parse value
    char* endptr;
    long int pval = strtol( s, &endptr, 0 );
    if( !strempty( endptr ) )  // syntax error
        return MB_TYPE_OUT_OF_RANGE;

    // check for overflow (parsing long int, returning int)
    value = pval;
    if( pval != (long int)value ) return MB_TYPE_OUT_OF_RANGE;

    return MB_SUCCESS;
}

ErrorCode FileOptions::get_ints_option( const char* name, std::vector< int >& values ) const
{
    const char* s;
    ErrorCode rval = get_option( name, s );
    if( MB_SUCCESS != rval ) return rval;

    // empty string
    if( strempty( s ) ) return MB_TYPE_OUT_OF_RANGE;

    // parse values
    while( !strempty( s ) )
    {
        char* endptr;
        long int sval = strtol( s, &endptr, 0 );

#define EATSPACE( a )                                             \
    while( ( *( a ) == ' ' || *( a ) == ',' ) && !strempty( a ) ) \
        ( a )++;
        EATSPACE( endptr );
        long int eval = sval;
        if( *endptr == '-' )
        {
            endptr++;
            s    = endptr;
            eval = strtol( s, &endptr, 0 );
            EATSPACE( endptr );
        }

        // check for overflow (parsing long int, returning int)
        int value = sval;
        if( sval != (long int)value ) return MB_TYPE_OUT_OF_RANGE;
        value = eval;
        if( eval != (long int)value ) return MB_TYPE_OUT_OF_RANGE;

        for( int i = sval; i <= eval; i++ )
            values.push_back( i );

        s = endptr;
    }

    return MB_SUCCESS;
}

ErrorCode FileOptions::get_reals_option( const char* name, std::vector< double >& values ) const
{
    const char* s;
    ErrorCode rval = get_option( name, s );
    if( MB_SUCCESS != rval ) return rval;

    // empty string
    if( strempty( s ) ) return MB_TYPE_OUT_OF_RANGE;

    // parse values
    while( !strempty( s ) )
    {
        char* endptr;
        double sval = strtod( s, &endptr );

        EATSPACE( endptr );
        values.push_back( sval );

        s = endptr;
    }

    return MB_SUCCESS;
}

ErrorCode FileOptions::get_real_option( const char* name, double& value ) const
{
    const char* s;
    ErrorCode rval = get_option( name, s );
    if( MB_SUCCESS != rval ) return rval;

    // empty string
    if( strempty( s ) ) return MB_TYPE_OUT_OF_RANGE;

    // parse value
    char* endptr;
    value = strtod( s, &endptr );
    if( !strempty( endptr ) )  // syntax error
        return MB_TYPE_OUT_OF_RANGE;

    return MB_SUCCESS;
}

ErrorCode FileOptions::get_strs_option( const char* name, std::vector< std::string >& values ) const
{
    const char* s;
    ErrorCode rval = get_option( name, s );
    if( MB_SUCCESS != rval ) return rval;

    // empty string
    if( strempty( s ) ) return MB_TYPE_OUT_OF_RANGE;

    // parse values
    char separator[3] = { ' ', ',', '\0' };
    char* tmp_str     = strdup( s );
    for( char* i = strtok( tmp_str, separator ); i; i = strtok( 0, separator ) )
        if( !strempty( i ) )  // skip empty strings
            values.push_back( std::string( i ) );
    free( tmp_str );

    return MB_SUCCESS;
}

ErrorCode FileOptions::get_str_option( const char* name, std::string& value ) const
{
    const char* s;
    ErrorCode rval = get_option( name, s );
    if( MB_SUCCESS != rval ) return rval;
    if( strempty( s ) ) return MB_TYPE_OUT_OF_RANGE;
    value = s;
    return MB_SUCCESS;
}

ErrorCode FileOptions::get_option( const char* name, std::string& value ) const
{
    const char* s;
    ErrorCode rval = get_option( name, s );
    if( MB_SUCCESS != rval ) return rval;

    value = s;
    return MB_SUCCESS;
}

ErrorCode FileOptions::get_option( const char* name, const char*& value ) const
{
    std::vector< const char* >::const_iterator i;
    for( i = mOptions.begin(); i != mOptions.end(); ++i )
    {
        const char* opt = *i;
        if( compare( name, opt ) )
        {
            value = opt + strlen( name );
            // if compare returned true, next char after option
            // name must be either the null char or an equals symbol.
            if( *value == '=' ) ++value;

            mSeen[i - mOptions.begin()] = true;
            return MB_SUCCESS;
        }
    }

    return MB_ENTITY_NOT_FOUND;
}

ErrorCode FileOptions::match_option( const char* name, const char* value ) const
{
    int idx;
    const char* array[] = { value, NULL };
    return match_option( name, array, idx );
}

ErrorCode FileOptions::match_option( const char* name, const char* const* values, int& index ) const
{
    const char* optval;
    ErrorCode rval = get_option( name, optval );
    if( MB_SUCCESS != rval ) return rval;

    for( index = 0; values[index]; ++index )
        if( compare( optval, values[index] ) ) return MB_SUCCESS;

    index = -1;
    return MB_FAILURE;
}

ErrorCode FileOptions::get_toggle_option( const char* name, bool default_value, bool& value ) const
{
    static const char* values[] = { "true", "yes", "1", "on", "false", "no", "0", "off", 0 };
    const int num_true          = 4;

    int index;
    ErrorCode result = match_option( name, values, index );
    if( result == MB_SUCCESS )
    {
        value = index < num_true;
    }
    else if( result == MB_ENTITY_NOT_FOUND )
    {
        value  = default_value;
        result = MB_SUCCESS;
    }
    else
    {
        result = MB_TYPE_OUT_OF_RANGE;
    }

    return result;
}

bool FileOptions::compare( const char* name, const char* option )
{
    while( !strempty( name ) && toupper( *name ) == toupper( *option ) )
    {
        ++name;
        ++option;
    }
    // match if name matched option for length of name,
    // and option either matched entirely or matches up to
    // and equals sign.
    return strempty( name ) && ( strempty( option ) || *option == '=' );
}

void FileOptions::get_options( std::vector< std::string >& list ) const
{
    list.clear();
    list.resize( mOptions.size() );
    std::copy( mOptions.begin(), mOptions.end(), list.begin() );
}

bool FileOptions::all_seen() const
{
    return std::find( mSeen.begin(), mSeen.end(), false ) == mSeen.end();
}

void FileOptions::mark_all_seen() const
{
    mSeen.clear();
    mSeen.resize( mOptions.size(), true );
}

ErrorCode FileOptions::get_unseen_option( std::string& name ) const
{
    std::vector< bool >::iterator i = std::find( mSeen.begin(), mSeen.end(), false );
    if( i == mSeen.end() )
    {
        name.clear();
        return MB_ENTITY_NOT_FOUND;
    }

    const char* opt = mOptions[i - mSeen.begin()];
    const char* end = strchr( opt, '=' );
    name            = end ? std::string( opt, end - opt ) : std::string( opt );
    return MB_SUCCESS;
}

}  // namespace moab

#ifdef TEST

using namespace moab;

#include <iostream>

#define CHECK( A )                                                                            \
    if( MB_SUCCESS != ( A ) )                                                                 \
    {                                                                                         \
        std::cerr << "Failure at line " << __LINE__ << ": error code " << ( A ) << std::endl; \
        return 1;                                                                             \
    }

#define EQUAL( A, B )                                                                                               \
    if( ( A ) != ( B ) )                                                                                            \
    {                                                                                                               \
        std::cerr << "Failure at line " << __LINE__ << ": expected " << ( B ) << " but got " << ( A ) << std::endl; \
        return 2;                                                                                                   \
    }

int main()
{
    FileOptions tool( "INT1=1;NUL1;STR1=ABC;DBL1=1.0;dbl2=2.0;DBL3=3.0;INT2=2;nul2;NUL3;INT3=3;str2=once upon a "
                      "time;str3==fubar=;;INTS=1-3,5,6;DBLS=1.0,2.0, 3.0;STRS=var1, var2_var2;STRS2=" );

    std::string s;
    int i;
    double d;
    ErrorCode rval;

    // test basic get_option method without deleting entry
    rval = tool.get_option( "STR1", s );
    CHECK( rval );
    EQUAL( s, "ABC" );

    // test basic get_option method again, this time deleting the entry
    rval = tool.get_option( "STR1", s );
    CHECK( rval );
    EQUAL( s, "ABC" );

    // test basig get_option method with a null option
    rval = tool.get_option( "NUL2", s );
    CHECK( rval );
    EQUAL( s.empty(), true );

    // test null option
    rval = tool.get_null_option( "nul1" );
    CHECK( rval );

    // try null option method on non-null value
    rval = tool.get_null_option( "INT1" );
    EQUAL( rval, MB_TYPE_OUT_OF_RANGE );

    // test integer option
    rval = tool.get_int_option( "int1", i );
    CHECK( rval );
    EQUAL( i, 1 );

    rval = tool.get_int_option( "int2", i );
    CHECK( rval );
    EQUAL( i, 2 );

    // test integer option on non-integer value
    rval = tool.get_int_option( "dbl2", i );
    EQUAL( rval, MB_TYPE_OUT_OF_RANGE );

    // test integer option on null value
    rval = tool.get_int_option( "NUL3", i );
    EQUAL( rval, MB_TYPE_OUT_OF_RANGE );

    // test double option
    rval = tool.get_real_option( "dbl1", d );
    CHECK( rval );
    EQUAL( d, 1.0 );

    rval = tool.get_real_option( "dbl2", d );
    CHECK( rval );
    EQUAL( d, 2.0 );

    rval = tool.get_real_option( "int3", d );
    CHECK( rval );
    EQUAL( d, 3.0 );

    // test real option on non-real value
    rval = tool.get_real_option( "str2", d );
    EQUAL( rval, MB_TYPE_OUT_OF_RANGE );

    // test real option on null value
    rval = tool.get_real_option( "NUL3", d );
    EQUAL( rval, MB_TYPE_OUT_OF_RANGE );

    // test get a simple string option
    rval = tool.get_str_option( "DBL3", s );
    CHECK( rval );
    EQUAL( s, "3.0" );

    // test get a string with spaces
    rval = tool.get_str_option( "STR2", s );
    CHECK( rval );
    EQUAL( s, "once upon a time" );

    // try to get a string value for a null option
    rval = tool.get_str_option( "nul3", s );
    EQUAL( rval, MB_TYPE_OUT_OF_RANGE );

    // We haven't looked at all of the options yet
    EQUAL( false, tool.all_seen() );
    rval = tool.get_unseen_option( s );
    CHECK( rval );
    EQUAL( s, "str3" );

    // test options using generic get_option method

    rval = tool.get_option( "NUL3", s );
    CHECK( rval );
    EQUAL( s.empty(), true );

    rval = tool.get_option( "STR3", s );
    CHECK( rval );
    EQUAL( s, "=fubar=" );

    // test size of options string
    unsigned l = tool.size();
    EQUAL( l, 16u );

    // test ints option
    std::vector< int > ivals;
    rval = tool.get_ints_option( "INTS", ivals );
    CHECK( rval );
    EQUAL( 5, ivals.size() );
    EQUAL( 1, ivals[0] );
    EQUAL( 2, ivals[1] );
    EQUAL( 3, ivals[2] );
    EQUAL( 5, ivals[3] );
    EQUAL( 6, ivals[4] );

    // test dbls option
    std::vector< double > vals;
    rval = tool.get_reals_option( "DBLS", vals );
    CHECK( rval );
    EQUAL( 3, vals.size() );
    EQUAL( 1.0, vals[0] );
    EQUAL( 2.0, vals[1] );
    EQUAL( 3.0, vals[2] );

    // test strs option
    std::vector< std::string > svals;
    rval = tool.get_strs_option( "STRS", svals );
    CHECK( rval );
    EQUAL( 2, svals.size() );
    EQUAL( "var1", svals[0] );
    EQUAL( "var2_var2", svals[1] );

    svals.clear();
    rval = tool.get_strs_option( "STRS2", svals );
    EQUAL( MB_TYPE_OUT_OF_RANGE, rval );

    // We requested every option
    EQUAL( true, tool.all_seen() );
    rval = tool.get_unseen_option( s );
    EQUAL( MB_ENTITY_NOT_FOUND, rval );

    // test alternate separator

    FileOptions tool2( ";+OPT1=ABC+OPT2=" );
    l = tool2.size();
    EQUAL( l, 2 );

    // We haven't looked at all of the options yet
    EQUAL( false, tool2.all_seen() );
    rval = tool2.get_unseen_option( s );
    CHECK( rval );
    EQUAL( s, "OPT1" );

    rval = tool2.get_option( "opt1", s );
    CHECK( rval );
    EQUAL( s, "ABC" );

    rval = tool2.get_option( "opt2", s );
    CHECK( rval );
    bool e = s.empty();
    EQUAL( e, true );

    l = tool2.size();
    EQUAL( l, 2 );

    // We requested every option
    EQUAL( true, tool2.all_seen() );
    rval = tool2.get_unseen_option( s );
    EQUAL( MB_ENTITY_NOT_FOUND, rval );

    // test empty options string

    FileOptions tool3( ";;;;" );
    e = tool3.empty();
    EQUAL( e, true );
    l = tool3.size();
    EQUAL( l, 0 );
    EQUAL( true, tool3.all_seen() );

    FileOptions tool4( NULL );
    e = tool4.empty();
    EQUAL( e, true );
    l = tool4.size();
    EQUAL( l, 0 );
    EQUAL( true, tool4.all_seen() );

    FileOptions tool5( ";+" );
    e = tool5.empty();
    EQUAL( e, true );
    l = tool5.size();
    EQUAL( l, 0 );
    EQUAL( true, tool5.all_seen() );

    // test copy constructor

    const FileOptions& tool6( tool2 );

    rval = tool6.get_option( "opt1", s );
    CHECK( rval );
    EQUAL( s, "ABC" );

    rval = tool6.get_option( "opt2", s );
    CHECK( rval );
    e = s.empty();
    EQUAL( e, true );

    l = tool6.size();
    EQUAL( l, 2 );

    const FileOptions& tool7( tool5 );
    e = tool7.empty();
    EQUAL( e, true );
    l = tool7.size();
    EQUAL( l, 0 );

    // test assignment operator

    FileOptions tool8( tool2 );
    tool8 = tool;<--- 'tool8' is assigned value 'tool' here.
    EQUAL( tool8.size(), tool.size() );

    return 0;
}

#endif