]> cygwin.com Git - cygwin-apps/setup.git/blob - libgetopt++/src/GetOption.cc
2002-04-14 Robert Collins <rbtcollins@hotmail.com>
[cygwin-apps/setup.git] / libgetopt++ / src / GetOption.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 "GetOption.h"
20 #include "Option.h"
21
22 GetOption
23 GetOption::Instance;
24
25 GetOption & GetOption::GetInstance ()
26 {
27 if (Instance.inited != 42)
28 {
29 Instance.Init ();
30 }
31 return Instance;
32 }
33
34 bool GetOption::Process (int argc, char **argv)
35 {
36 if (argc == 1)
37 {
38 // log (LOG_TIMESTAMP, "No command line options pass\n");
39 return true;
40 }
41 if (optCount == 0)
42 {
43 // log (LOG_TIMESTAMP,
44 // "%d Command line options passed, and no options registered\n",
45 // argc);
46 return false;
47 }
48 // log (LOG_TIMESTAMP, "Process command line options\n");
49 struct option
50 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
72 lastoption;
73 while ((lastoption = getopt_long (argc, argv, opts, longopts, 0)) != -1)
74 {
75 if (lastoption)
76 {
77 if (lastoption == '\?')
78 {
79 //ambigous option
80 #if HAVE_STRING___H
81 delete[]opts;
82 #endif
83 return false;
84 }
85 for (int i = 0; i < optCount; ++i)
86 {
87 if (longopts[i].val == lastoption && !longopts[i].flag)
88 options[i]->Process (optarg);
89 }
90 }
91 }
92 #if HAVE_STRING___H
93 delete[]opts;
94 #endif
95 return true;
96 }
97
98 void
99 GetOption::Init ()
100 {
101 optCount = 0;
102 options = 0;
103 inited = 42;
104 }
105
106 //FIXME: check for conflicts.
107 void
108 GetOption::Register (Option * anOption)
109 {
110 Option **t = new Option *[optCount + 1];
111 for (int i = 0; i < optCount; ++i)
112 t[i] = options[i];
113 t[optCount++] = anOption;
114 delete[]options;
115 options = t;
116 }
This page took 0.041484 seconds and 5 git commands to generate.