[RTL]
[reactos.git] / reactos / tools / rbuild_helper / argv_parser.h
1 /*
2 Copyright (c) 2009 KJK::Hyperion
3
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:
10
11 The above copyright notice and this permission notice shall be included in
12 all copies or substantial portions of the Software.
13
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.
21 */
22
23 #ifndef KJK_ARGV_PARSER_H_
24 #define KJK_ARGV_PARSER_H_
25
26 #include <iterator>
27 #include <null_output_iterator.h>
28
29 namespace kjk
30 {
31 namespace details
32 {
33 template<class Traits>
34 bool is_separator(typename Traits::int_type c)
35 {
36 return Traits::eq_int_type(c, ' ') || Traits::eq_int_type(c, '\t');
37 }
38 }
39
40 template<class Elem, class Traits, class InIter, class OutIter>
41 InIter copy_argument(InIter cur, InIter end, OutIter arg)
42 {
43 typename Traits::int_type c;
44 bool quoting = false;
45
46 while(cur != end)
47 {
48 c = Traits::to_int_type(*cur);
49
50 if(!details::is_separator<Traits>(c))
51 break;
52
53 ++ cur;
54 }
55
56 while(cur != end)
57 {
58 typedef typename std::iterator_traits<InIter>::difference_type difference_type;
59 difference_type backslashes(0);
60
61 do
62 {
63 c = Traits::to_int_type(*cur);
64 ++ cur;
65
66 if(Traits::eq_int_type(c, '\\'))
67 ++ backslashes;
68 else
69 break;
70 }
71 while(cur != end);
72
73 if(Traits::eq_int_type(c, '"'))
74 {
75 // c == '"'
76
77 if((backslashes % 2) == 0)
78 {
79 // 2N backslashes + "" in quote = N backslashes, literal "
80 if(quoting && cur != end && Traits::eq_int_type(Traits::to_int_type(*cur), '"'))
81 {
82 c = '"';
83 ++ cur;
84 }
85 // 2N backslashes + " = N backslashes, toggle quoting
86 else
87 {
88 quoting = !quoting;
89 c = Traits::eof();
90 }
91
92 }
93 // 2N+1 backslashes + " = N backslashes, literal "
94
95 backslashes /= 2;
96 }
97
98 // Flush backslashes
99 for(difference_type i = 0; i < backslashes; ++ i)
100 *arg ++ = Traits::to_char_type('\\');
101
102 // Handle current character, unless it was a special quote
103 if(c != Traits::eof())
104 {
105 if(details::is_separator<Traits>(c) && !quoting)
106 break;
107 else
108 *arg ++ = Traits::to_char_type(c);
109 }
110 }
111
112 while(cur != end)
113 {
114 c = Traits::to_int_type(*cur);
115
116 if(!details::is_separator<Traits>(c))
117 break;
118
119 ++ cur;
120 }
121
122 return cur;
123 }
124
125 template<class InIter, class OutIter>
126 InIter copy_argument(InIter cur, InIter end, OutIter arg)
127 {
128 return copy_argument
129 <
130 typename std::iterator_traits<InIter>::value_type,
131 typename std::char_traits<typename std::iterator_traits<InIter>::value_type>,
132 InIter,
133 OutIter
134 >
135 (cur, end, arg);
136 }
137
138 template<class Elem, class Traits, class InIter>
139 InIter skip_argument(InIter cur, InIter end)
140 {
141 return copy_argument(cur, end, null_output_iterator<Elem>());
142 }
143
144 template<class InIter>
145 InIter skip_argument(InIter cur, InIter end)
146 {
147 return copy_argument
148 <
149 typename std::iterator_traits<InIter>::value_type,
150 typename std::char_traits<typename std::iterator_traits<InIter>::value_type>,
151 InIter
152 >
153 (cur, end, null_output_iterator<typename std::iterator_traits<InIter>::value_type>());
154 }
155 }
156
157 #endif
158
159 // EOF