|
1 /* |
|
2 * Copyright (c) 2002 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: Implementation of text recognizer class. |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 #include <apmrec.h> |
|
20 #include <apmstd.h> |
|
21 #include <e32svr.h> |
|
22 #include <implementationproxy.h> |
|
23 |
|
24 #include "TxtRecog.h" |
|
25 #include <barsread.h> |
|
26 #include <eikenv.h> |
|
27 #include <f32file.h> |
|
28 #include <data_caging_path_literals.hrh> |
|
29 |
|
30 #include <TxtRecog.rsg> |
|
31 |
|
32 |
|
33 // CONSTANTS |
|
34 _LIT8(KTextMimeType, "text/plain"); |
|
35 const TInt KMaxBufferSize(512); |
|
36 _LIT(KTextExtension, ".txt"); |
|
37 |
|
38 |
|
39 _LIT( KResourceFile, "TxtRecog.rsc" ); |
|
40 |
|
41 |
|
42 |
|
43 // ============================ MEMBER FUNCTIONS =============================== |
|
44 |
|
45 // ----------------------------------------------------------------------------- |
|
46 // CKthreeTextRecognizer::CKthreeTextRecognizer |
|
47 // ----------------------------------------------------------------------------- |
|
48 // |
|
49 CKthreeTextRecognizer::CKthreeTextRecognizer() |
|
50 :CApaDataRecognizerType( |
|
51 TDataType(KTextMimeType).Uid(), |
|
52 CApaDataRecognizerType::ELow ) |
|
53 { |
|
54 iCountDataTypes = 1; |
|
55 } |
|
56 // ----------------------------------------------------------------------------- |
|
57 // CKthreeTextRecognizer::NewL |
|
58 // ----------------------------------------------------------------------------- |
|
59 // |
|
60 CKthreeTextRecognizer* CKthreeTextRecognizer::NewL() |
|
61 { |
|
62 CKthreeTextRecognizer* self = new(ELeave) CKthreeTextRecognizer(); |
|
63 CleanupStack::PushL( self ); |
|
64 self->ConstructL(); |
|
65 CleanupStack::Pop(); |
|
66 return self; |
|
67 } |
|
68 // ----------------------------------------------------------------------------- |
|
69 // CKthreeTextRecognizer::ConstructL() |
|
70 // ----------------------------------------------------------------------------- |
|
71 // |
|
72 void CKthreeTextRecognizer::ConstructL() |
|
73 { |
|
74 TInt err = 0; |
|
75 RFs fs; |
|
76 RResourceFile resourceFile; |
|
77 TResourceReader theReader; |
|
78 TBuf<254> resourceFileName; |
|
79 |
|
80 err = fs.Connect(); |
|
81 |
|
82 if (err) |
|
83 { |
|
84 fs.Close(); |
|
85 User::LeaveIfError( err ); |
|
86 } |
|
87 |
|
88 TParse* fp = new(ELeave) TParse(); |
|
89 fp->Set(KResourceFile, &KDC_APP_RESOURCE_DIR, NULL); |
|
90 |
|
91 |
|
92 resourceFileName.Copy(fp ->FullName()); |
|
93 TPtrC driveLetter = TParsePtrC( RProcess().FileName() ).Drive(); |
|
94 |
|
95 TFileName fileName( driveLetter ); |
|
96 fileName.Append( resourceFileName ); |
|
97 |
|
98 TRAP(err, resourceFile.OpenL(fs, fileName)); |
|
99 if (err) |
|
100 { |
|
101 fs.Close(); |
|
102 delete fp; |
|
103 fp =NULL; |
|
104 User::LeaveIfError( err ); |
|
105 } |
|
106 |
|
107 delete fp; |
|
108 fp =NULL; |
|
109 |
|
110 HBufC8* res = resourceFile.AllocReadL( R_BLACK_LIST_EXTENSIONS_LIST ); |
|
111 theReader.SetBuffer(res); |
|
112 |
|
113 TInt count = theReader.ReadInt16() ; |
|
114 |
|
115 for( TInt i = 0 ; i < count ; i++) |
|
116 { |
|
117 HBufC* blExt = theReader.ReadHBufCL() ; |
|
118 iResListData.Append( blExt ); |
|
119 } |
|
120 |
|
121 |
|
122 delete res; |
|
123 res = NULL; |
|
124 resourceFile.Close(); |
|
125 fs.Close(); |
|
126 |
|
127 |
|
128 } |
|
129 |
|
130 |
|
131 // ----------------------------------------------------------------------------- |
|
132 // CKthreeTextRecognizer::PreferredBufSize |
|
133 // ----------------------------------------------------------------------------- |
|
134 // |
|
135 CKthreeTextRecognizer::~CKthreeTextRecognizer() |
|
136 { |
|
137 //iResListData.Close(); |
|
138 iResListData.ResetAndDestroy(); |
|
139 } |
|
140 // ----------------------------------------------------------------------------- |
|
141 // CKthreeTextRecognizer::PreferredBufSize |
|
142 // ----------------------------------------------------------------------------- |
|
143 // |
|
144 TUint CKthreeTextRecognizer::PreferredBufSize() |
|
145 { |
|
146 return KMaxBufferSize; |
|
147 } |
|
148 |
|
149 // ----------------------------------------------------------------------------- |
|
150 // CKthreeTextRecognizer::SupportedDataTypeL |
|
151 // ----------------------------------------------------------------------------- |
|
152 // |
|
153 TDataType CKthreeTextRecognizer::SupportedDataTypeL(TInt ) const |
|
154 { |
|
155 return TDataType(KTextMimeType); |
|
156 } |
|
157 |
|
158 // ----------------------------------------------------------------------------- |
|
159 // CKthreeTextRecognizer::DoRecognizeL |
|
160 // ----------------------------------------------------------------------------- |
|
161 // |
|
162 void CKthreeTextRecognizer::DoRecognizeL( |
|
163 const TDesC& aName, |
|
164 const TDesC8& aBuffer ) |
|
165 { |
|
166 iConfidence = ENotRecognized; |
|
167 TBool isTextExtension(EFalse); |
|
168 |
|
169 |
|
170 /* If extension is .txt and the buffer is empty return Probable */ |
|
171 if ( aName.Length() > KTextExtension().Length() ) |
|
172 { |
|
173 TPtrC extension = aName.Right(KTextExtension().Length()); |
|
174 |
|
175 if (IsInBlackListL(extension)) |
|
176 { |
|
177 iConfidence = ENotRecognized; |
|
178 return ; |
|
179 } |
|
180 |
|
181 if ( extension.CompareF(KTextExtension) == 0 ) |
|
182 { |
|
183 isTextExtension = ETrue; |
|
184 iDataType = TDataType(KTextMimeType); |
|
185 iConfidence = EProbable; |
|
186 /* There may be a buffer of characters available for |
|
187 us to scan through and we might get more confident */ |
|
188 } |
|
189 } |
|
190 |
|
191 const TInt size(aBuffer.Size()); |
|
192 |
|
193 /* This is the second call where we also get data in the buffer */ |
|
194 if ( size > 0 ) |
|
195 { |
|
196 /* If BOM and .txt -> Certain. If BOM only -> Probable */ |
|
197 if ( size > 1 && |
|
198 (( aBuffer[0] == 0xfe && aBuffer[1] == 0xff ) || |
|
199 ( aBuffer[0] == 0xff && aBuffer[1] == 0xfe ) )) |
|
200 { |
|
201 iDataType = TDataType(KTextMimeType); |
|
202 iConfidence = isTextExtension ? ECertain : EProbable; |
|
203 } |
|
204 |
|
205 /* If No BOM. It is ASCII */ |
|
206 else |
|
207 { |
|
208 /* See if all characters are valid ASCII. Any invalid ASCII chars make this non UNICODE file not a text file */ |
|
209 TInt i(0); |
|
210 while ( i < size && !IsInvalidChar(aBuffer[i])) |
|
211 { |
|
212 i++; |
|
213 } |
|
214 if ( i == size )// chars in aBuffer are all valid. |
|
215 { |
|
216 iDataType = TDataType(KTextMimeType); |
|
217 |
|
218 if ( i >= KMaxBufferSize ) // we were given at least as many samples as we asked for |
|
219 { |
|
220 if (isTextExtension) |
|
221 { |
|
222 iConfidence = ECertain; // No BOM but .txt and losts of ascii characters |
|
223 } |
|
224 else |
|
225 { |
|
226 iConfidence = EProbable; // No BOM or .txt but lots of ascii characters |
|
227 } |
|
228 } |
|
229 else //not enough samples to be certain |
|
230 { |
|
231 if (isTextExtension) |
|
232 { |
|
233 iConfidence = EProbable; // No BOM but .txt and a few ascii characters |
|
234 } |
|
235 else |
|
236 { |
|
237 iConfidence = EPossible; // No BOM or .txt but some ascii characters |
|
238 } |
|
239 } |
|
240 } //end all valid chars |
|
241 } //end no BOM |
|
242 } //end size > 0 |
|
243 } |
|
244 // ----------------------------------------------------------------------------- |
|
245 // CKthreeTextRecognizer::IsInvalidChar |
|
246 // ----------------------------------------------------------------------------- |
|
247 // |
|
248 TBool CKthreeTextRecognizer::IsInvalidChar( const TUint8& aByte ) |
|
249 { |
|
250 return aByte == 0x7f || aByte <= 0x07 || |
|
251 ( aByte >= 0x0e && aByte <= 0x1f && aByte != 0x1b ); |
|
252 } |
|
253 |
|
254 // ----------------------------------------------------------------------------- |
|
255 // CKthreeTextRecognizer::IsInBlackListL |
|
256 // ----------------------------------------------------------------------------- |
|
257 // |
|
258 TBool CKthreeTextRecognizer::IsInBlackListL(TPtrC aExt) |
|
259 |
|
260 { |
|
261 TBool ret = EFalse; |
|
262 |
|
263 TInt count = iResListData.Count(); |
|
264 |
|
265 for ( TInt index = 0; index < count; index++) |
|
266 { |
|
267 if (aExt.CompareF(*iResListData[index]) == 0 ) |
|
268 { |
|
269 ret = ETrue ; |
|
270 break ; |
|
271 } |
|
272 } |
|
273 return ret; |
|
274 } |
|
275 |
|
276 |
|
277 // ========================== OTHER EXPORTED FUNCTIONS ========================= |
|
278 |
|
279 // ----------------------------------------------------------------------------- |
|
280 // CApaDataRecognizerType* CreateRecognizer |
|
281 // The gate function - ordinal 1 |
|
282 // ----------------------------------------------------------------------------- |
|
283 // |
|
284 #ifdef EKA2 |
|
285 CApaDataRecognizerType* CKthreeTextRecognizer::CreateRecognizerL() |
|
286 { |
|
287 //return new (ELeave) CKthreeTextRecognizer(); |
|
288 return CKthreeTextRecognizer::NewL(); |
|
289 } |
|
290 |
|
291 const TImplementationProxy ImplementationTable[] = |
|
292 { |
|
293 IMPLEMENTATION_PROXY_ENTRY(KTxtRecognizerImplementationUid, |
|
294 CKthreeTextRecognizer::CreateRecognizerL) |
|
295 }; |
|
296 |
|
297 EXPORT_C const TImplementationProxy* ImplementationGroupProxy(TInt& aTableCount) |
|
298 { |
|
299 aTableCount = sizeof(ImplementationTable) / sizeof(TImplementationProxy); |
|
300 return ImplementationTable; |
|
301 } |
|
302 |
|
303 #else |
|
304 EXPORT_C CApaDataRecognizerType* CreateRecognizer() |
|
305 { |
|
306 CApaDataRecognizerType* thing=new CKthreeTextRecognizer(); |
|
307 return thing; // NULL if new failed |
|
308 } |
|
309 |
|
310 #endif |
|
311 // ----------------------------------------------------------------------------- |
|
312 // E32Dll |
|
313 // DLL entry point |
|
314 // ----------------------------------------------------------------------------- |
|
315 // |
|
316 |
|
317 #ifndef EKA2 |
|
318 GLDEF_C TInt E32Dll(TDllReason /*aReason*/) |
|
319 { |
|
320 return KErrNone; |
|
321 } |
|
322 #endif |
|
323 |
|
324 // End of File |