imgtools/sisutils/src/sisutils.cpp
changeset 0 044383f39525
child 590 360bd6b35136
equal deleted inserted replaced
-1:000000000000 0:044383f39525
       
     1 /*
       
     2 * Copyright (c) 2008-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 the License "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: 
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #ifdef WIN32
       
    20 #include <windows.h>
       
    21 #include <direct.h>
       
    22 #endif
       
    23 
       
    24 #include "sisutils.h"
       
    25 
       
    26 /**
       
    27 Constructor: SisUtilsException class
       
    28 Initilize the parameters to data members.
       
    29 
       
    30 @internalComponent
       
    31 @released
       
    32 
       
    33 @param aFile	- Name of the file
       
    34 @param aErrMessage - Error message
       
    35 */
       
    36 SisUtilsException::SisUtilsException(char* aFile, char* aErrMessage) : \
       
    37 	iSisFileName(aFile), iErrMessage(aErrMessage)
       
    38 {
       
    39 }
       
    40 
       
    41 /**
       
    42 Destructor: SisUtilsException class
       
    43 Deallocates the memory for data members
       
    44 
       
    45 @internalComponent
       
    46 @released
       
    47 */
       
    48 SisUtilsException::~SisUtilsException()
       
    49 {
       
    50 }
       
    51 
       
    52 /**
       
    53 Report: Reports error message on the console
       
    54 
       
    55 @internalComponent
       
    56 @released
       
    57 */
       
    58 void SisUtilsException::Report()
       
    59 {
       
    60 	std::cout << "Error : ";
       
    61 	std::cout << iSisFileName.c_str() << " : ";
       
    62 	std::cout << iErrMessage.c_str();
       
    63 	std::cout << std::endl;
       
    64 }
       
    65 
       
    66 /**
       
    67 Constructor: SisUtils class
       
    68 Initilize the parameters to data members.
       
    69 
       
    70 @internalComponent
       
    71 @released
       
    72 
       
    73 @param aFile	- Name of the SIS file
       
    74 */
       
    75 SisUtils::SisUtils(char* aFile) :  iVerboseMode(EFalse),iSisFile(aFile)
       
    76 {
       
    77 }
       
    78 
       
    79 /**
       
    80 Destructor: SisUtils class
       
    81 Deallocates the memory for data members
       
    82 
       
    83 @internalComponent
       
    84 @released
       
    85 */
       
    86 SisUtils::~SisUtils()
       
    87 {
       
    88 }
       
    89 
       
    90 /**
       
    91 SetVerboseMode: Sets the verbose mode
       
    92 
       
    93 @internalComponent
       
    94 @released
       
    95 */
       
    96 void SisUtils::SetVerboseMode()
       
    97 {
       
    98 	iVerboseMode = ETrue;
       
    99 }
       
   100 
       
   101 /**
       
   102 SisFileName: Returns the SIS file name
       
   103 
       
   104 @internalComponent
       
   105 @released
       
   106 */
       
   107 String SisUtils::SisFileName()
       
   108 {
       
   109 	return iSisFile;
       
   110 }
       
   111 
       
   112 /**
       
   113 IsVerboseMode: Returns the status of the verbose mode
       
   114 
       
   115 @internalComponent
       
   116 @released
       
   117 */
       
   118 TBool SisUtils::IsVerboseMode()
       
   119 {
       
   120 	return iVerboseMode;
       
   121 }
       
   122 
       
   123 /**
       
   124 IsFileExist: Tests whether the give file exists or not
       
   125 
       
   126 @internalComponent
       
   127 @released
       
   128 
       
   129 @param aFile - Name of the file
       
   130 */
       
   131 TBool SisUtils::IsFileExist(String aFile)
       
   132 {
       
   133 	std::ifstream aIfs;
       
   134 
       
   135 	TrimQuotes(aFile);
       
   136 
       
   137 	aIfs.open((char*)aFile.data(), std::ios::in);
       
   138 
       
   139 	if( aIfs.fail() )
       
   140 	{
       
   141 		aIfs.close();
       
   142 		return EFalse;
       
   143 	}
       
   144 
       
   145 	aIfs.close();
       
   146 
       
   147 	return ETrue;
       
   148 }
       
   149 
       
   150 /**
       
   151 RunCommand: Runs the given command
       
   152 
       
   153 @internalComponent
       
   154 @released
       
   155 
       
   156 @param cmd - Command line as string
       
   157 */
       
   158 TUint32 SisUtils::RunCommand(String cmd)
       
   159 {
       
   160 	TUint32 iExitCode = STAT_SUCCESS;
       
   161 
       
   162 #ifdef WIN32
       
   163     STARTUPINFO si;
       
   164     PROCESS_INFORMATION pi;
       
   165 	DWORD dwWaitResult;
       
   166 
       
   167     memset(&si, 0, sizeof(si));
       
   168     si.cb = sizeof(si);
       
   169     memset(&pi, 0, sizeof(pi));
       
   170 
       
   171     if( !::CreateProcess( NULL,   // No module name (use command line)
       
   172         (char*)cmd.data(),        // Command line
       
   173         NULL,           // Process handle not inheritable
       
   174         NULL,           // Thread handle not inheritable
       
   175         FALSE,          // Set handle inheritance to FALSE
       
   176         DETACHED_PROCESS | CREATE_NO_WINDOW,              // process creation flags
       
   177         NULL,           // Use parent's environment block
       
   178         NULL,           // Use parent's starting directory 
       
   179         &si,            // Pointer to STARTUPINFO structure
       
   180         &pi )           // Pointer to PROCESS_INFORMATION structure
       
   181     ) 
       
   182     {
       
   183 		return static_cast<TUint32>(STAT_FAILURE);
       
   184     }
       
   185 
       
   186 	dwWaitResult = ::WaitForSingleObject( pi.hProcess, INFINITE );
       
   187 
       
   188 	if(dwWaitResult == WAIT_OBJECT_0)
       
   189 	{
       
   190 		::GetExitCodeProcess(pi.hProcess, &iExitCode);
       
   191 		if(iExitCode != STAT_SUCCESS)
       
   192 		{
       
   193 			iExitCode = static_cast<TUint32>(STAT_FAILURE);
       
   194 		}
       
   195 	}
       
   196 	else
       
   197 	{
       
   198 		iExitCode = static_cast<TUint32>(STAT_FAILURE);
       
   199 	}
       
   200 
       
   201 	::CloseHandle( pi.hProcess );
       
   202 	::CloseHandle( pi.hThread );
       
   203 #else
       
   204 #error "TODO: Implement this function under other OS than Windows"
       
   205 #endif
       
   206 
       
   207 	return iExitCode;
       
   208 }
       
   209 
       
   210 /**
       
   211 TrimQuotes: Remove the quotes in the given file name
       
   212 
       
   213 @internalComponent
       
   214 @released
       
   215 
       
   216 @param aStr - File name
       
   217 */
       
   218 void SisUtils::TrimQuotes(String& aStr)
       
   219 {
       
   220 	TUint spos = 0, epos = 0;
       
   221 
       
   222 	spos = aStr.find("\"");
       
   223 	if(spos == String::npos)
       
   224 		return;
       
   225 
       
   226 	epos = aStr.rfind("\"");
       
   227 
       
   228 	if(spos == epos)
       
   229 	{
       
   230 		epos = aStr.size();
       
   231 		aStr = aStr.substr(spos+1,epos);
       
   232 	}
       
   233 	else
       
   234 	{
       
   235 		aStr = aStr.substr(spos+1,epos-1);
       
   236 
       
   237 		spos = aStr.find("\"");
       
   238 		while( spos != String::npos )
       
   239 		{
       
   240 			aStr.erase(spos,1);
       
   241 			spos = aStr.find("\"");
       
   242 		}
       
   243 	}
       
   244 
       
   245 	return;
       
   246 }
       
   247 
       
   248 /**
       
   249 MakeDirectory: Creates directory if it is not exist
       
   250 
       
   251 @internalComponent
       
   252 @released
       
   253 
       
   254 @param aPath - Directory name to be created
       
   255 */
       
   256 TBool SisUtils::MakeDirectory(String aPath)
       
   257 {
       
   258 	TBool status = ETrue;
       
   259 	TUint currpos = 0;
       
   260 	String dir;
       
   261 
       
   262 	do
       
   263 	{
       
   264 		currpos = aPath.find_first_of(PATHSEPARATOR, currpos);
       
   265 		if(currpos == String::npos)
       
   266 		{
       
   267 			dir = aPath.substr(0, aPath.length());
       
   268 		}
       
   269 		else
       
   270 		{
       
   271 			dir = aPath.substr(0, currpos);
       
   272 			currpos++;
       
   273 		}
       
   274 
       
   275 #ifdef WIN32
       
   276 		if(mkdir((char*)dir.data()) != 0)
       
   277 		{
       
   278 			if(errno != EEXIST)
       
   279 			{
       
   280 				status = EFalse;
       
   281 			}
       
   282 		}
       
   283 #else
       
   284 #error "TODO: Implement this function under other OS than Windows"
       
   285 #endif
       
   286 		if(status == EFalse)
       
   287 			break;
       
   288 	} while(currpos != String::npos);
       
   289 
       
   290 	return status;
       
   291 }