skins/AknSkins/sdcsrc/SDCInput.cpp
changeset 0 05e9090e2422
child 32 d9c996538b26
equal deleted inserted replaced
-1:000000000000 0:05e9090e2422
       
     1 /*
       
     2 * Copyright (c) 2003-2008 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:  ?Description
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <string.h>
       
    20 #include "SDCGlobals.h"
       
    21 #include "SDCInput.h"
       
    22 
       
    23 // Make std namespace available for compatibility
       
    24 namespace std {}
       
    25 using namespace std;
       
    26 
       
    27 //////////////////////////////////////////////////////////////////////
       
    28 // Construction/Destruction
       
    29 //////////////////////////////////////////////////////////////////////
       
    30 
       
    31 CSDCInput::CSDCInput()
       
    32     {
       
    33     iLineNumber = -1;
       
    34     iFile = NULL;
       
    35     iHash = 0;
       
    36     }
       
    37 
       
    38 CSDCInput::~CSDCInput()
       
    39     {
       
    40     if( iFile )
       
    41         {
       
    42         fclose( iFile );
       
    43         iFile = NULL;
       
    44         }
       
    45     }
       
    46 
       
    47 //////////////////////////////////////////////////////////////////////
       
    48 // Other methods
       
    49 //////////////////////////////////////////////////////////////////////
       
    50 
       
    51 void CSDCInput::Open( const char *aFilename )
       
    52     {
       
    53     iLineNumber = 1;
       
    54     iFile = fopen( aFilename, "rb" );
       
    55     if( !iFile ) throw CSDCException( ESDCFileOpenError, "Cannot open input file" );
       
    56 
       
    57     int testChar1, testChar2;
       
    58     testChar1 = fgetc( iFile );
       
    59     testChar2 = fgetc( iFile );
       
    60     if( (testChar1==EOF) || (testChar2==EOF) ) throw CSDCException( ESDCPrematureEOIError, "Unicode detection failed" );
       
    61     if( (testChar1==255) && (testChar2==254) )
       
    62         {
       
    63         iUnicode = true;
       
    64         }
       
    65     else
       
    66         {
       
    67         iUnicode = false;
       
    68         }
       
    69 
       
    70     if( !iUnicode )
       
    71         {
       
    72         if( fseek( iFile, 0, SEEK_SET ) ) throw CSDCException( ESDCSeekError, "Seek failed after Unicode detection" );
       
    73         }
       
    74 
       
    75     iFirstInLine = true;
       
    76 
       
    77     ReadNextChar();
       
    78     }
       
    79 
       
    80 bool CSDCInput::NextToken()
       
    81     {
       
    82     return ParseToken( L" \t", L" \t\r\n" );
       
    83     }
       
    84 
       
    85 bool CSDCInput::NextTokenAllowLF()
       
    86     {
       
    87     return ParseToken( L" \t\r\n", L" \t\r\n" );
       
    88     }
       
    89 
       
    90 bool CSDCInput::NextStringToken()
       
    91     {
       
    92     wcscpy( iToken, L"" );
       
    93 
       
    94     while( wcschr( L" \t", iNextChar ) ) ReadNextChar();
       
    95     if( iNextChar != L'\"' ) throw CSDCException( ESDCParseError, "No opening quotation mark for string found" );
       
    96 
       
    97     bool escaped = false;
       
    98     wchar_t escapeToken[512];
       
    99     wcscpy( escapeToken, L"" );
       
   100 
       
   101     ReadNextChar();
       
   102     while( iNextChar != L'\"' )
       
   103         {
       
   104         if( (iNextChar==L'\n') || (iNextChar==L'\r') || (iNextChar==WEOF) ) throw CSDCException( ESDCParseError, "Unterminated string" );
       
   105         if( escaped )
       
   106             {
       
   107             if( iNextChar == L'>' )
       
   108                 {
       
   109                 escaped = false;
       
   110                 wchar_t escapedCh = (wchar_t)ConvertToNumber( escapeToken );
       
   111                 wcsncat( iToken, &escapedCh, 1 );
       
   112                 wcscpy( escapeToken, L"" );
       
   113                 }
       
   114             else
       
   115                 {
       
   116                 wcsncat( escapeToken, &iNextChar, 1 );
       
   117                 }
       
   118             }
       
   119         else
       
   120             {
       
   121             if( iNextChar == L'<' )
       
   122                 {
       
   123                 escaped = true;
       
   124                 }
       
   125             else
       
   126                 {
       
   127                 wcsncat( iToken, &iNextChar, 1 );
       
   128                 }
       
   129             }
       
   130         ReadNextChar();
       
   131         }
       
   132     if( escaped ) throw CSDCException( ESDCParseError, "Unterminated escape in string" );
       
   133     ReadNextChar();
       
   134 
       
   135     return wcslen(iToken)>0?true:false;
       
   136     }
       
   137 
       
   138 int CSDCInput::NextRawToken()
       
   139     {
       
   140     wcscpy( iToken, L"" );
       
   141     int count = 0;
       
   142 
       
   143     while( wcschr( L" \t", iNextChar ) ) ReadNextChar();
       
   144     if( iNextChar != L'[' ) throw CSDCException( ESDCParseError, "No opening [ for raw data found" );
       
   145 
       
   146     bool inData = false;
       
   147     wchar_t dataToken[512];
       
   148     wcscpy( dataToken, L"" );
       
   149 
       
   150     ReadNextChar();
       
   151     bool quit = false;
       
   152     while( !quit )
       
   153         {
       
   154         if( (iNextChar==L'\n') || (iNextChar==L'\r') || (iNextChar==WEOF) )
       
   155             throw CSDCException( ESDCParseError, "Unterminated raw data sequence, ] missing" );
       
   156 
       
   157         if( inData )
       
   158             {
       
   159             if( iNextChar == L' ' || iNextChar == L']')
       
   160                 {
       
   161                 inData = false;
       
   162                 wchar_t data = (wchar_t)ConvertToNumber( dataToken );
       
   163                 iToken[count] = data;
       
   164                 wcscpy( dataToken, L"" );
       
   165                 count++;
       
   166                 }
       
   167             else
       
   168                 {
       
   169                 wcsncat( dataToken, &iNextChar, 1 );
       
   170                 }
       
   171             }
       
   172         else
       
   173             {
       
   174             if( iNextChar != L' ' && iNextChar != L']')
       
   175                 {
       
   176                 inData = true;
       
   177                 if( count+1 >= 512 )
       
   178                     throw CSDCException( ESDCParseError, "Raw data sequence too long, maximum is 512 items" );
       
   179                 continue; // We need to skip that ReadNextChar because 0 is part of the number token
       
   180                 }
       
   181             }
       
   182 
       
   183         if( iNextChar == L']' )
       
   184             {
       
   185             quit = true;
       
   186             // Because the next ReadNextChar is executed the trailing ] is
       
   187             // skipped.
       
   188             }
       
   189 
       
   190         ReadNextChar();
       
   191         }
       
   192 
       
   193     return count;
       
   194     }
       
   195 
       
   196 int CSDCInput::ConvertToNumber( const wchar_t* aToken )
       
   197     {
       
   198     bool hexadecimal = false;
       
   199     if( _wcsnicmp( aToken, L"0x", 2 ) == 0 ) hexadecimal = true;
       
   200 
       
   201     int result = 0;
       
   202     const wchar_t *p = aToken;
       
   203     if( hexadecimal ) p = p+2;
       
   204 
       
   205     if( *p == 0 ) throw CSDCException( ESDCParseError, "Number token is empty" );
       
   206 
       
   207     bool negative = false;
       
   208     if( *p == L'-' )
       
   209         {
       
   210         negative = true;
       
   211         p = p+1;
       
   212         }
       
   213 
       
   214     while( *p )
       
   215         {
       
   216         if( hexadecimal )
       
   217             {
       
   218             result *= 16;
       
   219             if( (*p>=L'0') && (*p<=L'9') ) result += *p - L'0';
       
   220             else if( (*p>=L'A') && (*p<=L'F') ) result += *p - L'A' + 10;
       
   221             else if( (*p>=L'a') && (*p<=L'f') ) result += *p - L'a' + 10;
       
   222             else throw CSDCException( ESDCParseError, "Non-hexadecimal character in number token" );
       
   223             }
       
   224         else
       
   225             {
       
   226             result *= 10;
       
   227             if( (*p>=L'0') && (*p<=L'9') ) result += *p - L'0';
       
   228             else throw CSDCException( ESDCParseError, "Non-numeric character in number token" );
       
   229             }
       
   230 
       
   231         p = p+1;
       
   232         }
       
   233 
       
   234     if( negative ) result = 0-result;
       
   235     return result;
       
   236     }
       
   237 
       
   238 void CSDCInput::ConvertToAscii( char* aTarget, const wchar_t* aSource )
       
   239     {
       
   240     int i = 0;
       
   241     while( true )
       
   242         {
       
   243         aTarget[i] = (char)aSource[i];
       
   244         if( !aSource[i] ) break;
       
   245         i++;
       
   246         }
       
   247     }
       
   248 
       
   249 void CSDCInput::ConvertToAsciiWithCPPEscapes( char* aTarget, const wchar_t* aSource )
       
   250     {
       
   251     int srcI = 0;
       
   252     int trgI = 0;
       
   253     while( true )
       
   254         {
       
   255         char ch = (char)aSource[srcI++];
       
   256         if( !ch )
       
   257             {
       
   258             aTarget[trgI++] = ch;
       
   259             break;
       
   260             }
       
   261 
       
   262         switch( ch )
       
   263             {
       
   264             case '\\':
       
   265                 aTarget[trgI++] = '\\';
       
   266                 aTarget[trgI++] = '\\';
       
   267                 break;
       
   268             default:
       
   269                 aTarget[trgI++] = ch;
       
   270             }
       
   271         }
       
   272     }
       
   273 
       
   274 TSDCColorDepth CSDCInput::ConvertToColorDepth( const wchar_t* aToken )
       
   275     {
       
   276     if( _wcsicmp( aToken, L"1" ) == 0) return ESDCColorDepth1;
       
   277     else if( _wcsicmp( aToken, L"2" ) == 0) return ESDCColorDepth2;
       
   278     else if( _wcsicmp( aToken, L"4" ) == 0) return ESDCColorDepth4;
       
   279     else if( _wcsicmp( aToken, L"8" ) == 0) return ESDCColorDepth8;
       
   280     else if( _wcsicmp( aToken, L"c4" ) == 0) return ESDCColorDepthC4;
       
   281     else if( _wcsicmp( aToken, L"c8" ) == 0) return ESDCColorDepthC8;
       
   282     else if( _wcsicmp( aToken, L"c12" ) == 0) return ESDCColorDepthC12;
       
   283     else if( _wcsicmp( aToken, L"c16" ) == 0) return ESDCColorDepthC16;
       
   284     else if( _wcsicmp( aToken, L"c24" ) == 0) return ESDCColorDepthC24;
       
   285     else if( _wcsicmp( aToken, L"c32" ) == 0) return ESDCColorDepthC32;
       
   286     throw CSDCException( ESDCParseError, "Unknown color depth parameter" );
       
   287     }
       
   288 
       
   289 int CSDCInput::ConvertToLayer( const wchar_t* aToken )
       
   290     {
       
   291     if( _wcsicmp( aToken, L"none" ) == 0 ) return 0x0001;
       
   292 
       
   293     wchar_t buf[512];
       
   294     wcscpy( buf, aToken );
       
   295 
       
   296     wchar_t* p = wcsstr( buf, L"/" );
       
   297     if( !p ) throw CSDCException( ESDCParseError, "Slash expected in layer parameter" );
       
   298     *p = 0;
       
   299     p = p+1;
       
   300 
       
   301     int layer = ConvertToNumber( buf ) << 8;
       
   302     if( _wcsicmp( p, L"RGB" ) == 0 )
       
   303         {
       
   304         layer |= 0x02;
       
   305         }
       
   306     else if( _wcsicmp( p, L"A" ) == 0 )
       
   307         {
       
   308         layer |= 0x04;
       
   309         }
       
   310     else if( _wcsicmp( p, L"RGBA" ) == 0 )
       
   311         {
       
   312         layer |= 0x08;
       
   313         }
       
   314     else
       
   315         {
       
   316         throw CSDCException( ESDCParseError, "Unidentified layer specifier" );
       
   317         }
       
   318 
       
   319     return layer;
       
   320     }
       
   321 
       
   322 bool CSDCInput::IsSvgFile( const wchar_t* aFilename )
       
   323     {
       
   324     int len = wcslen( aFilename );
       
   325     if( len<4 ) return false;
       
   326 
       
   327     const wchar_t* p = aFilename + len - 4;
       
   328     if( _wcsicmp( p, L".svg" ) == 0 )
       
   329         {
       
   330         return true;
       
   331         }
       
   332     return false;
       
   333     }
       
   334 
       
   335 void CSDCInput::RawRead()
       
   336     {
       
   337     if( iUnicode )
       
   338         {
       
   339         iNextChar = fgetwc( iFile );
       
   340         }
       
   341     else
       
   342         {
       
   343         int ch = fgetc( iFile );
       
   344         if( ch == EOF )
       
   345             {
       
   346             iNextChar = WEOF;
       
   347             }
       
   348         else
       
   349             {
       
   350             iNextChar = ch;
       
   351             }
       
   352         }
       
   353 
       
   354     iHash += iNextChar;
       
   355     }
       
   356 
       
   357 bool CSDCInput::ReadNextChar()
       
   358     {
       
   359     RawRead();
       
   360 
       
   361     if( iFirstInLine )
       
   362         {
       
   363         iFirstInLine = false;
       
   364         if( iNextChar == L'/' )
       
   365             {
       
   366             while( (iNextChar!=L'\n') && (iNextChar!=WEOF) ) RawRead();
       
   367             }
       
   368         }
       
   369 
       
   370     if( iNextChar == L'\n' )
       
   371         {
       
   372         iLineNumber++;
       
   373         iFirstInLine = true;
       
   374         }
       
   375 
       
   376     return iNextChar==WEOF?false:true;
       
   377     }
       
   378 
       
   379 bool CSDCInput::ParseToken( const wchar_t* aSeparators, const wchar_t* aTerminators )
       
   380     {
       
   381     wcscpy( iToken, L"" );
       
   382 
       
   383     while( wcschr( aSeparators, iNextChar ) )
       
   384         {
       
   385         ReadNextChar();
       
   386         }
       
   387     if( iNextChar == WEOF ) return false;
       
   388 
       
   389     while( !wcschr( aTerminators, iNextChar ) )
       
   390         {
       
   391         if( iNextChar == WEOF ) break;
       
   392 
       
   393         wcsncat( iToken, &iNextChar, 1 );
       
   394 
       
   395         ReadNextChar();
       
   396         }
       
   397 
       
   398     return wcslen(iToken)>0?true:false;
       
   399     }
       
   400 
       
   401 // End of file