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. default:
  109. return NULL;
  110. }
  111. }
  112. boolean ISETTINGFILE_SetInt(TSettingItems item,int nVal)
  113. {
  114. if(FALSE==ISETTINGFILE_CreateInstance())
  115. return FALSE;
  116. switch(item)
  117. {
  118. case WAP_CONNECTSET:
  119. pSetting->m_nConnectWay=nVal;
  120. break;
  121. case WAP_GATEWAPPORT:
  122. pSetting->m_nGatewayPort=nVal;
  123. break;
  124. default:
  125. return FALSE;
  126. }
  127. //将保存的设置写入文件
  128. return ISETTINGFILE_Save();
  129. }
  130. int ISETTINGFILE_GetInt(TSettingItems item)
  131. {
  132. if(FALSE==ISETTINGFILE_CreateInstance())
  133. return 0;
  134. switch(item)
  135. {
  136. case WAP_CONNECTSET:
  137. return pSetting->m_nConnectWay;
  138. case WAP_GATEWAPPORT:
  139. return pSetting->m_nGatewayPort;
  140. default:
  141. return 0;
  142. }
  143. }
  144. ////////////////////////////////////////////////////////
  145. ///  Static Function Defined ////
  146. ////////////////////////////////////////////////////////
  147. static boolean ISETTINGFILE_ReadStr(IFile* pFile,AECHAR** pVal)
  148. {
  149. int nSize=0;
  150. //释放指针原来的内存空间
  151. FREEIF(*pVal);
  152. //读取新的字符串的空间长度
  153. IFILE_Read(pFile,&nSize,sizeof(nSize));
  154. if(nSize>0)
  155. {
  156. //根据长度分配内存空间并将字符串从文件读入新分配的内存
  157. *pVal=MALLOC(nSize);
  158. if(nSize!=IFILE_Read(pFile,*pVal,nSize))
  159. {
  160. FREEIF(*pVal);
  161. }
  162. else
  163. return TRUE;
  164. }
  165. return FALSE;
  166. }
  167. static int ISETTINGFILE_ReadInt(IFile* pFile)
  168. {
  169. int nVal=0;
  170. IFILE_Read(pFile,&nVal,sizeof(nVal));
  171. return nVal;
  172. }
  173. static boolean ISETTINGFILE_WriteStr(IFile* pFile,int nType,AECHAR* pStr)
  174. {
  175. int nSize=0;
  176. if(pStr)
  177. nSize=WSTRSIZE(pStr);
  178. //向文件中写入参数的类型和值的大小
  179. //如果值为空指针,大小为0
  180. IFILE_Write(pFile,&nType,sizeof(nType));
  181. IFILE_Write(pFile,&nSize,sizeof(nSize));
  182. //如果值不为空指针,将字符串值写入文件
  183. if(pStr)
  184. IFILE_Write(pFile,pStr,nSize);
  185. return TRUE;
  186. }
  187. static boolean ISETTINGFILE_WriteInt(IFile* pFile,int nType,int nVal)
  188. {
  189. //向文件中写入参数的类型
  190. IFILE_Write(pFile,&nType,sizeof(nType));
  191. //项文件中写入参数的值
  192. //但参数为整形时,不需要写入值的大小
  193. IFILE_Write(pFile,&nVal,sizeof(nVal));
  194. return TRUE;
  195. }
  196. static void ISETTINGFILE_WSTRDUP(AECHAR** pVal,AECHAR* pNewVal)
  197. {
  198. FREEIF(*pVal);
  199. if(pNewVal)
  200. *pVal=WSTRDUP(pNewVal);
  201. }
  202. static void ISETTINGFILE_STRTOWSTR(AECHAR** pVal,char* pNewVal)
  203. {
  204. FREEIF(*pVal);
  205. if(pNewVal)
  206. {
  207. int nSize=sizeof(AECHAR)*(STRLEN(pNewVal)+1);
  208. *pVal=MALLOC(nSize);
  209. STRTOWSTR(pNewVal,*pVal,nSize);
  210. }
  211. }
  212. static boolean ISETTINGFILE_Default()
  213. {
  214. if(NULL==pSetting)
  215. return FALSE;
  216. //设置联接方式
  217. pSetting->m_nConnectWay=CONNECTION_TYPE_WSP_CL;
  218. //设置网关端口
  219. pSetting->m_nGatewayPort=80;
  220. //设置网关IP
  221. ISETTINGFILE_STRTOWSTR(&pSetting->m_pcGatewayIP,"10.0.0.172");
  222. return TRUE;
  223. }
  224. static boolean ISETTINGFILE_Load()
  225. {
  226. IFileMgr* pFileMgr;
  227. IFile*    pFile;
  228. int    nHead;
  229. int   nSize;
  230. AEEApplet* pApp=(AEEApplet*)GETAPPINSTANCE();
  231. //打开文件
  232. if(!pApp||!pSetting) return FALSE;
  233. if(SUCCESS!=ISHELL_CreateInstance(pApp->m_pIShell,AEECLSID_FILEMGR,(void**)&pFileMgr))
  234. return FALSE;
  235. pFile=IFILEMGR_OpenFile(pFileMgr,WAP_SETTINGFILE_NAME,_OFM_READ);
  236. IFILEMGR_Release(pFileMgr);
  237. if(NULL==pFile) return FALSE;
  238. //从文件中导入数据
  239. while(TRUE)
  240. {
  241. int nByte=IFILE_Read(pFile,&nHead,sizeof(nHead));
  242. if(sizeof(nHead)!=nByte) break;
  243. nByte=IFILE_Read(pFile,&nSize,sizeof(nSize));
  244. if(sizeof(nSize)!=nByte) break;
  245. switch(nHead)
  246. {
  247. case WAP_CONNECTSET:
  248. pSetting->m_nConnectWay=ISETTINGFILE_ReadInt(pFile);
  249. break;
  250. case WAP_GATEWAYIP:
  251. ISETTINGFILE_ReadStr(pFile,&pSetting->m_pcGatewayIP);
  252. break;
  253. case WAP_GATEWAPPORT:
  254. pSetting->m_nGatewayPort=ISETTINGFILE_ReadInt(pFile);
  255. break;
  256. case WAP_OPENURL:
  257. ISETTINGFILE_ReadStr(pFile, &pSetting->m_pcOpenUrl);
  258. break;
  259. case WAP_DEFHOMEPAGENAME:
  260. ISETTINGFILE_ReadStr(pFile, &pSetting->m_pcDefHomePageName);
  261. break;
  262. case WAP_DEFHOMEPAGEURL:
  263. ISETTINGFILE_ReadStr(pFile,&pSetting->m_pcDefHomePageUrl);
  264. break;
  265. case WAP_GATEWAPUSER:
  266. ISETTINGFILE_ReadStr(pFile, &pSetting->m_pcGatewayUSER);
  267. break;
  268. case WAP_GATEWAPPASSWORD:
  269. ISETTINGFILE_ReadStr(pFile, &pSetting->m_pcGatewayPASSWORD);
  270. break;
  271. case WAP_BOOKMARKNAME:
  272. ISETTINGFILE_ReadStr(pFile, &pSetting->m_pcBookmarkName);
  273. break;
  274. case WAP_BOOKMARKURL:
  275. ISETTINGFILE_ReadStr(pFile, &pSetting->m_pcBookmarkUrl);
  276. break;
  277. default:
  278. IFILE_Release(pFile);
  279. return FALSE;
  280. }
  281. }
  282. IFILE_Release(pFile);
  283. return TRUE;
  284. }
  285. static boolean ISETTINGFILE_Save()
  286. {
  287. IFileMgr* pFileMgr;
  288. IFile*    pFile;
  289. AEEApplet* pApp=(AEEApplet*)GETAPPINSTANCE();
  290. //打开文件
  291. if(!pApp||!pSetting) return FALSE;
  292. if(SUCCESS!=ISHELL_CreateInstance(pApp->m_pIShell,AEECLSID_FILEMGR,(void**)&pFileMgr))
  293. return FALSE;
  294. if(SUCCESS==IFILEMGR_Test(pFileMgr,WAP_SETTINGFILE_NAME))
  295. pFile=IFILEMGR_OpenFile(pFileMgr,WAP_SETTINGFILE_NAME,_OFM_READWRITE);
  296. else
  297. pFile=IFILEMGR_OpenFile(pFileMgr,WAP_SETTINGFILE_NAME,_OFM_CREATE);
  298. IFILEMGR_Release(pFileMgr);
  299. if(NULL==pFile) return FALSE;
  300. //向文件中导入数据
  301. ISETTINGFILE_WriteStr(pFile,WAP_GATEWAYIP,pSetting->m_pcGatewayIP);
  302. ISETTINGFILE_WriteInt(pFile,WAP_CONNECTSET,pSetting->m_nConnectWay);
  303. ISETTINGFILE_WriteInt(pFile,WAP_GATEWAPPORT,pSetting->m_nGatewayPort);
  304. ISETTINGFILE_WriteStr(pFile,WAP_OPENURL,pSetting->m_pcOpenUrl);
  305.        ISETTINGFILE_WriteStr(pFile,WAP_GATEWAPUSER,pSetting->m_pcGatewayUSER);
  306. ISETTINGFILE_WriteStr(pFile,WAP_GATEWAPPASSWORD,pSetting->m_pcGatewayPASSWORD);
  307. ISETTINGFILE_WriteStr(pFile,WAP_DEFHOMEPAGENAME,pSetting->m_pcDefHomePageName);
  308. ISETTINGFILE_WriteStr(pFile,WAP_DEFHOMEPAGEURL,pSetting->m_pcDefHomePageUrl);
  309. ISETTINGFILE_WriteStr(pFile,WAP_BOOKMARKNAME,pSetting->m_pcBookmarkName);
  310. ISETTINGFILE_WriteStr(pFile,WAP_BOOKMARKURL,pSetting->m_pcBookmarkUrl);
  311. IFILE_Release(pFile);
  312. return TRUE;
  313. }
  314. ////////////////////////////////////////////////////////////////////
  315. int ISETTINGFILE_Test()
  316. {
  317. AECHAR pp[]={'A','B','C','D','E','F',0};
  318. ISETTINGFILE_CreateInstance();
  319. ISETTINGFILE_Release();
  320. ISETTINGFILE_Save();
  321. ISETTINGFILE_Load();
  322. ISETTINGFILE_SetStr(WAP_GATEWAYIP,pp);
  323. return 0;
  324. }