gfxconversion/mifconv/src/mifconv_bitmapheaderconverter.cpp
changeset 0 f453ebb75370
equal deleted inserted replaced
-1:000000000000 0:f453ebb75370
       
     1 /*
       
     2 * Copyright (c) 2009 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:  Mifconv bitmap header converter class.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "mifconv.h"
       
    20 #include "mifconv_bitmapheaderconverter.h"
       
    21 #include "mifconv_argumentmanager.h"
       
    22 #include "mifconv_util.h"
       
    23 #include "mifconv_exception.h"
       
    24 
       
    25 const int MIF_ID_FIRST = 16384;
       
    26 
       
    27 /**
       
    28  *
       
    29  */
       
    30 MifConvBitmapHeaderConverter::MifConvBitmapHeaderConverter()
       
    31 :
       
    32 iCurrentSourceId(MIF_ID_FIRST)
       
    33 {
       
    34 	MifConvArgumentManager* argMgr = MifConvArgumentManager::Instance();
       
    35     iHeaderFilename = argMgr->StringValue(MifConvHeaderArg);
       
    36 }
       
    37 
       
    38 /**
       
    39  *
       
    40  */
       
    41 MifConvBitmapHeaderConverter::~MifConvBitmapHeaderConverter()
       
    42 {
       
    43 }
       
    44 
       
    45 /**
       
    46  *
       
    47  */
       
    48 void MifConvBitmapHeaderConverter::Init()
       
    49 {
       
    50     CleanupTargetFiles();
       
    51 }
       
    52 
       
    53 /**
       
    54  *
       
    55  */
       
    56 void MifConvBitmapHeaderConverter::CleanupTargetFiles()
       
    57 {
       
    58 	if( MifConvUtil::FileExists(iHeaderFilename) )
       
    59 	{
       
    60         // Try to remove file MIFCONV_MAX_REMOVE_TRIES times, no exception in case of failure:
       
    61         MifConvUtil::RemoveFile(iHeaderFilename, MIFCONV_MAX_REMOVE_TRIES, true);
       
    62 	}
       
    63 }
       
    64 
       
    65 /**
       
    66  *
       
    67  */
       
    68 void MifConvBitmapHeaderConverter::AppendFile( const MifConvSourceFile& sourcefile )
       
    69 {    
       
    70 	iSourceFiles.push_back( sourcefile );	
       
    71 }
       
    72 
       
    73 /**
       
    74  *
       
    75  */
       
    76 void MifConvBitmapHeaderConverter::Convert()
       
    77 {
       
    78     // Check if the headerfile name is given. Return if not:    
       
    79     if( iHeaderFilename.length() > 0 )
       
    80     {
       
    81         cout << "Writing mbg: " << iHeaderFilename << endl;
       
    82         MifConvUtil::EnsurePathExists(iHeaderFilename, true);
       
    83     }
       
    84     else
       
    85     {
       
    86         return;
       
    87     }
       
    88 
       
    89     // Create / open the header file:
       
    90     fstream headerFile(iHeaderFilename.c_str(), ios::out|ios::trunc);
       
    91     
       
    92     // If file creation/opening was not successful, give warning and return:
       
    93     if( !headerFile.is_open() )
       
    94     {
       
    95         MifConvString debugStr("WARNING: Headerfile " + iHeaderFilename + " cannot be opened for writing.");
       
    96         cout <<  debugStr  << endl;
       
    97         MifConvUtil::DebugLog(debugStr);
       
    98         return;
       
    99     }
       
   100     iDestFile = &headerFile; // Save the pointer to the file stream    
       
   101     (*iDestFile) << endl;
       
   102     // Write the beginning of the header file:
       
   103     WriteStart();
       
   104 
       
   105     // Add source file items to enumerator:
       
   106     for( MifConvSourceFileList::iterator i = iSourceFiles.begin(); i != iSourceFiles.end(); ++i )
       
   107     {
       
   108         WriteItemToHeader(*i);
       
   109     }
       
   110 
       
   111     // Write the end of the file...
       
   112     WriteEnd();
       
   113 
       
   114     // ...and close the file:
       
   115     iDestFile->close();
       
   116 }
       
   117 
       
   118 /**
       
   119  *
       
   120  */
       
   121 void MifConvBitmapHeaderConverter::WriteStart()
       
   122 {
       
   123     (*iDestFile) << "/* This file has been generated, DO NOT MODIFY. */" << endl;
       
   124     (*iDestFile) << "enum TMif" << MifConvUtil::UnadornedFilename(iHeaderFilename) << "\n\t{" << endl;
       
   125 }
       
   126 
       
   127 /**
       
   128  *
       
   129  */
       
   130 void MifConvBitmapHeaderConverter::WriteItemToHeader( const MifConvSourceFile& source )
       
   131 {
       
   132     MifConvString unadornedHeaderFile(MifConvUtil::UnadornedFilename(iHeaderFilename));
       
   133     
       
   134     (*iDestFile) << "\tEMbm" << unadornedHeaderFile << MifConvUtil::UnadornedFilename(source.Filename()) <<  " = " << iCurrentSourceId++ << "," << endl;
       
   135 
       
   136     // Write mask also, if any:
       
   137     if( source.MaskDepth() != IconMaskDepth_Undefined )
       
   138     {
       
   139         (*iDestFile) << "\tEMbm" << unadornedHeaderFile << MifConvUtil::UnadornedFilename(source.Filename()) <<  "_mask = " << iCurrentSourceId++ << "," << endl;
       
   140     }
       
   141     else
       
   142     {
       
   143         // Skip mask ID if mask not present so that adding a mask later does not change IDs.
       
   144         iCurrentSourceId++;
       
   145     }
       
   146 }
       
   147 
       
   148 /**
       
   149  *
       
   150  */
       
   151 void MifConvBitmapHeaderConverter::WriteEnd()
       
   152 {
       
   153     (*iDestFile) << "\tEMbm" << MifConvUtil::UnadornedFilename(iHeaderFilename) << "LastElement" << endl;
       
   154     (*iDestFile) << "\t};" << endl;
       
   155 }
       
   156 
       
   157 /**
       
   158  *
       
   159  */
       
   160 void MifConvBitmapHeaderConverter::Cleanup(bool err)
       
   161 {
       
   162     if( err )
       
   163 	{
       
   164 	    CleanupTargetFiles();
       
   165 	}
       
   166 }