pimappsupport/chinesecalendaralg/tsrc/ConvertDates.cpp
branchRCL_3
changeset 12 38571fd2a704
equal deleted inserted replaced
5:42814f902fe6 12:38571fd2a704
       
     1 // Copyright (c) 2002-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 KFirstConvertYear = 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 KDayLen = 2;
       
    32 //
       
    33 _LIT(KRoot,						"C:\\");
       
    34 _LIT(KSpace,					" ");
       
    35 _LIT(KResult,					" Result ");
       
    36 _LIT(KTestHeader,				"Test Results");
       
    37 _LIT(KFileNotPresent,			"test file %S not present - test failed\n");
       
    38 _LIT(KCalconDirectory,			"c:\\calcontest\\");
       
    39 _LIT(KCalconLogFileDirectory,	"c:\\calcon\\");
       
    40 _LIT(KCalconLogFile,			"c:\\calcon\\calcon.txt");
       
    41 _LIT(KInputFile1,				"z:\\calcontest\\input1.txt");
       
    42 _LIT(KOutputFile1,				"c:\\calcontest\\output1.txt");
       
    43 _LIT(KInputFile2,				"z:\\calcontest\\input2.txt");
       
    44 _LIT(KOutputFile2,				"c:\\calcontest\\output2.txt");
       
    45 _LIT(KOutputFileRef1,			"z:\\calcontest\\output1REFERENCE.txt");
       
    46 _LIT(KOutputFileRef2,			"z:\\calcontest\\output2REFERENCE.txt");
       
    47 _LIT8(KImcvCRLF,				"\r\n");
       
    48 _LIT8(KOutOfRange,				" *** Out of range ***");
       
    49 
       
    50 
       
    51 //
       
    52 // Construction/Destruction
       
    53 //
       
    54 
       
    55 CConvertDates::CConvertDates() : iTest(_L("Calcon Test"))
       
    56 	{
       
    57 	}
       
    58 
       
    59 CConvertDates::~CConvertDates()
       
    60 	{
       
    61 	if (iCalendarConverter)
       
    62 		delete iCalendarConverter;
       
    63 	iInputFile.Close();
       
    64 	iOutputFile.Close();
       
    65 	iLogger.Close();
       
    66 	iRFSession.Close();
       
    67 	}
       
    68 
       
    69 CConvertDates* CConvertDates::NewL()
       
    70 	{
       
    71 	CConvertDates* self = CConvertDates::NewLC();
       
    72 	CleanupStack::Pop();
       
    73 	return self;
       
    74 	}
       
    75 
       
    76 CConvertDates* CConvertDates::NewLC()
       
    77 	{
       
    78 	CConvertDates* self = new(ELeave) CConvertDates;
       
    79 	CleanupStack::PushL(self);
       
    80 	self->ConstructL();
       
    81 	return self;
       
    82 	}
       
    83 
       
    84 void CConvertDates::ConstructL()
       
    85 	{
       
    86 	User::LeaveIfError(iRFSession.Connect());
       
    87 	iRFSession.SetSessionPath(KRoot);
       
    88 	iRFSession.MkDirAll(KCalconLogFileDirectory);			// ignore errors -- any FS errors will present themselves later
       
    89 	iRFSession.MkDirAll(KCalconDirectory);
       
    90 
       
    91 	if	(iLogger.LogValid())
       
    92 		iLogger.CloseLog();
       
    93 	TParse p;
       
    94 	p.Set(KCalconLogFile, NULL, NULL);
       
    95 	iLogger.Connect();
       
    96 	iLogger.CreateLog(KCalconLogFileDirectory, p.NameAndExt(), EFileLoggingModeOverwrite);
       
    97 	iLogger.SetDateAndTime(EFalse, EFalse);
       
    98 	iLogger.Write(KTestHeader);
       
    99 
       
   100 	iCalendarConverter=CChineseCalendarConverter::NewL();
       
   101 	}
       
   102 
       
   103 void CConvertDates::ConvertL(TInt aInput)
       
   104 	{
       
   105 	iInputFileNumber=aInput;
       
   106 
       
   107 	if (iInputFileNumber==1)
       
   108 		iTest.Printf(_L("Generating c:\\output1.txt\n"));
       
   109 	else
       
   110 		iTest.Printf(_L("Generating c:\\output2.txt\n"));
       
   111 
       
   112 	TBuf8<100> logLine;
       
   113 	TBuf<64> errBuf;
       
   114 	// open the input and output files
       
   115 	OpenFilesL();
       
   116 
       
   117 	TInt fileErr = KErrNone;
       
   118 	while(fileErr == KErrNone)
       
   119 		{
       
   120 		// read date from the input file
       
   121 		fileErr = ReadLine(iInputFile, iDateLine);
       
   122 		if	(fileErr==KErrNone)
       
   123 			{
       
   124 			// get the cal types to convert from and to
       
   125 			TRAPD(err,GetCalL(iDateLine));
       
   126 			if	(err)
       
   127 				{
       
   128 				errBuf.Format(_L("\r\nError get calendar conversion type %d\r\n"),err);
       
   129 				iLogger.Write(errBuf);
       
   130 				}
       
   131 			else
       
   132 				{
       
   133 				// do the conversion
       
   134 				TBool success=Cal1ToCal2();
       
   135 
       
   136 				// set the cal types to convert from and to
       
   137 				logLine.Zero();
       
   138 				TRAP(err,SetCalL(logLine,success));
       
   139 				if	(err)
       
   140 					{
       
   141 					errBuf.Format(_L("\r\nError set calendar conversion type %d\r\n"),err);
       
   142 					iDateLine.Append(errBuf);
       
   143 					}
       
   144 				else
       
   145 					{
       
   146 					iDateLine.Append(logLine);
       
   147 					iDateLine.Append(_L8("\r\n"));
       
   148 					iLogger.Write(iDateLine);
       
   149 					if	(iOutputFile.Write(iDateLine))
       
   150 						{
       
   151 						User::Leave(KErrCorrupt);
       
   152 						}
       
   153 					}
       
   154 				}
       
   155 			}
       
   156 		}
       
   157 	}
       
   158 
       
   159 void CConvertDates::OpenFilesL()
       
   160 	{
       
   161 	TInt err=KErrNone;
       
   162 	if (iInputFileNumber==1)
       
   163 		{
       
   164 		err = iInputFile.Open(iRFSession, KInputFile1, EFileStreamText|EFileRead|EFileShareExclusive);
       
   165 		if (err)
       
   166 			{
       
   167 			if(KErrNotFound == err)
       
   168 				{
       
   169 				iTest.Printf(KFileNotPresent,&(KInputFile1));
       
   170 				}
       
   171 			User::Leave(err);
       
   172 			}
       
   173 		err = iOutputFile.Replace(iRFSession, KOutputFile1, EFileStreamText|EFileWrite|EFileShareExclusive);
       
   174 		User::LeaveIfError(err);
       
   175 		}
       
   176 	else
       
   177 		{
       
   178 		err = iInputFile.Open(iRFSession, KInputFile2, EFileStreamText|EFileRead|EFileShareExclusive);
       
   179 		if (err)
       
   180 			{
       
   181 			if(KErrNotFound == err)
       
   182 				{
       
   183 				iTest.Printf(KFileNotPresent,&(KInputFile2));
       
   184 				}
       
   185 			User::Leave(err);
       
   186 			}
       
   187 		err = iOutputFile.Replace(iRFSession, KOutputFile2, EFileStreamText|EFileWrite|EFileShareExclusive);
       
   188 		User::LeaveIfError(err);
       
   189 		}
       
   190 	}
       
   191 
       
   192 void CConvertDates::GetCalL(TDes8& aDateToConvert)
       
   193 	{
       
   194 	TInt dateError=KErrNone;
       
   195 	TLex8 lexCycle(aDateToConvert.Mid(KCycleOffset,KCycleLen));
       
   196 	TLex8 lexYear(aDateToConvert.Mid(KYearOffset,KYearLen));
       
   197 	TLex8 lexMonth(aDateToConvert.Mid(KMnthOffset,KMonthLen));
       
   198 	TLex8 lexDay(aDateToConvert.Mid(KDyOffset,KDayLen));
       
   199 
       
   200 	// get cycle
       
   201 	if	(lexCycle.Val(iChinDateOut.iCycle) != KErrNone)
       
   202 		{
       
   203 		User::Leave(KErrCorrupt);
       
   204 		}
       
   205 	// get year
       
   206 	if	(lexYear.Val(iArithDateOut.iYear) != KErrNone)
       
   207 		{
       
   208 		User::Leave(KErrGeneral);
       
   209 		}
       
   210 	iChinDateOut.iYear = iArithDateOut.iYear;
       
   211 	// get month
       
   212 	if	(lexMonth.Val(iArithDateOut.iMonth) != KErrNone)
       
   213 		{
       
   214 		User::Leave(KErrGeneral);
       
   215 		}
       
   216 	iChinDateOut.iMonth = iArithDateOut.iMonth;
       
   217 	// get leap month
       
   218 	if	(aDateToConvert[KLeapMonthOffset] == 't')
       
   219 		{
       
   220 		iChinDateOut.iLeapMonth = ETrue;
       
   221 		}
       
   222 	else
       
   223 		{
       
   224 		iChinDateOut.iLeapMonth = EFalse;
       
   225 		}
       
   226 	// get day
       
   227 	if	(lexDay.Val(iArithDateOut.iDay) != KErrNone)
       
   228 		{
       
   229 		User::Leave(KErrCorrupt);
       
   230 		}
       
   231 	iChinDateOut.iDay = iArithDateOut.iDay;
       
   232 
       
   233 
       
   234 	switch(aDateToConvert[KFromOffset])
       
   235 		{
       
   236 	case 'g':	//iArithDateOut contains the date to convert from
       
   237 		iCalTypeFrom = EGreg;
       
   238 		break;
       
   239 	case 'c':
       
   240 		iCalTypeFrom = EChin;
       
   241 		break;
       
   242 	default:
       
   243 		dateError = KErrCorrupt;
       
   244 		break;
       
   245 		}
       
   246 
       
   247 	if	(dateError)
       
   248 		User::Leave(dateError);
       
   249 
       
   250 	switch(aDateToConvert[KToOffset])
       
   251 		{
       
   252 	case 'g':
       
   253 		iCalTypeTo = EGreg;
       
   254 		break;
       
   255 	case 'c':
       
   256 		iCalTypeTo = EChin;
       
   257 		break;
       
   258 	default:
       
   259 		break;
       
   260 		}
       
   261 	}
       
   262 
       
   263 TBool CConvertDates::Cal1ToCal2()
       
   264 	{
       
   265 	switch(iCalTypeFrom)
       
   266 		{
       
   267 	case EGreg:
       
   268 		{
       
   269 		switch(iCalTypeTo)
       
   270 			{
       
   271 		case EChin:
       
   272 			{	//iArithDateOut contains the date to convert from
       
   273 			TDateTime dt(iArithDateOut.iYear, (TMonth)(iArithDateOut.iMonth-1), iArithDateOut.iDay-1, 0,0,0,0);
       
   274 			CChineseCalendarConverter* c=iCalendarConverter;
       
   275 			TRAPD(err,c->DateTimeToChineseL(dt, iChinDateOut));
       
   276 			if (err)
       
   277 				return EFalse;
       
   278 			break;
       
   279 			}
       
   280 		default:
       
   281 			break;
       
   282 			}
       
   283 		}
       
   284 		break;
       
   285 	case EChin:
       
   286 		{
       
   287 		switch(iCalTypeTo)
       
   288 			{	//iArithDateOut contains the date to convert from
       
   289 			case EGreg:
       
   290 				{
       
   291 				CChineseCalendarConverter* c=iCalendarConverter;
       
   292 				TDateTime dt(0,EJanuary,0,0,0,0,0);
       
   293 				TRAPD(err,c->ChineseToDateTimeL(iChinDateOut, dt));
       
   294 				if (err)
       
   295 					return EFalse;
       
   296 				iArithDateOut.iYear=dt.Year();
       
   297 				iArithDateOut.iMonth=(TInt)(dt.Month())+1;
       
   298 				iArithDateOut.iDay=dt.Day()+1;
       
   299 				break;
       
   300 				}
       
   301 			default:
       
   302 				break;
       
   303 			}
       
   304 		}
       
   305 		break;
       
   306 	default:
       
   307 		break;
       
   308 		}
       
   309 	return ETrue;
       
   310 	}
       
   311 
       
   312 
       
   313 void CConvertDates::SetCalL(TDes8& aConvertedDate, TBool aSuccess)
       
   314 	{
       
   315 	TArithmeticalDate Date;
       
   316 	TChineseDate ChinDate;
       
   317 	TBuf8<KYearLen> TempDate;
       
   318 
       
   319 	switch(iCalTypeFrom)
       
   320 		{
       
   321 	case EGreg:
       
   322 		{
       
   323 		switch(iCalTypeTo)
       
   324 			{
       
   325 		case EChin:
       
   326 			ChinDate=iChinDateOut;
       
   327 			break;
       
   328 		default:
       
   329 			break;
       
   330 			}
       
   331 		}
       
   332 		break;
       
   333 	case EChin:
       
   334 		{
       
   335 		switch(iCalTypeTo)
       
   336 			{
       
   337 		case EGreg:
       
   338 			Date=iArithDateOut;
       
   339 			TempDate.Copy(_L("greg "));
       
   340 		default:
       
   341 			break;
       
   342 			}
       
   343 		}
       
   344 		break;
       
   345 	default:
       
   346 		break;
       
   347 		}
       
   348 
       
   349 	if (!aSuccess)
       
   350 		{
       
   351 		aConvertedDate.Append(KOutOfRange);
       
   352 		return;
       
   353 		}
       
   354 	
       
   355 	if	(iCalTypeTo == EChin)
       
   356 		{
       
   357 		TBuf8<10> TempFormatStr;
       
   358 
       
   359 		TempFormatStr.FillZ();
       
   360 		TempFormatStr.Format(_L8("%%0%dd"),KCycleLen);
       
   361 		TempDate.FillZ();
       
   362 		TempDate.Format(TempFormatStr,ChinDate.iCycle);
       
   363 		aConvertedDate.Append(KResult);
       
   364 		aConvertedDate.Append(TempDate);
       
   365 		aConvertedDate.Append(KSpace);
       
   366 
       
   367 		TempDate.FillZ();
       
   368 		TempDate.Format(_L8("+"));
       
   369 		aConvertedDate.Append(TempDate);
       
   370 
       
   371 		TempFormatStr.FillZ();
       
   372 		TempFormatStr.Format(_L8("%%0%dd"),KYearLen - KSignLen);
       
   373 		TempDate.FillZ();
       
   374 		TempDate.Format(TempFormatStr,ChinDate.iYear);
       
   375 		aConvertedDate.Append(TempDate);
       
   376 		aConvertedDate.Append(KSpace);
       
   377 
       
   378 		TempFormatStr.FillZ();
       
   379 		TempFormatStr.Format(_L8("%%0%dd"),KMonthLen);
       
   380 		TempDate.FillZ();
       
   381 		TempDate.Format(TempFormatStr,ChinDate.iMonth);
       
   382 		aConvertedDate.Append(TempDate);
       
   383 		aConvertedDate.Append(KSpace);
       
   384 
       
   385 		TempDate.FillZ();
       
   386 		if	(ChinDate.iLeapMonth == EFalse)
       
   387 			{
       
   388 			TempDate.Format(_L8("f"));
       
   389 			}
       
   390 		else
       
   391 			{
       
   392 			TempDate.Format(_L8("t"));
       
   393 			}
       
   394 		aConvertedDate.Append(TempDate);
       
   395 		aConvertedDate.Append(KSpace);
       
   396 
       
   397 		TempFormatStr.FillZ();
       
   398 		TempFormatStr.Format(_L8("%%0%dd"),KDayLen);
       
   399 		TempDate.FillZ();
       
   400 		TempDate.Format(TempFormatStr,ChinDate.iDay);
       
   401 		aConvertedDate.Append(TempDate);
       
   402 		}
       
   403 	else
       
   404 		{
       
   405 		TBuf8<10> TempFormatStr;
       
   406 
       
   407 		if	(Date.iYear >= KFirstConvertYear)
       
   408 			{
       
   409 			TempDate.Format(_L8("+"));
       
   410 			}
       
   411 		else
       
   412 			{
       
   413 			TempDate.Format(_L8("-"));
       
   414 			}
       
   415 		aConvertedDate.Append(KResult);
       
   416 		aConvertedDate.Append(TempDate);
       
   417 
       
   418 		TempFormatStr.FillZ();
       
   419 		TempFormatStr.Format(_L8("%%0%dd"),KYearLen - KSignLen);
       
   420 		TempDate.FillZ();
       
   421 		if	(Date.iYear >= KFirstConvertYear)
       
   422 			{
       
   423 			TempDate.Format(TempFormatStr,Date.iYear);
       
   424 			}
       
   425 		else
       
   426 			{
       
   427 			TempDate.Format(TempFormatStr,-Date.iYear);
       
   428 			}
       
   429 		aConvertedDate.Append(TempDate);
       
   430 		aConvertedDate.Append(KSpace);
       
   431 
       
   432 		TempFormatStr.FillZ();
       
   433 		TempFormatStr.Format(_L8("%%0%dd"),KMonthLen);
       
   434 		TempDate.FillZ();
       
   435 		TempDate.Format(TempFormatStr,Date.iMonth);
       
   436 		aConvertedDate.Append(TempDate);
       
   437 		aConvertedDate.Append(KSpace);
       
   438 
       
   439 		TempFormatStr.FillZ();
       
   440 		TempFormatStr.Format(_L8("%%0%dd"),KDayLen);
       
   441 		TempDate.FillZ();
       
   442 		TempDate.Format(TempFormatStr,Date.iDay);
       
   443 		aConvertedDate.Append(TempDate);
       
   444 		}
       
   445 	}
       
   446 
       
   447 void CConvertDates::SetCal16L(TDes& aConvertedDate, TBool /*aSuccess*/)
       
   448 	{
       
   449 	TArithmeticalDate Date;
       
   450 	TChineseDate ChinDate;
       
   451 	TBuf<KYearLen> TempDate;
       
   452 
       
   453 	switch(iCalTypeFrom)
       
   454 		{
       
   455 	case EGreg:
       
   456 		{
       
   457 		switch(iCalTypeTo)
       
   458 			{
       
   459 		case EChin:
       
   460 			ChinDate=iChinDateOut;
       
   461 			break;
       
   462 		default:
       
   463 			break;
       
   464 			}
       
   465 		}
       
   466 		break;
       
   467 	case EChin:
       
   468 		{
       
   469 		switch(iCalTypeTo)
       
   470 			{
       
   471 		case EGreg:
       
   472 			Date=iArithDateOut;
       
   473 			TempDate.Copy(_L("greg "));
       
   474 		default:
       
   475 			break;
       
   476 			}
       
   477 		}
       
   478 		break;
       
   479 	default:
       
   480 		break;
       
   481 		}
       
   482 
       
   483 	if	(iCalTypeTo == EChin)
       
   484 		{
       
   485 		TBuf<10> TempFormatStr;
       
   486 
       
   487 		TempFormatStr.FillZ();
       
   488 		TempFormatStr.Format(_L("%%0%dd"),KCycleLen);
       
   489 		TempDate.FillZ();
       
   490 		TempDate.Format(TempFormatStr,ChinDate.iCycle);
       
   491 		aConvertedDate.Append(_L(" Chinese "));
       
   492 		aConvertedDate.Append(TempDate);
       
   493 		aConvertedDate.Append(KSpace);
       
   494 
       
   495 		TempDate.FillZ();
       
   496 		TempDate.Format(_L("+"));
       
   497 		aConvertedDate.Append(TempDate);
       
   498 
       
   499 		TempFormatStr.FillZ();
       
   500 		TempFormatStr.Format(_L("%%0%dd"),KYearLen - KSignLen);
       
   501 		TempDate.FillZ();
       
   502 		TempDate.Format(TempFormatStr,ChinDate.iYear);
       
   503 		aConvertedDate.Append(TempDate);
       
   504 		aConvertedDate.Append(KSpace);
       
   505 
       
   506 		TempFormatStr.FillZ();
       
   507 		TempFormatStr.Format(_L("%%0%dd"),KMonthLen);
       
   508 		TempDate.FillZ();
       
   509 		TempDate.Format(TempFormatStr,ChinDate.iMonth);
       
   510 		aConvertedDate.Append(TempDate);
       
   511 		aConvertedDate.Append(KSpace);
       
   512 
       
   513 		TempDate.FillZ();
       
   514 		if	(ChinDate.iLeapMonth == EFalse)
       
   515 			{
       
   516 			TempDate.Format(_L("f"));
       
   517 			}
       
   518 		else
       
   519 			{
       
   520 			TempDate.Format(_L("t"));
       
   521 			}
       
   522 		aConvertedDate.Append(TempDate);
       
   523 		aConvertedDate.Append(KSpace);
       
   524 
       
   525 		TempFormatStr.FillZ();
       
   526 		TempFormatStr.Format(_L("%%0%dd"),KDayLen);
       
   527 		TempDate.FillZ();
       
   528 		TempDate.Format(TempFormatStr,ChinDate.iDay);
       
   529 		aConvertedDate.Append(TempDate);
       
   530 		}
       
   531 	else
       
   532 		{
       
   533 		TBuf<10> TempFormatStr;
       
   534 
       
   535 		if	(Date.iYear >= KFirstConvertYear)
       
   536 			{
       
   537 			TempDate.Format(_L("+"));
       
   538 			}
       
   539 		else
       
   540 			{
       
   541 			TempDate.Format(_L("-"));
       
   542 			}
       
   543 		aConvertedDate.Append(_L("Greg "));
       
   544 		aConvertedDate.Append(TempDate);
       
   545 
       
   546 		TempFormatStr.FillZ();
       
   547 		TempFormatStr.Format(_L("%%0%dd"),KYearLen - KSignLen);
       
   548 		TempDate.FillZ();
       
   549 		if	(Date.iYear >= KFirstConvertYear)
       
   550 			{
       
   551 			TempDate.Format(TempFormatStr,Date.iYear);
       
   552 			}
       
   553 		else
       
   554 			{
       
   555 			TempDate.Format(TempFormatStr,-Date.iYear);
       
   556 			}
       
   557 		aConvertedDate.Append(TempDate);
       
   558 		aConvertedDate.Append(KSpace);
       
   559 
       
   560 		TempFormatStr.FillZ();
       
   561 		TempFormatStr.Format(_L("%%0%dd"),KMonthLen);
       
   562 		TempDate.FillZ();
       
   563 		TempDate.Format(TempFormatStr,Date.iMonth);
       
   564 		aConvertedDate.Append(TempDate);
       
   565 		aConvertedDate.Append(KSpace);
       
   566 
       
   567 		TempFormatStr.FillZ();
       
   568 		TempFormatStr.Format(_L("%%0%dd"),KDayLen);
       
   569 		TempDate.FillZ();
       
   570 		TempDate.Format(TempFormatStr,Date.iDay);
       
   571 		aConvertedDate.Append(TempDate);
       
   572 		}
       
   573 	}
       
   574 
       
   575 //
       
   576 // Read line of test from file
       
   577 //
       
   578 TInt CConvertDates::ReadLine(RFile& aInputFile, TBuf8<KDateLen>& aDateLine)
       
   579     {
       
   580     TInt err = KErrNone;
       
   581 
       
   582     //  Read into the buffer
       
   583     TBuf8<256> buffer;              // Max read of the Read() function..    
       
   584     aDateLine.Zero();
       
   585 
       
   586     //  Get the current file position
       
   587     TInt filePos = 0;
       
   588     aInputFile.Seek(ESeekCurrent, filePos);
       
   589 
       
   590     //  Read the buffer
       
   591     err = aInputFile.Read(buffer);
       
   592     
       
   593     //end of file?
       
   594     TInt s = buffer.Length();
       
   595     if	( s == 0)
       
   596         err = KErrEof;
       
   597      
       
   598     if	(err == KErrNone)
       
   599         {
       
   600         //  Copy to the lfcr and then set the file pointer
       
   601         //  to the point after that...
       
   602         TInt pos = buffer.Find(KImcvCRLF);
       
   603         if	( pos != -1)
       
   604             {
       
   605             aDateLine.Justify(buffer, pos, ELeft, ' ');
       
   606             filePos += (pos+2);
       
   607 
       
   608             //  Set the file pointer back to after the lfcr..
       
   609             aInputFile.Seek(ESeekStart, filePos);                
       
   610             }
       
   611         //  Else fill the whole buffer 256 chars..
       
   612         else
       
   613             {
       
   614             aDateLine.Copy(buffer);
       
   615             }
       
   616         }
       
   617     return err;
       
   618     }
       
   619 
       
   620 TBool CConvertDates::IntegrityTestL()
       
   621 	{
       
   622 	iTest.Printf(_L("Integrity test:\n"));
       
   623 	iTest.Printf(_L("Stepping through every possible Chinese date in the range of the calendar\n"));
       
   624 	iTest.Printf(_L("checking that either the Chinese date is invalid, or that the\n"));
       
   625 	iTest.Printf(_L("returned Julian Day is one greater than the previous Julian Day."));
       
   626 
       
   627 	TDateTime dateTime;
       
   628 	CChineseCalendarConverter* chineseCalendar=CChineseCalendarConverter::NewL();
       
   629 
       
   630 	TChineseDate start;
       
   631 	TChineseDate finish;
       
   632 	chineseCalendar->DateRange(start, finish);
       
   633 	TDateTime startDT;
       
   634 	TDateTime finishDT;
       
   635 	chineseCalendar->DateRange(startDT, finishDT);
       
   636 
       
   637 	TBuf<KDateLen>	outputBuf;	
       
   638 	iCalTypeFrom=EChin;
       
   639 	iCalTypeTo=EGreg;
       
   640 	iArithDateOut.iDay=startDT.Day()+1;
       
   641 	iArithDateOut.iMonth=(TInt)startDT.Month()+1;
       
   642 	iArithDateOut.iYear=startDT.Year();
       
   643 	SetCal16L(outputBuf);
       
   644 	iChinDateOut=start;
       
   645 	iCalTypeFrom=EGreg;
       
   646 	iCalTypeTo=EChin;
       
   647 	SetCal16L(outputBuf);
       
   648 	iTest.Printf(_L("\n\nCalendar start date\n"));
       
   649 	iTest.Printf(outputBuf);
       
   650 
       
   651 	chineseCalendar->DateTimeToChineseL(startDT,start);
       
   652 	TReal startJulian=chineseCalendar->JulianDate();
       
   653 	chineseCalendar->DateTimeToChineseL(finishDT,finish);
       
   654 	TReal finishJulian=chineseCalendar->JulianDate();
       
   655 	iTest.Printf(_L("\nJulian: %f"),startJulian);
       
   656 
       
   657 	chineseCalendar->DateRange(start, finish);
       
   658 	//can check here that start & finish do not change
       
   659 
       
   660 	outputBuf.SetLength(0);
       
   661 	iCalTypeFrom=EChin;
       
   662 	iCalTypeTo=EGreg;
       
   663 	iArithDateOut.iDay=finishDT.Day()+1;
       
   664 	iArithDateOut.iMonth=(TInt)finishDT.Month()+1;
       
   665 	iArithDateOut.iYear=finishDT.Year();
       
   666 	SetCal16L(outputBuf);
       
   667 	iChinDateOut=finish;
       
   668 	iCalTypeFrom=EGreg;
       
   669 	iCalTypeTo=EChin;
       
   670 	SetCal16L(outputBuf);
       
   671 	iTest.Printf(_L("\n\nCalendar end date\n"));
       
   672 	iTest.Printf(outputBuf);
       
   673 	iTest.Printf(_L("\nJulian: %f\n\n"),finishJulian);
       
   674 
       
   675 	start.iCycle--; //check that errors are return for date earlier than the lower bound
       
   676 	finish.iCycle+=2;//check that errors are return for date later than the upper bound
       
   677 
       
   678 	TInt err=KErrNone;
       
   679 
       
   680 	do
       
   681 		{
       
   682 		NextDate(start);
       
   683 		TRAP(err,chineseCalendar->ChineseToDateTimeL(start, dateTime));
       
   684 		}
       
   685 	while (err==KErrArgument);
       
   686 
       
   687 	TReal currentJulian=chineseCalendar->JulianDate();
       
   688 
       
   689 	iState=EChangeDay;
       
   690 
       
   691 	TDateTime lowerBound;
       
   692 	TDateTime upperBound;
       
   693 	chineseCalendar->DateRange(lowerBound, upperBound);
       
   694 	TChineseDate lowerBoundChin;
       
   695 	TChineseDate upperBoundChin;
       
   696 	chineseCalendar->DateRange(lowerBoundChin, upperBoundChin);
       
   697 	TRAP(err,chineseCalendar->ChineseToDateTimeL(upperBoundChin, dateTime));
       
   698 
       
   699 	while (start.iCycle<finish.iCycle)
       
   700 		{
       
   701 		NextDate(start);
       
   702 		TRAP(err,chineseCalendar->ChineseToDateTimeL(start, dateTime));
       
   703 		
       
   704 		if (err==KErrNone)
       
   705 			{
       
   706 			TReal julian=chineseCalendar->JulianDate();
       
   707 		
       
   708 			if (julian!=++currentJulian)
       
   709 				{
       
   710 				iTest.Printf(_L("%f %f"),julian,currentJulian);
       
   711 
       
   712 				delete chineseCalendar;
       
   713 				return EFalse;
       
   714 				}
       
   715 			}
       
   716 
       
   717 		//	chineseCalendar.GetDate(checkDate);
       
   718 		//	if (!(checkDate==start))
       
   719 		//		User::Invariant();
       
   720 		}
       
   721 	delete chineseCalendar;
       
   722 	return ETrue;
       
   723 	}
       
   724 
       
   725 void CConvertDates::NextDate(TChineseDate& aDate)
       
   726 	{
       
   727 top:
       
   728 	TBool cont=EFalse;
       
   729 
       
   730 	switch (iState)
       
   731 		{
       
   732 		case EChangeDay:
       
   733 			if (aDate.iDay==30)
       
   734 				{
       
   735 				iState=EChangeMonth;
       
   736 				aDate.iDay=1;
       
   737 				cont=ETrue;
       
   738 				}
       
   739 			else
       
   740 				{
       
   741 				aDate.iDay++;
       
   742 				}
       
   743 			break;
       
   744 		case EChangeMonth:
       
   745 			if (aDate.iLeapMonth)
       
   746 				{
       
   747 				aDate.iLeapMonth=EFalse;
       
   748 				if (aDate.iMonth==12)
       
   749 					{
       
   750 					iState=EChangeYear;
       
   751 					aDate.iMonth=1;
       
   752 					cont=ETrue;
       
   753 					}
       
   754 				else
       
   755 					{
       
   756 					aDate.iMonth++;
       
   757 					iState=EChangeDay;
       
   758 					}
       
   759 				}
       
   760 			else
       
   761 				{
       
   762 				aDate.iLeapMonth=ETrue;
       
   763 				iState=EChangeDay;
       
   764 				}
       
   765 			break;
       
   766 		case EChangeYear:
       
   767 			if (aDate.iYear==60)
       
   768 				{
       
   769 				iState=EChangeCycle;
       
   770 				aDate.iYear=1;
       
   771 				cont=ETrue;
       
   772 				}
       
   773 			else
       
   774 				{
       
   775 				aDate.iYear++;
       
   776 				iState=EChangeDay;
       
   777 				}
       
   778 			break;
       
   779 		case EChangeCycle:
       
   780 			aDate.iCycle++;
       
   781 			iState=EChangeDay;
       
   782 			break;
       
   783 		default:
       
   784 			User::Invariant();
       
   785 		}
       
   786 	if (cont) goto top;
       
   787 	}
       
   788 
       
   789 				
       
   790 
       
   791 //
       
   792 // Construction/Destruction
       
   793 //
       
   794 
       
   795 //------------------------------------------------------
       
   796 // Class:       TArithmeticalDate
       
   797 // Function:    TArithmeticalDate
       
   798 // Arguments:   None
       
   799 //
       
   800 // Comments:    Constructor
       
   801 //
       
   802 // Return:      None
       
   803 //------------------------------------------------------
       
   804 TArithmeticalDate::TArithmeticalDate()
       
   805 	{
       
   806 	}
       
   807 
       
   808 //------------------------------------------------------
       
   809 // Class:       TArithmeticalDate
       
   810 // Function:    operator ==
       
   811 // Arguments:   const TArithmeticalDate &
       
   812 //
       
   813 // Comments:    overloaded == operator
       
   814 //
       
   815 // Return:      TBool - ETrue if both the TArithmeticalDate 
       
   816 //				classes are the same
       
   817 //------------------------------------------------------
       
   818 TBool TArithmeticalDate::operator ==(const TArithmeticalDate &aDate) const
       
   819 	{
       
   820 	TBool rtn = ETrue;
       
   821 	if(iYear != aDate.iYear)
       
   822 		{
       
   823 		rtn = EFalse;
       
   824 		}
       
   825 	if(iMonth != aDate.iMonth)
       
   826 		{
       
   827 		rtn = EFalse;
       
   828 		}
       
   829 	if(iDay != aDate.iDay)
       
   830 		{
       
   831 		rtn = EFalse;
       
   832 		}
       
   833 	return rtn;
       
   834 	}
       
   835 
       
   836 TBool CConvertDates::CompareL()
       
   837 	{
       
   838 	iInputFile.Close();
       
   839 	iOutputFile.Close();
       
   840 	
       
   841 	TBool ret=ETrue;
       
   842 	TInt err=KErrNone;
       
   843 
       
   844 	if (iInputFileNumber==1)
       
   845 		{
       
   846 		err = iOutputFile.Open(iRFSession, KOutputFile1, EFileStreamText|EFileRead|EFileShareExclusive);
       
   847 		if (err)
       
   848 			{
       
   849 			if(KErrNotFound == err)
       
   850 				{
       
   851 				iTest.Printf(KFileNotPresent,&(KOutputFile1));
       
   852 				}
       
   853 			User::Leave(err);
       
   854 			}
       
   855 		err = iOutputFileRef.Open(iRFSession, KOutputFileRef1, EFileStreamText|EFileRead|EFileShareExclusive);
       
   856 		if (err)
       
   857 			{
       
   858 			if(KErrNotFound == err)
       
   859 				{
       
   860 				iTest.Printf(KFileNotPresent,&(KOutputFileRef1));
       
   861 				}
       
   862 			User::Leave(err);
       
   863 			}
       
   864 		}
       
   865 	else
       
   866 		{
       
   867 		err = iOutputFile.Open(iRFSession, KOutputFile2, EFileStreamText|EFileRead|EFileShareExclusive);
       
   868 		if (err)
       
   869 			{
       
   870 			if(KErrNotFound == err)
       
   871 				{
       
   872 				iTest.Printf(KFileNotPresent,&(KOutputFile2));
       
   873 				}
       
   874 			User::Leave(err);
       
   875 			}
       
   876 		err = iOutputFileRef.Open(iRFSession, KOutputFileRef2, EFileStreamText|EFileRead|EFileShareExclusive);
       
   877 		if (err)
       
   878 			{
       
   879 			if(KErrNotFound == err)
       
   880 				{
       
   881 				iTest.Printf(KFileNotPresent,&(KOutputFileRef2));
       
   882 				}
       
   883 			User::Leave(err);
       
   884 			}
       
   885 		}
       
   886 	
       
   887 	TBuf8<KDateLen>	outputBuf;
       
   888 	TBuf8<KDateLen>	outputBufRef;
       
   889 
       
   890 	while (err==KErrNone)
       
   891 		{
       
   892 		err=ReadLine(iOutputFile,outputBuf);
       
   893 		if (err==KErrNone)
       
   894 			{
       
   895 			ReadLine(iOutputFileRef,outputBufRef);
       
   896 			TInt m=outputBuf.FindF(KOutOfRange);
       
   897 			if (m==KErrNotFound)
       
   898 				{
       
   899 				if (outputBuf.CompareF(outputBufRef)!=0)
       
   900 					{
       
   901 					ret=EFalse;
       
   902 					break;
       
   903 					}	
       
   904 				}
       
   905 			}
       
   906 		}
       
   907 	iOutputFile.Close();
       
   908 	iOutputFileRef.Close();
       
   909 	return ret;
       
   910 	}