This file is indexed.

/usr/include/stxxl/bits/common/cmdline.h is in libstxxl-dev 1.4.1-2.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
651
652
653
654
655
656
657
658
659
660
661
662
/***************************************************************************
 *  include/stxxl/bits/common/cmdline.h
 *
 *  Part of the STXXL. See http://stxxl.sourceforge.net
 *
 *  Copyright (C) 2013 Timo Bingmann <tb@panthema.net>
 *
 *  Distributed under the Boost Software License, Version 1.0.
 *  (See accompanying file LICENSE_1_0.txt or copy at
 *  http://www.boost.org/LICENSE_1_0.txt)
 **************************************************************************/

#ifndef STXXL_COMMON_CMDLINE_HEADER
#define STXXL_COMMON_CMDLINE_HEADER

#include <cstddef>
#include <cstdio>
#include <iostream>
#include <string>
#include <vector>

#include <stxxl/bits/common/types.h>
#include <stxxl/bits/common/utils.h>
#include <stxxl/bits/namespace.h>
#include <stxxl/bits/noncopyable.h>

STXXL_BEGIN_NAMESPACE

//! \addtogroup support
//! \{

/**
 * Command line parser which automatically fills variables and prints nice
 * usage messages.
 *
 * This is a straightforward command line parser in C++, which will recognize
 * short options -s, long options --long and parameters, both required and
 * optional. It will automatically parse integers and <b>byte sizes</b> with
 * SI/IEC suffixes (e.g. 1 GiB). It also works with lists of strings,
 * e.g. multiple filenames.
 *
 * Maybe most important it will nicely format the options and parameters
 * description using word wrapping.
 */
class cmdline_parser : private noncopyable
{
protected:
    //! base class of all options and parameters
    struct argument
    {
        //! single letter short option, or 0 is none
        char m_key;
        //! long option key or name for parameters
        std::string m_longkey;
        //! option type description, e.g. "<#>" to indicate numbers
        std::string m_keytype;
        //! longer description, which will be wrapped
        std::string m_desc;
        //! required, process() fails if the option/parameter is not found.
        bool m_required;
        //! found during processing of command line
        bool m_found;
        //! repeated argument, i.e. std::vector<std::string>
        bool m_repeated;

        //! contructor filling most attributes
        argument(char key, const std::string& longkey, const std::string& keytype,
                 const std::string& desc, bool required)
            : m_key(key), m_longkey(longkey), m_keytype(keytype),
              m_desc(desc), m_required(required), m_found(false), m_repeated(false)
        { }

        //! empty virtual destructor
        virtual ~argument() { }

        //! return formatted type name to user
        virtual const char * type_name() const = 0;

        //! process one item from command line for this argument
        virtual bool process(int& argc, const char* const*& argv) = 0;

        //! format value to ostream
        virtual void print_value(std::ostream& os) const = 0;

        //! return 'longkey [keytype]'
        std::string param_text() const
        {
            std::string s = m_longkey;
            if (m_keytype.size()) {
                s += ' ' + m_keytype;
            }
            return s;
        }

        //! return '-s, --longkey [keytype]'
        std::string option_text() const
        {
            std::string s;
            if (m_key) {
                s += '-';
                s += m_key;
                s += ", ";
            }
            s += "--";
            s += m_longkey;
            if (m_keytype.size()) {
                s += ' ' + m_keytype;
            }
            return s;
        }
    };

    //! specialization of argument for boolean flags (can only be set to true).
    struct argument_flag : public argument
    {
        //! reference to boolean to set to true
        bool& m_dest;

        //! contructor filling most attributes
        argument_flag(char key, const std::string& longkey, const std::string& keytype,
                      const std::string& desc, bool required, bool& dest)
            : argument(key, longkey, keytype, desc, required),
              m_dest(dest)
        { }

        virtual const char * type_name() const
        { return "flag"; }

        //! "process" argument: just set to true, no argument is used.
        virtual bool process(int&, const char* const*&)
        {
            m_dest = true;
            return true;
        }

        virtual void print_value(std::ostream& os) const
        { os << (m_dest ? "true" : "false"); }
    };

