2 Copyright (c) 2009 KJK::Hyperion
4 Permission is hereby granted, free of charge, to any person obtaining a
5 copy of this software and associated documentation files (the "Software"),
6 to deal in the Software without restriction, including without limitation
7 the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 and/or sell copies of the Software, and to permit persons to whom the
9 Software is furnished to do so, subject to the following conditions:
11 The above copyright notice and this permission notice shall be included in
12 all copies or substantial portions of the Software.
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 DEALINGS IN THE SOFTWARE.
23 #ifndef KJK_ARGV_PARSER_H_
24 #define KJK_ARGV_PARSER_H_
27 #include <null_output_iterator.h>
33 template<class Traits
>
34 bool is_separator(typename
Traits::int_type c
)
36 return Traits::eq_int_type(c
, ' ') || Traits::eq_int_type(c
, '\t');
40 template<class Elem
, class Traits
, class InIter
, class OutIter
>
41 InIter
copy_argument(InIter cur
, InIter end
, OutIter arg
)
43 typename
Traits::int_type c
;
48 c
= Traits::to_int_type(*cur
);
50 if(!details::is_separator
<Traits
>(c
))
58 typedef typename
std::iterator_traits
<InIter
>::difference_type difference_type
;
59 difference_type
backslashes(0);
63 c
= Traits::to_int_type(*cur
);
66 if(Traits::eq_int_type(c
, '\\'))
73 if(Traits::eq_int_type(c
, '"'))
77 if((backslashes
% 2) == 0)
79 // 2N backslashes + "" in quote = N backslashes, literal "
80 if(quoting
&& cur
!= end
&& Traits::eq_int_type(Traits::to_int_type(*cur
), '"'))
85 // 2N backslashes + " = N backslashes, toggle quoting
93 // 2N+1 backslashes + " = N backslashes, literal "
99 for(difference_type i
= 0; i
< backslashes
; ++ i
)
100 *arg
++ = Traits::to_char_type('\\');
102 // Handle current character, unless it was a special quote
103 if(c
!= Traits::eof())
105 if(details::is_separator
<Traits
>(c
) && !quoting
)
108 *arg
++ = Traits::to_char_type(c
);
114 c
= Traits::to_int_type(*cur
);
116 if(!details::is_separator
<Traits
>(c
))
125 template<class InIter
, class OutIter
>
126 InIter
copy_argument(InIter cur
, InIter end
, OutIter arg
)
130 typename
std::iterator_traits
<InIter
>::value_type
,
131 typename
std::char_traits
<typename
std::iterator_traits
<InIter
>::value_type
>,
138 template<class Elem
, class Traits
, class InIter
>
139 InIter
skip_argument(InIter cur
, InIter end
)
141 return copy_argument(cur
, end
, null_output_iterator
<Elem
>());
144 template<class InIter
>
145 InIter
skip_argument(InIter cur
, InIter end
)
149 typename
std::iterator_traits
<InIter
>::value_type
,
150 typename
std::char_traits
<typename
std::iterator_traits
<InIter
>::value_type
>,
153 (cur
, end
, null_output_iterator
<typename
std::iterator_traits
<InIter
>::value_type
>());