textinput/ptienginev2/src/PtiUserDictionary.cpp
changeset 0 eb1f2e154e89
equal deleted inserted replaced
-1:000000000000 0:eb1f2e154e89
       
     1 /*
       
     2 * Copyright (c) 2003 Nokia Corporation and/or its subsidiary(-ies). 
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "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 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:   Predective text input engine user dictionary 
       
    15 *               class implementation.
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 #include <s32file.h>
       
    21 #include "PtiUserDictionary.h"
       
    22 
       
    23 //                                 Core UID          Symbol class      CRC checksum
       
    24 #define UDB_HEADER_SIZE (TInt32)(sizeof(iCoreUID) + sizeof(TUint32) + sizeof(TUint32))
       
    25 
       
    26 // ---------------------------------------------------------------------------
       
    27 // CPtiUserDictionary::NewL
       
    28 // 
       
    29 // ---------------------------------------------------------------------------
       
    30 //
       
    31 EXPORT_C CPtiUserDictionary* CPtiUserDictionary::NewL(TInt aMaxSize)
       
    32 	{
       
    33 	CPtiUserDictionary* ud = new (ELeave) CPtiUserDictionary();
       
    34 	CleanupStack::PushL( ud );
       
    35 	ud->ConstructL(aMaxSize);
       
    36 	CleanupStack::Pop(); // ud
       
    37 	return ud;
       
    38 	}
       
    39 	
       
    40 // ---------------------------------------------------------------------------
       
    41 // CPtiUserDictionary::NewL
       
    42 // 
       
    43 // ---------------------------------------------------------------------------
       
    44 //
       
    45 EXPORT_C CPtiUserDictionary* CPtiUserDictionary::NewL(TDesC& aFileName, TInt aMaxSize)
       
    46 	{
       
    47 	CPtiUserDictionary* ud = new (ELeave) CPtiUserDictionary();
       
    48 	CleanupStack::PushL( ud );
       
    49 	ud->ConstructL(aFileName, aMaxSize);
       
    50 	CleanupStack::Pop(); // ud
       
    51 	return ud;
       
    52 	}
       
    53 
       
    54 // ---------------------------------------------------------------------------
       
    55 // CPtiUserDictionary::CPtiUserDictionary
       
    56 // 
       
    57 // ---------------------------------------------------------------------------
       
    58 //
       
    59 EXPORT_C CPtiUserDictionary::CPtiUserDictionary()
       
    60 	{
       
    61 	}
       
    62 
       
    63 // ---------------------------------------------------------------------------
       
    64 // CPtiUserDictionary::~CPtiUserDictionary
       
    65 // 
       
    66 // ---------------------------------------------------------------------------
       
    67 //	
       
    68 EXPORT_C CPtiUserDictionary::~CPtiUserDictionary()
       
    69 	{
       
    70 	delete iData;
       
    71 	}
       
    72 
       
    73 // ---------------------------------------------------------------------------
       
    74 // CPtiUserDictionary::ConstructL
       
    75 // 
       
    76 // ---------------------------------------------------------------------------
       
    77 //
       
    78 void CPtiUserDictionary::ConstructL(TInt aMaxSize)
       
    79 	{
       
    80 	iData = HBufC8::NewL(aMaxSize);
       
    81 	TPtr8 bufP8( iData->Des() );
       
    82 	bufP8.SetLength(aMaxSize);
       
    83 	bufP8.FillZ();
       
    84 	}
       
    85 
       
    86 // ---------------------------------------------------------------------------
       
    87 // CPtiUserDictionary::ConstructL
       
    88 // 
       
    89 // ---------------------------------------------------------------------------
       
    90 //
       
    91 void CPtiUserDictionary::ConstructL(TDesC& aFileName, TInt aMaxSize)
       
    92 	{
       
    93 	User::LeaveIfError(OpenL(aFileName, aMaxSize));
       
    94 	iFileName.Copy(aFileName);
       
    95 	}
       
    96 
       
    97 // ---------------------------------------------------------------------------
       
    98 // CPtiUserDictionary::OpenL
       
    99 // 
       
   100 // ---------------------------------------------------------------------------
       
   101 //
       
   102 EXPORT_C TInt CPtiUserDictionary::OpenL(TDesC& aFileName, TInt aMaxSize)
       
   103 	{
       
   104 	iFileName.Copy(aFileName);
       
   105 
       
   106 	RFs fsSession;
       
   107 	User::LeaveIfError(fsSession.Connect()); // Start session
       
   108 	CleanupClosePushL(fsSession);
       
   109 
       
   110 	TEntry entry;
       
   111 	User::LeaveIfError(fsSession.Entry(aFileName, entry));
       
   112 
       
   113 	RFileReadStream inStream;
       
   114 	inStream.PushL();
       
   115 	User::LeaveIfError(inStream.Open(fsSession, aFileName, EFileRead|EFileShareAny));
       
   116 
       
   117 	if (entry.iSize > aMaxSize + UDB_HEADER_SIZE)
       
   118 		{
       
   119 		User::Leave(KErrCorrupt);
       
   120 		}
       
   121 		
       
   122 	delete iData;
       
   123 	iData = NULL;
       
   124 	iData = HBufC8::NewL(aMaxSize);
       
   125 	TPtr8 dataDes = iData->Des();
       
   126 	dataDes.SetLength(aMaxSize);
       
   127 	dataDes.FillZ();		
       
   128 	
       
   129 	// First read header data.
       
   130 	iCoreUID = inStream.ReadInt32L();
       
   131 	iSymbolClass = inStream.ReadInt32L();
       
   132 	
       
   133 	// Then read and check actual data if it is there.
       
   134 	if (entry.iSize > sizeof(TInt32) + sizeof(TInt32))	
       
   135 		{	
       
   136 		TUint32 crc = inStream.ReadInt32L();
       
   137 		inStream.ReadL(dataDes, entry.iSize - UDB_HEADER_SIZE);		
       
   138 		
       
   139 		TUint32 dataCrc = 0;
       
   140 		Mem::Crc32(dataCrc, Data().Ptr(), Data().Length());
       
   141 		if (dataCrc != crc)
       
   142 			{
       
   143 			// Crc check failed, something wierd going on, initialize data and leave.
       
   144 			dataDes.FillZ();
       
   145 			User::Leave(KErrCorrupt);	
       
   146 			}		
       
   147 		iChecksum = crc;			
       
   148 		}
       
   149 		
       
   150 	CleanupStack::PopAndDestroy(2); // fsSession, inStream
       
   151 	
       
   152 	return KErrNone;
       
   153 	}
       
   154 
       
   155 // ---------------------------------------------------------------------------
       
   156 // CPtiUserDictionary::WriteL
       
   157 // 
       
   158 // ---------------------------------------------------------------------------
       
   159 //
       
   160 EXPORT_C TInt CPtiUserDictionary::WriteL(TDesC& aFileName)
       
   161 	{
       
   162 	RFs fsSession;
       
   163 	User::LeaveIfError(fsSession.Connect()); // Start session
       
   164 	CleanupClosePushL(fsSession);
       
   165 
       
   166 	RFileWriteStream outStream;
       
   167 	outStream.PushL();
       
   168 	User::LeaveIfError(outStream.Open(fsSession, aFileName, EFileWrite|EFileShareAny));
       
   169 	
       
   170 	// First write header data.	
       
   171 	outStream.WriteInt32L(iCoreUID);
       
   172 	outStream.WriteInt32L(iSymbolClass);
       
   173 	TUint32 c = 0;		
       
   174 	Mem::Crc32(c, Data().Ptr(), Data().Length());
       
   175 	outStream.WriteInt32L(c);
       
   176 	
       
   177 	// Then write actual data.
       
   178 	outStream.WriteL(Data().Ptr(), Data().Length());	
       
   179 		
       
   180 	outStream.CommitL();	
       
   181 	CleanupStack::PopAndDestroy(2); // fsSession, outStream  	
       
   182 		
       
   183 	iChecksum = c;
       
   184 
       
   185 	return KErrNone;
       
   186 	}
       
   187 
       
   188 // ---------------------------------------------------------------------------
       
   189 // CPtiUserDictionary::WriteL
       
   190 // 
       
   191 // ---------------------------------------------------------------------------
       
   192 //
       
   193 EXPORT_C TInt CPtiUserDictionary::WriteL()
       
   194 	{
       
   195 	if (iFileName.Length() == 0)
       
   196 		{
       
   197 		return KErrCorrupt;
       
   198 		}
       
   199 	
       
   200 	return WriteL( iFileName );
       
   201 	}
       
   202 
       
   203 // ---------------------------------------------------------------------------
       
   204 // CPtiUserDictionary::SpaceLeft
       
   205 // 
       
   206 // ---------------------------------------------------------------------------
       
   207 //
       
   208 EXPORT_C TInt CPtiUserDictionary::SpaceLeft() const	
       
   209 	{
       
   210 	return -1;
       
   211 	}
       
   212 
       
   213 // ---------------------------------------------------------------------------
       
   214 // CPtiUserDictionary::SetDataL
       
   215 // 
       
   216 // ---------------------------------------------------------------------------
       
   217 //
       
   218 EXPORT_C void CPtiUserDictionary::SetDataL(TAny* aData, TInt aLength)
       
   219 	{
       
   220 	// Keep possible existing maxlength value, because it is coming from 
       
   221 	// host.
       
   222 	TInt maxLength = (iData ? iData->Des().MaxLength() : aLength);
       
   223 
       
   224 	delete iData;
       
   225 	iData = NULL;
       
   226 	iData = HBufC8::NewL(maxLength);
       
   227 	TPtr8 dataP((TUint8*)aData, maxLength);
       
   228 	dataP.Set((TUint8*)aData, aLength, maxLength);
       
   229 	(*iData) = dataP;
       
   230 	}
       
   231 
       
   232 // End of file
       
   233