notepad/notepad1/LibSrc/NpdUtil.cpp
changeset 0 f979ecb2b13e
equal deleted inserted replaced
-1:000000000000 0:f979ecb2b13e
       
     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 Static Utility functions of Notepad Library.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include <sysutil.h>
       
    21 #include <AknGlobalNote.h>
       
    22 #include <coemain.h>
       
    23 #include <txtetext.h>
       
    24 #include <s32file.h>
       
    25 #include <charconv.h>
       
    26 #include <NpdLib.rsg>
       
    27 #include "NpdUtil.h"
       
    28 #include <StringLoader.h>
       
    29 
       
    30 static const TInt KMaxSampleLengthForAutoDetection(1024);
       
    31 //static const TInt KDbCorruptedMessageMaxLength(256);
       
    32 //0x21B2 is the Linefeed symbol
       
    33 //const TChar linefeed(0x21B2);
       
    34 // ============================ MEMBER FUNCTIONS ===============================
       
    35 
       
    36 // -----------------------------------------------------------------------------
       
    37 // NotepadUtil::LeaveIfCriticalLevelL
       
    38 // -----------------------------------------------------------------------------
       
    39 //
       
    40 void NotepadUtil::LeaveIfCriticalLevelL( 
       
    41     CCoeEnv& aCoeEnv,
       
    42     TInt aBytesToWrite )
       
    43     {
       
    44     if ( SysUtil::FFSSpaceBelowCriticalLevelL(&(aCoeEnv.FsSession()), 
       
    45             aBytesToWrite) )
       
    46         {
       
    47         User::Leave(KErrDiskFull);
       
    48         }
       
    49     }
       
    50 
       
    51 // -----------------------------------------------------------------------------
       
    52 // NotepadUtil::ShowCorruptedNoteL
       
    53 // -----------------------------------------------------------------------------
       
    54 //
       
    55 void NotepadUtil::ShowCorruptedNoteL(CCoeEnv& /*aCoeEnv*/)
       
    56     {
       
    57     CAknGlobalNote* note = CAknGlobalNote::NewLC();
       
    58     
       
    59     HBufC* buf = StringLoader::LoadLC(R_NOTEPAD_QTN_MEMLO_DATABASE_CORRUPTED);
       
    60     note->ShowNoteL(EAknGlobalInformationNote, *buf);
       
    61     CleanupStack::PopAndDestroy(buf); 
       
    62     CleanupStack::PopAndDestroy(note); // note
       
    63     }
       
    64 
       
    65 // -----------------------------------------------------------------------------
       
    66 // NotepadUtil::IsEmpty
       
    67 // -----------------------------------------------------------------------------
       
    68 //
       
    69 TBool NotepadUtil::IsEmpty(const TDesC& aText)
       
    70     {
       
    71     const TChar linefeed(0x21B2);
       
    72     for ( TInt i(0); i < aText.Length(); i++ )
       
    73         {
       
    74         TChar c(aText[i]);
       
    75         if ( !c.IsControl() && !c.IsSpace()&& c != linefeed )
       
    76             {
       
    77             return EFalse;
       
    78             }
       
    79         }
       
    80     return ETrue;
       
    81     }
       
    82 
       
    83 // -----------------------------------------------------------------------------
       
    84 // NotepadUtil::AppendLabel
       
    85 // Perl like semantics:
       
    86 //     $x = aText;
       
    87 //     if ( aRemoveHeadingSpaces ) { $x =~ s/^\s+//g; }
       
    88 //     else { $x =~ s/\s/ /g; }
       
    89 //     aBuf .= $x;
       
    90 // Because when the caption contains edwin newline code (0x2029)
       
    91 // 'pinboard' shows only last one line in it's link name form,
       
    92 // we must replace Edwin newline code (0x2029) with ' '.
       
    93 //
       
    94 //               IsSpace?   IsControl?  IsHead? -> Write
       
    95 // -----------------------------------------------------
       
    96 // \n,\t,\r,' '    Yes         Yes        Yes      nothing 
       
    97 //                 Yes         Yes        No        ' '
       
    98 // other control   No          Yes        Yes      nothing
       
    99 //   characters    No          Yes        No       nothing
       
   100 // normal chars    No          No         Yes      the char
       
   101 //                 No          No         No       the char
       
   102 // -----------------------------------------------------------------------------
       
   103 //
       
   104 void NotepadUtil::AppendLabel(
       
   105     TDes& aBuf, 
       
   106     const TDesC& aText,
       
   107     const TBool aRemoveHeadingSpaces )
       
   108     {
       
   109     TInt i(0);
       
   110     TInt j(aBuf.Length());
       
   111     TBool isHeading(aRemoveHeadingSpaces);
       
   112     while (i <  aText.Length() && j < aBuf.MaxLength())
       
   113         {
       
   114         TChar c(aText[i]);
       
   115         if ( c.IsSpace() )
       
   116             {
       
   117             if ( !isHeading )
       
   118                 {
       
   119                 aBuf.Append(' ');
       
   120                 j++;
       
   121                 }
       
   122             }
       
   123         else if ( !c.IsControl() )
       
   124             {
       
   125             aBuf.Append(c);
       
   126             j++;
       
   127             isHeading = EFalse;
       
   128             }
       
   129         i++;
       
   130         }
       
   131     }
       
   132 
       
   133 // -----------------------------------------------------------------------------
       
   134 // NotepadUtil::LoadFileL
       
   135 // -----------------------------------------------------------------------------
       
   136 //
       
   137 void NotepadUtil::LoadFileL(
       
   138     CCoeEnv& aCoeEnv,
       
   139     const TDesC& aFileName, 
       
   140     TBool aGuessEncoding,
       
   141     TUint aEncoding, 
       
   142     CPlainText& aText )
       
   143     {
       
   144     RFile file;
       
   145     User::LeaveIfError(file.Open(aCoeEnv.FsSession(), aFileName, EFileRead));
       
   146     CleanupClosePushL(file);
       
   147     TInt size(0);
       
   148     User::LeaveIfError( file.Size(size) );
       
   149     if ( size > 0 )
       
   150         {
       
   151         CPlainText::TImportExportParam param;
       
   152         if ( !aGuessEncoding )
       
   153             {
       
   154             param.iForeignEncoding = aEncoding;
       
   155             }
       
   156         else
       
   157             {
       
   158             TBuf8<KMaxSampleLengthForAutoDetection> sample;
       
   159 
       
   160             User::LeaveIfError( file.Read(sample, 
       
   161                 size > KMaxSampleLengthForAutoDetection ? 
       
   162                 KMaxSampleLengthForAutoDetection : size) );
       
   163 
       
   164             if (IsBigEndianUnicodeText(sample))
       
   165                 {
       
   166                 param.iForeignEncoding = KCharacterSetIdentifierUnicodeBig; 
       
   167                 }
       
   168             else if (IsLittleEndianUnicodeText(sample))
       
   169                 {
       
   170                 param.iForeignEncoding = KCharacterSetIdentifierUnicodeLittle; 
       
   171                 }
       
   172             else if (IsUTF8Text(sample))
       
   173                 {
       
   174                 param.iForeignEncoding = KCharacterSetIdentifierUtf8; 
       
   175                 }
       
   176             else if ( IsShiftJisText(sample) )
       
   177                 {
       
   178                 param.iForeignEncoding = KCharacterSetIdentifierShiftJis; 
       
   179                 }
       
   180             else
       
   181                 {
       
   182                 param.iForeignEncoding = GuessEncodingFromLanguage();
       
   183                 if (param.iForeignEncoding == 0) // Variant not found
       
   184                     {
       
   185                     CArrayFix<CCnvCharacterSetConverter::SCharacterSet>* 
       
   186                         lists = CCnvCharacterSetConverter::
       
   187                         CreateArrayOfCharacterSetsAvailableLC(
       
   188                         aCoeEnv.FsSession() );
       
   189                     
       
   190                     TInt confidence(0);
       
   191                     TUint charset(0);
       
   192                     CCnvCharacterSetConverter::AutoDetectCharacterSetL(
       
   193                         confidence, charset, *lists, sample);
       
   194                     CleanupStack::PopAndDestroy();
       
   195                     
       
   196                     if ( confidence < 95)
       
   197                         {
       
   198                         param.iForeignEncoding = KCharacterSetIdentifierAscii;
       
   199                         }
       
   200                     else 
       
   201                         {
       
   202                         param.iForeignEncoding = charset;
       
   203                         }  
       
   204                     }
       
   205                 }
       
   206             }
       
   207 
       
   208         CPlainText::TImportExportResult result;
       
   209         RFileReadStream stream(file);
       
   210         CleanupClosePushL(stream);
       
   211 		TInt err;
       
   212 		TRAP(err, aText.ImportTextL(0, stream, param, result));
       
   213         stream.Release();
       
   214 
       
   215         CleanupStack::PopAndDestroy(); // stream
       
   216         }
       
   217     CleanupStack::PopAndDestroy(); // file*/
       
   218     }
       
   219 
       
   220 // -----------------------------------------------------------------------------
       
   221 // NotepadUtil::LoadFileL file handle
       
   222 // -----------------------------------------------------------------------------
       
   223 //
       
   224 TBool NotepadUtil::LoadFileL(
       
   225     CCoeEnv& aCoeEnv,
       
   226     RFile& aFile, 
       
   227     TBool aGuessEncoding,
       
   228     TUint aEncoding, 
       
   229     CPlainText& aText )
       
   230     {
       
   231     TInt size(0);
       
   232     User::LeaveIfError( aFile.Size(size) );
       
   233     
       
   234     CleanupClosePushL(aFile);
       
   235     TBool returnErr = KErrNone;
       
   236     
       
   237     if ( size > 0 )
       
   238         {
       
   239         CPlainText::TImportExportParam param;
       
   240         if ( !aGuessEncoding )
       
   241             {
       
   242             param.iForeignEncoding = aEncoding;
       
   243             }
       
   244         else
       
   245             {
       
   246             TBuf8<KMaxSampleLengthForAutoDetection> sample;
       
   247             User::LeaveIfError( aFile.Read(sample, 
       
   248                 size > KMaxSampleLengthForAutoDetection ? 
       
   249                 KMaxSampleLengthForAutoDetection : size) );
       
   250                 
       
   251             CArrayFix<CCnvCharacterSetConverter::SCharacterSet>* 
       
   252             lists = CCnvCharacterSetConverter::
       
   253             CreateArrayOfCharacterSetsAvailableLC(
       
   254             aCoeEnv.FsSession() );
       
   255             
       
   256             TInt confidence(0);
       
   257             TUint charset(0);
       
   258             CCnvCharacterSetConverter::AutoDetectCharacterSetL(
       
   259                 confidence, charset, *lists, sample);
       
   260             CleanupStack::PopAndDestroy();
       
   261             
       
   262             if ( confidence > 50)
       
   263                 {
       
   264                 param.iForeignEncoding = charset;
       
   265                 }
       
   266             else 
       
   267                 {
       
   268                 
       
   269 	            if (IsBigEndianUnicodeText(sample))
       
   270 	                {
       
   271 	                param.iForeignEncoding = KCharacterSetIdentifierUnicodeBig; 
       
   272 	                }
       
   273 	            else if (IsLittleEndianUnicodeText(sample))
       
   274 	                {
       
   275 	                param.iForeignEncoding = KCharacterSetIdentifierUnicodeLittle; 
       
   276 	                }
       
   277 	            else if (IsUTF8Text(sample))
       
   278 	                {
       
   279 	                param.iForeignEncoding = KCharacterSetIdentifierUtf8; 
       
   280 	                }
       
   281 	            else if ( IsShiftJisText(sample) )
       
   282 	                {
       
   283 	                param.iForeignEncoding = KCharacterSetIdentifierShiftJis; 
       
   284 	                }
       
   285 	            else
       
   286 	                {
       
   287 	                param.iForeignEncoding = GuessEncodingFromLanguage();
       
   288 	                if (param.iForeignEncoding == 0) // Variant not found
       
   289 	                    {
       
   290 	                    param.iForeignEncoding = KCharacterSetIdentifierAscii;
       
   291 	                    }
       
   292 
       
   293 	                }
       
   294                 }
       
   295             }
       
   296         
       
   297         CPlainText::TImportExportResult result;
       
   298         
       
   299         RFile tempFile;
       
   300         tempFile.Duplicate( aFile );
       
   301         
       
   302         RFileReadStream stream(tempFile);
       
   303         CleanupClosePushL(stream);
       
   304 	    
       
   305 	    TInt err = KErrNone;
       
   306 		TRAP(err, aText.ImportTextL(0, stream, param, result));
       
   307 		
       
   308 	
       
   309 		stream.Release();// closing of tempfile
       
   310 
       
   311 		CleanupStack::PopAndDestroy(); // stream 
       
   312 		
       
   313 		aFile.Close();
       
   314 		
       
   315 		CleanupStack::PopAndDestroy(&aFile); // afile 
       
   316 
       
   317 
       
   318 	   switch ( err )
       
   319         {
       
   320         case KErrCorrupt:
       
   321              returnErr = KErrCorrupt;
       
   322         case KErrNone: 
       
   323             break;
       
   324         default:
       
   325             User::Leave(err);
       
   326             break;
       
   327         }
       
   328 	         
       
   329 	  	  
       
   330 	    
       
   331         
       
   332     }//size > 0
       
   333     else
       
   334     {
       
   335     	  aFile.Close();
       
   336     	  CleanupStack::PopAndDestroy(&aFile);
       
   337     }
       
   338         
       
   339       return returnErr;  
       
   340     }
       
   341 
       
   342 
       
   343 // -----------------------------------------------------------------------------
       
   344 // NotepadUtil::IsBigEndianUnicodeText
       
   345 // -----------------------------------------------------------------------------
       
   346 //
       
   347 bool NotepadUtil::IsBigEndianUnicodeText(TDesC8& aText)
       
   348     {
       
   349     if (( aText.Length() >= 2) && ( aText[0] == 0xfe && aText[1] == 0xff ) )
       
   350         {
       
   351         return ETrue;
       
   352         }
       
   353 
       
   354     return EFalse;
       
   355     }
       
   356 
       
   357 // -----------------------------------------------------------------------------
       
   358 // NotepadUtil::IsLittleEndianUnicodeText
       
   359 // -----------------------------------------------------------------------------
       
   360 //
       
   361 bool NotepadUtil::IsLittleEndianUnicodeText(TDesC8& aText)
       
   362     {
       
   363     if (( aText.Length() >= 2) && ( aText[0] == 0xff && aText[1] == 0xfe ) )
       
   364         {
       
   365         return ETrue;
       
   366         }
       
   367 
       
   368     return EFalse;
       
   369     }
       
   370 
       
   371 // -----------------------------------------------------------------------------
       
   372 // NotepadUtil::IsUTF8Text
       
   373 // -----------------------------------------------------------------------------
       
   374 //
       
   375 bool NotepadUtil::IsUTF8Text(TDesC8& aText)
       
   376     {
       
   377     if ( (aText.Length() >= 3) &&
       
   378        ( aText[0] == 0xef && aText[1] == 0xbb && aText[2] == 0xbf ) )
       
   379         {
       
   380         return ETrue;
       
   381         }
       
   382 
       
   383     return EFalse;
       
   384     }
       
   385 
       
   386 // -----------------------------------------------------------------------------
       
   387 // NotepadUtil::IsShiftJisText
       
   388 // -----------------------------------------------------------------------------
       
   389 //
       
   390 bool NotepadUtil::IsShiftJisText(TDesC8& aText)
       
   391     {
       
   392     TInt maxlength = aText.Length();//(aText.Length() < 10 ? aText.Length() : 10);
       
   393 
       
   394     if(maxlength < 2)
       
   395         {
       
   396         return EFalse;
       
   397         }
       
   398     TInt charsneeded = ((maxlength / 6 > 0 ? (maxlength/6) : 1));
       
   399     TInt charcount = 0;
       
   400 
       
   401     for(TInt i = 0; i+1 < maxlength ; i++)
       
   402         {
       
   403         if(IsShiftJisChar(aText[i], aText[i+1]))
       
   404             {
       
   405             charcount += 1;
       
   406             i++; // Skip the second byte so its not re-checked.
       
   407             if (charcount >= charsneeded)
       
   408                 {
       
   409                 return ETrue;
       
   410                 }
       
   411             }
       
   412         }
       
   413 
       
   414     return EFalse;
       
   415     }
       
   416 
       
   417 // -----------------------------------------------------------------------------
       
   418 // NotepadUtil::IsShiftJisChar
       
   419 // -----------------------------------------------------------------------------
       
   420 //
       
   421 bool NotepadUtil::IsShiftJisChar(const TUint& aFirst, const TUint& aSecond)
       
   422     {
       
   423     if (  // Type 1 
       
   424         ( (129 <= aFirst && aFirst <= 159) && 
       
   425         ( 64 <= aSecond && aSecond <= 158 && aSecond != 127) 
       
   426         ||  // Type 2
       
   427         ( (224 <= aFirst && aFirst <= 239) && 
       
   428         ( 64 <= aSecond && aSecond <= 158 && aSecond != 127) )
       
   429         ||  // Type 3
       
   430         ( (129 <= aFirst && aFirst <= 159) && 
       
   431         (159 <= aSecond && aSecond <= 252) )
       
   432         ||  // Type 4
       
   433         ( (224 <= aFirst && aFirst <= 239) && 
       
   434         (159 <= aSecond && aSecond <= 252) )
       
   435         ) )
       
   436         {
       
   437         return ETrue;
       
   438         }
       
   439     return EFalse;
       
   440     }
       
   441 
       
   442 TUint NotepadUtil::GuessEncodingFromLanguage()
       
   443     {
       
   444     TLanguage language = User::Language();
       
   445 
       
   446     return GetISO8859Variant(language);
       
   447     }
       
   448 
       
   449 TUint NotepadUtil::GetISO8859Variant(TLanguage aLanguage)
       
   450     {  
       
   451     switch (aLanguage)
       
   452         {
       
   453         case ELangEnglish:
       
   454         case ELangCatalan:
       
   455         case ELangDanish:
       
   456         case ELangGerman:
       
   457         case ELangIcelandic:
       
   458         case ELangIrish:
       
   459         case ELangItalian:
       
   460         case ELangNorwegian:
       
   461         case ELangPortuguese:
       
   462         case ELangSpanish:
       
   463         case ELangAlbanian:
       
   464         case ELangAfrikaans:
       
   465         case ELangSwahili:
       
   466         case ELangSwedish:
       
   467         case ELangFinlandSwedish:
       
   468             return KCharacterSetIdentifierIso88591;
       
   469 
       
   470         case ELangPolish:
       
   471         case ELangCzech:
       
   472         case ELangSlovak:
       
   473         case ELangSlovenian:
       
   474         case ELangHungarian:
       
   475             return KCharacterSetIdentifierIso88592;
       
   476 
       
   477         case ELangEstonian:
       
   478         case ELangLatvian:
       
   479         case ELangLithuanian:
       
   480             return KCharacterSetIdentifierIso88594;
       
   481 
       
   482         case ELangBulgarian:
       
   483         case ELangMacedonian:
       
   484         case ELangRussian:
       
   485         case ELangSerbian:
       
   486         case ELangUkrainian:
       
   487             return KCharacterSetIdentifierIso88595;
       
   488 
       
   489         case ELangArabic:
       
   490             return KCharacterSetIdentifierIso88596;
       
   491 
       
   492         case ELangGreek:
       
   493         case ELangCyprusGreek:
       
   494             return KCharacterSetIdentifierIso88597;
       
   495 
       
   496         case ELangHebrew:
       
   497             return KCharacterSetIdentifierIso88598;
       
   498 
       
   499         case ELangTurkish:
       
   500             return KCharacterSetIdentifierIso88599;
       
   501 
       
   502         case ELangScotsGaelic:
       
   503         case ELangWelsh:
       
   504             return KCharacterSetIdentifierIso885914;
       
   505 
       
   506         case ELangFrench:
       
   507         case ELangFinnish:
       
   508             return KCharacterSetIdentifierIso885915;
       
   509 
       
   510         default:
       
   511             return 0;
       
   512         }
       
   513     }
       
   514 
       
   515 // End of File