chxavurllist.cpp
上传用户:zhongxx05
上传日期:2007-06-06
资源大小:33641k
文件大小:6k
- /************************************************************************
- * chxavurllist.cpp
- * ----------------
- *
- * Synopsis:
- * Contains the implementation of the CHXAvURLList class. It's basically
- * a neat way to keep track of the urls most recently viewed.
- *
- *
- * Target:
- * Symbian OS
- *
- *
- * (c) 1995-2003 RealNetworks, Inc. Patents pending. All rights reserved.
- *
- *****************************************************************************/
- // Symbian includes...
- #include <e32base.h>
- // Standard includes...
- #include <stdio.h>
- #include <stdlib.h>
- // Helix includes...
- #include "unkimp.h"
- #include "ihxpckts.h"
- #include "hxstring.h"
- #include "hxurl.h"
- #include "hxassert.h"
- #include "hxwintyp.h"
- #include "hxcom.h"
- #include "hxbuffer.h"
- #include "hxcomm.h"
- #include "hxmon.h"
- // Include from this project...
- #include "chxavurlinfo.h"
- #include "chxavurllist.h"
- #include "chxavstringutils.h"
-
- CHXAvURLList::CHXAvURLList() : m_maxURLs(10)
- {
- }
- CHXAvURLList::~CHXAvURLList()
- {
- Clear();
- }
- void
- CHXAvURLList::InitializeL()
- {
- HX_ASSERT(m_urls.IsEmpty());
- }
- CHXAvURLList::CHXAvURLList(int maxURLs)
- : m_maxURLs(maxURLs)
- {}
- int
- CHXAvURLList::NumURLs() const
- {
- return m_urls.GetCount();
- }
- void
- CHXAvURLList::Remove(const CHXAvURLInfo*& pURLInfo)
- {
- LISTPOSITION position = m_urls.Find((void*)pURLInfo, NULL);
- if (position != NULL)
- {
- HX_DELETE(pURLInfo);
- m_urls.RemoveAt(position);
- }
- }
- void
- CHXAvURLList::Clear()
- {
- LISTPOSITION position = m_urls.GetHeadPosition();
- while (position != NULL)
- {
- CHXAvURLInfo* pURLInfo = (CHXAvURLInfo*)(m_urls.GetNext(position));
- delete pURLInfo;
- }
- m_urls.RemoveAll();
- }
- int
- CHXAvURLList::GetMax() const
- {
- return m_maxURLs;
- }
- // remove all entries matching given url; return count of removed items
- int CHXAvURLList::RemoveMatches(const TDesC& url)
- {
- int removeCount = 0;
-
- LISTPOSITION position = m_urls.GetHeadPosition();
- LISTPOSITION prevPosition = position;
- while (position != NULL)
- {
- CHXAvURLInfo* pInfo = (CHXAvURLInfo*)(m_urls.GetNext(position));
- if ((pInfo != NULL) && (pInfo->URL() == url))
- {
- // this url matches; delete and remove
- ++removeCount;
- delete pInfo;
- m_urls.RemoveAt(prevPosition);
- break;
- }
- prevPosition = position;
- }
- return removeCount;
- }
- // we assume ownership of pURLInfo
- void
- CHXAvURLList::AddHead(const CHXAvURLInfo*& pURLInfo)
- {
- // remove existing urls that match the one being added
- RemoveMatches(pURLInfo->URL());
- // add new one to head
- m_urls.AddHead((void*)pURLInfo);
- // drop old one off tail if list is full
- if (m_urls.GetCount() > m_maxURLs)
- {
- CHXAvURLInfo* pInfo = (CHXAvURLInfo*)(m_urls.GetTail());
- delete pInfo;
- m_urls.RemoveTail();
- }
- }
- // we assume ownership of pURLInfo
- void
- CHXAvURLList::AddTail(const CHXAvURLInfo*& pURLInfo)
- {
- // remove existing urls that match the one being added
- RemoveMatches(pURLInfo->URL());
- if( m_urls.GetCount() < m_maxURLs )
- {
- // add new one to tail
- m_urls.AddTail((void*)pURLInfo);
- }
- else
- {
- // can't add; delete
- HX_DELETE(pURLInfo);
- }
- }
- void CHXAvURLList::SetMax(int count)
- {
- m_maxURLs = count;
- while (m_urls.GetCount() > m_maxURLs)
- {
- CHXAvURLInfo* pInfo = (CHXAvURLInfo*)(m_urls.GetTail());
- delete pInfo;
- m_urls.RemoveTail();
- }
- }
- CHXAvURLInfo*
- CHXAvURLList::GetURL(int index) const
- {
- if (index < 0 || index > m_urls.GetCount())
- return NULL;
- LISTPOSITION position = m_urls.FindIndex(index);
- if (position != NULL)
- {
- return ((CHXAvURLInfo*)m_urls.GetAt(position));
- }
- return NULL;
- }
- // we assume ownership of pURLInfo
- void
- CHXAvURLList::SetURL(int index, const CHXAvURLInfo*& pURLInfo)
- {
- if (index < 0 || index > m_urls.GetCount())
- return;
- CHXAvURLInfo* pInfo = NULL;
- LISTPOSITION position = m_urls.FindIndex(index);
- if (position != NULL)
- {
- pInfo = (CHXAvURLInfo*)m_urls.GetAt(position);
- delete pInfo;
- m_urls.SetAt(position, (void*)pURLInfo);
- }
- }
- bool
- CHXAvURLList::ReadFile(const CHXString& filename)
- {
- bool ret = false;
- CHXString surl;
- CHXString stitle;
- CHXString tempStr;
- CHXBuffer *buffer = new (ELeave) CHXBuffer(); //XXXLCM leave
- buffer->AddRef();
- buffer->SetSize(320);
- UCHAR *ptr = buffer->GetBuffer();
- int count = 0;
- FILE* fp = ::fopen((const char *)filename, "rt");
- if (fp)
- {
- while(!feof(fp) && count < m_maxURLs)
- {
- if (::fgets((char *)ptr, buffer->GetSize(), fp))
- {
- // Get rid of the new line...
- tempStr = ptr;
- stitle = tempStr.Left(tempStr.GetLength() - 1);
- }
- if (::fgets((char *)ptr, buffer->GetSize(), fp))
- {
- // Get rid of the new line...
- tempStr = ptr;
- surl = tempStr.Left(tempStr.GetLength() - 1);
- }
- if ((!feof(fp)) && (stitle.GetLength() > 0) && (surl.GetLength() > 0))
- {
- // Add to the head of the list....
- CHXAvURLInfo* pClip = new (ELeave) CHXAvURLInfo(surl, stitle);
- AddTail(pClip);
- ++count;
- }
- }
- ret = true;
- ::fclose(fp);
- }
- HX_RELEASE(buffer);
- return ret;
- }
- bool
- CHXAvURLList::WriteFile(const CHXString& filename) const
- {
- bool ret = false;
-
- FILE* fp = ::fopen(filename, "wt");
- if (fp)
- {
- CHXString stitle;
- CHXString surl;
- LISTPOSITION position = m_urls.GetHeadPosition();
- while (position != NULL)
- {
- CHXAvURLInfo* pURL = (CHXAvURLInfo*)(m_urls.GetNext(position));
- HX_ASSERT(pURL != NULL);
- if (pURL != NULL)
- {
- CHXAvStringUtils::DesToString(pURL->Title(), stitle);
- CHXAvStringUtils::DesToString(pURL->URL(), surl);
- ::fprintf(fp, "%sn", (const char*)stitle);
- ::fprintf(fp, "%sn", (const char*)surl);
- }
- }
- ::fclose(fp);
- ret = true;
- }
- return ret;
- }