|
1 // Copyright (c) 2000-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 #include "ConvertDates.h" |
|
17 |
|
18 // Constants |
|
19 const TInt KFirstYear = 0; |
|
20 const TInt KFromOffset = 0; |
|
21 const TInt KToOffset = 5; |
|
22 const TInt KCycleOffset = 10; |
|
23 const TInt KYearOffset = 13; |
|
24 const TInt KMnthOffset = 19; |
|
25 const TInt KLeapMonthOffset = 22; |
|
26 const TInt KDyOffset = 24; |
|
27 const TInt KCycleLen = 2; |
|
28 const TInt KSignLen = 1; |
|
29 const TInt KYearLen = 5; |
|
30 const TInt KMonthLen = 2; |
|
31 //const TInt KLeapMonthLen = 1; |
|
32 const TInt KDayLen = 2; |
|
33 // |
|
34 _LIT(KRoot, "C:\\"); |
|
35 _LIT(KSpace, " "); |
|
36 _LIT(KResult, " Result "); |
|
37 _LIT(KTestHeader, "Test Results"); |
|
38 _LIT(KCalconLogFileDirectory, "calcon"); |
|
39 _LIT(KCalconLogFile, "c:\\calcon\\calcon.txt"); |
|
40 _LIT(KInputFile, "c:\\input.txt"); |
|
41 _LIT(KOutputFile, "c:\\output.txt"); |
|
42 _LIT8(KImcvCRLF, "\r\n"); |
|
43 |
|
44 |
|
45 |
|
46 // |
|
47 // Construction/Destruction |
|
48 // |
|
49 |
|
50 CConvertDates::~CConvertDates() |
|
51 { |
|
52 iInputFile.Close(); |
|
53 iOutputFile.Close(); |
|
54 iRFSession.Close(); |
|
55 } |
|
56 |
|
57 CConvertDates* CConvertDates::NewL() |
|
58 { |
|
59 CConvertDates* self = CConvertDates::NewLC(); |
|
60 CleanupStack::Pop(); |
|
61 return self; |
|
62 } |
|
63 |
|
64 CConvertDates* CConvertDates::NewLC() |
|
65 { |
|
66 CConvertDates* self = new(ELeave) CConvertDates; |
|
67 CleanupStack::PushL(self); |
|
68 self->ConstructL(); |
|
69 return self; |
|
70 } |
|
71 |
|
72 void CConvertDates::ConstructL() |
|
73 { |
|
74 // constructors, public member data, static casts |
|
75 TGregorianCalendar gregorianCalendar; |
|
76 TChineseCalendar chineseCalendar; |
|
77 TArithmeticalDate date; |
|
78 date.iYear=2001; |
|
79 date.iMonth=1; |
|
80 date.iDay=24; // 24th January 2001 |
|
81 gregorianCalendar.SetDate(date); |
|
82 static_cast<TCalendar&>(chineseCalendar)=gregorianCalendar; |
|
83 TChineseDate chineseDate; |
|
84 chineseCalendar.GetDate(chineseDate); // cycle=78, Year=18, Month=1, Day=1 (New Year's day) |
|
85 |
|
86 |
|
87 User::LeaveIfError(iRFSession.Connect()); |
|
88 iRFSession.SetSessionPath(KRoot); |
|
89 |
|
90 if (iLogger.LogValid()) |
|
91 iLogger.CloseLog(); |
|
92 TParse p; |
|
93 p.Set(KCalconLogFile, NULL, NULL); |
|
94 iLogger.Connect(); |
|
95 iLogger.CreateLog(KCalconLogFileDirectory, p.NameAndExt(), EFileLoggingModeOverwrite); |
|
96 iLogger.SetDateAndTime(EFalse, EFalse); |
|
97 iLogger.Write(KTestHeader); |
|
98 } |
|
99 |
|
100 void CConvertDates::ConvertL() |
|
101 { |
|
102 TBuf8<100> logLine; |
|
103 TBuf<64> errBuf; |
|
104 // open the input and output files |
|
105 OpenFilesL(); |
|
106 |
|
107 TInt fileErr = KErrNone; |
|
108 while(fileErr == KErrNone) |
|
109 { |
|
110 // read date from the input file |
|
111 fileErr = ReadLine(); |
|
112 if (fileErr==KErrNone) |
|
113 { |
|
114 // get the cal types to convert from and to |
|
115 TRAPD(err,GetCalL(iDateLine)); |
|
116 if (err) |
|
117 { |
|
118 errBuf.Format(_L("\r\nError get calendar conversion type %d\r\n"),err); |
|
119 iLogger.Write(errBuf); |
|
120 } |
|
121 else |
|
122 { |
|
123 // do the conversion |
|
124 Cal1ToCal2(); |
|
125 |
|
126 // set the cal types to convert from and to |
|
127 logLine.Zero(); |
|
128 TRAP(err,SetCalL(logLine)); |
|
129 if (err) |
|
130 { |
|
131 errBuf.Format(_L("\r\nError set calendar conversion type %d\r\n"),err); |
|
132 iDateLine.Append(errBuf); |
|
133 } |
|
134 else |
|
135 { |
|
136 iDateLine.Append(logLine); |
|
137 iDateLine.Append(_L8("\r\n")); |
|
138 iLogger.Write(iDateLine); |
|
139 if (iOutputFile.Write(iDateLine)) |
|
140 { |
|
141 User::Leave(KErrCorrupt); |
|
142 } |
|
143 } |
|
144 } |
|
145 } |
|
146 } |
|
147 } |
|
148 |
|
149 void CConvertDates::OpenFilesL() |
|
150 { |
|
151 User::LeaveIfError(iInputFile.Open(iRFSession, KInputFile, EFileStreamText|EFileRead|EFileShareExclusive)); |
|
152 User::LeaveIfError(iOutputFile.Replace(iRFSession, KOutputFile, EFileStreamText|EFileWrite|EFileShareExclusive)); |
|
153 } |
|
154 |
|
155 void CConvertDates::GetCalL(TDes8& aDateToConvert) |
|
156 { |
|
157 TInt dateError; |
|
158 TDateTime dt(0,EJanuary,0,0,0,0,0); |
|
159 TLex8 lexCycle(aDateToConvert.Mid(KCycleOffset,KCycleLen)); |
|
160 TLex8 lexYear(aDateToConvert.Mid(KYearOffset,KYearLen)); |
|
161 TLex8 lexMonth(aDateToConvert.Mid(KMnthOffset,KMonthLen)); |
|
162 // TLex8 lexLeapMonth(aDateToConvert.Mid(KLeapMonthOffset,KLeapMonthLen)); |
|
163 TLex8 lexDay(aDateToConvert.Mid(KDyOffset,KDayLen)); |
|
164 |
|
165 // get cycle |
|
166 if (lexCycle.Val(iChinDateOut.iCycle) != KErrNone) |
|
167 { |
|
168 User::Leave(KErrCorrupt); |
|
169 } |
|
170 // get year |
|
171 if (lexYear.Val(iArithDateOut.iYear) != KErrNone) |
|
172 { |
|
173 User::Leave(KErrGeneral); |
|
174 } |
|
175 iChinDateOut.iYear = iArithDateOut.iYear; |
|
176 // get month |
|
177 if (lexMonth.Val(iArithDateOut.iMonth) != KErrNone) |
|
178 { |
|
179 User::Leave(KErrGeneral); |
|
180 } |
|
181 iChinDateOut.iMonth = iArithDateOut.iMonth; |
|
182 // get leap month |
|
183 if (aDateToConvert[KLeapMonthOffset] == 't') |
|
184 { |
|
185 iChinDateOut.iLeapMonth = ETrue; |
|
186 } |
|
187 else |
|
188 { |
|
189 iChinDateOut.iLeapMonth = EFalse; |
|
190 } |
|
191 // get day |
|
192 if (lexDay.Val(iArithDateOut.iDay) != KErrNone) |
|
193 { |
|
194 User::Leave(KErrCorrupt); |
|
195 } |
|
196 iChinDateOut.iDay = iArithDateOut.iDay; |
|
197 |
|
198 |
|
199 switch(aDateToConvert[KFromOffset]) |
|
200 { |
|
201 case 'g': |
|
202 iCalTypeFrom = EGreg; |
|
203 dateError = iGreg.SetDate(iArithDateOut); |
|
204 iGreg.GregorianToDateTime(dt); |
|
205 iGreg.DateTimeToGregorian(dt); |
|
206 break; |
|
207 case 'c': |
|
208 iCalTypeFrom = EChin; |
|
209 dateError = iChin.SetDate(iChinDateOut); |
|
210 iChin.ChineseToDateTime(dt); |
|
211 iChin.DateTimeToChinese(dt); |
|
212 break; |
|
213 default: |
|
214 dateError = KErrCorrupt; |
|
215 break; |
|
216 } |
|
217 |
|
218 if (dateError) |
|
219 User::Leave(dateError); |
|
220 |
|
221 switch(aDateToConvert[KToOffset]) |
|
222 { |
|
223 case 'g': |
|
224 iCalTypeTo = EGreg; |
|
225 break; |
|
226 case 'c': |
|
227 iCalTypeTo = EChin; |
|
228 break; |
|
229 default: |
|
230 break; |
|
231 } |
|
232 } |
|
233 |
|
234 void CConvertDates::Cal1ToCal2() const |
|
235 { |
|
236 switch(iCalTypeFrom) |
|
237 { |
|
238 case EGreg: |
|
239 { |
|
240 switch(iCalTypeTo) |
|
241 { |
|
242 case EChin: |
|
243 (TCalendar&)(iChin) = (TCalendar&)(iGreg); |
|
244 break; |
|
245 default: |
|
246 break; |
|
247 } |
|
248 } |
|
249 break; |
|
250 case EChin: |
|
251 { |
|
252 switch(iCalTypeTo) |
|
253 { |
|
254 case EGreg: |
|
255 (TCalendar&)(iGreg) = (TCalendar&)(iChin); |
|
256 break; |
|
257 default: |
|
258 break; |
|
259 } |
|
260 } |
|
261 break; |
|
262 default: |
|
263 break; |
|
264 } |
|
265 } |
|
266 |
|
267 void CConvertDates::SetCalL(TDes8& aConvertedDate) |
|
268 { |
|
269 TArithmeticalDate Date; |
|
270 TChineseDate ChinDate; |
|
271 TBuf8<KYearLen> TempDate; |
|
272 |
|
273 switch(iCalTypeFrom) |
|
274 { |
|
275 case EGreg: |
|
276 { |
|
277 switch(iCalTypeTo) |
|
278 { |
|
279 case EChin: |
|
280 iChin.GetDate(ChinDate); |
|
281 break; |
|
282 default: |
|
283 break; |
|
284 } |
|
285 } |
|
286 break; |
|
287 case EChin: |
|
288 { |
|
289 switch(iCalTypeTo) |
|
290 { |
|
291 case EGreg: |
|
292 iGreg.GetDate(Date); |
|
293 TempDate.Copy(_L("greg ")); |
|
294 default: |
|
295 break; |
|
296 } |
|
297 } |
|
298 break; |
|
299 default: |
|
300 break; |
|
301 } |
|
302 |
|
303 if (iCalTypeTo == EChin) |
|
304 { |
|
305 TBuf8<10> TempFormatStr; |
|
306 |
|
307 TempFormatStr.FillZ(); |
|
308 TempFormatStr.Format(_L8("%%0%dd"),KCycleLen); |
|
309 TempDate.FillZ(); |
|
310 TempDate.Format(TempFormatStr,ChinDate.iCycle); |
|
311 aConvertedDate.Append(KResult); |
|
312 aConvertedDate.Append(TempDate); |
|
313 aConvertedDate.Append(KSpace); |
|
314 |
|
315 TempDate.FillZ(); |
|
316 TempDate.Format(_L8("+")); |
|
317 aConvertedDate.Append(TempDate); |
|
318 |
|
319 TempFormatStr.FillZ(); |
|
320 TempFormatStr.Format(_L8("%%0%dd"),KYearLen - KSignLen); |
|
321 TempDate.FillZ(); |
|
322 TempDate.Format(TempFormatStr,ChinDate.iYear); |
|
323 aConvertedDate.Append(TempDate); |
|
324 aConvertedDate.Append(KSpace); |
|
325 |
|
326 TempFormatStr.FillZ(); |
|
327 TempFormatStr.Format(_L8("%%0%dd"),KMonthLen); |
|
328 TempDate.FillZ(); |
|
329 TempDate.Format(TempFormatStr,ChinDate.iMonth); |
|
330 aConvertedDate.Append(TempDate); |
|
331 aConvertedDate.Append(KSpace); |
|
332 |
|
333 TempDate.FillZ(); |
|
334 if (ChinDate.iLeapMonth == EFalse) |
|
335 { |
|
336 TempDate.Format(_L8("f")); |
|
337 } |
|
338 else |
|
339 { |
|
340 TempDate.Format(_L8("t")); |
|
341 } |
|
342 aConvertedDate.Append(TempDate); |
|
343 aConvertedDate.Append(KSpace); |
|
344 |
|
345 TempFormatStr.FillZ(); |
|
346 TempFormatStr.Format(_L8("%%0%dd"),KDayLen); |
|
347 TempDate.FillZ(); |
|
348 TempDate.Format(TempFormatStr,ChinDate.iDay); |
|
349 aConvertedDate.Append(TempDate); |
|
350 } |
|
351 else |
|
352 { |
|
353 TBuf8<10> TempFormatStr; |
|
354 |
|
355 if (Date.iYear >= KFirstYear) |
|
356 { |
|
357 TempDate.Format(_L8("+")); |
|
358 } |
|
359 else |
|
360 { |
|
361 TempDate.Format(_L8("-")); |
|
362 } |
|
363 aConvertedDate.Append(KResult); |
|
364 aConvertedDate.Append(TempDate); |
|
365 |
|
366 TempFormatStr.FillZ(); |
|
367 TempFormatStr.Format(_L8("%%0%dd"),KYearLen - KSignLen); |
|
368 TempDate.FillZ(); |
|
369 if (Date.iYear >= KFirstYear) |
|
370 { |
|
371 TempDate.Format(TempFormatStr,Date.iYear); |
|
372 } |
|
373 else |
|
374 { |
|
375 TempDate.Format(TempFormatStr,-Date.iYear); |
|
376 } |
|
377 aConvertedDate.Append(TempDate); |
|
378 aConvertedDate.Append(KSpace); |
|
379 |
|
380 TempFormatStr.FillZ(); |
|
381 TempFormatStr.Format(_L8("%%0%dd"),KMonthLen); |
|
382 TempDate.FillZ(); |
|
383 TempDate.Format(TempFormatStr,Date.iMonth); |
|
384 aConvertedDate.Append(TempDate); |
|
385 aConvertedDate.Append(KSpace); |
|
386 |
|
387 TempFormatStr.FillZ(); |
|
388 TempFormatStr.Format(_L8("%%0%dd"),KDayLen); |
|
389 TempDate.FillZ(); |
|
390 TempDate.Format(TempFormatStr,Date.iDay); |
|
391 aConvertedDate.Append(TempDate); |
|
392 } |
|
393 } |
|
394 |
|
395 // |
|
396 // Read line of test from file |
|
397 // |
|
398 TInt CConvertDates::ReadLine() |
|
399 { |
|
400 TInt err = KErrNone; |
|
401 |
|
402 // Read into the buffer |
|
403 TBuf8<256> buffer; // Max read of the Read() function.. |
|
404 iDateLine.Zero(); |
|
405 |
|
406 // Get the current file position |
|
407 TInt filePos = 0; |
|
408 iInputFile.Seek(ESeekCurrent, filePos); |
|
409 |
|
410 // Read the buffer |
|
411 err = iInputFile.Read(buffer); |
|
412 |
|
413 //end of file? |
|
414 TInt s = buffer.Length(); |
|
415 if ( s == 0) |
|
416 { |
|
417 err = KErrEof; |
|
418 } |
|
419 |
|
420 if (err == KErrNone) |
|
421 { |
|
422 // Copy to the lfcr and then set the file pointer |
|
423 // to the point after that... |
|
424 TInt pos = buffer.Find(KImcvCRLF); |
|
425 if ( pos != -1) |
|
426 { |
|
427 iDateLine.Justify(buffer, pos, ELeft, ' '); |
|
428 filePos += (pos+2); |
|
429 |
|
430 // Set the file pointer back to after the lfcr.. |
|
431 iInputFile.Seek(ESeekStart, filePos); |
|
432 } |
|
433 // Else fill the whole buffer 256 chars.. |
|
434 else |
|
435 { |
|
436 iDateLine.Copy(buffer); |
|
437 } |
|
438 } |
|
439 return err; |
|
440 } |