ISettingFile.c
上传用户:duwei1288
上传日期:2009-12-27
资源大小:451k
文件大小:9k
源码类别:

BREW编程

开发平台:

Visual C++

  1. #include "ISettingFile.h"
  2. #include "AeeFile.h"
  3. #include "AeeAppGen.h"
  4. #define WAP_SETTINGFILE_NAME "Settings.dat"
  5. static ISettingFile* pSetting=NULL;
  6. ////////////////////////////////////////////////////////
  7. ///  Static Function Declare ////
  8. ////////////////////////////////////////////////////////
  9. //将文件内容导入内存
  10. static boolean ISETTINGFILE_Load();
  11. //将内存内容写入文件
  12. static boolean ISETTINGFILE_Save();
  13. //为内存中的数据设置缺省值
  14. static boolean ISETTINGFILE_Default();
  15. //字符串拷贝,先释放*pVal旧的空间,然后为新值分配空间并拷贝新值
  16. static void ISETTINGFILE_WSTRDUP(AECHAR** pVal,AECHAR* pNewVal);
  17. static void ISETTINGFILE_STRTOWSTR(AECHAR** pVal,char* pNewVal);
  18. //读一个字符串,先读长度,再读数据
  19. static boolean ISETTINGFILE_ReadStr(IFile* pFile,AECHAR** pVal);
  20. //直接读取一个整数数据
  21. static int     ISETTINGFILE_ReadInt(IFile* pFile);
  22. //写一个字符串,先写类型,再写长度,最后写数据
  23. static boolean ISETTINGFILE_WriteStr(IFile* pFile,int nType,AECHAR* pStr);
  24. //写一个整数数据,先写类型,再写数据
  25. static boolean ISETTINGFILE_WriteInt(IFile* pFile,int nType,int nVal);
  26. ////////////////////////////////////////////////////////
  27. ///  Public Function        ////
  28. ////////////////////////////////////////////////////////
  29. boolean ISETTINGFILE_CreateInstance()
  30. {
  31. //分配内存空间
  32. if(NULL==pSetting)
  33. pSetting=MALLOCREC(ISettingFile);
  34. else
  35. return TRUE;
  36. if(NULL==pSetting)
  37. return FALSE;
  38. //如果从文件中读取数据失败,则将数据设置为缺省值
  39. if(FALSE==ISETTINGFILE_Load())
  40. return ISETTINGFILE_Default();
  41. else
  42. return TRUE;
  43. }
  44. boolean ISETTINGFILE_Release()
  45. {
  46. if(pSetting)
  47. {
  48. //释放pSetting的数据成员空间
  49. FREEIF(pSetting->m_pcGatewayIP);
  50. //释放pSetting的空间
  51. FREE(pSetting);
  52. pSetting=NULL;
  53. }
  54. return TRUE;
  55. }
  56. boolean ISETTINGFILE_SetStr(TSettingItems item,AECHAR* pVal)
  57. {
  58. //保证配置数据已经载入内存
  59. if(FALSE==ISETTINGFILE_CreateInstance())
  60. return FALSE;
  61. switch(item)
  62. {
  63. case WAP_DEFHOMEPAGENAME:
  64. ISETTINGFILE_WSTRDUP(&pSetting->m_pcDefHomePageName,pVal);
  65. break;
  66. case WAP_DEFHOMEPAGEURL:
  67. ISETTINGFILE_WSTRDUP(&pSetting->m_pcDefHomePageUrl,pVal);
  68. break;
  69. case WAP_GATEWAYIP:
  70. ISETTINGFILE_WSTRDUP(&pSetting->m_pcGatewayIP,pVal);
  71. break;
  72. case WAP_GATEWAPUSER:
  73. ISETTINGFILE_WSTRDUP(&pSetting->m_pcGatewayUSER,pVal);
  74. break;
  75. case WAP_GATEWAPPASSWORD:
  76. ISETTINGFILE_WSTRDUP(&pSetting->m_pcGatewayPASSWORD,pVal);
  77. break;
  78. case WAP_BOOKMARKNAME:
  79. ISETTINGFILE_WSTRDUP(&pSetting->m_pcBookmarkName,pVal);
  80. break;
  81. case WAP_BOOKMARKURL:
  82. ISETTINGFILE_WSTRDUP(&pSetting->m_pcBookmarkUrl,pVal);
  83. break;
  84. default:
  85. return FALSE;
  86. }
  87. //将保存的设置写入文件
  88. return ISETTINGFILE_Save();
  89. }
  90. AECHAR* ISETTINGFILE_GetStr(TSettingItems item)
  91. {
  92. if(FALSE==ISETTINGFILE_CreateInstance())
  93. return NULL;
  94. switch(item)
  95. {
  96. case WAP_OPENURL:
  97. return pSetting->m_pcOpenUrl;
  98. case WAP_DEFHOMEPAGENAME:
  99. return pSetting->m_pcDefHomePageName;
  100. case WAP_DEFHOMEPAGEURL:
  101. return pSetting->m_pcDefHomePageUrl;
  102. case WAP_GATEWAYIP:
  103. return pSetting->m_pcGatewayIP;
  104. case WAP_GATEWAPUSER:
  105. return pSetting->m_pcGatewayUSER;
  106. case WAP_GATEWAPPASSWORD:
  107. return pSetting->m_pcGatewayPASSWORD;
  108. case WAP_BOOKMARKNAME:
  109. return pSetting->m_pcBookmarkName;
  110. case WAP_BOOKMARKURL:
  111. return pSetting->m_pcBookmarkUrl;
  112. default:
  113. return NULL;
  114. }
  115. }
  116. boolean ISETTINGFILE_SetInt(TSettingItems item,int nVal)
  117. {
  118. if(FALSE==ISETTINGFILE_CreateInstance())
  119. return FALSE;
  120. switch(item)
  121. {
  122. case WAP_CONNECTSET:
  123. pSetting->m_nConnectWay=nVal;
  124. break;
  125. case WAP_GATEWAPPORT:
  126. pSetting->m_nGatewayPort=nVal;
  127. break;
  128. default:
  129. return FALSE;
  130. }
  131. //将保存的设置写入文件
  132. return ISETTINGFILE_Save();
  133. }
  134. int ISETTINGFILE_GetInt(TSettingItems item)
  135. {
  136. if(FALSE==ISETTINGFILE_CreateInstance())
  137. return 0;
  138. switch(item)
  139. {
  140. case WAP_CONNECTSET:
  141. return pSetting->m_nConnectWay;
  142. case WAP_GATEWAPPORT:
  143. return pSetting->m_nGatewayPort;
  144. default:
  145. return 0;
  146. }
  147. }
  148. ////////////////////////////////////////////////////////
  149. ///  Static Function Defined ////
  150. ////////////////////////////////////////////////////////
  151. static boolean ISETTINGFILE_ReadStr(IFile* pFile,AECHAR** pVal)
  152. {
  153. int nSize=0;
  154. //释放指针原来的内存空间
  155. FREEIF(*pVal);
  156. //读取新的字符串的空间长度
  157. IFILE_Read(pFile,&nSize,sizeof(nSize));
  158. if(nSize>0)
  159. {
  160. //根据长度分配内存空间并将字符串从文件读入新分配的内存
  161. *pVal=MALLOC(nSize);
  162. if(nSize!=IFILE_Read(pFile,*pVal,nSize))
  163. {
  164. FREEIF(*pVal);
  165. }
  166. else
  167. return TRUE;
  168. }
  169. return FALSE;
  170. }
  171. static int ISETTINGFILE_ReadInt(IFile* pFile)
  172. {
  173. int nVal=0;
  174. IFILE_Read(pFile,&nVal,sizeof(nVal));
  175. return nVal;
  176. }
  177. static boolean ISETTINGFILE_WriteStr(IFile* pFile,int nType,AECHAR* pStr)
  178. {
  179. int nSize=0;
  180. if(pStr)
  181. nSize=WSTRSIZE(pStr);
  182. //向文件中写入参数的类型和值的大小
  183. //如果值为空指针,大小为0
  184. IFILE_Write(pFile,&nType,sizeof(nType));
  185. IFILE_Write(pFile,&nSize,sizeof(nSize));
  186. //如果值不为空指针,将字符串值写入文件
  187. if(pStr)
  188. IFILE_Write(pFile,pStr,nSize);
  189. return TRUE;
  190. }
  191. static boolean ISETTINGFILE_WriteInt(IFile* pFile,int nType,int nVal)
  192. {
  193. //向文件中写入参数的类型
  194. IFILE_Write(pFile,&nType,sizeof(nType));
  195. //项文件中写入参数的值
  196. //但参数为整形时,不需要写入值的大小
  197. IFILE_Write(pFile,&nVal,sizeof(nVal));
  198. return TRUE;
  199. }
  200. static void ISETTINGFILE_WSTRDUP(AECHAR** pVal,AECHAR* pNewVal)
  201. {
  202. FREEIF(*pVal);
  203. if(pNewVal)
  204. *pVal=WSTRDUP(pNewVal);
  205. }
  206. static void ISETTINGFILE_STRTOWSTR(AECHAR** pVal,char* pNewVal)
  207. {
  208. FREEIF(*pVal);
  209. if(pNewVal)
  210. {
  211. int nSize=sizeof(AECHAR)*(STRLEN(pNewVal)+1);
  212. *pVal=MALLOC(nSize);
  213. STRTOWSTR(pNewVal,*pVal,nSize);
  214. }
  215. }
  216. static boolean ISETTINGFILE_Default()
  217. {
  218. if(NULL==pSetting)
  219. return FALSE;
  220. //设置联接方式
  221. pSetting->m_nConnectWay=CONNECTION_TYPE_WSP_CL;
  222. //设置网关端口
  223. pSetting->m_nGatewayPort=80;
  224. //设置网关IP
  225. ISETTINGFILE_STRTOWSTR(&pSetting->m_pcGatewayIP,"10.0.0.172");
  226. return TRUE;
  227. }
  228. static boolean ISETTINGFILE_Load()
  229. {
  230. IFileMgr* pFileMgr;
  231. IFile*    pFile;
  232. int    nHead;
  233. int   nSize;
  234. AEEApplet* pApp=(AEEApplet*)GETAPPINSTANCE();
  235. //打开文件
  236. if(!pApp||!pSetting) return FALSE;
  237. if(SUCCESS!=ISHELL_CreateInstance(pApp->m_pIShell,AEECLSID_FILEMGR,(void**)&pFileMgr))
  238. return FALSE;
  239. pFile=IFILEMGR_OpenFile(pFileMgr,WAP_SETTINGFILE_NAME,_OFM_READ);
  240. IFILEMGR_Release(pFileMgr);
  241. if(NULL==pFile) return FALSE;
  242. //从文件中导入数据
  243. while(TRUE)
  244. {
  245. int nByte=IFILE_Read(pFile,&nHead,sizeof(nHead));
  246. if(sizeof(nHead)!=nByte) break;
  247. nByte=IFILE_Read(pFile,&nSize,sizeof(nSize));
  248. if(sizeof(nSize)!=nByte) break;
  249. switch(nHead)
  250. {
  251. case WAP_CONNECTSET:
  252. pSetting->m_nConnectWay=ISETTINGFILE_ReadInt(pFile);
  253. break;
  254. case WAP_GATEWAYIP:
  255. ISETTINGFILE_ReadStr(pFile,&pSetting->m_pcGatewayIP);
  256. break;
  257. case WAP_GATEWAPPORT:
  258. pSetting->m_nGatewayPort=ISETTINGFILE_ReadInt(pFile);
  259. break;
  260. case WAP_OPENURL:
  261. ISETTINGFILE_ReadStr(pFile, &pSetting->m_pcOpenUrl);
  262. break;
  263. case WAP_DEFHOMEPAGENAME:
  264. ISETTINGFILE_ReadStr(pFile, &pSetting->m_pcDefHomePageName);
  265. break;
  266. case WAP_DEFHOMEPAGEURL:
  267. ISETTINGFILE_ReadStr(pFile,&pSetting->m_pcDefHomePageUrl);
  268. break;
  269. case WAP_GATEWAPUSER:
  270. ISETTINGFILE_ReadStr(pFile, &pSetting->m_pcGatewayUSER);
  271. break;
  272. case WAP_GATEWAPPASSWORD:
  273. ISETTINGFILE_ReadStr(pFile, &pSetting->m_pcGatewayPASSWORD);
  274. break;
  275. case WAP_BOOKMARKNAME:
  276. ISETTINGFILE_ReadStr(pFile, &pSetting->m_pcBookmarkName);
  277. break;
  278. case WAP_BOOKMARKURL:
  279. ISETTINGFILE_ReadStr(pFile, &pSetting->m_pcBookmarkUrl);
  280. break;
  281. default:
  282. IFILE_Release(pFile);
  283. return FALSE;
  284. }
  285. }
  286. IFILE_Release(pFile);
  287. return TRUE;
  288. }
  289. static boolean ISETTINGFILE_Save()
  290. {
  291. IFileMgr* pFileMgr;
  292. IFile*    pFile;
  293. AEEApplet* pApp=(AEEApplet*)GETAPPINSTANCE();
  294. //打开文件
  295. if(!pApp||!pSetting) return FALSE;
  296. if(SUCCESS!=ISHELL_CreateInstance(pApp->m_pIShell,AEECLSID_FILEMGR,(void**)&pFileMgr))
  297. return FALSE;
  298. if(SUCCESS==IFILEMGR_Test(pFileMgr,WAP_SETTINGFILE_NAME))
  299. pFile=IFILEMGR_OpenFile(pFileMgr,WAP_SETTINGFILE_NAME,_OFM_READWRITE);
  300. else
  301. pFile=IFILEMGR_OpenFile(pFileMgr,WAP_SETTINGFILE_NAME,_OFM_CREATE);
  302. IFILEMGR_Release(pFileMgr);
  303. if(NULL==pFile) return FALSE;
  304. //向文件中导入数据
  305. ISETTINGFILE_WriteStr(pFile,WAP_GATEWAYIP,pSetting->m_pcGatewayIP);
  306. ISETTINGFILE_WriteInt(pFile,WAP_CONNECTSET,pSetting->m_nConnectWay);
  307. ISETTINGFILE_WriteInt(pFile,WAP_GATEWAPPORT,pSetting->m_nGatewayPort);
  308. ISETTINGFILE_WriteStr(pFile,WAP_OPENURL,pSetting->m_pcOpenUrl);
  309.        ISETTINGFILE_WriteStr(pFile,WAP_GATEWAPUSER,pSetting->m_pcGatewayUSER);
  310. ISETTINGFILE_WriteStr(pFile,WAP_GATEWAPPASSWORD,pSetting->m_pcGatewayPASSWORD);
  311. ISETTINGFILE_WriteStr(pFile,WAP_DEFHOMEPAGENAME,pSetting->m_pcDefHomePageName);
  312. ISETTINGFILE_WriteStr(pFile,WAP_DEFHOMEPAGEURL,pSetting->m_pcDefHomePageUrl);
  313. ISETTINGFILE_WriteStr(pFile,WAP_BOOKMARKNAME,pSetting->m_pcBookmarkName);
  314. ISETTINGFILE_WriteStr(pFile,WAP_BOOKMARKURL,pSetting->m_pcBookmarkUrl);
  315. IFILE_Release(pFile);
  316. return TRUE;
  317. }
  318. ////////////////////////////////////////////////////////////////////
  319. int ISETTINGFILE_Test()
  320. {
  321. AECHAR pp[]={'A','B','C','D','E','F',0};
  322. ISETTINGFILE_CreateInstance();
  323. ISETTINGFILE_Release();
  324. ISETTINGFILE_Save();
  325. ISETTINGFILE_Load();
  326. ISETTINGFILE_SetStr(WAP_GATEWAYIP,pp);
  327. return 0;
  328. }