    //! specialization of argument for integer options or parameters
    struct argument_int : public argument
    {
        int& m_dest;

        //! contructor filling most attributes
        argument_int(char key, const std::string& longkey, const std::string& keytype,
                     const std::string& desc, bool required, int& dest)
            : argument(key, longkey, keytype, desc, required),
              m_dest(dest)
        { }

        virtual const char * type_name() const
        { return "integer"; }

        //! parse signed integer using sscanf.
        virtual bool process(int& argc, const char* const*& argv)
        {
            if (argc == 0) return false;
            if (sscanf(argv[0], "%d", &m_dest) == 1) {
                --argc, ++argv;
                return true;
            } else {
                return false;
            }
        }

        virtual void print_value(std::ostream& os) const
        { os << m_dest; }
    };

    //! specialization of argument for unsigned integer options or parameters
    struct argument_uint : public argument
    {
        unsigned int& m_dest;

        //! contructor filling most attributes
        argument_uint(char key, const std::string& longkey, const std::string& keytype,
                      const std::string& desc, bool required, unsigned int& dest)
            : argument(key, longkey, keytype, desc, required),
              m_dest(dest)
        { }

        virtual const char * type_name() const
        { return "unsigned integer"; }

        //! parse unsigned integer using sscanf.
        virtual bool process(int& argc, const char* const*& argv)
        {
            if (argc == 0) return false;
            if (sscanf(argv[0], "%u", &m_dest) == 1) {
                --argc, ++argv;
                return true;
            } else {
                return false;
            }
        }

        virtual void print_value(std::ostream& os) const
        { os << m_dest; }
    };

    //! specialization of argument for SI/IEC suffixes byte size options or parameters
    struct argument_bytes32 : public argument
    {
        uint32& m_dest;

        //! contructor filling most attributes
        argument_bytes32(char key, const std::string& longkey, const std::string& keytype,
                         const std::string& desc, bool required, uint32& dest)
            : argument(key, longkey, keytype, desc, required),
              m_dest(dest)
        { }

        virtual const char * type_name() const
        { return "bytes"; }

        //! parse byte size using SI/IEC parser from stxxl.
        virtual bool process(int& argc, const char* const*& argv)
        {
            if (argc == 0) return false;
            uint64 dest;
            if (parse_SI_IEC_size(argv[0], dest) &&
                (uint64)(m_dest = (uint32)dest) == dest) {
                --argc, ++argv;
                return true;
            } else {
                return false;
            }
        }

        virtual void print_value(std::ostream& os) const
        { os << m_dest; }
    };

    //! specialization of argument for SI/IEC suffixes byte size options or parameters
    struct argument_bytes64 : public argument
    {
        uint64& m_dest;

        //! contructor filling most attributes
        argument_bytes64(char key, const std::string& longkey, const std::string& keytype,
                         const std::string& desc, bool required, uint64& dest)
            : argument(key, longkey, keytype, desc, required),
              m_dest(dest)
        { }

        virtual const char * type_name() const
        { return "bytes"; }

        //! parse byte size using SI/IEC parser from stxxl.
        virtual bool process(int& argc, const char* const*& argv)
        {
            if (argc == 0) return false;
            if (parse_SI_IEC_size(argv[0], m_dest)) {
                --argc, ++argv;
                return true;
            } else {
                return false;
            }
        }

        virtual void print_value(std::ostream& os) const
        { os << m_dest; }
    };

    //! specialization of argument for string options or parameters
    struct argument_string : public argument
    {
        std::string& m_dest;

        //! contructor filling most attributes
        argument_string(char key, const std::string& longkey, const std::string& keytype,
                        const std::string& desc, bool required, std::string& dest)
            : argument(key, longkey, keytype, desc, required),
              m_dest(dest)
        { }

        virtual const char * type_name() const
        { return "string"; }

        //! "process" string argument just by storing it.
        virtual bool process(int& argc, const char* const*& argv)
        {
            if (argc == 0) return false;
            m_dest = argv[0];
            --argc, ++argv;
            return true;
        }

        virtual void print_value(std::ostream& os) const
        { os << '"' << m_dest << '"'; }
    };

