1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 #include <precomp.h>
23 #include "defdescr.hxx"
24 
25 
26 // NOT FULLY DEFINED SERVICES
27 #include <prprpr.hxx>
28 
29 
30 
31 
32 namespace cpp
33 {
34 
35 
36 
37 
DefineDescription(const String & i_sName,const str_vector & i_rDefinition)38 DefineDescription::DefineDescription( const String  &       i_sName,
39                                       const str_vector &    i_rDefinition )
40     :   sName(i_sName),
41         // aParams,
42         aDefinition(i_rDefinition),
43         eDefineType(type_define)
44 {
45 }
46 
DefineDescription(const String & i_sName,const str_vector & i_rParams,const str_vector & i_rDefinition)47 DefineDescription::DefineDescription( const String  &       i_sName,
48                                       const str_vector &    i_rParams,
49                                       const str_vector &    i_rDefinition )
50     :   sName(i_sName),
51         aParams(i_rParams),
52         aDefinition(i_rDefinition),
53         eDefineType(type_macro)
54 {
55 }
56 
~DefineDescription()57 DefineDescription::~DefineDescription()
58 {
59 }
60 
61 void
GetDefineText(csv::StreamStr & o_rText) const62 DefineDescription::GetDefineText( csv::StreamStr & o_rText ) const
63 {
64     if ( aDefinition.begin() == aDefinition.end() OR eDefineType != type_define )
65         return;
66 
67 
68     bool bSwitch_Stringify = false;
69     bool bSwitch_Concatenate = false;
70 
71     for ( str_vector::const_iterator it = aDefinition.begin();
72           it != aDefinition.end();
73           ++it )
74     {
75         if ( HandleOperatorsBeforeTextItem( o_rText,
76                                             bSwitch_Stringify,
77                                             bSwitch_Concatenate,
78                                             *it ) )
79         {
80             continue;
81         }
82 
83         o_rText << (*it);
84 
85         Do_bStringify_end(o_rText, bSwitch_Stringify);
86         o_rText << " ";
87     }
88     o_rText.seekp(-1, csv::cur);
89 }
90 
91 void
GetMacroText(csv::StreamStr & o_rText,const StringVector & i_rGivenArguments) const92 DefineDescription::GetMacroText( csv::StreamStr &               o_rText,
93                                  const StringVector & i_rGivenArguments ) const
94 {
95     bool bSwitch_Stringify = false;
96     bool bSwitch_Concatenate = false;
97     intt nActiveParamNr = -1;
98 
99     if ( aDefinition.begin() == aDefinition.end() OR eDefineType != type_macro )
100         return;
101 
102     for ( str_vector::const_iterator it = aDefinition.begin();
103           it != aDefinition.end();
104           ++it )
105     {
106         if ( HandleOperatorsBeforeTextItem( o_rText,
107                                             bSwitch_Stringify,
108                                             bSwitch_Concatenate,
109                                             *it ) )
110         {
111             continue;
112         }
113 
114         for ( str_vector::const_iterator param_it = aParams.begin();
115               param_it != aParams.end() AND nActiveParamNr == -1;
116               ++param_it )
117         {
118          	if ( strcmp(*it, *param_it) == 0 )
119                 nActiveParamNr = param_it - aParams.begin();
120         }
121         if ( nActiveParamNr == -1 )
122         {
123             o_rText << (*it);
124         }
125         else
126         {
127             o_rText << i_rGivenArguments[nActiveParamNr];
128             nActiveParamNr = -1;
129         }
130 
131         Do_bStringify_end(o_rText, bSwitch_Stringify);
132         o_rText << " ";
133     }
134     o_rText.seekp(-1, csv::cur);
135 }
136 
137 
138 
139 }   // end namespace cpp
140 
141 
142 
143 
144 
145 bool
CheckForOperator(bool & o_bStringify,bool & o_bConcatenate,const String & i_sTextItem)146 CheckForOperator( bool &              o_bStringify,
147                   bool &              o_bConcatenate,
148                   const String &      i_sTextItem )
149 {
150     if ( strcmp(i_sTextItem, "##") == 0 )
151     {
152         o_bConcatenate = true;
153         return true;
154     }
155     else if ( strcmp(i_sTextItem, "#") == 0 )
156     {
157         o_bStringify = true;
158         return true;
159     }
160     return false;
161 }
162 
163 void
Do_bConcatenate(csv::StreamStr & o_rText,bool & io_bConcatenate)164 Do_bConcatenate( csv::StreamStr &    o_rText,
165                  bool &              io_bConcatenate )
166 {
167     if ( io_bConcatenate )
168     {
169         uintt nPos;
170         for ( nPos = o_rText.tellp() - 1;
171               nPos > 0 ? o_rText.c_str()[nPos] == ' ' : false;
172               --nPos ) ;
173         o_rText.seekp(nPos+1);
174         io_bConcatenate = false;
175     }
176 }
177 
178 void
Do_bStringify_begin(csv::StreamStr & o_rText,bool i_bStringify)179 Do_bStringify_begin( csv::StreamStr & o_rText,
180                      bool             i_bStringify )
181 {
182     if ( i_bStringify )
183     {
184         o_rText << "\"";
185     }
186 }
187 
188 void
Do_bStringify_end(csv::StreamStr & o_rText,bool & io_bStringify)189 Do_bStringify_end( csv::StreamStr & o_rText,
190                    bool &           io_bStringify )
191 {
192     if ( io_bStringify )
193     {
194         o_rText << "\"";
195         io_bStringify = false;
196     }
197 }
198 
199 
200 bool
HandleOperatorsBeforeTextItem(csv::StreamStr & o_rText,bool & io_bStringify,bool & io_bConcatenate,const String & i_sTextItem)201 HandleOperatorsBeforeTextItem( csv::StreamStr &    o_rText,
202                                bool &              io_bStringify,
203                                bool &              io_bConcatenate,
204                                const String  &     i_sTextItem )
205 {
206     if ( CheckForOperator( io_bStringify,
207                            io_bConcatenate,
208                            i_sTextItem) )
209     {
210         return true;
211     }
212     Do_bConcatenate(o_rText, io_bConcatenate);
213     Do_bStringify_begin(o_rText, io_bStringify);
214 
215 	return false;
216 }
217 
218 
219 
220