secureswitools/swisistools/source/sisxlibrary/utility_windows.cpp
changeset 0 ba25891c3a9e
child 12 7ca52d38f8c3
equal deleted inserted replaced
-1:000000000000 0:ba25891c3a9e
       
     1 /*
       
     2 * Copyright (c) 2007-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 /**
       
    20  @file 
       
    21  @internalComponent
       
    22  @released
       
    23 */
       
    24 
       
    25 #include "utility_interface.h"
       
    26 #include "exception.h"
       
    27 
       
    28 #include <direct.h>
       
    29 #include <windows.h>
       
    30 #include <tchar.h>
       
    31 #include <stdio.h>
       
    32 #include <io.h>
       
    33 #include <sys/stat.h>
       
    34 
       
    35 wchar_t** CommandLineArgs(int &argc ,char *argv[])
       
    36 	{
       
    37 	// Entry point of tool.
       
    38 	return CommandLineToArgvW( GetCommandLineW(), &argc);
       
    39 	}
       
    40 
       
    41 void cleanup(int argc, wchar_t **argv)
       
    42 	{
       
    43 	return;
       
    44 	}
       
    45 
       
    46 int CompareTwoString(wchar_t* string ,wchar_t* option)
       
    47 	{
       
    48 	return wcsicmp(string,option);
       
    49 	}
       
    50 
       
    51 int CompareNString(wchar_t* string ,wchar_t* option, int len)
       
    52 	{
       
    53 	return wcsnicmp(string,option,len);
       
    54 	}
       
    55 
       
    56 bool UnicodeMarker(void *hfile)
       
    57 	{
       
    58 	return 1;
       
    59 	}
       
    60 
       
    61 TUint64 GetSizeOfFile(HANDLE hFile)
       
    62 	{
       
    63 	ULARGE_INTEGER li;
       
    64 	li.LowPart = GetFileSize(hFile, &li.HighPart);
       
    65 	::CloseHandle(hFile);
       
    66 	return li.QuadPart;
       
    67 	}
       
    68 
       
    69 char* GetTempFile()
       
    70 	{
       
    71 	char *tmpName = _tempnam(NULL, "tmpmakesis");
       
    72 	return tmpName;
       
    73 	}
       
    74 
       
    75 void TransferFileData(const wchar_t *Fromfile , char* Tofile)
       
    76 	{
       
    77 	int numread = 0;
       
    78 	byte buffer[512];
       
    79 
       
    80 	FILE *fp = NULL, *tp = NULL;
       
    81 
       
    82 	if((fp=_wfopen(Fromfile, L"rb")) != NULL)
       
    83 		{
       
    84 		if((tp = fopen(Tofile, "wb")) != NULL)
       
    85 			{
       
    86 			while(!feof(fp))
       
    87 				{
       
    88 				if((numread = fread(buffer, sizeof(byte), 255, fp))>0)
       
    89 					{
       
    90 					fwrite(buffer, sizeof(byte), numread, tp);
       
    91 					}
       
    92 				}
       
    93 			fclose(fp);
       
    94 			fclose(tp);
       
    95 			}
       
    96 		}
       
    97 	}
       
    98 
       
    99 int FullPath(wchar_t * pszAbsolutePath, const wchar_t * pszRelativePath, size_t maxLength)
       
   100 // Converts a relative path to an absolute path under Win95 and WinNT
       
   101 	{
       
   102 	char pszMultiByteRelative[PATHMAX] = "\0";
       
   103 	char pszMultiByteAbsolute[PATHMAX] = "\0";
       
   104 	LPWSTR p=(LPWSTR)pszRelativePath;
       
   105 	
       
   106 	if (!wcsncmp(pszRelativePath,L".\\",2)) p+=2;
       
   107 	::WideCharToMultiByte(CP_OEMCP,				// code page
       
   108 		0,					// performance and mapping flags
       
   109 		p,			 		// address of wide-character string
       
   110 		-1,					// number of characters in string
       
   111 		pszMultiByteRelative,	// address of buffer for new string
       
   112 		PATHMAX,		// size of buffer
       
   113 		NULL,				// address of default for unmappable characters
       
   114 		NULL);				// address of flag set when default char. used
       
   115 	
       
   116 	int returnValue = _fullpath(pszMultiByteAbsolute, pszMultiByteRelative, maxLength)
       
   117 		? 1 : 0;
       
   118 	
       
   119 	::MultiByteToWideChar(CP_OEMCP,
       
   120 		0,
       
   121 		pszMultiByteAbsolute,
       
   122 		-1,
       
   123 		pszAbsolutePath,
       
   124 		PATHMAX);
       
   125 	
       
   126 	return returnValue;
       
   127 	}
       
   128 
       
   129 HANDLE MakeSISOpenFile(LPCWSTR pszFilename, DWORD dwAccessMode, DWORD dwCreateFlags)
       
   130 // Open file with Unicode filename correctly under Win95 and WinNT
       
   131 	{
       
   132 	return CreateFileW(pszFilename, dwAccessMode, 0, NULL, dwCreateFlags, FILE_ATTRIBUTE_NORMAL, NULL);
       
   133 	}
       
   134 
       
   135 BOOL MakeSISDeleteFile(LPCWSTR pszFilename)
       
   136 	{
       
   137 	// Delete file with Unicode filename correctly under Win95 and WinNT
       
   138 	return DeleteFileW(pszFilename);
       
   139 	}
       
   140 
       
   141 bool CreateDir(const wchar_t* aPathName)
       
   142 	{
       
   143 	return ::CreateDirectoryW(aPathName, NULL)? true: false;
       
   144 	}
       
   145 
       
   146 int FileAttributes( LPCTSTR lpFileName )
       
   147 	{
       
   148 	return ::GetFileAttributesW( lpFileName );
       
   149 	}
       
   150 
       
   151 void WriteToFile(const std::wstring& aFileName, const TUint8* aFileData, int aFileLength)
       
   152 	{
       
   153 	_wchmod(aFileName.c_str(), _S_IREAD | _S_IWRITE);
       
   154 	HANDLE file = ::MakeSISOpenFile(aFileName.c_str(), GENERIC_WRITE, CREATE_ALWAYS);
       
   155 	CSISException::ThrowIf ((INVALID_HANDLE_VALUE == file), CSISException::EFileProblem, std::wstring (L"cannot create ") + aFileName);
       
   156 	
       
   157 	size_t written = 0;
       
   158 	::WriteFile(file,aFileData, aFileLength, (DWORD*)&written,0);
       
   159 	::CloseHandle(file);
       
   160 	CSISException::ThrowIf ((aFileLength != written), CSISException::EFileProblem, std::wstring (L"error writting to file ") + aFileName);
       
   161 	}
       
   162 
       
   163 int ConvertWideCharToMultiByte(const wchar_t* aSource, int aSourceLen, char* aTarget, int aTargetLen, TUint32 aCodePage)
       
   164 	{
       
   165 	if(0 == aCodePage)
       
   166 		{
       
   167 		aCodePage = CP_OEMCP;
       
   168 		}
       
   169 	return WideCharToMultiByte( aCodePage, 0, aSource, aSourceLen, aTarget, aTargetLen, NULL, NULL);
       
   170 	}
       
   171 
       
   172 int ConvertMultiByteToWideChar(const char* aSource, int aSourceLen, wchar_t* aTarget, int aTargetLen, TUint32 aCodePage)
       
   173 	{
       
   174 	if(0 == aCodePage)
       
   175 		{
       
   176 		aCodePage = CP_UTF8;
       
   177 		}
       
   178 	return MultiByteToWideChar( aCodePage, 0, aSource, aSourceLen, aTarget, aTargetLen);
       
   179 	}
       
   180 
       
   181 int GetErrorValue()
       
   182 	{
       
   183 	return ::GetLastError();
       
   184 	}
       
   185