ISettingFile.c
资源名称:MenuList.rar [点击查看]
上传用户:duwei1288
上传日期:2009-12-27
资源大小:451k
文件大小:9k
源码类别:
BREW编程
开发平台:
Visual C++
- #include "ISettingFile.h"
- #include "AeeFile.h"
- #include "AeeAppGen.h"
- #define WAP_SETTINGFILE_NAME "Settings.dat"
- static ISettingFile* pSetting=NULL;
- ////////////////////////////////////////////////////////
- /// Static Function Declare ////
- ////////////////////////////////////////////////////////
- //将文件内容导入内存
- static boolean ISETTINGFILE_Load();
- //将内存内容写入文件
- static boolean ISETTINGFILE_Save();
- //为内存中的数据设置缺省值
- static boolean ISETTINGFILE_Default();
- //字符串拷贝,先释放*pVal旧的空间,然后为新值分配空间并拷贝新值
- static void ISETTINGFILE_WSTRDUP(AECHAR** pVal,AECHAR* pNewVal);
- static void ISETTINGFILE_STRTOWSTR(AECHAR** pVal,char* pNewVal);
- //读一个字符串,先读长度,再读数据
- static boolean ISETTINGFILE_ReadStr(IFile* pFile,AECHAR** pVal);
- //直接读取一个整数数据
- static int ISETTINGFILE_ReadInt(IFile* pFile);
- //写一个字符串,先写类型,再写长度,最后写数据
- static boolean ISETTINGFILE_WriteStr(IFile* pFile,int nType,AECHAR* pStr);
- //写一个整数数据,先写类型,再写数据
- static boolean ISETTINGFILE_WriteInt(IFile* pFile,int nType,int nVal);
- ////////////////////////////////////////////////////////
- /// Public Function ////
- ////////////////////////////////////////////////////////
- boolean ISETTINGFILE_CreateInstance()
- {
- //分配内存空间
- if(NULL==pSetting)
- pSetting=MALLOCREC(ISettingFile);
- else
- return TRUE;
- if(NULL==pSetting)
- return FALSE;
- //如果从文件中读取数据失败,则将数据设置为缺省值
- if(FALSE==ISETTINGFILE_Load())
- return ISETTINGFILE_Default();
- else
- return TRUE;
- }
- boolean ISETTINGFILE_Release()
- {
- if(pSetting)
- {
- //释放pSetting的数据成员空间
- FREEIF(pSetting->m_pcGatewayIP);
- //释放pSetting的空间
- FREE(pSetting);
- pSetting=NULL;
- }
- return TRUE;
- }
- boolean ISETTINGFILE_SetStr(TSettingItems item,AECHAR* pVal)
- {
- //保证配置数据已经载入内存
- if(FALSE==ISETTINGFILE_CreateInstance())
- return FALSE;
- switch(item)
- {
- case WAP_DEFHOMEPAGENAME:
- ISETTINGFILE_WSTRDUP(&pSetting->m_pcDefHomePageName,pVal);
- break;
- case WAP_DEFHOMEPAGEURL:
- ISETTINGFILE_WSTRDUP(&pSetting->m_pcDefHomePageUrl,pVal);
- break;
- case WAP_GATEWAYIP:
- ISETTINGFILE_WSTRDUP(&pSetting->m_pcGatewayIP,pVal);
- break;
- case WAP_GATEWAPUSER:
- ISETTINGFILE_WSTRDUP(&pSetting->m_pcGatewayUSER,pVal);
- break;
- case WAP_GATEWAPPASSWORD:
- ISETTINGFILE_WSTRDUP(&pSetting->m_pcGatewayPASSWORD,pVal);
- break;
- case WAP_BOOKMARKNAME:
- ISETTINGFILE_WSTRDUP(&pSetting->m_pcBookmarkName,pVal);
- break;
- case WAP_BOOKMARKURL:
- ISETTINGFILE_WSTRDUP(&pSetting->m_pcBookmarkUrl,pVal);
- break;
- default:
- return FALSE;
- }
- //将保存的设置写入文件
- return ISETTINGFILE_Save();
- }
- AECHAR* ISETTINGFILE_GetStr(TSettingItems item)
- {
- if(FALSE==ISETTINGFILE_CreateInstance())
- return NULL;
- switch(item)
- {
- case WAP_OPENURL:
- return pSetting->m_pcOpenUrl;
- case WAP_DEFHOMEPAGENAME:
- return pSetting->m_pcDefHomePageName;
- case WAP_DEFHOMEPAGEURL:
- return pSetting->m_pcDefHomePageUrl;
- case WAP_GATEWAYIP:
- return pSetting->m_pcGatewayIP;
- case WAP_GATEWAPUSER:
- return pSetting->m_pcGatewayUSER;
- case WAP_GATEWAPPASSWORD:
- return pSetting->m_pcGatewayPASSWORD;
- default:
- return NULL;
- }
- }
- boolean ISETTINGFILE_SetInt(TSettingItems item,int nVal)
- {
- if(FALSE==ISETTINGFILE_CreateInstance())
- return FALSE;
- switch(item)
- {
- case WAP_CONNECTSET:
- pSetting->m_nConnectWay=nVal;
- break;
- case WAP_GATEWAPPORT:
- pSetting->m_nGatewayPort=nVal;
- break;
- default:
- return FALSE;
- }
- //将保存的设置写入文件
- return ISETTINGFILE_Save();
- }
- int ISETTINGFILE_GetInt(TSettingItems item)
- {
- if(FALSE==ISETTINGFILE_CreateInstance())
- return 0;
- switch(item)
- {
- case WAP_CONNECTSET:
- return pSetting->m_nConnectWay;
- case WAP_GATEWAPPORT:
- return pSetting->m_nGatewayPort;
- default:
- return 0;
- }
- }
- ////////////////////////////////////////////////////////
- /// Static Function Defined ////
- ////////////////////////////////////////////////////////
- static boolean ISETTINGFILE_ReadStr(IFile* pFile,AECHAR** pVal)
- {
- int nSize=0;
- //释放指针原来的内存空间
- FREEIF(*pVal);
- //读取新的字符串的空间长度
- IFILE_Read(pFile,&nSize,sizeof(nSize));
- if(nSize>0)
- {
- //根据长度分配内存空间并将字符串从文件读入新分配的内存
- *pVal=MALLOC(nSize);
- if(nSize!=IFILE_Read(pFile,*pVal,nSize))
- {
- FREEIF(*pVal);
- }
- else
- return TRUE;
- }
- return FALSE;
- }
- static int ISETTINGFILE_ReadInt(IFile* pFile)
- {
- int nVal=0;
- IFILE_Read(pFile,&nVal,sizeof(nVal));
- return nVal;
- }
- static boolean ISETTINGFILE_WriteStr(IFile* pFile,int nType,AECHAR* pStr)
- {
- int nSize=0;
- if(pStr)
- nSize=WSTRSIZE(pStr);
- //向文件中写入参数的类型和值的大小
- //如果值为空指针,大小为0
- IFILE_Write(pFile,&nType,sizeof(nType));
- IFILE_Write(pFile,&nSize,sizeof(nSize));
- //如果值不为空指针,将字符串值写入文件
- if(pStr)
- IFILE_Write(pFile,pStr,nSize);
- return TRUE;
- }
- static boolean ISETTINGFILE_WriteInt(IFile* pFile,int nType,int nVal)
- {
- //向文件中写入参数的类型
- IFILE_Write(pFile,&nType,sizeof(nType));
- //项文件中写入参数的值
- //但参数为整形时,不需要写入值的大小
- IFILE_Write(pFile,&nVal,sizeof(nVal));
- return TRUE;
- }
- static void ISETTINGFILE_WSTRDUP(AECHAR** pVal,AECHAR* pNewVal)
- {
- FREEIF(*pVal);
- if(pNewVal)
- *pVal=WSTRDUP(pNewVal);
- }
- static void ISETTINGFILE_STRTOWSTR(AECHAR** pVal,char* pNewVal)
- {
- FREEIF(*pVal);
- if(pNewVal)
- {
- int nSize=sizeof(AECHAR)*(STRLEN(pNewVal)+1);
- *pVal=MALLOC(nSize);
- STRTOWSTR(pNewVal,*pVal,nSize);
- }
- }
- static boolean ISETTINGFILE_Default()
- {
- if(NULL==pSetting)
- return FALSE;
- //设置联接方式
- pSetting->m_nConnectWay=CONNECTION_TYPE_WSP_CL;
- //设置网关端口
- pSetting->m_nGatewayPort=80;
- //设置网关IP
- ISETTINGFILE_STRTOWSTR(&pSetting->m_pcGatewayIP,"10.0.0.172");
- return TRUE;
- }
- static boolean ISETTINGFILE_Load()
- {
- IFileMgr* pFileMgr;
- IFile* pFile;
- int nHead;
- int nSize;
- AEEApplet* pApp=(AEEApplet*)GETAPPINSTANCE();
- //打开文件
- if(!pApp||!pSetting) return FALSE;
- if(SUCCESS!=ISHELL_CreateInstance(pApp->m_pIShell,AEECLSID_FILEMGR,(void**)&pFileMgr))
- return FALSE;
- pFile=IFILEMGR_OpenFile(pFileMgr,WAP_SETTINGFILE_NAME,_OFM_READ);
- IFILEMGR_Release(pFileMgr);
- if(NULL==pFile) return FALSE;
- //从文件中导入数据
- while(TRUE)
- {
- int nByte=IFILE_Read(pFile,&nHead,sizeof(nHead));
- if(sizeof(nHead)!=nByte) break;
- nByte=IFILE_Read(pFile,&nSize,sizeof(nSize));
- if(sizeof(nSize)!=nByte) break;
- switch(nHead)
- {
- case WAP_CONNECTSET:
- pSetting->m_nConnectWay=ISETTINGFILE_ReadInt(pFile);
- break;
- case WAP_GATEWAYIP:
- ISETTINGFILE_ReadStr(pFile,&pSetting->m_pcGatewayIP);
- break;
- case WAP_GATEWAPPORT:
- pSetting->m_nGatewayPort=ISETTINGFILE_ReadInt(pFile);
- break;
- case WAP_OPENURL:
- ISETTINGFILE_ReadStr(pFile, &pSetting->m_pcOpenUrl);
- break;
- case WAP_DEFHOMEPAGENAME:
- ISETTINGFILE_ReadStr(pFile, &pSetting->m_pcDefHomePageName);
- break;
- case WAP_DEFHOMEPAGEURL:
- ISETTINGFILE_ReadStr(pFile,&pSetting->m_pcDefHomePageUrl);
- break;
- case WAP_GATEWAPUSER:
- ISETTINGFILE_ReadStr(pFile, &pSetting->m_pcGatewayUSER);
- break;
- case WAP_GATEWAPPASSWORD:
- ISETTINGFILE_ReadStr(pFile, &pSetting->m_pcGatewayPASSWORD);
- break;
- case WAP_BOOKMARKNAME:
- ISETTINGFILE_ReadStr(pFile, &pSetting->m_pcBookmarkName);
- break;
- case WAP_BOOKMARKURL:
- ISETTINGFILE_ReadStr(pFile, &pSetting->m_pcBookmarkUrl);
- break;
- default:
- IFILE_Release(pFile);
- return FALSE;
- }
- }
- IFILE_Release(pFile);
- return TRUE;
- }
- static boolean ISETTINGFILE_Save()
- {
- IFileMgr* pFileMgr;
- IFile* pFile;
- AEEApplet* pApp=(AEEApplet*)GETAPPINSTANCE();
- //打开文件
- if(!pApp||!pSetting) return FALSE;
- if(SUCCESS!=ISHELL_CreateInstance(pApp->m_pIShell,AEECLSID_FILEMGR,(void**)&pFileMgr))
- return FALSE;
- if(SUCCESS==IFILEMGR_Test(pFileMgr,WAP_SETTINGFILE_NAME))
- pFile=IFILEMGR_OpenFile(pFileMgr,WAP_SETTINGFILE_NAME,_OFM_READWRITE);
- else
- pFile=IFILEMGR_OpenFile(pFileMgr,WAP_SETTINGFILE_NAME,_OFM_CREATE);
- IFILEMGR_Release(pFileMgr);
- if(NULL==pFile) return FALSE;
- //向文件中导入数据
- ISETTINGFILE_WriteStr(pFile,WAP_GATEWAYIP,pSetting->m_pcGatewayIP);
- ISETTINGFILE_WriteInt(pFile,WAP_CONNECTSET,pSetting->m_nConnectWay);
- ISETTINGFILE_WriteInt(pFile,WAP_GATEWAPPORT,pSetting->m_nGatewayPort);
- ISETTINGFILE_WriteStr(pFile,WAP_OPENURL,pSetting->m_pcOpenUrl);
- ISETTINGFILE_WriteStr(pFile,WAP_GATEWAPUSER,pSetting->m_pcGatewayUSER);
- ISETTINGFILE_WriteStr(pFile,WAP_GATEWAPPASSWORD,pSetting->m_pcGatewayPASSWORD);
- ISETTINGFILE_WriteStr(pFile,WAP_DEFHOMEPAGENAME,pSetting->m_pcDefHomePageName);
- ISETTINGFILE_WriteStr(pFile,WAP_DEFHOMEPAGEURL,pSetting->m_pcDefHomePageUrl);
- ISETTINGFILE_WriteStr(pFile,WAP_BOOKMARKNAME,pSetting->m_pcBookmarkName);
- ISETTINGFILE_WriteStr(pFile,WAP_BOOKMARKURL,pSetting->m_pcBookmarkUrl);
- IFILE_Release(pFile);
- return TRUE;
- }
- ////////////////////////////////////////////////////////////////////
- int ISETTINGFILE_Test()
- {
- AECHAR pp[]={'A','B','C','D','E','F',0};
- ISETTINGFILE_CreateInstance();
- ISETTINGFILE_Release();
- ISETTINGFILE_Save();
- ISETTINGFILE_Load();
- ISETTINGFILE_SetStr(WAP_GATEWAYIP,pp);
- return 0;
- }