    //! specialization of argument for multiple string options or parameters
    struct argument_stringlist : public argument
    {
        std::vector<std::string>& m_dest;

        //! contructor filling most attributes
        argument_stringlist(char key, const std::string& longkey, const std::string& keytype,
                            const std::string& desc, bool required, std::vector<std::string>& dest)
            : argument(key, longkey, keytype, desc, required),
              m_dest(dest)
        {
            m_repeated = true;
        }

        virtual const char * type_name() const
        { return "string list"; }

        //! "process" string argument just by storing it in vector.
        virtual bool process(int& argc, const char* const*& argv)
        {
            if (argc == 0) return false;
            m_dest.push_back(argv[0]);
            --argc, ++argv;
            return true;
        }

        virtual void print_value(std::ostream& os) const
        {
            os << '[';
            for (size_t i = 0; i < m_dest.size(); ++i)
            {
                if (i != 0) os << ',';
                os << '"' << m_dest[i] << '"';
            }
            os << ']';
        }
    };

protected:
    //! option and parameter list type
    typedef std::vector<argument*> arglist_type;

    //! list of options available
    arglist_type m_optlist;
    //! list of parameters, both required and optional
    arglist_type m_paramlist;

    //! formatting width for options, '-s, --switch <#>'
    int m_opt_maxlong;
    //! formatting width for parameters, 'param <#>'
    int m_param_maxlong;

    //! argv[0] for usage.
    const char* m_progname;

    //! verbose processing of arguments
    bool m_verbose_process;

    //! user set description of program, will be wrapped
    std::string m_description;
    //! user set author of program, will be wrapped
    std::string m_author;

    //! set line wrap length
    unsigned int m_linewrap;

    //! maximum length of a type_name() result
    static const int m_maxtypename = 16;

private:
    //! update maximum formatting width for new option
    void calc_opt_max(const argument* arg)
    {
        m_opt_maxlong = STXXL_MAX((int)arg->option_text().size() + 2,
                                  m_opt_maxlong);
    }

    //! update maximum formatting width for new parameter
    void calc_param_max(const argument* arg)
    {
        m_param_maxlong = STXXL_MAX((int)arg->param_text().size() + 2,
                                    m_param_maxlong);
    }

public:
    //! Wrap a long string at spaces into lines. Prefix is added
    //! unconditionally to each line. Lines are wrapped after wraplen
    //! characters if possible.
    static void
    output_wrap(std::ostream& os, const std::string& text, size_t wraplen,
                size_t indent_first = 0, size_t indent_rest = 0,
                size_t current = 0, size_t indent_newline = 0);

public:
    //! Construct new command line parser
    cmdline_parser()
        : m_opt_maxlong(8),
          m_param_maxlong(8),
          m_progname(NULL),
          m_verbose_process(true),
          m_linewrap(80)
    { }

    //! Delete all added arguments
    ~cmdline_parser()
    {
        for (size_t i = 0; i < m_optlist.size(); ++i)
            delete m_optlist[i];
        m_optlist.clear();

        for (size_t i = 0; i < m_paramlist.size(); ++i)
            delete m_paramlist[i];
        m_paramlist.clear();
    }

    //! Set description of program, text will be wrapped
    void set_description(const std::string& description)
    {
        m_description = description;
    }

    //! Set author of program, will be wrapped.
    void set_author(const std::string& author)
    {
        m_author = author;
    }

    //! Set verbose processing of command line arguments
    void set_verbose_process(bool verbose_process)
    {
        m_verbose_process = verbose_process;
    }

    // ************************************************************************

    //! add boolean option flag -key, --longkey [keytype] with description and store to dest
    void add_flag(char key, const std::string& longkey, const std::string& keytype, const std::string& desc, bool& dest)
    {
        m_optlist.push_back(
            new argument_flag(key, longkey, keytype, desc, false, dest)
            );
        calc_opt_max(m_optlist.back());
    }

    //! add signed integer option -key, --longkey [keytype] with description and store to dest
    void add_int(char key, const std::string& longkey, const std::string& keytype, const std::string& desc, int& dest)
    {
        m_optlist.push_back(
            new argument_int(key, longkey, keytype, desc, false, dest)
            );
        calc_opt_max(m_optlist.back());
    }

