⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 util.cpp

📁 PGP8.0源码 请认真阅读您的文件包然后写出其具体功能
💻 CPP
📖 第 1 页 / 共 2 页
字号:
////////////////////////////////////////////////////////////////////////////////
//
//  File : Util.cpp
//
//  Copyright (C) 2002 PGP Corporation
//
//  ABSTRACT
//		Utility routines
// 
//  Author: Satya S. Das
//
////////////////////////////////////////////////////////////////////////////////

#include "util.h"
#include "globals.h"
#include "globdefs.h"
#include <stdio.h>
#include <crtdbg.h>
#include "pgpdebug.h"
#include "pgplnlib.h"
#include "resource.h"
#include "windows.h"

//function to convert wide string to ansi
//NOTE: caller must test for null return value. no null
//terminated empty wide string is returned from this fn
char* ConvertToAnsi(OLECHAR *szWideStringToConvert)
{
	//locals
	int iRet=0;
	ULONG ulCnt=0;
	char *pStr=NULL;

	//check parameters
	pgpAssert(NULL != szWideStringToConvert);
	if(NULL == szWideStringToConvert)
		return NULL;

	//find out how long of a buffer we need to convert. the return
	//length includes the terminating null character
	ulCnt = WideCharToMultiByte(CP_ACP, 0, szWideStringToConvert, -1, 
				NULL, 0, NULL, NULL);

	//allocate the buffer
	try
	{
		pStr = new char[ulCnt];
	}
	catch(...)
	{
	}

	//if allocation failed we return null
	if(NULL == pStr)
		return NULL;

	//fill it up with the converted string
	iRet=WideCharToMultiByte(CP_ACP, 0, szWideStringToConvert, -1, 
			pStr, ulCnt, NULL, NULL);
	pgpAssert(0 != iRet);//successful translation
	pgpAssert(iRet == (int)ulCnt);//counts are ok

	return pStr;
} 

//function to convert ansi string to wide
//NOTE: caller must test for null return value. no null
//terminated empty ansi string is returned from this fn
OLECHAR* ConvertToUnicode(char *szAnsiString)
{
	//locals
	int iRet=0;
	ULONG ulCnt=0;
	OLECHAR	*pOleStr=0;

	//check parameters
	pgpAssert(NULL != szAnsiString);
	if(NULL == szAnsiString)
		return NULL;

	//see how big the unicode string will be
	ulCnt=MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, szAnsiString, -1, NULL, 0)+1;

	//allocate a buffer for the unicode string
	try
	{
		pOleStr = new OLECHAR[ulCnt];
	}
	catch(...)
	{
	}

	//if allocation failed we return null
	if(NULL == pOleStr)
		return NULL;

	//fill the buffer with the converted string
	iRet=MultiByteToWideChar(CP_ACP, 0, szAnsiString, -1, pOleStr, ulCnt);
	pgpAssert(0 != iRet);//successful translation
	pgpAssert(iRet+1 == (int)ulCnt);//counts are good

	return pOleStr;
}


void ErrorMessage(LPSTR lpsz)
{
	MessageBox(NULL, lpsz, "Error", MB_OK|MB_ICONINFORMATION);
}


//tree item information retrieval
BOOL TV_GetString(HWND hwndTV, HTREEITEM hItem, TCHAR *szText, UINT cchTextMax)
{
    TV_ITEM	tvItem;


	if(IsWindow(hwndTV) && hItem) 
	{
		tvItem.mask = TVIF_TEXT;
		tvItem.hItem = hItem;
		tvItem.pszText = szText;
		tvItem.cchTextMax = cchTextMax;
		return TreeView_GetItem(hwndTV, &tvItem);
	}
	else
		return FALSE;
}

BSTR A2WBSTR(LPCSTR lpszAnsiString, int nLen/*= -1*/)
{
	BSTR bstrOutputString = NULL;//buffer for output string
	WCHAR *pwszWideString=NULL;
	int iRet=0, iConvertedLen=0;

	//check parameters
	pgpAssert(NULL != lpszAnsiString);//the ansi string parameter cannot be null
	if(NULL == lpszAnsiString)
		return NULL;

	//if length of the ansi string was passed to us
	if(-1 != nLen)
	{
		//make sure the buffer is readable
		pgpAssert(TRUE != IsBadReadPtr(lpszAnsiString, nLen));
		if(TRUE == IsBadReadPtr(lpszAnsiString, nLen))
			return NULL;
	}
	
	//find out the buffer requirements for wide char conversion
	iRet = MultiByteToWideChar(CP_ACP, 0, lpszAnsiString, 
			nLen, NULL, NULL);
	pgpAssert(0 != iRet);
	if(0 == iRet)//failure encountered during conversion
	{
		//TODO:do an error string lookup and debug output string
		return NULL;
	}

	//if we were told to find out the length requirements on our own
	if (-1 == nLen)
		iConvertedLen = iRet+1;

	//allocate the required length
	pwszWideString=(WCHAR *)calloc(iConvertedLen, sizeof(WCHAR));
	pgpAssert(NULL != pwszWideString);
	if (NULL != pwszWideString)
	{
		iRet=MultiByteToWideChar(CP_ACP, 0, lpszAnsiString, -1, 
				pwszWideString, iConvertedLen);
		pgpAssert(0 != iRet);//should succeed
		if(0 == iRet)//if failed to convert
		{
			//TODO:do an error string lookup and debug output string
			//free up the allocated buffer
			free(pwszWideString);
			pwszWideString=NULL;
			return NULL;//bail out
		}

		bstrOutputString=SysAllocString(pwszWideString);
		pgpAssert(NULL != bstrOutputString);
	}

	if(NULL != pwszWideString)
	{
		free(pwszWideString);
		pwszWideString=NULL;
	}

	return bstrOutputString;
} 

