~ubuntu-branches/ubuntu/raring/simutrans/raring-proposed

« back to all changes in this revision

Viewing changes to utils/fetchopt.cc

  • Committer: Package Import Robot
  • Author(s): Ansgar Burchardt
  • Date: 2011-11-03 19:59:02 UTC
  • mfrom: (1.2.7)
  • Revision ID: package-import@ubuntu.com-20111103195902-uopgwf488mfctb75
Tags: 111.0-1
* New upstream release.
* debian/rules: Update get-orig-source target for new upstream release.
* Use xz compression for source and binary packages.
* Use override_* targets to simplify debian/rules.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* fetchopt.cc
 
2
 *
 
3
 * Options parsing class
 
4
 * May 2011
 
5
 * by Timothy Baldock <tb@entropy.me.uk>
 
6
 */
 
7
 
 
8
#include <string.h>
 
9
#include "fetchopt.h"
 
10
 
 
11
Fetchopt_t::Fetchopt_t(int argc, char **argv, const char *optstring) {
 
12
        optarg = NULL;
 
13
        optind = 1;
 
14
        optstr = optstring;
 
15
        ac = argc;
 
16
        av = argv;
 
17
        pos = 1;
 
18
}
 
19
 
 
20
char *Fetchopt_t::get_optarg() {
 
21
        return optarg;
 
22
}
 
23
 
 
24
int Fetchopt_t::get_optind() {
 
25
        return optind;
 
26
}
 
27
 
 
28
int Fetchopt_t::next() {
 
29
        optarg = NULL;
 
30
        if (optind >= ac || av[optind][0] != '-') {
 
31
                return -1;
 
32
        }
 
33
        int optchar = av[optind][pos];
 
34
        const char *offset = strchr(optstr, optchar);
 
35
        if (offset == NULL || optchar == ':') {
 
36
                // Invalid option
 
37
                return '?';
 
38
        }
 
39
        if (*(offset+1) == ':') {
 
40
                // Option with argument
 
41
                if (av[optind][pos+1] == '\0') {
 
42
                        // Use next argument for option's argument
 
43
                        if (ac < optind+2) {
 
44
                                // Missing argument
 
45
                                return '?';
 
46
                        } else {
 
47
                                optarg = av[optind+1];
 
48
                                optind += 2;
 
49
                        }
 
50
                } else {
 
51
                        // Use rest of current argument for option's argument
 
52
                        optarg = av[optind]+pos+1;
 
53
                        optind++;
 
54
                }
 
55
                pos = 1;
 
56
                return optchar;
 
57
        } else {
 
58
                // Simple option
 
59
                pos++;
 
60
                if (av[optind][pos] == '\0') {
 
61
                        // Next argument
 
62
                        pos = 1;
 
63
                        optind++;
 
64
                }
 
65
                return optchar;
 
66
        }
 
67
}