    //! add unsigned integer option -key, --longkey [keytype] with description and store to dest
    void add_uint(char key, const std::string& longkey, const std::string& keytype, const std::string& desc, unsigned int& dest)
    {
        m_optlist.push_back(
            new argument_uint(key, longkey, keytype, desc, false, dest)
            );
        calc_opt_max(m_optlist.back());
    }

    //! add SI/IEC suffixes byte size option -key, --longkey [keytype] and store to 64-bit dest
    void add_bytes(char key, const std::string& longkey, const std::string& keytype, const std::string& desc, stxxl::uint32& dest)
    {
        m_optlist.push_back(
            new argument_bytes32(key, longkey, keytype, desc, false, dest)
            );
        calc_opt_max(m_optlist.back());
    }

    //! add SI/IEC suffixes byte size option -key, --longkey [keytype] and store to 64-bit dest
    void add_bytes(char key, const std::string& longkey, const std::string& keytype, const std::string& desc, stxxl::uint64& dest)
    {
        m_optlist.push_back(
            new argument_bytes64(key, longkey, keytype, desc, false, dest)
            );
        calc_opt_max(m_optlist.back());
    }

    //! add string option -key, --longkey [keytype] and store to dest
    void add_string(char key, const std::string& longkey, const std::string& keytype, const std::string& desc, std::string& dest)
    {
        m_optlist.push_back(
            new argument_string(key, longkey, keytype, desc, false, dest)
            );
        calc_opt_max(m_optlist.back());
    }

    //! add string list option -key, --longkey [keytype] and store to dest
    void add_stringlist(char key, const std::string& longkey, const std::string& keytype, const std::string& desc, std::vector<std::string>& dest)
    {
        m_optlist.push_back(
            new argument_stringlist(key, longkey, keytype, desc, false, dest)
            );
        calc_opt_max(m_optlist.back());
    }

    //! add boolean option flag -key, --longkey with description and store to dest
    void add_flag(char key, const std::string& longkey, const std::string& desc, bool& dest)
    { return add_flag(key, longkey, "", desc, dest); }

    //! add signed integer option -key, --longkey with description and store to dest
    void add_int(char key, const std::string& longkey, const std::string& desc, int& dest)
    { return add_int(key, longkey, "", desc, dest); }

    //! add unsigned integer option -key, --longkey [keytype] with description and store to dest
    void add_uint(char key, const std::string& longkey, const std::string& desc, unsigned int& dest)
    { return add_uint(key, longkey, "", desc, dest); }

    //! add SI/IEC suffixes byte size option -key, --longkey [keytype] and store to 32-bit dest
    void add_bytes(char key, const std::string& longkey, const std::string& desc, stxxl::uint32& dest)
    { return add_bytes(key, longkey, "", desc, dest); }

    //! add SI/IEC suffixes byte size option -key, --longkey [keytype] and store to 64-bit dest
    void add_bytes(char key, const std::string& longkey, const std::string& desc, stxxl::uint64& dest)
    { return add_bytes(key, longkey, "", desc, dest); }

    //! add string option -key, --longkey [keytype] and store to dest
    void add_string(char key, const std::string& longkey, const std::string& desc, std::string& dest)
    { return add_string(key, longkey, "", desc, dest); }

    //! add string list option -key, --longkey [keytype] and store to dest
    void add_stringlist(char key, const std::string& longkey, const std::string& desc, std::vector<std::string>& dest)
    { return add_stringlist(key, longkey, "", desc, dest); }

    // ************************************************************************

    //! add signed integer parameter [name] with description and store to dest
    void add_param_int(const std::string& name, const std::string& desc, int& dest)
    {
        m_paramlist.push_back(
            new argument_int(0, name, "", desc, true, dest)
            );
        calc_param_max(m_paramlist.back());
    }

    //! add unsigned integer parameter [name] with description and store to dest
    void add_param_uint(const std::string& name, const std::string& desc, unsigned int& dest)
    {
        m_paramlist.push_back(
            new argument_uint(0, name, "", desc, true, dest)
            );
        calc_param_max(m_paramlist.back());
    }

