~ubuntu-branches/ubuntu/gutsy/poco/gutsy

« back to all changes in this revision

Viewing changes to Util/src/Option.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Krzysztof Burghardt
  • Date: 2007-04-27 18:33:48 UTC
  • Revision ID: james.westby@ubuntu.com-20070427183348-xgnpct0qd6a2ip34
Tags: upstream-1.2.9
ImportĀ upstreamĀ versionĀ 1.2.9

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
// Option.cpp
 
3
//
 
4
// $Id: //poco/1.2/Util/src/Option.cpp#3 $
 
5
//
 
6
// Library: Util
 
7
// Package: Options
 
8
// Module:  Option
 
9
//
 
10
// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
 
11
// and Contributors.
 
12
//
 
13
// Permission is hereby granted, free of charge, to any person or organization
 
14
// obtaining a copy of the software and accompanying documentation covered by
 
15
// this license (the "Software") to use, reproduce, display, distribute,
 
16
// execute, and transmit the Software, and to prepare derivative works of the
 
17
// Software, and to permit third-parties to whom the Software is furnished to
 
18
// do so, all subject to the following:
 
19
// 
 
20
// The copyright notices in the Software and this entire statement, including
 
21
// the above license grant, this restriction and the following disclaimer,
 
22
// must be included in all copies of the Software, in whole or in part, and
 
23
// all derivative works of the Software, unless such copies or derivative
 
24
// works are solely in the form of machine-executable object code generated by
 
25
// a source language processor.
 
26
// 
 
27
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 
28
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 
29
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
 
30
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
 
31
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
 
32
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 
33
// DEALINGS IN THE SOFTWARE.
 
34
//
 
35
 
 
36
 
 
37
#include "Poco/Util/Option.h"
 
38
#include "Poco/Util/OptionException.h"
 
39
#include "Poco/Util/Validator.h"
 
40
#include "Poco/Util/AbstractConfiguration.h"
 
41
#include "Poco/String.h"
 
42
#include <algorithm>
 
43
 
 
44
 
 
45
using Poco::icompare;
 
