chxavramparser.cpp
上传用户:zhongxx05
上传日期:2007-06-06
资源大小:33641k
文件大小:3k
- /*============================================================================*
- *
- * (c) 1995-2002 RealNetworks, Inc. Patents pending. All rights reserved.
- *
- *============================================================================*/
-
- /*
- * Description:
- *
- */
- #include <ctype.h>
- #include "hxstring.h"
- #include "chxavurlrep.h"
- #include "chxavramparser.h"
- #include "chxavurlrep.h"
- #include "chxavurlfix.h"
- #include "chxavparseiterator.h"
- #include "chxavramparser.h"
- class URLParser {
- public:
- URLParser();
- bool Parse(const CHXString& line);
- bool Valid() const;
- void Reset();
- const CHXAvURLRep& Value() const;
-
- private:
- CHXAvURLRep m_url;
- };
- inline
- static
- CHXString Trim(const CHXString& line)
- {
- int len = line.GetLength();
- // trim left
- const char* s = line;
- while (*s && isspace(*s))
- {
- ++s;
- --len;
- }
- // trim right
- const char* p = strrchr(s, '#');
- if (p)
- len = p - s;
- while (len > 0 && isspace(s[len-1]))
- --len;
-
- return CHXString((const char*)s, len);
- }
- URLParser::URLParser()
- : m_url("")
- {}
- bool URLParser::Parse(const CHXString& line)
- {
- CHXString trimmed = Trim(line);
- // Make sure the line contains characters
- // and has '://' which should be present in
- // all RAM file URLs
- if ((trimmed.GetLength() > 0) &&
- (strstr((const char*)trimmed, "://")))
- {
- CHXAvURLRep url(trimmed);
-
- if (!url.Valid())
- {
- // Perhaps invalid because it has back-slashes instead of forward slashes
- if(stricmp(url.Protocol(), "file") == 0)
- {
- CHXString strNewPath = url.Path();
- if( strNewPath.FindAndReplace("\", "/", TRUE) )
- {
- url = CHXAvURLRep(url.Protocol(), url.Host(), url.Port(), strNewPath);
- }
- }
- }
- if (!url.Valid())
- {
- // Perhaps invalid because it has unescaped chars
- url = CHXAvURLFixup::TryEscapeURL(url);
- }
-
- // check for valid URL and supported protocols
- if (url.Valid() &&
- (stricmp(url.Protocol(), "rtsp") == 0 ||
- stricmp(url.Protocol(), "http") == 0 ||
- stricmp(url.Protocol(), "file") == 0))
- {
- m_url = url;
- return true;
- }
- }
- return false;
- }
- bool URLParser::Valid() const
- {
- return (m_url.Valid() && (m_url.Protocol().GetLength() > 0));
- }
- void URLParser::Reset()
- {
- m_url = CHXAvURLRep("");
- }
- const CHXAvURLRep& URLParser::Value() const
- {
- return m_url;
- }
- //////////////////////////////////////////////////////////
- //
- // the returned playlist is guaranteed to be valid (have at least one item)
- //
- CHXAvPlaylistPtr CHXAvRAMParser::Parse(const CHXString& filename)
- {
- CHXAvPlaylistPtr pList = new (ELeave) CHXAvPlaylist(); // XXXLCM can leave
- CHXAvParseIterator<URLParser> urls(filename);
- for (; urls.More(); urls.Next())
- {
- const URLParser& url = urls.Current();
- if (url.Valid())
- pList->Append(url.Value());
- }
- return pList->Length() > 0 ? pList : CHXAvPlaylistPtr(0);
- }