    //! add SI/IEC suffixes byte size parameter [name] with description and store to dest
    void add_param_bytes(const std::string& name, const std::string& desc, uint32& dest)
    {
        m_paramlist.push_back(
            new argument_bytes32(0, name, "", desc, true, dest)
            );
        calc_param_max(m_paramlist.back());
    }

    //! add SI/IEC suffixes byte size parameter [name] with description and store to dest
    void add_param_bytes(const std::string& name, const std::string& desc, uint64& dest)
    {
        m_paramlist.push_back(
            new argument_bytes64(0, name, "", desc, true, dest)
            );
        calc_param_max(m_paramlist.back());
    }

    //! add string parameter [name] with description and store to dest
    void add_param_string(const std::string& name, const std::string& desc, std::string& dest)
    {
        m_paramlist.push_back(
            new argument_string(0, name, "", desc, true, dest)
            );
        calc_param_max(m_paramlist.back());
    }

    //! add string list parameter [name] with description and store to dest.
    //! \warning this parameter must be last, as it will gobble all non-option arguments!
    void add_param_stringlist(const std::string& name, const std::string& desc, std::vector<std::string>& dest)
    {
        m_paramlist.push_back(
            new argument_stringlist(0, name, "", desc, true, dest)
            );
        calc_param_max(m_paramlist.back());
    }

    // ************************************************************************

    //! add optional signed integer parameter [name] with description and store to dest
    void add_opt_param_int(const std::string& name, const std::string& desc, int& dest)
    {
        m_paramlist.push_back(
            new argument_int(0, name, "", desc, false, dest)
            );
        calc_param_max(m_paramlist.back());
    }

    //! add optional unsigned integer parameter [name] with description and store to dest
    void add_opt_param_uint(const std::string& name, const std::string& desc, unsigned int& dest)
    {
        m_paramlist.push_back(
            new argument_uint(0, name, "", desc, false, dest)
            );
        calc_param_max(m_paramlist.back());
    }

    //! add optional SI/IEC suffixes byte size parameter [name] with description and store to dest
    void add_opt_param_bytes(const std::string& name, const std::string& desc, uint32& dest)
    {
        m_paramlist.push_back(
            new argument_bytes32(0, name, "", desc, false, dest)
            );
        calc_param_max(m_paramlist.back());
    }

    //! add optional SI/IEC suffixes byte size parameter [name] with description and store to dest
    void add_opt_param_bytes(const std::string& name, const std::string& desc, uint64& dest)
    {
        m_paramlist.push_back(
            new argument_bytes64(0, name, "", desc, false, dest)
            );
        calc_param_max(m_paramlist.back());
    }

    //! add optional string parameter [name] with description and store to dest
    void add_opt_param_string(const std::string& name, const std::string& desc, std::string& dest)
    {
        m_paramlist.push_back(
            new argument_string(0, name, "", desc, false, dest)
            );
        calc_param_max(m_paramlist.back());
    }

    //! add optional string parameter [name] with description and store to dest
    //! \warning this parameter must be last, as it will gobble all non-option arguments!
    void add_opt_param_stringlist(const std::string& name, const std::string& desc, std::vector<std::string>& dest)
    {
        m_paramlist.push_back(
            new argument_stringlist(0, name, "", desc, false, dest)
            );
        calc_param_max(m_paramlist.back());
    }

    // ************************************************************************

    //! output nicely formatted usage information including description of all
    //! parameters and options.
    void print_usage(std::ostream& os = std::cout);

private:
    //! print error about option.
    void print_option_error(int argc, const char* const* argv, const argument* arg,
                            std::ostream& os);

    //! print error about parameter.
    void print_param_error(int argc, const char* const* argv, const argument* arg,
                           std::ostream& os);

public:
    //! parse command line options as specified by the options and parameters added.
    //! \return true if command line is okay and all required parameters are present.
    bool process(int argc, const char* const* argv, std::ostream& os = std::cout);

    //! print nicely formatted result of processing
    void print_result(std::ostream& os = std::cout);
};

//! \}

STXXL_END_NAMESPACE

#endif // !STXXL_COMMON_CMDLINE_HEADER