Debian Codegraph @

rudeconfig/src/Writer.cpp

raw
1 // Writer.cpp
2 //
3 // Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005 Matthew Flood
4 // See file AUTHORS for contact information
5 //
6 // This file is part of RudeConfig.
7 //
8 // RudeConfig is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 2, or (at your option)
11 // any later version.
12 //
13 // RudeConfig is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with RudeConfig; (see COPYING) if not, write to the Free Software
20 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
21 // 02111-1307, USA.
22 //------------------------------------------------------------------------
23
24 #include "../config.h"
25
26 #include "Writer.h"
27
28 #ifndef INCLUDED_File_H
29 #include "File.h"
30 #endif
31
32 #ifndef INCLUDED_Section_H
33 #include "Section.h"
34 #endif
35
36 #ifndef INCLUDED_KeyValue_H
37 #include "KeyValue.h"
38 #endif
39
40 #ifndef INCLUDED_Comment_H
41 #include "Comment.h"
42 #endif
43
44 #ifndef INCLUDED_WhiteSpace_H
45 #include "WhiteSpace.h"
46 #endif
47
48 #ifndef INCLUDED_CSTDLIB
49 #include <cstdlib>
50 #define INCLUDED_CSTDLIB
51 #endif
52
53 #ifndef INCLUDED_CCTYPE
54 #include <cctype>
55 #define INCLUDED_CCTYPE
56 #endif
57
58 #ifndef INCLUDED_IOSTREAM
59 #include <iostream>
60 #define INCLUDED_IOSTREAM
61 #endif
62
63 #ifndef INCLUDED_STRING
64 #include <string>
65 #define INCLUDED_STRING
66 #endif
67
68 using namespace std;
69
70 namespace rude{
71 namespace config{
72
73
74 Writer::Writer()
75 {
76
77 }
78
79 Writer::~Writer()
80 {
81
82 }
83
84
85 void Writer::visitFile(const File& configfile) const
86 {
87
88 }
89
90 void Writer::visitSection(const Section& configsection) const
91 {
92 std::string name = configsection.getSectionName();
93
94 // if there is a section name, we print it out [between brackets]
95 //
96 if(name != "")
97 {
98 if( configsection.isDeleted() )
99 {
100 if ( d_commentchar && d_preservedeleted )
101 {
102 *d_outputstream << d_commentchar << " ";
103 }
104 else
105 {
106 return;
107 }
108 }
109
110 int position = 0;
111 size_t location;
112
113 // escape all backslashes
114 //
115 while( (location = name.find("\\", position)) != string::npos )
116 {
117 // location points right at the '\'
118 name.insert(location, "\\");
119 position = (int) location + 2;
120 }
121
122 // escape all ']''s
123 //
124 while( (location = name.find("]", position)) != string::npos )
125 {
126 // location points right at the ']'
127 name.insert(location, "\\");
128 position = (int) location + 2;
129 }
130
131 *d_outputstream << "[" << name << "]";
132
133 // If the section name has a comment, we print it out after the section name
134 //
135 if(configsection.getSectionComment()[0] != 0 && d_commentchar)
136 {
137 *d_outputstream << "\t" << d_commentchar << configsection.getSectionComment();
138 }
139
140 // newline ends the section header
141 //
142 *d_outputstream << "\n";
143 }
144 }
145
146 void Writer::visitComment(const Comment& comment) const
147 {
148 if(d_preservecomments && d_commentchar && ( !comment.isDeleted() || d_preservedeleted ))
149 {
150 *d_outputstream << d_commentchar << comment.getComment() << "\n";
151 }
152 }
153
154 void Writer::visitWhiteSpace(const WhiteSpace& whitespace) const
155 {
156 if(d_preservewhitespace && ( !whitespace.isDeleted() || d_preservedeleted ))
157 {
158 *d_outputstream << whitespace.getWhiteSpace();
159 }
160 }
161
162
163 void Writer::visitKeyValue(const KeyValue& dataline) const
164 {
165 if(dataline.isDeleted() && ( !d_commentchar || !d_preservedeleted))
166 {
167 // Don't preserve deleted data when comments are null
168 // or we don't want to preserve them
169 return;
170 }
171 else
172 {
173 string key = dataline.getName();
174 string value = dataline.getValue();
175 string comment = dataline.getComment();
176
177 string commentchar(1,d_commentchar);
178
179
180 if(dataline.isDeleted())
181 {
182 // print the comment character
183 //
184 *d_outputstream << d_commentchar << " ";
185 }
186
187 if(key != "")
188 {
189
190
191 int position = 0;
192 size_t location;
193
194 // escape all backslashes
195 //
196 while( (location = key.find("\\", position)) != string::npos )
197 {
198 // location points right at the '\'
199 key.insert(location, "\\");
200 position = (int) location + 2;
201 }
202
203
204 // escape comment characters
205 // unless the comment character is a backslash, which we've already escaped....
206 //
207 if(d_commentchar && d_commentchar != '\\')
208 {
209 position = 0;
210
211 while( (location = key.find(commentchar, position)) != string::npos )
212 {
213 // location points right at the '"'
214 key.insert(location, "\\");
215 position = (int) location + 2;
216 }
217 }
218
219 // escape all delimiters, unless the delimiter is a backslash or the same as the comment, god forbid.
220 //
221 //
222 if(d_delimiter != '\\' && d_delimiter != d_commentchar)
223 {
224 if(d_delimiter)
225 {
226 string delimiter(1, d_delimiter);
227 position = 0;
228
229 while( (location = key.find(delimiter, position)) != string::npos )
230 {
231 // location points right at the '"'
232 key.insert(location, "\\");
233 position = (int) location + 2;
234 }
235 }
236 else
237 {
238 position = 0;
239
240 while( (location = key.find("\t", position)) != string::npos )
241 {
242 // location points right at the '"'
243 key.insert(location, "\\");
244 position = (int) location + 2;
245 }
246
247 position = 0;
248
249 while( (location = key.find(" ", position)) != string::npos )
250 {
251 // location points right at the '"'
252 key.insert(location, "\\");
253 position = (int) location + 2;
254 }
255
256 }
257 }
258
259 // print the key
260 //
261 *d_outputstream << key;
262 }
263
264 if(value != "")
265 {
266 // print out the delimiter
267 //
268 *d_outputstream << " " << ( d_delimiter ? d_delimiter : '\t' ) << " ";
269
270 int position = 0;
271 size_t location;
272
273 // escape all backslashes
274 //
275 string backslash(1,'\\');
276
277 while( (location = value.find("\\", position)) != string::npos )
278 {
279 // location points right at the '\'
280 value.insert(location, "\\");
281 position = (int) location + 2;
282 }
283
284 // escape all quotes
285 //
286 string quote(1,'"');
287 position = 0;
288
289 while( (location = value.find("\"", position)) != string::npos )
290 {
291 // location points right at the '"'
292 value.insert(location, "\\");
293 position = (int) location + 2;
294 }
295
296 // escape comment characters
297 // unless the comment character is a backslash or a quote, which we've already escaped....
298 //
299 if(d_commentchar && d_commentchar != '\\' && d_commentchar != '"')
300 {
301 position = 0;
302
303 while( (location = value.find(commentchar, position)) != string::npos )
304 {
305 // location points right at the '"'
306 value.insert(location, "\\");
307 position = (int) location + 2;
308 }
309 }
310
311
312
313
314
315 // if value starts with whitespace, ends with whitespace or contains the comment character or CRLF's, quote the value,
316 //
317 int size = value.size();
318
319 if( isspace(value[0]) ||
320 isspace(value[size-1]) ||
321 (value.find("\r", 0) != string::npos) ||
322 (value.find("\f", 0) != string::npos) ||
323 (value.find("\n", 0)!= string::npos)
324 )
325 {
326
327
328 value.insert(0, "\"");
329 value += "\"";
330 }
331
332 // if data is deleted and there are CRLFS
333 // then each line must start with a comment character.
334 // The safest way is just to follow every newline character with a comment
335 //
336 if( dataline.isDeleted() && (
337 (value.find("\r", 0) != string::npos) ||
338 (value.find("\f", 0) != string::npos) ||
339 (value.find("\n", 0)!= string::npos) )
340 )
341 {
342 position = 0;
343
344 while( (location = value.find("\r", position)) != string::npos )
345 {
346 // location points right at the '"'
347 value.insert(location + 1, commentchar);
348 position = (int) location + 2;
349 }
350 position = 0;
351 while( (location = value.find("\f", position)) != string::npos )
352 {
353 // location points right at the '"'
354 value.insert(location + 1, commentchar);
355 position = (int) location + 2;
356 }
357
358 position = 0;
359 while( (location = value.find("\n", position)) != string::npos )
360 {
361 // location points right at the '"'
362 value.insert(location + 1, commentchar);
363 position = (int) location + 2;
364 }
365 }
366
367 *d_outputstream << value;
368
369 }
370
371 if( comment != "" && d_commentchar && d_preservecomments)
372 {
373 // add value's comment at end if it exists
374 // and there is a comment character
375 //
376 *d_outputstream << "\t " << d_commentchar << comment;
377 }
378
379 // end the entry with a newline
380 //
381 *d_outputstream << "\n";
382 }
383 }
384
385
386 }} // end namespaces
387