1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 #include <precomp.h>
29 #include "defdescr.hxx"
30 
31 
32 // NOT FULLY DEFINED SERVICES
33 #include <prprpr.hxx>
34 
35 
36 
37 
38 namespace cpp
39 {
40 
41 
42 
43 
44 DefineDescription::DefineDescription( const String  &       i_sName,
45                                       const str_vector &    i_rDefinition )
46     :   sName(i_sName),
47         // aParams,
48         aDefinition(i_rDefinition),
49         eDefineType(type_define)
50 {
51 }
52 
53 DefineDescription::DefineDescription( const String  &       i_sName,
54                                       const str_vector &    i_rParams,
55                                       const str_vector &    i_rDefinition )
56     :   sName(i_sName),
57         aParams(i_rParams),
58         aDefinition(i_rDefinition),
59         eDefineType(type_macro)
60 {
61 }
62 
63 DefineDescription::~DefineDescription()
64 {
65 }
66 
67 void
68 DefineDescription::GetDefineText( csv::StreamStr & o_rText ) const
69 {
70     if ( aDefinition.begin() == aDefinition.end() OR eDefineType != type_define )
71         return;
72 
73 
74     bool bSwitch_Stringify = false;
75     bool bSwitch_Concatenate = false;
76 
77     for ( str_vector::const_iterator it = aDefinition.begin();
78           it != aDefinition.end();
79           ++it )
80     {
81         if ( HandleOperatorsBeforeTextItem( o_rText,
82                                             bSwitch_Stringify,
83                                             bSwitch_Concatenate,
84                                             *it ) )
85         {
86             continue;
87         }
88 
89         o_rText << (*it);
90 
91         Do_bStringify_end(o_rText, bSwitch_Stringify);
92         o_rText << " ";
93     }
94     o_rText.seekp(-1, csv::cur);
95 }
96 
97 void
98 DefineDescription::GetMacroText( csv::StreamStr &               o_rText,
99                                  const StringVector & i_rGivenArguments ) const
100 {
101     bool bSwitch_Stringify = false;
102     bool bSwitch_Concatenate = false;
103     intt nActiveParamNr = -1;
104 
105     if ( aDefinition.begin() == aDefinition.end() OR eDefineType != type_macro )
106         return;
107 
108     for ( str_vector::const_iterator it = aDefinition.begin();
109           it != aDefinition.end();
110           ++it )
111     {
112         if ( HandleOperatorsBeforeTextItem( o_rText,
113                                             bSwitch_Stringify,
114                                             bSwitch_Concatenate,
115                                             *it ) )
116         {
117             continue;
118         }
119 
120         for ( str_vector::const_iterator param_it = aParams.begin();
121               param_it != aParams.end() AND nActiveParamNr == -1;
122               ++param_it )
123         {
124          	if ( strcmp(*it, *param_it) == 0 )
125                 nActiveParamNr = param_it - aParams.begin();
126         }
127         if ( nActiveParamNr == -1 )
128         {
129             o_rText << (*it);
130         }
131         else
132         {
133             o_rText << i_rGivenArguments[nActiveParamNr];
134             nActiveParamNr = -1;
135         }
136 
137         Do_bStringify_end(o_rText, bSwitch_Stringify);
138         o_rText << " ";
139     }
140     o_rText.seekp(-1, csv::cur);
141 }
142 
143 
144 
145 }   // end namespace cpp
146 
147 
148 
149 
150 
151 bool
152 CheckForOperator( bool &              o_bStringify,
153                   bool &              o_bConcatenate,
154                   const String &      i_sTextItem )
155 {
156     if ( strcmp(i_sTextItem, "##") == 0 )
157     {
158         o_bConcatenate = true;
159         return true;
160     }
161     else if ( strcmp(i_sTextItem, "#") == 0 )
162     {
163         o_bStringify = true;
164         return true;
165     }
166     return false;
167 }
168 
169 void
170 Do_bConcatenate( csv::StreamStr &    o_rText,
171                  bool &              io_bConcatenate )
172 {
173     if ( io_bConcatenate )
174     {
175         uintt nPos;
176         for ( nPos = o_rText.tellp() - 1;
177               nPos > 0 ? o_rText.c_str()[nPos] == ' ' : false;
178               --nPos ) ;
179         o_rText.seekp(nPos+1);
180         io_bConcatenate = false;
181     }
182 }
183 
184 void
185 Do_bStringify_begin( csv::StreamStr & o_rText,
186                      bool             i_bStringify )
187 {
188     if ( i_bStringify )
189     {
190         o_rText << "\"";
191     }
192 }
193 
194 void
195 Do_bStringify_end( csv::StreamStr & o_rText,
196                    bool &           io_bStringify )
197 {
198     if ( io_bStringify )
199     {
200         o_rText << "\"";
201         io_bStringify = false;
202     }
203 }
204 
205 
206 bool
207 HandleOperatorsBeforeTextItem( csv::StreamStr &    o_rText,
208                                bool &              io_bStringify,
209                                bool &              io_bConcatenate,
210                                const String  &     i_sTextItem )
211 {
212     if ( CheckForOperator( io_bStringify,
213                            io_bConcatenate,
214                            i_sTextItem) )
215     {
216         return true;
217     }
218     Do_bConcatenate(o_rText, io_bConcatenate);
219     Do_bStringify_begin(o_rText, io_bStringify);
220 
221 	return false;
222 }
223 
224 
225 
226