?? wk_basefunc.cpp
字號:
// wk_BaseFunc.cpp
//////////////////////////////////////////////////////////////////////
#include "wk_BaseFunc.h"
#include "AEEHeap.h"
//////////////////////////////////////////////////////////////////////
//***********************************************************************
// Function:
// Purpose:
// - Loads a text string from the resource file as AECHAR.
// Parameters (in):
// - pIShell: Pointer to IShell interface
// - iResourceID: ID of the text string in the resource file
// - pBuf: Pointer to an AECHAR buffer to hold the string
// - szResFile: the resource file name
// Returns:
// - TRUE: String successfully loaded
// - FALSE: Failed to load string
//***********************************************************************
boolean LoadTextMessage(IShell * pIShell, int16 iResourceID, AECHAR * * pBuf, char * szResFile) {
AECHAR * wszMsg = NULL;
int iTextLength = 0;
// validate parameter(s)
if (!pIShell) {
return FALSE;
}
if (pBuf) {
// Using ISHELL_LoadResData to query the length of the string before we
// allocate the buffer and read it in.
wszMsg = (AECHAR *) ISHELL_LoadResData(pIShell, szResFile, iResourceID, RESTYPE_STRING);
if (wszMsg) {
iTextLength = WSTRLEN((AECHAR *) wszMsg);
ISHELL_FreeResData(pIShell, wszMsg);
wszMsg = NULL;
// if it was allocated before, release it
if (* pBuf) {
FREEIF(* pBuf);
}
* pBuf = (AECHAR *) MALLOC(sizeof(AECHAR) * (iTextLength + 1));
if (* pBuf) {
MEMSET(* pBuf, 0, sizeof(AECHAR) * (iTextLength + 1));
if (ISHELL_LoadResString(pIShell, szResFile, iResourceID, * pBuf, sizeof(AECHAR) * (iTextLength + 1))) {
return TRUE;
}
}
}
}
// couldn't load string from resource file - unrecoverable error, clean up & bail out
if (wszMsg) {
FREEIF(wszMsg);
}
if (pBuf && * pBuf) {
FREEIF(* pBuf);
}
return FALSE;
} // End of function
//***********************************************************************
// Function:
// Purpose:
// - Loads a text string from the resource file and converts it
// to a single-byte character format.
// Parameters (in):
// - pThis: Application data structure pointer
// - iResourceID: ID of the text string in the resource file
// - pBuf: Pointer to a character buffer to hold the string
// Returns:
// - TRUE: String successfully loaded
// - FALSE: Failed to load string or store it in app's data struct.
//***********************************************************************
boolean LoadTextMessageSingleByte(IShell * pIShell, int16 iResourceID, char * * pBuf, char * szResFile) {
AECHAR * wszText = NULL;
int iTextLength = 0;
// validate parameter(s)
if (!pIShell) {
return FALSE;
}
if (pBuf && LoadTextMessage(pIShell, iResourceID, & wszText, szResFile)) {
iTextLength = WSTRLEN(wszText);
// if it was allocated before, release it
if (* pBuf) {
FREEIF(* pBuf);
}
* pBuf = (char *) MALLOC(sizeof(char) * (iTextLength + 1));
if (* pBuf) {
MEMSET(* pBuf, 0, sizeof(char) * (iTextLength + 1));
WSTRTOSTR(wszText, * pBuf, iTextLength + 1);
FREEIF(wszText);
return TRUE;
}
}
// couldn't load string from resource file - unrecoverable error, clean up & bail out
if (wszText) {
FREEIF(wszText);
}
if (pBuf && * pBuf) {
FREEIF(* pBuf);
}
return FALSE;
} // End function
/*===========================================================================
FUNCTION GetMobileID
DESCRIPTION
獲取手機的ID
===========================================================================*/
boolean GetMobileID(IShell * pIShell, char * * pBuf) {
ITAPI * pITAPI = NULL;
boolean bRet = FALSE;
/* Test code /
if (* pBuf) { FREEIF(* pBuf); }
*pBuf = STRDUP("555555555");
return TRUE;
// End of Test code */
int i = ISHELL_CreateInstance(pIShell, AEECLSID_TAPI, (void * *) & pITAPI);
if (pBuf && i == SUCCESS)
{
TAPIStatus * ps = MALLOCREC(TAPIStatus);
if (ITAPI_GetStatus(pITAPI, ps) == SUCCESS)
{
if (* pBuf) FREEIF(* pBuf);
i = STRLEN(ps->szMobileID) + 1;
* pBuf = (char *) MALLOC(sizeof(char) * i);
if (* pBuf)
{
MEMCPY(* pBuf, ps->szMobileID, i);
bRet = TRUE;
}
}
else
{
if (* pBuf) FREEIF(* pBuf);
*pBuf = (char *)MALLOC(1);
MEMSET(pBuf, 0, 1);
}
if (ps) FREEIF(ps);
}
else
{
if (* pBuf) FREEIF(* pBuf);
*pBuf = (char *)MALLOC(1);
MEMSET(pBuf, 0, 1);
}
if (pITAPI)
{ // 釋放資源
ITAPI_Release(pITAPI);
pITAPI = NULL;
}
return bRet;
}
/*===========================================================================
FUNCTION GetMobileType
DESCRIPTION
獲取手機的型號
===========================================================================*/
boolean GetMobileType(IShell * pIShell, char * * pBuf)
{
AEEDeviceInfo device;
char szBuf[128];
long nLen;
device.wStructSize = sizeof(AEEDeviceInfo);
ISHELL_GetDeviceInfo(pIShell, &device);
SPRINTF(szBuf, "%d", device.dwPlatformID);
nLen = (STRLEN(szBuf) + 1) * sizeof(char);
if (* pBuf) FREEIF(* pBuf);
*pBuf = (char *)MALLOC(nLen);
MEMCPY(*pBuf, szBuf, nLen);
return TRUE;
}
/*===========================================================================
FUNCTION PrependStr
DESCRIPTION
insert the src to the begin of dest
===========================================================================*/
void PrependStr(char * * dest, char * src)
{
if (* dest && src) {
int lenDest = STRLEN(* dest);
int lenSrc = STRLEN(src);
char * temp = (char *) STRDUP(* dest);
FREEIF(* dest);
* dest = (char *) MALLOC(sizeof(char) * (lenDest + lenSrc + 1));
STRCAT(* dest, src);
STRCAT(* dest, temp);
FREEIF(temp);
}
}
/*===========================================================================
FUNCTION ReplaceChar
DESCRIPTION
Replace a char with another in a string
===========================================================================*/
void ReplaceChar( char * szSource, char cSrc, char cDes )
{
if ( szSource ) {
int size = STRLEN(szSource);
int i;
for ( i = 0; i < size; i++ )
{
if ( *szSource == cSrc )
*szSource = cDes;
szSource++;
}
}
}
/*===========================================================================
FUNCTION: ReleaseObj
DESCRIPTION:
Release an object if it is not NULL, and set it to NULL.
如果ppObj不為NULL,則釋放它,并將之置為NULL。
===========================================================================*/
void ReleaseObj(void * * ppObj)
{
if (ppObj && * ppObj)
{
(void)IBASE_Release(((IBase *) * ppObj));
* ppObj = NULL;
}
}
/*===========================================================================
FUNCTION: LoadWideText
DESCRIPTION:
Loads a text string from the resource file as AECHAR.
從資源文件中讀取一個寬字符串(AECHAR)。
===========================================================================*/
boolean LoadWideText (IShell * pIShell, // 指向IShell對象的指針
int16 iResourceID, // 字符串資源ID
AECHAR ** pBuf, // (輸出)保存字符串的緩沖,需要使用FREEIF()來釋放
char * szResFile // 資源文件名
)
{
AECHAR * wszMsg = NULL;
int iLength = 0;
if (!pIShell) return FALSE;
if (pBuf) {
// 首先使用ISHELL_LoadResData方法以獲取字符串的長度
wszMsg = (AECHAR *) ISHELL_LoadResData(pIShell, szResFile, iResourceID, RESTYPE_STRING);
if (wszMsg)
{
// 得到字符串的長度
iLength = WSTRLEN((AECHAR *) wszMsg);
ISHELL_FreeResData(pIShell, wszMsg);
wszMsg = NULL;
// 如果已經為pBuf分配過內存,則釋放它
if (* pBuf)
FREEIF(* pBuf);
// 為pBuf分配內存
* pBuf = (AECHAR *) MALLOC(sizeof(AECHAR) * (iLength + 1));
if (* pBuf)
{
// 將已分配的內存置零
MEMSET(* pBuf, 0, sizeof(AECHAR) * (iLength + 1));
if (ISHELL_LoadResString(pIShell, szResFile, iResourceID, * pBuf, sizeof(AECHAR) * (iLength + 1)))
{
// 成功讀取字符串,返回TRUE
return TRUE;
}
}
}
}
// 釋放已分配的內存
if (wszMsg)
FREEIF(wszMsg);
if (pBuf && * pBuf)
FREEIF(* pBuf);
// 讀取字符串失敗,返回FALSE
return FALSE;
}
/*===========================================================================
FUNCTION: LoadText
DESCRIPTION:
Loads a text string from the resource file as char.
從資源文件中讀取一個單字節字符串(char)。
===========================================================================*/
boolean LoadText (IShell * pIShell, // 指向IShell對象的指針
int16 iResourceID, // 字符串資源ID
char * * pBuf, // (輸出)保存字符串的緩沖,需要使用FREEIF()來釋放
char * szResFile // 資源文件名
)
{
AECHAR * wszText = NULL;
int iLength = 0;
if (!pIShell) return FALSE;
// 先將字符串從資源文件中作為AECHAR讀出,再轉換為char
if (pBuf && LoadWideText(pIShell, iResourceID, & wszText, szResFile))
{
iLength = WSTRLEN(wszText);
// 如果已經為pBuf分配過內存,則釋放它
if (* pBuf)
FREEIF(* pBuf);
// 為pBuf分配內存
* pBuf = (char *) MALLOC(sizeof(char) * (iLength + 1));
if (* pBuf)
{
// 將已分配的內存置零
MEMSET(* pBuf, 0, sizeof(char) * (iLength + 1));
// 將寬字符串轉為單字節字符串
WSTRTOSTR(wszText, * pBuf, iLength + 1);
// 釋放內存
FREEIF(wszText);
// 成功讀取字符串,返回TRUE
return TRUE;
}
}
// 釋放已分配的內存
if (wszText)
FREEIF(wszText);
if (pBuf && * pBuf)
FREEIF(* pBuf);
// 讀取字符串失敗,返回FALSE
return FALSE;
}
void PrintString(IStatic * pIStatic, AECHAR * szTitle, char * szText, boolean bAppend)
{
if (pIStatic) {
if (szTitle) ISTATIC_SetText(pIStatic, szTitle, NULL, AEE_FONT_BOLD, AEE_FONT_NORMAL);
if (szText) ISTATIC_SetTextEx(pIStatic, (byte *) szText, NULL, bAppend); // the last TRUE means APPEND text
ISTATIC_SetActive(pIStatic, TRUE);
ISTATIC_Redraw(pIStatic);
}
}
void PrintString2(IStatic * pIStatic, AECHAR * szTitle, AECHAR * szText, boolean bAppend)
{
if (pIStatic) {
if (szTitle) ISTATIC_SetText(pIStatic, szTitle, NULL, AEE_FONT_BOLD, AEE_FONT_NORMAL);
if (szText) ISTATIC_SetTextEx(pIStatic, (byte *) szText, NULL, bAppend); // the last TRUE means APPEND text
ISTATIC_SetActive(pIStatic, TRUE);
ISTATIC_Redraw(pIStatic);
}
}
boolean AddMenuItem(IMenuCtl * pMenu, uint16 wTextID, AECHAR * pText, uint16 wImageID, uint16 wItemID, uint32 dwData)
{
CtlAddItem ai;
if (pMenu == NULL) return FALSE;
// Fill in the CtlAddItem structure values
ai.pText = pText;
ai.pImage = NULL;
ai.pszResImage = RES_FILE;
ai.pszResText = RES_FILE;
ai.wText = wTextID;
ai.wFont = AEE_FONT_NORMAL;
ai.wImage = wImageID;
ai.wItemID = wItemID;
ai.dwData = dwData;
// Add the item to the menu control
return IMENUCTL_AddItemEx( pMenu, &ai );
}
// 保存文件
boolean SaveFile(IShell * pIShell, IFileMgr **ppFile, char * szFile, char * szData, char * szTmpFile, long nLength)
{
IFile *f;
if (pIShell == NULL || ppFile == NULL) return FALSE;
// 先刪除文件,避免重復
DeleteFile(pIShell, ppFile, szFile);
if(szData)
{
ISHELL_CreateInstance(pIShell, AEECLSID_FILEMGR, (void **)ppFile);
f = IFILEMGR_OpenFile (*ppFile, szFile, _OFM_CREATE);
if (f)
{
/*
//判斷空間
if(GetFileSpace(pIShell)<(uint32)nLength)
{
IFILE_Release(f);
IFILEMGR_Release(*ppFile);
*ppFile = NULL;
return FALSE;
}
*/
// 以1024字節為單位,逐步寫入文件信息
long cstLength = 1024;
long nLen = nLength;
long i = 0, nBuffer = cstLength;
char szBuffer[1024];
//IFILE_Release(f);
//f = IFILEMGR_OpenFile (*ppFile, szFile, _OFM_APPEND);
while (i < nLen)
{
if ((nLen - i) > cstLength)
nBuffer = cstLength;
else
nBuffer = nLen - i;
MEMCPY(szBuffer, szData + i, nBuffer);
IFILE_Write (f, szBuffer, nBuffer);
i += nBuffer;
}
IFILE_Release(f);
}
else
{
IFILEMGR_Release(*ppFile);
*ppFile = NULL;
return FALSE;
}
IFILEMGR_Release(*ppFile);
*ppFile = NULL;
}
else //將臨時文件里的內容保存
{
ISHELL_CreateInstance(pIShell, AEECLSID_FILEMGR, (void **)ppFile);
f = IFILEMGR_OpenFile (*ppFile, szTmpFile, _OFM_READ);
if (f)
{
IFile* f1;
FileInfo pInfo;
IFILE_GetInfo(f,&pInfo);
nLength = pInfo.dwSize;
f1 = IFILEMGR_OpenFile (*ppFile, szFile, _OFM_CREATE);
if(f1)
{
/*
//判斷空間
if(GetFileSpace(pIShell)<(uint32)nLength)
{
IFILE_Release(f);
IFILEMGR_Release(*ppFile);
*ppFile = NULL;
return FALSE;
}
*/
// 以1024字節為單位,逐步寫入文件信息
long cstLength = 1024;
long nLen = nLength;
long i = 0, nBuffer = cstLength;
char szBuffer[1024];
//IFILE_Release(f);
//f = IFILEMGR_OpenFile (*ppFile, szFile, _OFM_APPEND);
while (i < nLen)
{
if ((nLen - i) > cstLength)
nBuffer = cstLength;
else
nBuffer = nLen - i;
IFILE_Read(f,szBuffer,nBuffer);
IFILE_Write (f1, szBuffer, nBuffer);
i += nBuffer;
}
IFILE_Release(f);
IFILE_Release(f1);
}
else
{
IFILE_Release(f);
IFILEMGR_Release(*ppFile);
*ppFile = NULL;
return FALSE;
}
}
else
{
IFILEMGR_Release(*ppFile);
*ppFile = NULL;
return FALSE;
}
IFILEMGR_Release(*ppFile);
*ppFile = NULL;
}
return TRUE;
}
// 刪除指定文件
boolean DeleteFile(IShell * pIShell, IFileMgr **ppFile, char * szFile)
{
boolean bResult = FALSE;
if (pIShell == NULL || ppFile == NULL) return FALSE;
ISHELL_CreateInstance(pIShell, AEECLSID_FILEMGR, (void **)ppFile);
bResult = (IFILEMGR_Remove(*ppFile, szFile) == SUCCESS);
IFILEMGR_Release(*ppFile);
*ppFile = NULL;
return bResult;
}
// 保存文本信息至文件
int SaveContent(IShell * pIShell, IFileMgr **ppFile, char * szTempFile, AECHAR* szName, long nType, long nID)
{
char szFile[256];
char szExt[4];
boolean bTemp;
if (pIShell == NULL || ppFile == NULL) return SAVEFILE_ERROR;
// 獲取文件名稱
switch (nType & 0xffff0000)
{
case SOUND_MID:
STRCPY(szExt, "mid");
break;
case SOUND_MMF:
STRCPY(szExt, "mmf");
break;
case SOUND_PMD:
STRCPY(szExt, "pmd");
break;
case SOUND_MP3:
STRCPY(szExt, "mp3");
break;
case SOUND_QCP:
STRCPY(szExt, "qcp");
break;
case SOUND_WAV:
STRCPY(szExt, "wav");
break;
case PIC_PNG:
STRCPY(szExt, "png");
break;
case PIC_BMP:
STRCPY(szExt, "bmp");
break;
case PIC_GIF:
STRCPY(szExt, "gif");
break;
case PIC_JPG:
STRCPY(szExt, "jpg");
break;
case PIC_BCI:
STRCPY(szExt, "bci");
break;
default:
STRCPY(szExt, "txt");
break;
}
bTemp = CheckFileName(pIShell,ppFile, szFile, szExt, nID);
if (!bTemp)
{// 錯誤提示
/* AECHAR * str = NULL;
LoadWideText(pIShell, IDS_ERR_OVERMAXFILES, &str, RES_FILE);
PrintString2(pWebData->m_pIStatic, szName, str, FALSE);
FREEIF(str);*/
return SAVEFILE_EXIST;
}
// 保存文件
bTemp = SaveFile(pIShell, ppFile, szFile, NULL,szTempFile, 0);
if (!bTemp)
{// 錯誤提示
/* AECHAR * str = NULL;
LoadWideText(pMe->app.m_pIShell, IDS_ERR_WRITEFILE, &str, RES_FILE);
PrintString2(pWebData->m_pIStatic, pWebData->szCategory, str, FALSE);
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -