IBookmarkFile.c
资源名称:MenuList.rar [点击查看]
上传用户:duwei1288
上传日期:2009-12-27
资源大小:451k
文件大小:7k
源码类别:
BREW编程
开发平台:
Visual C++
- #include "IBookmarkFile.h"
- #include "AEEAppGen.h"
- #include "AeeFile.h"
- #define BOOKMARK_FILENAME "bookmark.dat"
- IBookmarkFile* pBookmark=NULL;
- //////////////////////////////////////////////////////////
- //////////////////////////////////////////////////////////
- static boolean IBOOKMARKFILE_Read();
- static boolean IBOOKMARKFILE_Write();
- static boolean IBOOKMARKFILE_Seek(uint32 nBookmarkID);
- //////////////////////////////////////////////////////////
- //////////////////////////////////////////////////////////
- boolean IBOOKMARKFILE_CreateInstance()
- {
- if(NULL==pBookmark)
- IBOOKMARKFILE_Read();
- if(NULL==pBookmark)
- return FALSE;
- else
- return TRUE;
- }
- boolean IBOOKMARKFILE_Release()
- {
- TBookmarkItem* p;
- if(NULL!=pBookmark)
- {
- while(pBookmark->m_pDataHead)
- {
- p=pBookmark->m_pDataHead;
- pBookmark->m_pDataHead=p->pNext;
- FREEIF(p->pName);
- FREEIF(p->pUrl);
- }
- FREEIF(pBookmark);
- }
- return TRUE;
- }
- boolean IBOOKMARKFILE_InsertBookmark(AECHAR* pName,AECHAR* pUrl)
- {
- TBookmarkItem* pItem=MALLOCREC(TBookmarkItem);
- if(!pItem) return FALSE;
- if(!IBOOKMARKFILE_CreateInstance())
- {
- FREEIF(pItem);
- return FALSE;
- }
- pItem->pName=WSTRDUP(pName);
- pItem->pUrl=WSTRDUP(pUrl);
- pItem->uiID=GETTIMEMS();
- if(NULL==pBookmark->m_pDataHead)
- pBookmark->m_pDataHead=pItem;
- else
- {
- pBookmark->m_pCurrentData=pBookmark->m_pDataHead;
- while(pBookmark->m_pCurrentData->pNext)
- pBookmark->m_pCurrentData=pBookmark->m_pCurrentData->pNext;
- pBookmark->m_pCurrentData->pNext=pItem;
- }
- IBOOKMARKFILE_Write();
- return TRUE;
- }
- boolean IBOOKMARKFILE_RemoveBookmark(uint32 nBookmarkID)
- {
- //初始化数据,保证数据读入内存中
- TBookmarkItem* pItem=NULL;
- TBookmarkItem* pBeforeItem=NULL;
- if(!IBOOKMARKFILE_CreateInstance())
- return FALSE;
- //如果数据没有读入内存或不存在记录直接返回
- if(NULL==pBookmark||NULL==pBookmark->m_pDataHead)
- return FALSE;
- //查找内存中要删除纪录的位置
- if(nBookmarkID==pBookmark->m_pDataHead->uiID)
- {
- pItem=pBookmark->m_pDataHead;
- pBookmark->m_pDataHead=pBookmark->m_pDataHead->pNext;
- }
- else
- {
- pBeforeItem=pBookmark->m_pDataHead;
- pItem=pBookmark->m_pDataHead->pNext;
- while(pItem)
- {
- if(nBookmarkID==pItem->uiID)
- {
- pBeforeItem->pNext=pItem->pNext;
- break;
- }
- else
- {
- pBeforeItem=pItem;
- pItem=pItem->pNext;
- }
- }
- }
- //如果找到则删除纪录
- if(pItem)
- {
- FREEIF(pItem->pName);
- FREEIF(pItem->pUrl);
- FREEIF(pItem);
- IBOOKMARKFILE_Write();
- return TRUE;
- }
- else
- return FALSE;
- }
- boolean IBOOKMARKFILE_EditStart(uint32 uiBookmarkID)
- {
- return IBOOKMARKFILE_Seek(uiBookmarkID);
- }
- boolean IBOOKMARKFILE_EditFinish(AECHAR* pName,AECHAR* pUrl)
- {
- if(!pBookmark||!pBookmark->m_pCurrentData)
- return FALSE;
- FREEIF(pBookmark->m_pCurrentData->pName);
- FREEIF(pBookmark->m_pCurrentData->pUrl);
- pBookmark->m_pCurrentData->pName=WSTRDUP(pName);
- pBookmark->m_pCurrentData->pUrl=WSTRDUP(pUrl);
- IBOOKMARKFILE_Write();
- return TRUE;
- }
- boolean IBOOKMARKFILE_StartToSeek()
- {
- if(!IBOOKMARKFILE_CreateInstance())
- return FALSE;
- if(NULL==pBookmark->m_pDataHead)
- return FALSE;
- else
- {
- pBookmark->m_pCurrentData=pBookmark->m_pDataHead;
- return TRUE;
- }
- }
- boolean IBOOKMARKFILE_GetNext(AECHAR** pName,uint32* nID)
- {
- if(NULL==pBookmark||NULL==pBookmark->m_pCurrentData)
- return FALSE;
- else
- {
- *nID=pBookmark->m_pCurrentData->uiID;
- *pName=pBookmark->m_pCurrentData->pName;
- pBookmark->m_pCurrentData=pBookmark->m_pCurrentData->pNext;
- return TRUE;
- }
- }
- AECHAR* IBOOKMARKFILE_GetUrl(uint32 uiBookmarkID)
- {
- if(!IBOOKMARKFILE_Seek(uiBookmarkID))
- return NULL;
- return pBookmark->m_pCurrentData->pUrl;
- }
- /////////////////////////////////////////////////////////////
- /////////////////////////////////////////////////////////////
- static boolean IBOOKMARKFILE_Seek(uint32 nBookmarkID)
- {
- //初始化数据,保证数据读入内存中
- TBookmarkItem* pItem=NULL;
- if(!IBOOKMARKFILE_CreateInstance())
- return FALSE;
- //如果数据没有读入内存直接返回
- if(NULL==pBookmark)
- return FALSE;
- //查找内存中纪录的位置
- pItem=pBookmark->m_pDataHead;
- while(pItem)
- {
- if(nBookmarkID==pItem->uiID)
- {
- pBookmark->m_pCurrentData=pItem;
- break;
- }
- else
- pItem=pItem->pNext;
- }
- return (NULL!=pItem);
- }
- static boolean IBOOKMARKFILE_Read()
- {
- IFileMgr* pFileMgr;
- IFile* pFile=NULL;
- uint32 uiLocal=0;
- uint32 uiID=1;
- FileInfo fileInfo;
- AECHAR* pFileBuf;
- TBookmarkItem* pItem=NULL;
- AEEApplet* pApp=(AEEApplet*)GETAPPINSTANCE();
- if(!pApp) return FALSE;
- //分配内存空间
- IBOOKMARKFILE_Release();
- pBookmark=MALLOCREC(IBookmarkFile);
- if(!pBookmark) return FALSE;
- //打开文件
- if(SUCCESS!=ISHELL_CreateInstance(pApp->m_pIShell, AEECLSID_FILEMGR, (void**)&pFileMgr))
- return FALSE;
- if(SUCCESS==IFILEMGR_Test(pFileMgr,BOOKMARK_FILENAME))
- pFile = IFILEMGR_OpenFile(pFileMgr,BOOKMARK_FILENAME, _OFM_READ);
- IFILEMGR_Release(pFileMgr);
- if(!pFile) return FALSE;
- IFILE_GetInfo(pFile,&fileInfo);
- if(fileInfo.dwSize<sizeof(AECHAR))
- {
- IFILE_Release(pFile);
- return FALSE;
- }
- pFileBuf=MALLOC(fileInfo.dwSize);
- if(!pFileBuf)
- {
- IFILE_Release(pFile);
- return FALSE;
- }
- IFILE_Read(pFile,(char*)pFileBuf,fileInfo.dwSize);
- IFILE_Release(pFile);
- if(0!=pFileBuf[fileInfo.dwSize/2-1])
- {
- FREE(pFileBuf);
- return FALSE;
- }
- //将数据读入内存
- while(uiLocal<fileInfo.dwSize)
- {
- pItem=MALLOCREC(TBookmarkItem);
- if(!pItem)
- {
- FREE(pFileBuf);
- return TRUE;
- }
- pItem->pName=WSTRDUP((AECHAR*)((char*)pFileBuf+uiLocal));
- uiLocal+=WSTRSIZE(pItem->pName);
- if(uiLocal>=fileInfo.dwSize)
- pItem->pUrl=NULL;
- else
- {
- pItem->pUrl=WSTRDUP((AECHAR*)((char*)pFileBuf+uiLocal));
- uiLocal+=WSTRSIZE(pItem->pUrl);
- }
- pItem->uiID=uiID++;
- if(NULL==pBookmark->m_pDataHead)
- {
- pBookmark->m_pDataHead=pItem;
- pBookmark->m_pCurrentData=pBookmark->m_pDataHead;
- }
- else
- {
- pBookmark->m_pCurrentData->pNext=pItem;
- pBookmark->m_pCurrentData=pItem;
- }
- }
- FREE(pFileBuf);
- return TRUE;
- }
- static boolean IBOOKMARKFILE_Write()
- {
- IFileMgr* pFileMgr;
- IFile* pFile;
- TBookmarkItem* pItem=NULL;
- AEEApplet* pApp=(AEEApplet*)GETAPPINSTANCE();
- if(!pApp||!pBookmark) return FALSE;
- //打开文件
- if(SUCCESS!=ISHELL_CreateInstance(pApp->m_pIShell, AEECLSID_FILEMGR, (void**)&pFileMgr))
- return FALSE;
- IFILEMGR_Remove(pFileMgr,BOOKMARK_FILENAME);
- pFile = IFILEMGR_OpenFile(pFileMgr,BOOKMARK_FILENAME, _OFM_CREATE);
- IFILEMGR_Release(pFileMgr);
- if(!pFile) return FALSE;
- //写入数据
- pBookmark->m_pCurrentData=pBookmark->m_pDataHead;
- while(pBookmark->m_pCurrentData)
- {
- IFILE_Write(pFile,pBookmark->m_pCurrentData->pName,WSTRSIZE(pBookmark->m_pCurrentData->pName));
- IFILE_Write(pFile,pBookmark->m_pCurrentData->pUrl,WSTRSIZE(pBookmark->m_pCurrentData->pUrl));
- pBookmark->m_pCurrentData=pBookmark->m_pCurrentData->pNext;
- }
- IFILE_Release(pFile);
- return TRUE;
- }