|
1 // Copyright (c) 2003-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
2 // All rights reserved. |
|
3 // This component and the accompanying materials are made available |
|
4 // under the terms of "Eclipse Public License v1.0" |
|
5 // which accompanies this distribution, and is available |
|
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
7 // |
|
8 // Initial Contributors: |
|
9 // Nokia Corporation - initial contribution. |
|
10 // |
|
11 // Contributors: |
|
12 // |
|
13 // Description: |
|
14 // |
|
15 |
|
16 /** |
|
17 @file |
|
18 */ |
|
19 |
|
20 #include "filedump.h" |
|
21 |
|
22 /*static*/ CFileDump* CFileDump::NewL(RFs& fsSession, TPtrC filename, TPtrC appname, TPtrC version, TInt bDebugOn, CConsoleBase* aConsole, TBool bWriteHeader) |
|
23 { |
|
24 CFileDump* fd = new (ELeave) CFileDump; |
|
25 CleanupStack::PushL(fd); |
|
26 fd->ConstructL(fsSession, filename, appname, version, bDebugOn, aConsole, bWriteHeader); |
|
27 CleanupStack::Pop(fd); |
|
28 return fd; |
|
29 } |
|
30 |
|
31 CFileDump::~CFileDump() |
|
32 { |
|
33 file.Close(); |
|
34 bInitialised = EFalse; |
|
35 } |
|
36 |
|
37 |
|
38 void CFileDump::ConstructL(RFs &fsSession, TPtrC filename, TPtrC appname, TPtrC version, TInt bDebugOn, CConsoleBase* aConsole, TBool bWriteHeader) |
|
39 /** |
|
40 Opens the output file |
|
41 |
|
42 @param fsSession A session with the file server |
|
43 @param filename |
|
44 @param appname |
|
45 @param version |
|
46 @param bDebugOn |
|
47 @param console |
|
48 @param bWriteHeader |
|
49 @return ETrue if successful or EFalse |
|
50 */ |
|
51 { |
|
52 if (oErr.Evaluate(file.Replace(fsSession, filename, EFileShareAny|EFileStream|EFileWrite))) |
|
53 { |
|
54 if (bWriteHeader) |
|
55 { |
|
56 buf.Format(_L("############################################################\r\n## AUTO-GENERATED CONFIGURATION FILE\r\n## %S\r\n## %S\r\n############################################################\r\n"), |
|
57 &appname, &version); |
|
58 WriteBufferToFile(buf, file); |
|
59 } |
|
60 |
|
61 bInitialised = ETrue; |
|
62 } |
|
63 bDebug = bDebugOn; |
|
64 pConsole = aConsole; |
|
65 } |
|
66 |
|
67 |
|
68 void CFileDump::WriteTableHeader(TPtrC table) |
|
69 /** |
|
70 Writes a table header to the internal buffer |
|
71 |
|
72 @param table A reference to a descriptor containing the name of a table |
|
73 */ |
|
74 { |
|
75 if (bInitialised && table.Length()) |
|
76 { |
|
77 buf.Format(_L("\r\n############################################################\r\n## %S\r\n## \r\n[%S]\r\n"), |
|
78 &table, &table); |
|
79 WriteBufferToFile(buf, file); |
|
80 } |
|
81 } |
|
82 |
|
83 |
|
84 void CFileDump::WriteSectionHeader(TInt aCommDbId) |
|
85 /** |
|
86 Writes a section header to the internal buffer |
|
87 |
|
88 @param aCommDbId Unique Id of Communication database |
|
89 */ |
|
90 { |
|
91 if (bInitialised) |
|
92 { |
|
93 switch (aCommDbId) |
|
94 { |
|
95 case -1: |
|
96 buf.Copy(_L("ADD_SECTION\r\n")); |
|
97 break; |
|
98 case 0: |
|
99 buf.Copy(_L("ADD_TEMPLATE\r\n")); |
|
100 break; |
|
101 default: |
|
102 buf.Format(_L("ADD_SECTION\r\n# COMMDB_ID = %d\r\n"), |
|
103 aCommDbId); |
|
104 break; |
|
105 } |
|
106 WriteBufferToFile(buf, file); |
|
107 } |
|
108 } |
|
109 |
|
110 |
|
111 void CFileDump::WriteFieldCount(TUint count) |
|
112 /** |
|
113 Writes a section header to the internal buffer |
|
114 |
|
115 @param count Field count |
|
116 */ |
|
117 { |
|
118 if (bInitialised) |
|
119 { |
|
120 buf.Format(_L("\tFIELD_COUNT=%d\r\n"), count); |
|
121 WriteBufferToFile(buf, file); |
|
122 } |
|
123 } |
|
124 |
|
125 |
|
126 void CFileDump::WriteColumnValue(TPtrC &column, TPtrC &setting) |
|
127 /** |
|
128 Writes a column value to the internal buffer |
|
129 |
|
130 @param column A reference to a descriptor containing the name of a column |
|
131 @param setting The setting |
|
132 */ |
|
133 { |
|
134 if (bInitialised && column.Length() && setting.Length()) |
|
135 { |
|
136 // Theoretical worst case is longest field is populated with non-ASCII |
|
137 static TBuf<KMaxExpandedFieldLen> TempValue; // static to keep it off the stack |
|
138 TempValue.SetLength(0); |
|
139 |
|
140 /* replace any special chars with a portable representation that's re-importable |
|
141 * Special characters are escaped in the 'C' language fashion with backslash. |
|
142 * Specials are: |
|
143 * \t = TAB |
|
144 * \n = LF |
|
145 * \[ = [ - Need for escaping is to prevent INI file processing presuming |
|
146 * \] = ] - them to be start of section block |
|
147 * \\ = \ |
|
148 * \xHHHH - Hex value for any other character falling outside the normal |
|
149 * ASCII range of 32 - 126. This is because we're most likely |
|
150 * writing to an ASCII file, so absolutely can't trust that |
|
151 * UniCode will get preserved on a round-trip through an editor |
|
152 */ |
|
153 |
|
154 TInt i = 0; |
|
155 for (i=0;i<setting.Length();i++) |
|
156 { |
|
157 TUint16 x = setting[i]; |
|
158 switch(x) |
|
159 { |
|
160 case '\n': |
|
161 TempValue.Append(_L("\\n")); |
|
162 break; |
|
163 case '\t': |
|
164 TempValue.Append(_L("\\t")); |
|
165 break; |
|
166 case '[': |
|
167 TempValue.Append(_L("\\[")); |
|
168 break; |
|
169 case ']': |
|
170 TempValue.Append(_L("\\]")); |
|
171 break; |
|
172 case '\\': |
|
173 TempValue.Append(_L("\\\\")); |
|
174 break; |
|
175 default: |
|
176 if(x < 32 || x > 126) |
|
177 { |
|
178 TempValue.AppendFormat(_L("\\x%04X"), x); |
|
179 } |
|
180 else |
|
181 { |
|
182 TempValue.Append(x); |
|
183 } |
|
184 break; |
|
185 } |
|
186 } |
|
187 |
|
188 setting.Set(TempValue); |
|
189 buf.Format(_L("\t%S=%S\r\n"), &column, &TempValue); |
|
190 WriteBufferToFile(buf, file); |
|
191 } |
|
192 } |
|
193 |
|
194 |
|
195 void CFileDump::WriteSectionFooter(TInt aCommDbId) |
|
196 /** |
|
197 Writes a section footer to the internal buffer |
|
198 |
|
199 @param aCommDbId Unique Id of Communication database |
|
200 */ |
|
201 { |
|
202 if (bInitialised) |
|
203 { |
|
204 if (aCommDbId == 0) |
|
205 { |
|
206 buf.Copy(_L("END_TEMPLATE\r\n\r\n")); |
|
207 } |
|
208 else |
|
209 { |
|
210 buf.Copy(_L("END_ADD\r\n\r\n")); |
|
211 } |
|
212 WriteBufferToFile(buf, file); |
|
213 } |
|
214 } |
|
215 |
|
216 |
|
217 void CFileDump::WriteErrorMessage(const TPtrC &message) |
|
218 /** |
|
219 Writes a message to the file |
|
220 |
|
221 @param message The error message |
|
222 */ |
|
223 { |
|
224 if (bInitialised && message.Length() && message.Length() < (MAX_COL_LONG_VAL_LEN - 140)) |
|
225 { |
|
226 buf.Format(_L("############################################################\r\n## ERROR\r\n## %S\r\n############################################################\r\n"), |
|
227 &message); |
|
228 WriteBufferToFile(buf, file); |
|
229 } |
|
230 } |
|
231 |
|
232 void CFileDump::Msg(TPtrC text, ...) |
|
233 { |
|
234 if (text.Length() && text.Length() < MAX_COL_LONG_VAL_LEN) |
|
235 { |
|
236 VA_LIST list; |
|
237 VA_START(list, text); |
|
238 |
|
239 buf.FormatList(text, list); |
|
240 buf.Append(_L("\r\n")); |
|
241 |
|
242 // Only output to the console if the debug option is enabled |
|
243 if (pConsole && bDebug) |
|
244 pConsole->Printf(buf); |
|
245 |
|
246 if (bInitialised) |
|
247 { |
|
248 // Always write to the log file |
|
249 WriteBufferToFile(buf, file); |
|
250 } |
|
251 |
|
252 VA_END(list); |
|
253 } |
|
254 } |
|
255 |
|
256 void CFileDump::Dbg(TPtrC text, ...) |
|
257 { |
|
258 if (bDebug && text.Length() && text.Length() < MAX_COL_LONG_VAL_LEN) |
|
259 { |
|
260 VA_LIST list; |
|
261 VA_START(list, text); |
|
262 |
|
263 buf.FormatList(text, list); |
|
264 buf.Append(_L("\r\n")); |
|
265 |
|
266 // Always write to the console if available |
|
267 if (pConsole) |
|
268 pConsole->Printf(buf); |
|
269 |
|
270 if (bInitialised) |
|
271 { |
|
272 // Always write to the log file |
|
273 WriteBufferToFile(buf, file); |
|
274 } |
|
275 |
|
276 VA_END(list); |
|
277 } |
|
278 } |
|
279 |
|
280 void CFileDump::Error(TPtrC text, ...) |
|
281 { |
|
282 if (bInitialised && text.Length() && text.Length() < MAX_COL_LONG_VAL_LEN) |
|
283 { |
|
284 VA_LIST list; |
|
285 VA_START(list, text); |
|
286 |
|
287 buf.FormatList(text, list); |
|
288 buf.Append(_L("\r\n")); |
|
289 |
|
290 // Always write to the console if available |
|
291 if (pConsole) |
|
292 pConsole->Printf(buf); |
|
293 |
|
294 if (bInitialised) |
|
295 { |
|
296 // Always write to the log file |
|
297 WriteBufferToFile(buf, file); |
|
298 } |
|
299 |
|
300 VA_END(list); |
|
301 } |
|
302 } |
|
303 |
|
304 TBool CFileDump::WriteBufferToFile(TDesC16 &buffer, RFile &handle) |
|
305 { |
|
306 static TBuf8<1024> outputBuffer; |
|
307 static TPtrC16 ptr; |
|
308 static TInt consumed; |
|
309 static TInt remainder = 0; |
|
310 |
|
311 TBool valid = EFalse; |
|
312 |
|
313 if (buffer.Length()) |
|
314 { |
|
315 ptr.Set(buffer); |
|
316 do |
|
317 { |
|
318 // get something to write |
|
319 consumed = Min(outputBuffer.MaxLength(), ptr.Length()); |
|
320 |
|
321 // write it |
|
322 outputBuffer.Copy(ptr.Left(consumed)); |
|
323 if (handle.Write(outputBuffer) != KErrNone) |
|
324 return EFalse; |
|
325 |
|
326 // get the next chunk |
|
327 remainder = ptr.Length() - consumed; |
|
328 if (remainder > 0) |
|
329 ptr.Set(ptr.Right(remainder)); |
|
330 |
|
331 }while (remainder > 0); |
|
332 |
|
333 valid = ETrue; |
|
334 } |
|
335 |
|
336 return valid; |
|
337 } |