00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00048 #ifndef STLSOFT_INCL_STLSOFT_SYSTEM_HPP_COMMANDLINE_PARSER
00049 #define STLSOFT_INCL_STLSOFT_SYSTEM_HPP_COMMANDLINE_PARSER
00050
00051 #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
00052 # define STLSOFT_VER_STLSOFT_SYSTEM_HPP_COMMANDLINE_PARSER_MAJOR 2
00053 # define STLSOFT_VER_STLSOFT_SYSTEM_HPP_COMMANDLINE_PARSER_MINOR 1
00054 # define STLSOFT_VER_STLSOFT_SYSTEM_HPP_COMMANDLINE_PARSER_REVISION 3
00055 # define STLSOFT_VER_STLSOFT_SYSTEM_HPP_COMMANDLINE_PARSER_EDIT 37
00056 #endif
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072 #ifndef STLSOFT_INCL_STLSOFT_H_STLSOFT
00073 # include <stlsoft/stlsoft.h>
00074 #endif
00075
00076 #if defined(STLSOFT_COMPILER_IS_MSVC) && \
00077 _MSC_VER < 1200
00078 # error stlsoft/system/commandline_parser.hpp is not compatible with Visual C++ 5.0 or earlier
00079 #endif
00080
00081 #ifndef STLSOFT_INCL_STLSOFT_MEMORY_HPP_ALLOCATOR_FEATURES
00082 # include <stlsoft/memory/allocator_features.hpp>
00083 #endif
00084 #ifndef STLSOFT_INCL_STLSOFT_MEMORY_HPP_ALLOCATOR_SELECTOR
00085 # include <stlsoft/memory/allocator_selector.hpp>
00086 #endif
00087 #ifndef STLSOFT_INCL_STLSOFT_MEMORY_HPP_AUTO_BUFFER
00088 # include <stlsoft/memory/auto_buffer.hpp>
00089 #endif
00090 #ifndef STLSOFT_INCL_STLSOFT_STRING_HPP_CHAR_TRAITS
00091 # include <stlsoft/string/char_traits.hpp>
00092 #endif
00093 #ifndef STLSOFT_INCL_STLSOFT_SHIMS_ACCESS_HPP_STRING
00094 # include <stlsoft/shims/access/string.hpp>
00095 #endif
00096
00097 #ifndef STLSOFT_INCL_H_CTYPE
00098 # define STLSOFT_INCL_H_CTYPE
00099 # include <ctype.h>
00100 #endif
00101
00102
00103
00104
00105
00106 #ifndef _STLSOFT_NO_NAMESPACE
00107 namespace stlsoft
00108 {
00109 #endif
00110
00111
00112
00113
00114
00132 template< ss_typename_param_k C
00133 , ss_typename_param_k T = stlsoft_ns_qual(stlsoft_char_traits)<C>
00134 , ss_typename_param_k A = ss_typename_type_def_k allocator_selector<C>::allocator_type
00135 >
00136 class basic_commandline_parser
00137 {
00140 public:
00142 typedef C char_type;
00144 typedef T traits_type;
00146 typedef A allocator_type;
00148 typedef basic_commandline_parser<C, T, A> class_type;
00149 private:
00150 typedef char_type* pointer_type;
00151 #ifdef STLSOFT_LF_ALLOCATOR_REBIND_SUPPORT
00152 typedef ss_typename_type_k A::ss_template_qual_k rebind<pointer_type>::other pointers_allocator_type;
00153 #else
00154 typedef ss_typename_type_k allocator_selector<pointer_type>::allocator_type pointers_allocator_type;
00155 #endif
00156 typedef stlsoft_ns_qual(auto_buffer)< char_type
00157 , 256
00158 , allocator_type
00159 > buffer_type;
00160 typedef stlsoft_ns_qual(auto_buffer)< pointer_type
00161 , 20
00162 , pointers_allocator_type
00163 > pointers_type;
00164 typedef ss_typename_type_k buffer_type::iterator iterator;
00165 public:
00167 typedef ss_typename_type_k pointers_type::value_type value_type;
00169 typedef ss_typename_type_k pointers_type::const_iterator const_iterator;
00171 typedef ss_size_t size_type;
00173
00176 public:
00180 ss_explicit_k basic_commandline_parser(char_type const* cmdLine)
00181 : m_buffer(1 + stlsoft_ns_qual(c_str_len)(cmdLine))
00182 , m_pointers(0)
00183 {
00184 STLSOFT_ASSERT(NULL != cmdLine);
00185
00186 traits_type::copy(&m_buffer[0], cmdLine, m_buffer.size());
00187
00188
00189
00190
00191
00192
00193 enum state_t
00194 {
00195 argument
00196 , quotedArgumentStart
00197 , quotedArgument
00198 , space
00199 };
00200
00201 state_t state = space;
00202 iterator b = m_buffer.begin();
00203 const iterator e = m_buffer.end() - 1;
00204
00205 for(; b != e; ++b)
00206 {
00207 const char_type ch = *b;
00208
00209 STLSOFT_ASSERT('\0' != ch);
00210
00211 if('"' == ch)
00212 {
00213 if(quotedArgumentStart == state)
00214 {
00215 state = space;
00216 }
00217 else if(quotedArgument == state)
00218 {
00219 *b = '\0';
00220 state = space;
00221 }
00222 else if(space == state)
00223 {
00224 state = quotedArgumentStart;
00225 }
00226 else
00227 {
00228 }
00229 }
00230 else if(isspace(ch))
00231 {
00232 if(quotedArgumentStart == state)
00233 {
00234 state = quotedArgument;
00235
00236 add_pointer(&*b);
00237 }
00238 else if(quotedArgument == state)
00239 {
00240 }
00241 else if(space == state)
00242 {
00243 }
00244 else
00245 {
00246 STLSOFT_ASSERT(argument == state);
00247
00248 *b = '\0';
00249
00250 state = space;
00251 }
00252 }
00253 else
00254 {
00255 if(quotedArgumentStart == state)
00256 {
00257 state = quotedArgument;
00258
00259 add_pointer(&*b);
00260 }
00261 else if(space == state)
00262 {
00263 state = argument;
00264
00265 add_pointer(&*b);
00266 }
00267 }
00268 }
00269 }
00271
00274 public:
00276 size_type size() const
00277 {
00278 return m_pointers.size();
00279 }
00280
00289 value_type const& operator [](size_type index) const
00290 {
00291 STLSOFT_ASSERT(index < size());
00292
00293 return m_pointers[index];
00294 }
00296
00299 public:
00301 const_iterator begin() const
00302 {
00303 return m_pointers.begin();
00304 }
00306 const_iterator end() const
00307 {
00308 return m_pointers.end();
00309 }
00311
00314 private:
00315 ss_bool_t add_pointer(pointer_type p)
00316 {
00317 if(!m_pointers.resize(1 + m_pointers.size()))
00318 {
00319 return false;
00320 }
00321
00322 m_pointers[m_pointers.size() - 1] = p;
00323
00324 return true;
00325 }
00327
00330 private:
00331 buffer_type m_buffer;
00332 pointers_type m_pointers;
00334 };
00335
00336
00337
00338
00339
00344 typedef basic_commandline_parser<ss_char_a_t> commandline_parser_a;
00349 typedef basic_commandline_parser<ss_char_w_t> commandline_parser_w;
00350
00352
00353
00354 #ifdef STLSOFT_UNITTEST
00355 # include "./unittest/commandline_parser_unittest_.h"
00356 #endif
00357
00358
00359
00360 #ifndef _STLSOFT_NO_NAMESPACE
00361 }
00362 #endif
00363
00364
00365
00366 #endif
00367
00368