1 // Copyright (c) 1999-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 the License "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 // Thindump is a small tool for dumping text files. The text file |
|
15 // given will be dumped using RDebug::Print meaning the text goes to |
|
16 // both the console and the Debug Serial Port. |
|
17 // |
|
18 // |
|
19 |
|
20 /** |
|
21 @file |
|
22 @internalComponent |
|
23 */ |
|
24 |
|
25 #include <e32test.h> |
|
26 #include <f32file.h> |
|
27 #ifndef SYMBIAN_BASEDUMP |
|
28 #include <s32file.h> |
|
29 #include <c32comm.h> |
|
30 #include <bacline.h> |
|
31 #endif // SYMBIAN_BASEDUMP |
|
32 |
|
33 |
|
34 GLDEF_D RTest gTest(_L("thindump utility")); |
|
35 GLDEF_D RFs gFs; |
|
36 |
|
37 LOCAL_C void SendFileL(const TPtrC &aFilename, const TInt &aPause) |
|
38 /** |
|
39 Dump narrow text file to console, comms & debug (WINS) |
|
40 */ |
|
41 { |
|
42 TInt err; |
|
43 |
|
44 // Open file and determine size |
|
45 RFile file; |
|
46 err = file.Open(gFs, aFilename, EFileRead); |
|
47 if( err == KErrNotFound || err == KErrBadName ) |
|
48 { |
|
49 gTest.Printf(_L("File %S does not exist"), &aFilename); |
|
50 return; |
|
51 } |
|
52 else if( err == KErrInUse ) // Test Execute opens with ShareAny |
|
53 { |
|
54 gTest.Printf(_L("Open failed as %S is in use. Retry open with EFileShareAny"), &aFilename); |
|
55 err = file.Open(gFs, aFilename, EFileRead|EFileShareAny); |
|
56 } |
|
57 |
|
58 // gTest.Printf(_L("SendFileL Line %d, RFile::Open err = %d"), __LINE__, err); |
|
59 // gTest.Getch(); |
|
60 User::LeaveIfError(err); |
|
61 |
|
62 TInt size; |
|
63 err = file.Size(size); |
|
64 // gTest.Printf(_L("SendFileL Line %d, RFile::Size err = %d"), __LINE__, err); |
|
65 // gTest.Getch(); |
|
66 User::LeaveIfError(err); |
|
67 |
|
68 gTest.Printf(_L("File %S (%d bytes)"), &aFilename, size); |
|
69 |
|
70 // Get ready to read file |
|
71 const TInt KBufferSize = 32; |
|
72 const TInt KBufferMaxLength = 1024; |
|
73 |
|
74 _LIT(KCharLF, "\x0a"); |
|
75 _LIT(KCharCR, "\x0d"); |
|
76 |
|
77 TInt pos = 0; |
|
78 TInt lfPos; |
|
79 TInt crPos; |
|
80 |
|
81 TBuf8<KBufferSize+1> buf8(KBufferSize+1); |
|
82 TBuf<KBufferSize+1> buf16(KBufferSize+1); |
|
83 |
|
84 HBufC* format16=HBufC::NewLC(KBufferMaxLength); |
|
85 |
|
86 // Read block from file |
|
87 while(pos < size) |
|
88 { |
|
89 err = file.Read(buf8, KBufferSize); |
|
90 // gTest.Printf(_L("SendFileL Line %d, RFile::Read err = %d"), __LINE__, err); |
|
91 // gTest.Getch(); |
|
92 User::LeaveIfError(err); |
|
93 |
|
94 // Expand to 16 bit chars |
|
95 buf16.Copy(buf8); |
|
96 |
|
97 // Remove carriage returns |
|
98 while ( (crPos = buf16.Find(KCharCR)) != KErrNotFound ) |
|
99 buf16.Delete(crPos, 1); |
|
100 |
|
101 // Find line feeds |
|
102 TPtr ptr16( format16->Des() ); |
|
103 while ( (lfPos = buf16.Find(KCharLF)) != KErrNotFound ) |
|
104 { |
|
105 // Extract this line & append to any buffered line |
|
106 TPtrC ptrLeft = buf16.Left(lfPos+1); |
|
107 ptr16.Append(ptrLeft); |
|
108 buf16.Delete(0, lfPos+1); |
|
109 |
|
110 // Print this line |
|
111 gTest.Printf(_L("%S"), &ptr16 ); |
|
112 ptr16.Zero(); |
|
113 |
|
114 // Small 10ms pause to prevent data loss during upload |
|
115 User::After(10000); |
|
116 } |
|
117 |
|
118 ptr16.Append(buf16); |
|
119 pos += KBufferSize; |
|
120 } |
|
121 |
|
122 // Output any partial line still in buffer |
|
123 if (format16->Length()) |
|
124 { |
|
125 TPtr ptr16( format16->Des() ); |
|
126 gTest.Printf(_L("%S"), &ptr16 ); |
|
127 } |
|
128 |
|
129 CleanupStack::PopAndDestroy(1, format16); |
|
130 |
|
131 if (aPause) |
|
132 { |
|
133 gTest.Printf(_L("Dump complete. Press any key ...")); |
|
134 gTest.Getch(); |
|
135 } |
|
136 |
|
137 file.Close(); |
|
138 } |
|
139 |
|
140 |
|
141 LOCAL_C void InitGlobalsL() |
|
142 /** |
|
143 Initialise global variables. |
|
144 */ |
|
145 { |
|
146 TInt err; |
|
147 |
|
148 err = gFs.Connect(); |
|
149 // gTest.Printf(_L("InitGlobals Line %d, RFs::Connect err = %d"), __LINE__, err); |
|
150 // gTest.Getch(); |
|
151 User::LeaveIfError(err); |
|
152 } |
|
153 |
|
154 |
|
155 LOCAL_C void DestroyGlobals() |
|
156 /** |
|
157 Free global variables |
|
158 */ |
|
159 { |
|
160 gFs.Close(); |
|
161 } |
|
162 |
|
163 |
|
164 LOCAL_C void RunSendFileL() |
|
165 /** |
|
166 Transmit the file down the Debug Port |
|
167 */ |
|
168 { |
|
169 InitGlobalsL(); |
|
170 |
|
171 TInt pause=EFalse; |
|
172 _LIT(KOptionNoPause,"-nop"); |
|
173 _LIT(KOptionPause,"-p"); |
|
174 _LIT(KDumpFileDefault, "c:\\log.txt"); |
|
175 |
|
176 // Obtain command line parameters |
|
177 TPtrC filename( KDumpFileDefault ); |
|
178 |
|
179 #ifndef SYMBIAN_BASEDUMP |
|
180 CCommandLineArguments* args = CCommandLineArguments::NewLC(); |
|
181 |
|
182 for(TInt i=1;i<args->Count();i++) |
|
183 { |
|
184 if(args->Arg(i).MatchF(KOptionNoPause)==0) |
|
185 { |
|
186 pause=EFalse; |
|
187 } |
|
188 else if(args->Arg(i).MatchF(KOptionPause)==0) |
|
189 { |
|
190 pause=ETrue; |
|
191 } |
|
192 else |
|
193 { |
|
194 filename.Set(args->Arg(i)); |
|
195 } |
|
196 } |
|
197 #else |
|
198 TBuf<256> cmd; |
|
199 User::CommandLine(cmd); |
|
200 TLex lex(cmd); |
|
201 |
|
202 while (!lex.Eos()) |
|
203 { |
|
204 TPtrC token; |
|
205 token.Set(lex.NextToken()); |
|
206 if (token.Length()==0) |
|
207 { |
|
208 break; // ignore trailing whitespace |
|
209 } |
|
210 else if (token==KOptionNoPause) |
|
211 { |
|
212 pause=EFalse; |
|
213 } |
|
214 else if (token==KOptionPause) |
|
215 { |
|
216 pause=ETrue; |
|
217 } |
|
218 else |
|
219 { |
|
220 filename.Set(token); |
|
221 } |
|
222 } |
|
223 #endif |
|
224 SendFileL( filename, pause ); |
|
225 |
|
226 #ifndef SYMBIAN_BASEDUMP |
|
227 CleanupStack::PopAndDestroy(1, args); |
|
228 #endif |
|
229 DestroyGlobals(); |
|
230 } |
|
231 |
|
232 |
|
233 EXPORT_C TInt E32Main() |
|
234 /** |
|
235 Main Program |
|
236 */ |
|
237 { |
|
238 CTrapCleanup* cleanup = CTrapCleanup::New(); |
|
239 CActiveScheduler* theActiveScheduler = new CActiveScheduler(); |
|
240 CActiveScheduler::Install(theActiveScheduler); |
|
241 |
|
242 __UHEAP_MARK; |
|
243 |
|
244 User::ResetInactivityTime(); |
|
245 |
|
246 gTest.Printf(_L("========== Start Log File ==========")); |
|
247 |
|
248 TRAPD(err,RunSendFileL()); |
|
249 if (err!=KErrNone) |
|
250 { |
|
251 gTest.Printf(_L("thindump left with Error No %d"), err); |
|
252 } |
|
253 |
|
254 gTest.Printf(_L("========== Finish Log File ==========")); |
|
255 |
|
256 gTest.Close(); |
|
257 |
|
258 __UHEAP_MARKEND; |
|
259 |
|
260 delete cleanup; |
|
261 delete theActiveScheduler; |
|
262 |
|
263 return KErrNone; |
|
264 } |
|