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

BREW编程

开发平台:

Visual C++

  1. #include "IBookmarkFile.h"
  2. #include "AEEAppGen.h"
  3. #include "AeeFile.h"
  4. #define BOOKMARK_FILENAME "bookmark.dat"
  5. IBookmarkFile*  pBookmark=NULL;
  6. //////////////////////////////////////////////////////////
  7. //////////////////////////////////////////////////////////
  8. static boolean IBOOKMARKFILE_Read();
  9. static boolean IBOOKMARKFILE_Write();
  10. static boolean IBOOKMARKFILE_Seek(uint32 nBookmarkID);
  11. //////////////////////////////////////////////////////////
  12. //////////////////////////////////////////////////////////
  13. boolean  IBOOKMARKFILE_CreateInstance()
  14. {
  15. if(NULL==pBookmark)
  16. IBOOKMARKFILE_Read();
  17. if(NULL==pBookmark)
  18. return FALSE;
  19. else
  20. return TRUE;
  21. }
  22. boolean IBOOKMARKFILE_Release()
  23. {
  24. TBookmarkItem* p;
  25. if(NULL!=pBookmark)
  26. {
  27. while(pBookmark->m_pDataHead)
  28. {
  29. p=pBookmark->m_pDataHead;
  30. pBookmark->m_pDataHead=p->pNext;
  31. FREEIF(p->pName);
  32. FREEIF(p->pUrl);
  33. }
  34. FREEIF(pBookmark);
  35. }
  36. return TRUE;
  37. }
  38. boolean IBOOKMARKFILE_InsertBookmark(AECHAR* pName,AECHAR* pUrl)
  39. {
  40. TBookmarkItem* pItem=MALLOCREC(TBookmarkItem);
  41. if(!pItem) return FALSE;
  42. if(!IBOOKMARKFILE_CreateInstance())
  43. {
  44. FREEIF(pItem);
  45. return FALSE;
  46. }
  47. pItem->pName=WSTRDUP(pName);
  48. pItem->pUrl=WSTRDUP(pUrl);
  49. pItem->uiID=GETTIMEMS();
  50. if(NULL==pBookmark->m_pDataHead)
  51. pBookmark->m_pDataHead=pItem;
  52. else
  53. {
  54. pBookmark->m_pCurrentData=pBookmark->m_pDataHead;
  55. while(pBookmark->m_pCurrentData->pNext)
  56. pBookmark->m_pCurrentData=pBookmark->m_pCurrentData->pNext;
  57. pBookmark->m_pCurrentData->pNext=pItem;
  58. }
  59. IBOOKMARKFILE_Write();
  60. return TRUE;
  61. }
  62. boolean IBOOKMARKFILE_RemoveBookmark(uint32 nBookmarkID)
  63. {
  64. //初始化数据,保证数据读入内存中
  65. TBookmarkItem* pItem=NULL;
  66. TBookmarkItem* pBeforeItem=NULL;
  67. if(!IBOOKMARKFILE_CreateInstance())
  68. return FALSE;
  69. //如果数据没有读入内存或不存在记录直接返回
  70. if(NULL==pBookmark||NULL==pBookmark->m_pDataHead)
  71. return FALSE;
  72. //查找内存中要删除纪录的位置
  73. if(nBookmarkID==pBookmark->m_pDataHead->uiID)
  74. {
  75. pItem=pBookmark->m_pDataHead;
  76. pBookmark->m_pDataHead=pBookmark->m_pDataHead->pNext;
  77. }
  78. else
  79. {
  80. pBeforeItem=pBookmark->m_pDataHead;
  81. pItem=pBookmark->m_pDataHead->pNext;
  82. while(pItem)
  83. {
  84. if(nBookmarkID==pItem->uiID)
  85. {
  86. pBeforeItem->pNext=pItem->pNext;
  87. break;
  88. }
  89. else
  90. {
  91. pBeforeItem=pItem;
  92. pItem=pItem->pNext;
  93. }
  94. }
  95. }
  96. //如果找到则删除纪录
  97. if(pItem)
  98. {
  99. FREEIF(pItem->pName);
  100. FREEIF(pItem->pUrl);
  101. FREEIF(pItem);
  102. IBOOKMARKFILE_Write();
  103. return TRUE;
  104. }
  105. else
  106. return FALSE;
  107. }
  108. boolean IBOOKMARKFILE_EditStart(uint32 uiBookmarkID)
  109. {
  110. return IBOOKMARKFILE_Seek(uiBookmarkID);
  111. }
  112. boolean IBOOKMARKFILE_EditFinish(AECHAR* pName,AECHAR* pUrl)
  113. {
  114. if(!pBookmark||!pBookmark->m_pCurrentData)
  115. return FALSE;
  116. FREEIF(pBookmark->m_pCurrentData->pName);
  117. FREEIF(pBookmark->m_pCurrentData->pUrl);
  118. pBookmark->m_pCurrentData->pName=WSTRDUP(pName);
  119. pBookmark->m_pCurrentData->pUrl=WSTRDUP(pUrl);
  120. IBOOKMARKFILE_Write();
  121. return TRUE;
  122. }
  123. boolean IBOOKMARKFILE_StartToSeek()
  124. {
  125. if(!IBOOKMARKFILE_CreateInstance())
  126. return FALSE;
  127. if(NULL==pBookmark->m_pDataHead)
  128. return FALSE;
  129. else
  130. {
  131. pBookmark->m_pCurrentData=pBookmark->m_pDataHead;
  132. return TRUE;
  133. }
  134. }
  135. boolean IBOOKMARKFILE_GetNext(AECHAR** pName,uint32* nID)
  136. {
  137. if(NULL==pBookmark||NULL==pBookmark->m_pCurrentData)
  138. return FALSE;
  139. else
  140. {
  141. *nID=pBookmark->m_pCurrentData->uiID;
  142. *pName=pBookmark->m_pCurrentData->pName;
  143. pBookmark->m_pCurrentData=pBookmark->m_pCurrentData->pNext;
  144. return TRUE;
  145. }
  146. }
  147. AECHAR* IBOOKMARKFILE_GetUrl(uint32 uiBookmarkID)
  148. {
  149. if(!IBOOKMARKFILE_Seek(uiBookmarkID))
  150. return NULL;
  151. return pBookmark->m_pCurrentData->pUrl;
  152. }
  153. /////////////////////////////////////////////////////////////
  154. /////////////////////////////////////////////////////////////
  155. static boolean IBOOKMARKFILE_Seek(uint32 nBookmarkID)
  156. {
  157. //初始化数据,保证数据读入内存中
  158. TBookmarkItem* pItem=NULL;
  159. if(!IBOOKMARKFILE_CreateInstance())
  160. return FALSE;
  161. //如果数据没有读入内存直接返回
  162. if(NULL==pBookmark)
  163. return FALSE;
  164. //查找内存中纪录的位置
  165. pItem=pBookmark->m_pDataHead;
  166. while(pItem)
  167. {
  168. if(nBookmarkID==pItem->uiID)
  169. {
  170. pBookmark->m_pCurrentData=pItem;
  171. break;
  172. }
  173. else
  174. pItem=pItem->pNext;
  175. }
  176. return (NULL!=pItem);
  177. }
  178. static boolean IBOOKMARKFILE_Read()
  179. {
  180. IFileMgr* pFileMgr;
  181. IFile*   pFile=NULL;
  182. uint32   uiLocal=0;
  183. uint32    uiID=1;
  184. FileInfo  fileInfo;
  185. AECHAR*   pFileBuf;
  186. TBookmarkItem* pItem=NULL;
  187. AEEApplet* pApp=(AEEApplet*)GETAPPINSTANCE();
  188. if(!pApp) return FALSE;
  189. //分配内存空间
  190. IBOOKMARKFILE_Release();
  191. pBookmark=MALLOCREC(IBookmarkFile);
  192. if(!pBookmark) return FALSE;
  193. //打开文件
  194. if(SUCCESS!=ISHELL_CreateInstance(pApp->m_pIShell, AEECLSID_FILEMGR, (void**)&pFileMgr))
  195. return FALSE;
  196. if(SUCCESS==IFILEMGR_Test(pFileMgr,BOOKMARK_FILENAME))
  197. pFile = IFILEMGR_OpenFile(pFileMgr,BOOKMARK_FILENAME, _OFM_READ);
  198. IFILEMGR_Release(pFileMgr);
  199. if(!pFile) return FALSE;
  200. IFILE_GetInfo(pFile,&fileInfo);
  201. if(fileInfo.dwSize<sizeof(AECHAR))
  202. {
  203. IFILE_Release(pFile);
  204. return FALSE;
  205. }
  206. pFileBuf=MALLOC(fileInfo.dwSize);
  207. if(!pFileBuf)
  208. {
  209. IFILE_Release(pFile);
  210. return FALSE;
  211. }
  212. IFILE_Read(pFile,(char*)pFileBuf,fileInfo.dwSize);
  213. IFILE_Release(pFile);
  214. if(0!=pFileBuf[fileInfo.dwSize/2-1])
  215. {
  216. FREE(pFileBuf);
  217. return FALSE;
  218. }
  219. //将数据读入内存
  220. while(uiLocal<fileInfo.dwSize)
  221. {
  222. pItem=MALLOCREC(TBookmarkItem);
  223. if(!pItem)
  224. {
  225. FREE(pFileBuf);
  226. return TRUE;
  227. }
  228. pItem->pName=WSTRDUP((AECHAR*)((char*)pFileBuf+uiLocal));
  229. uiLocal+=WSTRSIZE(pItem->pName);
  230. if(uiLocal>=fileInfo.dwSize)
  231. pItem->pUrl=NULL;
  232. else
  233. {
  234. pItem->pUrl=WSTRDUP((AECHAR*)((char*)pFileBuf+uiLocal));
  235. uiLocal+=WSTRSIZE(pItem->pUrl);
  236. }
  237. pItem->uiID=uiID++;
  238. if(NULL==pBookmark->m_pDataHead)
  239. {
  240. pBookmark->m_pDataHead=pItem;
  241. pBookmark->m_pCurrentData=pBookmark->m_pDataHead;
  242. }
  243. else
  244. {
  245. pBookmark->m_pCurrentData->pNext=pItem;
  246. pBookmark->m_pCurrentData=pItem;
  247. }
  248. }
  249. FREE(pFileBuf);
  250. return TRUE;
  251. }
  252. static boolean IBOOKMARKFILE_Write()
  253. {
  254. IFileMgr* pFileMgr;
  255. IFile*   pFile;
  256. TBookmarkItem* pItem=NULL;
  257. AEEApplet* pApp=(AEEApplet*)GETAPPINSTANCE();
  258. if(!pApp||!pBookmark) return FALSE;
  259. //打开文件
  260. if(SUCCESS!=ISHELL_CreateInstance(pApp->m_pIShell, AEECLSID_FILEMGR, (void**)&pFileMgr))
  261. return FALSE;
  262. IFILEMGR_Remove(pFileMgr,BOOKMARK_FILENAME);
  263. pFile = IFILEMGR_OpenFile(pFileMgr,BOOKMARK_FILENAME, _OFM_CREATE);
  264. IFILEMGR_Release(pFileMgr);
  265. if(!pFile) return FALSE;
  266. //写入数据
  267. pBookmark->m_pCurrentData=pBookmark->m_pDataHead;
  268. while(pBookmark->m_pCurrentData)
  269. {
  270. IFILE_Write(pFile,pBookmark->m_pCurrentData->pName,WSTRSIZE(pBookmark->m_pCurrentData->pName));
  271. IFILE_Write(pFile,pBookmark->m_pCurrentData->pUrl,WSTRSIZE(pBookmark->m_pCurrentData->pUrl));
  272. pBookmark->m_pCurrentData=pBookmark->m_pCurrentData->pNext;
  273. }
  274. IFILE_Release(pFile);
  275. return TRUE;
  276. }