]> cygwin.com Git - cygwin-apps/setup.git/blob - libgetopt++/src/OptionSet.cc
2002-04-14 Robert Collins <rbtcollins@hotmail.com>
[cygwin-apps/setup.git] / libgetopt++ / src / OptionSet.cc
1 /*
2 * Copyright (c) 2002 Robert Collins.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * A copy of the GNU General Public License can be found at
10 * http://www.gnu.org/
11 *
12 * Written by Robert Collins <robertc@hotmail.com>
13 *
14 */
15
16 #if HAVE_CONFIG_H
17 #include "autoconf.h"
18 #endif
19 #include "getopt++/OptionSet.h"
20 #include "getopt++/Option.h"
21
22 OptionSet::OptionSet () : options(0), optCount (0) {}
23 OptionSet::~OptionSet ()
24 {
25 delete[] options;
26 }
27
28 void
29 OptionSet::Init()
30 {
31 options = 0;
32 optCount = 0;
33 }
34
35 bool OptionSet::Process (int argc, char **argv)
36 {
37 if (argc == 1)
38 {
39 // log (LOG_TIMESTAMP, "No command line options pass\n");
40 return true;
41 }
42 if (optCount == 0)
43 {
44 // log (LOG_TIMESTAMP,
45 // "%d Command line options passed, and no options registered\n",
46 // argc);
47 return false;
48 }
49 // log (LOG_TIMESTAMP, "Process command line options\n");
50 struct option longopts[optCount + 1];
51 String
52 shortopts;
53 for (int i = 0; i < optCount; ++i)
54 {
55 Option *anOption = options[i];
56 shortopts += anOption->shortOption ();
57 longopts[i] = anOption->longOption ();
58 }
59 char const *
60 opts = shortopts.c_str ();
61 {
62 struct option
63 foo = {
64 0,
65 0,
66 0,
67 0 };
68 longopts[optCount] = foo;
69 }
70 // where is this correctly defined? opterr=0;
71 int lastoption;
72 while ((lastoption = getopt_long (argc, argv, opts, longopts, 0)) != -1)
73 {
74 if (lastoption)
75 {
76 if (lastoption == '\?')
77 {
78 //ambigous option
79 #if HAVE_STRING___H
80 delete[]opts;
81 #endif
82 return false;
83 }
84 for (int i = 0; i < optCount; ++i)
85 {
86 if (longopts[i].val == lastoption && !longopts[i].flag)
87 options[i]->Process (optarg);
88 }
89 }
90 }
91 #if HAVE_STRING___H
92 delete[]opts;
93 #endif
94 return true;
95 }
96
97 //FIXME: check for conflicts.
98 void
99 OptionSet::Register (Option * anOption)
100 {
101 Option **t = new Option *[optCount + 1];
102 for (int i = 0; i < optCount; ++i)
103 t[i] = options[i];
104 t[optCount++] = anOption;
105 delete[]options;
106 options = t;
107 }
This page took 0.046091 seconds and 6 git commands to generate.