46
 
 
47
 
 
48
namespace Poco {
 
49
namespace Util {
 
50
 
 
51
 
 
52
Option::Option(): 
 
53
        _required(false), 
 
54
        _repeatable(false), 
 
55
        _argRequired(false),
 
56
        _pValidator(0),
 
57
        _pCallback(0),
 
58
        _pConfig(0)
 
59
{
 
60
}
 
61
 
 
62
 
 
63
Option::Option(const Option& option):
 
64
        _shortName(option._shortName),
 
65
        _fullName(option._fullName),
 
66
        _description(option._description),
 
67
        _required(option._required),
 
68
        _repeatable(option._repeatable),
 
69
        _argName(option._argName),
 
70
        _argRequired(option._argRequired),
 
71
        _group(option._group),
 
72
        _binding(option._binding),
 
73
        _pValidator(option._pValidator),
 
74
        _pCallback(option._pCallback),
 
75
        _pConfig(option._pConfig)
 
76
{
 
77
        if (_pValidator) _pValidator->duplicate();
 
78
        if (_pCallback) _pCallback = _pCallback->clone();
 
79
        if (_pConfig) _pConfig->duplicate();
 
80
}
 
81
 
 
82
 
 
83
Option::Option(const std::string& fullName, const std::string& shortName):
 
84
        _shortName(shortName),
 
85
        _fullName(fullName),
 
86
        _required(false),
 
87
        _repeatable(false),
 
88
        _argRequired(false),
 
89
        _pValidator(0),
 
90
        _pCallback(0),
 
91
        _pConfig(0)
 
92
{
 
93
}
 
94
 
 
95
 
 
96
Option::Option(const std::string& fullName, const std::string& shortName, const std::string& description, bool required):
 
97
        _shortName(shortName),
 
98
        _fullName(fullName),
 
99
        _description(description),
 
100
        _required(required),
 
101
        _repeatable(false),
 
102
        _argRequired(false),
 
103
        _pValidator(0),
 
104
        _pCallback(0),
 
105
        _pConfig(0)
 
106
{
 
107
}
 
108
 
 
109
 
 
110
Option::Option(const std::string& fullName, const std::string& shortName, const std::string& description, bool required, const std::string& argName, bool argOptional):
 
111
        _shortName(shortName),
 
112
        _fullName(fullName),
 
113
        _description(description),
 
114
        _required(required),
 
115
        _repeatable(false),
 
116
        _argName(argName),
 
117
        _argRequired(argOptional),
 
118
        _pValidator(0),
 
119
        _pCallback(0),
 
120
        _pConfig(0)
 
121
{
 
122
}
 
123
 
 
124
 
 
125
Option::~Option()
 
126
{
 
127
        if (_pValidator) _pValidator->release();
 
128
        if (_pConfig) _pConfig->release();
 
129
        delete _pCallback;
 
130
}
 
131
 
 
132
 
 
133
Option& Option::operator = (const Option& option)
 
134
{
 
135
        if (&option != this)
 
136
        {
 
137
                Option tmp(option);
 
138
                swap(tmp);
 
139
        }
 
140
        return *this;
 
141
}
 
142
 
 
143
 
 
144
void Option::swap(Option& option)
 
145
{
 
146
        std::swap(_shortName, option._shortName);
 
147
        std::swap(_fullName, option._fullName);
 
148
        std::swap(_description, option._description);
 
149
        std::swap(_required, option._required);
 
150
        std::swap(_repeatable, option._repeatable);
 
151
        std::swap(_argName, option._argName);
 
152
        std::swap(_argRequired, option._argRequired);
 
153
        std::swap(_group, option._group);
 
154
        std::swap(_binding, option._binding);
 
155
        std::swap(_pValidator, option._pValidator);
 
156
        std::swap(_pCallback, option._pCallback);
 
157
        std::swap(_pConfig, option._pConfig);
 
158
}
 
159
 
 
160
        
 
161
Option& Option::shortName(const std::string& name)
 
162
{
 
163
        _shortName = name;
 
164
        return *this;
 
165
}
 
166
 
 
167
 
 
168
Option& Option::fullName(const std::string& name)
 
169
{
 
170
        _fullName = name;
 
171
        return *this;
 
172
}
 
173
 
 
174
        
 
175
Option& Option::description(const std::string& text)
 
176
{
 
177
        _description = text;
 
178
        return *this;
 
179
}
 
180
 
 
181
        
 
182
Option& Option::required(bool flag)
 
183
{
 
184
        _required = flag;
 
185
        return *this;
 
186
}
 
187
 
 
188
 
 
189
Option& Option::repeatable(bool flag)
 
190
{
 
191
        _repeatable = flag;
 
192
        return *this;
 
193
}
 
194
 
 
195
        
 
196
Option& Option::argument(const std::string& name, bool required)
 
197
{
 
198
        _argName     = name;
 
199
        _argRequired = required;
 
200
        return *this;
 
201
}
 
202
 
 
203
        
 
204
Option& Option::noArgument()
 
205
{
 
206
        _argName.clear();
 
207
        _argRequired = false;
 
208
        return *this;
 
209
}
 
210
 
 
211
 
 
212
Option& Option::group(const std::string& group)
 
213
{
 
214
        _group = group;
 
215
        return *this;
 
216
}
 
217
 
 
218
 
 
219
Option& Option::binding(const std::string& propertyName)
 
220
{
 
221
        return binding(propertyName, 0);
 
222
}
 
223
 
 
224
 
 
225
Option& Option::binding(const std::string& propertyName, AbstractConfiguration* pConfig)
 
226
{
 
227
        _binding = propertyName;
 
228
        if (_pConfig) _pConfig->release();
 
229
        _pConfig = pConfig;
 
230
        if (_pConfig) _pConfig->duplicate();
 
231
        return *this;
 
232
}
 
233
 
 
234
 
 
235
Option& Option::callback(const AbstractOptionCallback& cb)
 
236
{
 
237
        _pCallback = cb.clone();
 
238
        return *this;
 
239
}
 
240
 
 
241
 
 
242
Option& Option::validator(Validator* pValidator)
 
243
{
 
244
        if (_pValidator) _pValidator->release();
 
245
        _pValidator = pValidator;
 
246
        if (_pValidator) _pValidator->duplicate();
 
247
        return *this;
 
248
}
 
249
 
 
250
 
 
251
bool Option::matchesShort(const std::string& option) const
 
252
{
 
253
        return option.length() > 0 
 
254
                && !_shortName.empty() && option.compare(0, _shortName.length(), _shortName) == 0;
 
255
}
 
256
 
 
257
 
 
258
bool Option::matchesFull(const std::string& option) const
 
259
{
 
260
        std::string::size_type pos = option.find_first_of(":=");
 
261
        std::string::size_type len = pos == std::string::npos ? option.length() : pos;
 
262
        return len == _fullName.length()
 
263
                && icompare(option, 0, len, _fullName, 0, len) == 0;
 
264
}
 
265
 
 
266
 
 
267
bool Option::matchesPartial(const std::string& option) const
 
268
{
 
269
        std::string::size_type pos = option.find_first_of(":=");
 
270
        std::string::size_type len = pos == std::string::npos ? option.length() : pos;
 
271
        return option.length() > 0 
 
272
                && icompare(option, 0, len, _fullName, 0, len) == 0;
 
273
}
 
274
 
 
275
 
 
276
void Option::process(const std::string& option, std::string& arg) const
 
277
{
 
278
        std::string::size_type pos = option.find_first_of(":=");
 
279
        std::string::size_type len = pos == std::string::npos ? option.length() : pos;
 
280
        if (icompare(option, 0, len, _fullName, 0, len) == 0)
 
281
        {
 
282
                if (takesArgument())
 
283
                {
 
284
                        if (argumentRequired() && pos == std::string::npos)
 
285
                                throw MissingArgumentException(_fullName + " requires " + argumentName());
 
286
                        if (pos != std::string::npos)
 
287
                                arg.assign(option, pos + 1, option.length() - pos - 1);
 
288
                        else
 
289
                                arg.clear();
 
290
                }
 
291
                else if (pos != std::string::npos)
 
292
                {
 
293
                        throw UnexpectedArgumentException(option);
 
294
                }
 
295
                else arg.clear();
 
296
        }
 
297
        else if (!_shortName.empty() && option.compare(0, _shortName.length(), _shortName) == 0)
 
298
        {
 
299
                if (takesArgument())
 
300
                {
 
301
                        if (argumentRequired() && option.length() == _shortName.length())
 
302
                                throw MissingArgumentException(_shortName + " requires " + argumentName());
 
303
                        arg.assign(option, _shortName.length(), option.length() - _shortName.length());
 
304
                }
 
305
                else if (option.length() != _shortName.length())
 
306
                {
 
307
                        throw UnexpectedArgumentException(option);
 
308
                }
 
309
                else arg.clear();
 
310
        }
 
311
        else throw UnknownOptionException(option);
 
312
}
 
313
 
 
314
 
 
315
} } // namespace Poco::Util