-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtextutils.cpp
More file actions
298 lines (268 loc) · 8.13 KB
/
Copy pathtextutils.cpp
File metadata and controls
298 lines (268 loc) · 8.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
#include "textutils.h"
/*************************************************************
*
* My own implementation of TFileText. This properly supports 8bit
* and 16bit conversion!! This class also does not have the
* 256 chars per line limitation. This class can read a maximum of
* 100 characters at a time until the end of line. The caller is
* responsible for ensuring a buffer of sufficient size.
*
* It also exposes one property that contains the percentage of
* the text file that has been read. This property is only valid
* after a call to ReadLine.
*
* The string it returns is DEVOID of the \r\n!!!!
*
**************************************************************/
TInt CTextFileReader::ReadLine(TDes *aReadBuffer)
{
TInt iRetVal = KErrGeneral;
if (iFileSize > -1)
{
iInternalBuffer.Zero();
aReadBuffer->Zero();
while (iFile->Read(iInternalBuffer, iInternalBuffer.MaxLength()) == KErrNone)
{
// Did we reach the end of the file?
if (iInternalBuffer.Length() == 0)
{
iRetVal = KErrEof;
break;
}
TInt iLineTerminatorPos = iInternalBuffer.Find(_L8("\r\n"));
if (iLineTerminatorPos >= 0)
{
if ((aReadBuffer->Length() + iLineTerminatorPos) <= aReadBuffer->MaxLength())
{
iInternalConverterBuffer.Copy(iInternalBuffer.Left(iLineTerminatorPos));
aReadBuffer->Append(iInternalConverterBuffer);
iLineTerminatorPos = -(iInternalBuffer.Length() - iLineTerminatorPos) + 2;
if (iFile->Seek(ESeekCurrent, iLineTerminatorPos) == KErrNone)
{
iCurrentCursorOffset = iLineTerminatorPos;
iRetVal = KErrNone;
}
}
else
iRetVal = KErrOverflow;
break;
}
else
{
// Couldn't find the terminator. Keep reading.
if ((aReadBuffer->Length() + iInternalBuffer.Length()) <= aReadBuffer->MaxLength())
{
iInternalConverterBuffer.Copy(iInternalBuffer);
aReadBuffer->Append(iInternalConverterBuffer);
iCurrentCursorOffset += iInternalBuffer.Length();
}
else
iRetVal = KErrOverflow;
}
}
//iCurrentCursorOffset = 0;
//if (iFile->Seek(ESeekCurrent, iCurrentCursorOffset) == KErrNone)
iPercentRead = TInt((TReal(iCurrentCursorOffset) / TReal(iFileSize) * TReal(100) + 0.5));
}
return iRetVal;
}
TInt CTextFileReader::WriteLine(TDes *aWriteBuffer)
{
TInt iRetVal = KErrGeneral;
HBufC8 *iConvBuffer = HBufC8::NewL(aWriteBuffer->Length() * 2);
iConvBuffer->Des().Copy(*aWriteBuffer);
TPtr8 iPtr = iConvBuffer->Des();
if (iFile->Write(iPtr) == KErrNone)
{
if (iFile->Write(_L8("\r\n")) == KErrNone)
iRetVal = KErrNone;
}
delete iConvBuffer;
return iRetVal;
}
void CTextFileReader::Set(RFile *aFile)
{
iFile = aFile;
if (iFile->Size(iFileSize) != KErrNone)
iFileSize = -1;
}
/*************************************************************
*
* My CSV parser. :)
*
* Note: iColumnToExtract starts from 0 (as per C/C++ convention)
*
*************************************************************/
// Checks if a given character is a single quote. Returns:
// 0 = yes, is single quote
// 1 = no, current pos is quote but has quote before
// 2 = no, current pos is quote but has quote after
// 3 = no, current pos is not quote
// 4 = no, current pos is quote but has quote before and after
TInt CCSVLineParser::IsSingleQuote(TInt aOffsetLocation, TDesC *aBuffer)
{
TInt iRetVal = 3;
TBool iCurrentIsQuote = EFalse, iPrevIsQuote = EFalse, iNextIsQuote = EFalse;
if (aBuffer->Length() > 0)
{
// Check current char pos
if (aBuffer->Mid(aOffsetLocation, 1).Compare(_L("\"")) == 0)
iCurrentIsQuote = ETrue;
// Check next char pos
if (aOffsetLocation < (aBuffer->Length() - 1))
{
if (aBuffer->Mid(aOffsetLocation + 1, 1).Compare(_L("\"")) == 0)
iNextIsQuote = ETrue;
}
// Check prev char pos
if (aOffsetLocation > 0)
{
if (aBuffer->Mid(aOffsetLocation - 1, 1).Compare(_L("\"")) == 0)
iPrevIsQuote = ETrue;
}
if (iCurrentIsQuote)
{
if ((iPrevIsQuote) && (iNextIsQuote))
iRetVal = 4;
else if (iPrevIsQuote)
iRetVal = 1;
else if (iNextIsQuote)
iRetVal = 2;
else
iRetVal = 0;
}
}
return iRetVal;
}
// This function assumes the caller has already done all the necessary trimming to remove
// leading spaces. It will always assume the FIRST character MUST be a double quote
// It returns aEndQuotePos denoting the end char position of the quote.
// This char position is inclusive of the quote location
TInt CCSVLineParser::FindStartEndQuotes(TInt *aEndQuotePos, TDesC *aBuffer)
{
TInt iRetVal = KErrNotFound;
if (aBuffer->Left(1).Compare(_L("\"")) == 0)
{
TInt iCharToInspect = 1;
TInt iQuoteSearchResult;
while (iCharToInspect < aBuffer->Length())
{
iQuoteSearchResult = IsSingleQuote(iCharToInspect, aBuffer);
if ((iQuoteSearchResult == 2) || (iQuoteSearchResult == 4))
iCharToInspect++;
else if ((iQuoteSearchResult == 0) || (iQuoteSearchResult == 1))
{
*aEndQuotePos = iCharToInspect;
iRetVal = KErrNone;
break;
}
iCharToInspect++;
}
}
return iRetVal;
}
// Returns the text for a particular column given a line of CSV text. The text returned is
// stripped of the enclosing double quotes.
// It is assumed there is no \r\n at the end of the text line
TInt CCSVLineParser::GetColumn(TDesC *aInputBuffer, TDes *aOutputBuffer, TInt iColumnToExtract)
{
TInt iRetVal = KErrGeneral;
if (iColumnToExtract < 0)
iRetVal = KErrArgument;
else
{
if (aInputBuffer->Length() > 0)
{
HBufC *iTempBuffer = HBufC::NewL(aInputBuffer->Length());
// Trim leading and trailing spaces
iTempBuffer->Des().Copy(*aInputBuffer);
iTempBuffer->Des().Trim();
TPtr iPtr = iTempBuffer->Des();
TDes *iTempDes = &iPtr;
TInt iEndPos = 0;
TBool iFoundText = ETrue;
do
{
if (FindStartEndQuotes(&iEndPos, iTempDes) == KErrNone)
{
if (iColumnToExtract > 0)
iTempDes->Delete(0, iEndPos + 1);
}
else
{
iFoundText = EFalse;
break;
}
// Check for comma
if (iColumnToExtract > 0)
{
iTempDes->Trim();
if (iTempDes->Length() > 0)
{
if (iTempDes->Left(1).Compare(_L(",")) == 0)
{
iTempDes->Delete(0, 1);
iTempDes->Trim();
}
else
{
iFoundText = EFalse;
break;
}
}
}
iColumnToExtract--;
} while (iColumnToExtract >= 0);
if (iFoundText)
{
iRetVal = KErrNone;
// -2 to compensate for the double quote at the beginning and at the end
// of the string
if ((iEndPos - 1 + aOutputBuffer->Length() - 2) <= aOutputBuffer->MaxLength())
aOutputBuffer->Copy(iTempBuffer->Des().Mid(1, iEndPos - 1));
else
iRetVal = KErrOverflow;
}
delete iTempBuffer;
}
else
iRetVal = KErrNotFound;
}
return iRetVal;
}
// Searches a string and replaces all occurrences of aStrToReplace with aReplacementStr
void CCSVLineParser::ReplaceAll(TDes *aTheString, TPtrC aStrToReplace, TPtrC aReplacementStr)
{
HBufC *iTempBuffer = HBufC::NewL(aTheString->Length() * 2);
iTempBuffer->Des().Zero();
for (int i = 0; i < aTheString->Length(); i++)
{
if ((i + aStrToReplace.Length() - 1) < aTheString->Length())
{
if (aTheString->Mid(i, aStrToReplace.Length()).Compare(aStrToReplace) == 0)
{
iTempBuffer->Des().Append(aReplacementStr);
i += aStrToReplace.Length() - 1;
}
else
iTempBuffer->Des().Append(aTheString->Mid(i, 1));
}
else
iTempBuffer->Des().Append(aTheString->Mid(i, 1));
}
aTheString->Copy(*iTempBuffer);
delete iTempBuffer;
/* Even though this code below is shorter, it is both more inefficient and riskier (it has
// the possibility of getting stuck in an infinite loop until you run out of space on
// aTheString)
if (aTheString->Length() > 0)
{
TInt iStartLocation = aTheString->Find(aStrToReplace);
while (iStartLocation > 0)
{
aTheString->Replace(iStartLocation, aStrToReplace.Length(), aReplacementStr);
iStartLocation = aTheString->Find(aStrToReplace);
}
}
*/
}