char * GetIconsFilePath()
{
	char *pszTemp=NULL, *pszIconsFilePath=NULL;
	int iLenAlloced=MAX_PATH;//to keep track of length allocated
							 //we jump it by MAX_PATH everytime
							 //we think truncation happened
	int iLenReqd=0;
	int iIndex=-1;
	DWORD dwRet=0;

	while(TRUE)
	{
		//free up the earlier buffer allocations if any
		if(NULL != pszIconsFilePath)
		{
			free(pszIconsFilePath);
			pszIconsFilePath=NULL;
		}
		
		//allcate again
		pgpAssert(NULL == pszIconsFilePath);
		pszIconsFilePath = (char *)calloc(iLenAlloced, 1);
		pgpAssert(NULL != pszIconsFilePath);

		//if we failed to allocate
		if(NULL == pszIconsFilePath)
		{
			pgpAssert(FALSE);//ran out of memory ??
			return NULL;
		}

		//attempt to get the path again
		dwRet=GetModuleFileName(g_hInstance, pszIconsFilePath, iLenAlloced-1);
		if((dwRet > 0) && ((int)dwRet < iLenAlloced))//return value does not include null
		{
#ifdef _DEBUG
			//check if our dll name is at the end of path returned
			pszTemp=strrchr(pszIconsFilePath, '\\');
			pgpAssert(NULL != pszTemp);
			if(NULL != pszTemp)
				pgpAssert(0 == stricmp(pszTemp+1, MODULEFILENAME));
#endif
			break;//we have successfully retrieved the path
		}
		
		//bump the alloc length by max_path and lets hope that is enough
		iLenAlloced+=MAX_PATH;
	}


	PgpGwTrace("GetIconsFilePath returning %s\n", pszIconsFilePath);
	return pszIconsFilePath;
}

void DisplayMessage(HWND hWnd, UINT uiResourceID, ...)
{
	char *pszTitle=NULL;
	char *pszBuffer=NULL;
	int iCurCharInd=0;
	int iNoOfParamsExp=0;
	va_list valArgList;
		
	BOOL bRet=SafeLoadString(IDS_TITLE, &pszTitle);
	pgpAssert(TRUE == bRet);//string is not found or some error condition
	pgpAssert(NULL != pszTitle);//potential low memory
	if((FALSE == bRet) || (NULL == pszTitle))
		goto cleanup;

	bRet=SafeLoadString(uiResourceID, &pszBuffer);
	pgpAssert(TRUE == bRet);//string is not found or some error condition
	pgpAssert(NULL != pszBuffer);//potential low memory
	if((FALSE == bRet) || (NULL == pszBuffer))
		goto cleanup;

	//if there is at least one format specification in there
	if(NULL != strchr(pszBuffer, '%'))
	{
		//we take the args and format the error string
		char szMessage[512]={0};
		va_start(valArgList, uiResourceID);
		_vsnprintf(szMessage, sizeof(szMessage), pszBuffer, valArgList);
		MessageBox(hWnd, szMessage, pszTitle, MB_ICONEXCLAMATION);
	}
	else
	{
		//there are no format specifications to be taken care of
		MessageBox(hWnd, pszBuffer, pszTitle, MB_ICONEXCLAMATION);
	}

	cleanup:
	if(NULL != pszTitle)
	{
		free(pszTitle);
		pszTitle=NULL;
	}

	if(NULL != pszBuffer)
	{
		free(pszBuffer);
		pszBuffer=NULL;
	}

	return;
}

#if (WINVER < 0x0500)

//copied from winuser.h. quick fix for compilation failures
typedef struct tagGUITHREADINFO
{
    DWORD   cbSize;
    DWORD   flags;
    HWND    hwndActive;
    HWND    hwndFocus;
    HWND    hwndCapture;
    HWND    hwndMenuOwner;
    HWND    hwndMoveSize;
    HWND    hwndCaret;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -