|
1 // cat.cpp |
|
2 // |
|
3 // Copyright (c) 2005 - 2010 Accenture. All rights reserved. |
|
4 // This component and the accompanying materials are made available |
|
5 // under the terms of the "Eclipse Public License v1.0" |
|
6 // which accompanies this distribution, and is available |
|
7 // at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 // |
|
9 // Initial Contributors: |
|
10 // Accenture - Initial contribution |
|
11 // |
|
12 |
|
13 #include "character_converter.h" |
|
14 #include "cat.h" |
|
15 |
|
16 const TInt KDefaultBlockSize = 512; |
|
17 |
|
18 |
|
19 CCommandBase* CCmdCat::NewLC() |
|
20 { |
|
21 CCmdCat* self = new(ELeave) CCmdCat(); |
|
22 CleanupStack::PushL(self); |
|
23 self->ConstructL(); |
|
24 return self; |
|
25 } |
|
26 |
|
27 CCmdCat::~CCmdCat() |
|
28 { |
|
29 iFiles.Close(); |
|
30 iBuf16.Close(); |
|
31 delete iCharacterConverter; |
|
32 delete iFileReader; |
|
33 } |
|
34 |
|
35 CCmdCat::CCmdCat() |
|
36 : CCommandBase(EManualComplete), iBlockSize(KDefaultBlockSize) |
|
37 { |
|
38 } |
|
39 |
|
40 void CCmdCat::ConstructL() |
|
41 { |
|
42 BaseConstructL(); |
|
43 } |
|
44 |
|
45 void CCmdCat::ReadNextFile() |
|
46 { |
|
47 if (iFiles.Count() > 0) |
|
48 { |
|
49 iFileReader->Read(iFiles[0], *this); |
|
50 } |
|
51 else |
|
52 { |
|
53 Complete(); |
|
54 } |
|
55 } |
|
56 |
|
57 void CCmdCat::RemoveCurrentFileName() |
|
58 { |
|
59 iFiles.Remove(0); |
|
60 } |
|
61 |
|
62 void CCmdCat::WriteChunkToConsoleL(const TDesC8& aData, TReadType aType) |
|
63 { |
|
64 if (iEncoding == EBinary) |
|
65 { |
|
66 iBuf16.Copy(aData); |
|
67 Write(iBuf16); |
|
68 if (aType == MFileReaderObserver::ELast) |
|
69 { |
|
70 RemoveCurrentFileName(); |
|
71 ReadNextFile(); |
|
72 } |
|
73 } |
|
74 else if (iEncoding == ELtkUtf8) |
|
75 { |
|
76 iBuf16.Zero(); |
|
77 iBuf16.AppendUtf8L(aData); |
|
78 Write(iBuf16); |
|
79 if (aType == MFileReaderObserver::ELast) |
|
80 { |
|
81 TInt firstBad; |
|
82 iBuf16.FinalizeUtf8(firstBad); |
|
83 if (firstBad != KErrNotFound) PrintWarning(_L("Found bad UTF-8 sequence in %S at byte offset %d"), &iFiles[0], firstBad); |
|
84 |
|
85 RemoveCurrentFileName(); |
|
86 ReadNextFile(); |
|
87 } |
|
88 } |
|
89 else |
|
90 { |
|
91 switch (aType) |
|
92 { |
|
93 case MFileReaderObserver::EFirst: |
|
94 { |
|
95 Write(iCharacterConverter->ConvertChunkL(aData, CCharacterConverter::EFirst)); |
|
96 break; |
|
97 } |
|
98 case MFileReaderObserver::EMiddle: |
|
99 { |
|
100 Write(iCharacterConverter->ConvertChunkL(aData, CCharacterConverter::EMiddle)); |
|
101 break; |
|
102 } |
|
103 case MFileReaderObserver::ELast: |
|
104 { |
|
105 Write(iCharacterConverter->ConvertChunkL(aData, CCharacterConverter::ELast)); |
|
106 RemoveCurrentFileName(); |
|
107 ReadNextFile(); |
|
108 break; |
|
109 } |
|
110 default: |
|
111 { |
|
112 ASSERT(FALSE); |
|
113 } |
|
114 } |
|
115 } |
|
116 } |
|
117 |
|
118 const TDesC& CCmdCat::Name() const |
|
119 { |
|
120 _LIT(KName, "cat"); |
|
121 return KName; |
|
122 } |
|
123 |
|
124 void CCmdCat::DoRunL() |
|
125 { |
|
126 if (iBinary) |
|
127 { |
|
128 if (iOptions.IsPresent(&iEncoding)) |
|
129 { |
|
130 PrintWarning(_L("--encoding overrides legacy --binary option")); |
|
131 } |
|
132 else |
|
133 { |
|
134 iEncoding = EBinary; |
|
135 } |
|
136 } |
|
137 |
|
138 if (iEncoding == EBinary || iEncoding == ELtkUtf8 || iFiles.Count() == 0) |
|
139 { |
|
140 iBuf16.CreateL(iBlockSize); |
|
141 } |
|
142 else |
|
143 { |
|
144 TUint charset; |
|
145 switch (iEncoding) |
|
146 { |
|
147 case EUtf8: |
|
148 charset = KCharacterSetIdentifierUtf8; |
|
149 break; |
|
150 case ELatin1: |
|
151 charset = KCharacterSetIdentifierIso88591; |
|
152 break; |
|
153 case EAuto: |
|
154 default: |
|
155 charset = 0; |
|
156 break; |
|
157 |
|
158 } |
|
159 TRAPL(iCharacterConverter = CCharacterConverter::NewL(iBlockSize, FsL(), charset), _L("Couldn't load charconv - try using --binary mode")); |
|
160 } |
|
161 iFileReader = CFileReader::NewL(iBlockSize, FsL(), iForce); |
|
162 |
|
163 if (iFiles.Count() > 0) |
|
164 { |
|
165 ReadNextFile(); |
|
166 } |
|
167 else |
|
168 { |
|
169 TInt err = KErrNone; |
|
170 while (err == KErrNone) |
|
171 { |
|
172 err = Stdin().Read(iBuf16); |
|
173 if (err == KErrNone) |
|
174 { |
|
175 Write(iBuf16); |
|
176 } |
|
177 } |
|
178 if (err == KErrEof) |
|
179 { |
|
180 err = KErrNone; |
|
181 } |
|
182 Complete(err); |
|
183 } |
|
184 } |
|
185 |
|
186 void CCmdCat::OptionsL(RCommandOptionList& aOptions) |
|
187 { |
|
188 _LIT(KOptForce, "force"); |
|
189 _LIT(KOptBinary, "binary"); |
|
190 _LIT(KOptEncoding, "encoding"); |
|
191 _LIT(KOptBlockSize, "block-size"); |
|
192 |
|
193 aOptions.AppendBoolL(iForce, KOptForce); |
|
194 aOptions.AppendEnumL((TInt&)iEncoding, KOptEncoding); |
|
195 aOptions.AppendIntL(iBlockSize, KOptBlockSize); |
|
196 aOptions.AppendBoolL(iBinary, KOptBinary); |
|
197 } |
|
198 |
|
199 void CCmdCat::ArgumentsL(RCommandArgumentList& aArguments) |
|
200 { |
|
201 _LIT(KArgFiles, "file_name"); |
|
202 aArguments.AppendFileNameL(iFiles, KArgFiles); |
|
203 } |
|
204 |
|
205 CCommandBase& CCmdCat::Command() |
|
206 { |
|
207 return *this; |
|
208 } |
|
209 |
|
210 void CCmdCat::HandleFileData(const TDesC8& aData, TReadType aType, TBool& aContinue) |
|
211 { |
|
212 aContinue = ETrue; |
|
213 TRAPD(err, WriteChunkToConsoleL(aData, aType)); |
|
214 if (err) |
|
215 { |
|
216 aContinue = EFalse; |
|
217 PrintWarning(_L("Problem writing chunk of %S to console: %d"), &iFiles[0], err); |
|
218 RemoveCurrentFileName(); |
|
219 ReadNextFile(); |
|
220 } |
|
221 } |
|
222 |
|
223 void CCmdCat::HandleFileReadError(TInt aError) |
|
224 { |
|
225 PrintWarning(_L("Problem reading %S: %d"), &iFiles[0], aError); |
|
226 RemoveCurrentFileName(); |
|
227 ReadNextFile(); |
|
228 } |
|
229 |
|
230 |
|
231 #ifdef EXE_BUILD |
|
232 EXE_BOILER_PLATE(CCmdCat) |
|
233 #endif |
|
234 |