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

📄 wk_basefunc.cpp

📁 联通手机的宝典画面.用brew 平台编辑而成.
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// 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 + -