File: ArgumentList.cc

package info (click to toggle)
exactimage 1.2.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,040 kB
  • sloc: cpp: 35,940; ansic: 1,952; xml: 1,447; makefile: 338; perl: 138; sh: 110; python: 45; php: 37; ruby: 12
file content (215 lines) | stat: -rw-r--r-- 5,497 bytes parent folder | download | duplicates (10)
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
/*
 * --- GSMP-COPYRIGHT-NOTE-BEGIN ---
 * 
 * This copyright note is auto-generated by ./scripts/Create-CopyPatch.
 * Please add additional copyright information _after_ the line containing
 * the GSMP-COPYRIGHT-NOTE-END tag. Otherwise it might get removed by
 * the ./scripts/Create-CopyPatch script. Do not edit this copyright text!
 * 
 * GSMP: utility/src/ArgumentList.cc
 * General Sound Manipulation Program is Copyright (C) 2000 - 2004
 *   Valentin Ziegler and René ’ebe
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2. A copy of the GNU General
 * Public License can be found in the file LICENSE.
 * 
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANT-
 * ABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
 * Public License for more details.
 * 
 * --- GSMP-COPYRIGHT-NOTE-END ---
 */

#include <sstream>

#include "ArgumentList.hh"
#include "template/ArgumentList.tcc"

using namespace Utility;

BasicArgument::BasicArgument (const std::string& i_sname, const std::string& i_lname,
			      const std::string& i_desc,  int i_min_count, int i_max_count,
			      bool i_fragmented, bool i_reset)
  : list(true), count (0), pass_count (0)
{
  sname = i_sname;
  lname = i_lname;
  desc = i_desc;

  min_count  = i_min_count;
  max_count = i_max_count;
  fragmented = i_fragmented;
  reset = i_reset;
  
  // sanity check
  if (min_count > max_count)
    max_count = min_count;
  
  no_arg = max_count == 0;
}

BasicArgument::~BasicArgument ()
{
}

bool BasicArgument::Probe () {
      return count < max_count;
}

bool BasicArgument::Interrupt () {
  if (!no_arg && pass_count == 0) {
    std::cerr << "Error: No parameter for argument " << lname << " specified!"
	      << std::endl;
    return false;
  }
  else if (!fragmented && count < min_count) {
    std::cerr << "Error: No fragmentation for argument " << lname << " allowed!"
	      << std::endl
	      << "       At least " << min_count << " parameter required!" << std::endl;
    pass_count = 0;
    return false;
  }
  pass_count = 0;
  return InterruptImpl();
}
  
bool BasicArgument::Finalize () {
  if (count < min_count) {
    std::cerr << "Error: Too few parameter for argument " << lname
	      << ", at least " << min_count << " required!" << std::endl;
    return false;
  }
  return true;
}

ArgumentList::ArgumentList (bool i_residual)
{
  residual = i_residual;
}
  
ArgumentList::~ArgumentList() {
  
}

void ArgumentList::Add (BasicArgument* arg)
{
  short_content [arg->sname] = arg;
  long_content [arg->lname] = arg;
}
  
bool ArgumentList::Read (int argc, char** argv)
{
  char** argv_end = argv + argc;
  int errors = 0;
  
  // skip the program name ...
  ++argv;

  BasicArgument* argument = 0;
  bool residual_gathering = false;
  
  // parse all arguments
  for (; argv != argv_end; ++argv) {
    std::string arg (*argv);
    std::string targ = arg; // temp. mangled argument

    BasicArgument* new_argument = 0;
    
    // match options, does it start with a '-' ?
    if (arg.size () >= 1 && arg[0] == '-') {
      // long?
      if (arg.size () >= 2 && arg[0] == '-' && arg[1] == '-') {
	targ.erase (0, 2);
	iterator it = long_content.find (targ);
	if (it != long_content.end () ) {
	  new_argument = it->second;
	}
      }
      else { // match short
	targ.erase (0, 1);
	iterator it = short_content.find (targ);
	if (it != short_content.end () )
	  new_argument = it->second;
      }
    }
    
    if (new_argument) {
      if (argument && !argument->Interrupt () )
	++errors;
      argument = new_argument;
      argument->Start ();
    }
    // try to parse via the last matched argument
    // start residual gathering if not parseable
    else if (argument) {
      if (residual && !argument->Probe()) {
	argument = 0;
	residual_gathering = true;
      }
      else {
	if (!argument->Read (arg) )
	  ++errors;
      }
    }
    else if (residual) {
      argument = 0;
      residual_gathering = true;
    } 
    
    // immediately throw into argument if it does not need parameters
    // (most often bools)
    if (argument && argument->no_arg) {
      if (!argument->Read () ) {
	argument = 0;
	++errors;
      }
    }
    
    // warning if in residual_gathering mode
    if (residual_gathering) {
      residuals.push_back (arg);
      if (argument)
	std::cerr << "Warning: Residual parameter " << arg
		  << " matches an argument" <<  std::endl;
    }
    else
      if (!argument) {
	std::cerr << "Error: Unrecognized argument: " << arg << std::endl;
	++errors;
      }    
  }
  // interrupt the last parsed argument
  if (argument && !argument->Interrupt () )
    ++errors;
  
  // final checks
  for (iterator it = long_content.begin (); it != long_content.end (); ++it) {
    if (!it->second->Finalize())
      ++errors;
  }
  
  return errors == 0;
}

const std::vector<std::string>& ArgumentList::Residuals () const
{
  return residuals;
}

void ArgumentList::Usage (std::ostream& os) const
{
  for (const_iterator it = long_content.begin (); it != long_content.end (); ++it)
    if (it->second->list) {
      if (! it->second->sname.empty() )
	os << "  -" << it->second->sname << ", ";
      else
	os << "      ";
      
      os << "--" << it->second->lname
	 << std::endl << "\t\t"
	 << it->second->desc << std::endl;
    }
}