2 * Copyright (C) 2011-2012 Team XBMC
5 * This Program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2, or (at your option)
10 * This Program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with XBMC; see the file COPYING. If not, write to
17 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
18 * http://www.gnu.org/copyleft/gpl.html
22 #include "FileURLProtocol.h"
23 #include "filesystem/File.h"
24 #include "utils/log.h"
27 #define URL_RDONLY 1 /**< read-only */
28 #define URL_WRONLY 2 /**< write-only */
29 #define URL_RDWR (URL_RDONLY|URL_WRONLY) /**< read-write */
31 #define AVSEEK_SIZE 0x10000
32 #define AVSEEK_FORCE 0x20000
34 //========================================================================
35 int CFileURLProtocol::Open(AML_URLContext *h, const char *filename, int flags)
37 if (flags != URL_RDONLY)
39 CLog::Log(LOGDEBUG, "CFileURLProtocol::Open: Only read-only is supported");
43 CStdString url = filename;
44 if (url.Left(strlen("xb-http://")).Equals("xb-http://"))
46 url = url.Right(url.size() - strlen("xb-"));
48 else if (url.Left(strlen("xb-https://")).Equals("xb-https://"))
50 url = url.Right(url.size() - strlen("xb-"));
52 else if (url.Left(strlen("xb-ftp://")).Equals("xb-ftp://"))
54 url = url.Right(url.size() - strlen("xb-"));
56 else if (url.Left(strlen("xb-ftps://")).Equals("xb-ftps://"))
58 url = url.Right(url.size() - strlen("xb-"));
61 CLog::Log(LOGDEBUG, "CFileURLProtocol::Open filename2(%s)", url.c_str());
62 // open the file, always in read mode, calc bitrate
63 unsigned int cflags = READ_BITRATE;
64 XFILE::CFile *cfile = new XFILE::CFile();
66 if (CFileItem(url, true).IsInternetStream())
67 cflags |= READ_CACHED;
69 // open file in binary mode
70 if (!cfile->Open(url, cflags))
76 h->priv_data = (void *)cfile;
81 //========================================================================
82 int CFileURLProtocol::Read(AML_URLContext *h, unsigned char *buf, int size)
84 XFILE::CFile *cfile = (XFILE::CFile*)h->priv_data;
86 int readsize = cfile->Read(buf, size);
87 //CLog::Log(LOGDEBUG, "CFileURLProtocol::Read size(%d), readsize(%d)", size, readsize);
92 //========================================================================
93 int CFileURLProtocol::Write(AML_URLContext *h, unsigned char *buf, int size)
95 //CLog::Log(LOGDEBUG, "CFileURLProtocol::Write size(%d)", size);
99 //========================================================================
100 int64_t CFileURLProtocol::Seek(AML_URLContext *h, int64_t pos, int whence)
102 //CLog::Log(LOGDEBUG, "CFileURLProtocol::Seek1 pos(%lld), whence(%d)", pos, whence);
103 XFILE::CFile *cfile = (XFILE::CFile*)h->priv_data;
104 whence &= ~AVSEEK_FORCE;
106 // seek to the end of file
107 if (pos == -1 || whence == AVSEEK_SIZE)
108 pos = cfile->GetLength();
110 pos = cfile->Seek(pos, whence);
112 //CLog::Log(LOGDEBUG, "CFileURLProtocol::Seek2 pos(%lld), whence(%d)", pos, whence);
117 //========================================================================
118 int64_t CFileURLProtocol::SeekEx(AML_URLContext *h, int64_t pos, int whence)
120 //CLog::Log(LOGDEBUG, "CFileURLProtocol::SeekEx1 pos(%lld), whence(%d)", pos, whence);
121 XFILE::CFile *cfile = (XFILE::CFile*)h->priv_data;
122 whence &= ~AVSEEK_FORCE;
124 // seek to the end of file
125 if (pos == -1 || whence == AVSEEK_SIZE)
126 pos = cfile->GetLength();
128 pos = cfile->Seek(pos, whence);
130 //CLog::Log(LOGDEBUG, "CFileURLProtocol::SeekEx2 pos(%lld), whence(%d)", pos, whence);
135 //========================================================================
136 int CFileURLProtocol::Close(AML_URLContext *h)
138 CLog::Log(LOGDEBUG, "CFileURLProtocol::Close");
139 XFILE::CFile *cfile = (XFILE::CFile*)h->priv_data;