Merge pull request #4594 from FernetMenta/paplayer
[xbmc.git] / xbmc / filesystem / ISOFile.cpp
blob8c403fbd6706c3d22f4d1dfc955778c27ba5396d
1 /*
2 * Copyright (c) 2002 Frodo
3 * Portions Copyright (c) by the authors of ffmpeg and xvid
4 * Copyright (C) 2002-2013 Team XBMC
5 * http://xbmc.org
7 * This Program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2, or (at your option)
10 * any later version.
12 * This Program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with XBMC; see the file COPYING. If not, see
19 * <http://www.gnu.org/licenses/>.
23 #include "ISOFile.h"
24 #include "URL.h"
25 #include "iso9660.h"
27 #include <sys/stat.h>
29 using namespace std;
30 using namespace XFILE;
32 //////////////////////////////////////////////////////////////////////
33 // Construction/Destruction
34 //////////////////////////////////////////////////////////////////////
35 //*********************************************************************************************
36 CISOFile::CISOFile()
38 m_bOpened = false;
41 //*********************************************************************************************
42 CISOFile::~CISOFile()
44 if (m_bOpened)
46 Close();
49 //*********************************************************************************************
50 bool CISOFile::Open(const CURL& url)
52 string strFName = "\\";
53 strFName += url.GetFileName();
54 for (int i = 0; i < (int)strFName.size(); ++i )
56 if (strFName[i] == '/') strFName[i] = '\\';
58 m_hFile = m_isoReader.OpenFile((char*)strFName.c_str());
59 if (m_hFile == INVALID_HANDLE_VALUE)
61 m_bOpened = false;
62 return false;
65 m_bOpened = true;
66 return true;
69 //*********************************************************************************************
70 unsigned int CISOFile::Read(void *lpBuf, int64_t uiBufSize)
72 if (!m_bOpened) return 0;
73 char *pData = (char *)lpBuf;
75 if (m_cache.getSize() > 0)
77 long lTotalBytesRead = 0;
78 while (uiBufSize > 0)
80 if (m_cache.getMaxReadSize() )
82 long lBytes2Read = m_cache.getMaxReadSize();
83 if (lBytes2Read > uiBufSize) lBytes2Read = (long)uiBufSize;
84 m_cache.ReadData(pData, lBytes2Read );
85 uiBufSize -= lBytes2Read ;
86 pData += lBytes2Read;
87 lTotalBytesRead += lBytes2Read ;
90 if (m_cache.getMaxWriteSize() > 5000)
92 uint8_t buffer[5000];
93 long lBytesRead = m_isoReader.ReadFile( m_hFile, buffer, sizeof(buffer));
94 if (lBytesRead > 0)
95 m_cache.WriteData((char*)buffer, lBytesRead);
96 else
97 return 0;
100 return lTotalBytesRead;
102 int iResult = m_isoReader.ReadFile( m_hFile, (uint8_t*)pData, (long)uiBufSize);
103 if (iResult == -1)
104 return 0;
105 return iResult;
108 //*********************************************************************************************
109 void CISOFile::Close()
111 if (!m_bOpened) return ;
112 m_isoReader.CloseFile( m_hFile);
115 //*********************************************************************************************
116 int64_t CISOFile::Seek(int64_t iFilePosition, int iWhence)
118 if (!m_bOpened) return -1;
119 int64_t lNewPos = m_isoReader.Seek(m_hFile, iFilePosition, iWhence);
120 if(lNewPos >= 0)
121 m_cache.Clear();
122 return lNewPos;
125 //*********************************************************************************************
126 int64_t CISOFile::GetLength()
128 if (!m_bOpened) return -1;
129 return m_isoReader.GetFileSize(m_hFile);
132 //*********************************************************************************************
133 int64_t CISOFile::GetPosition()
135 if (!m_bOpened) return -1;
136 return m_isoReader.GetFilePosition(m_hFile);
139 bool CISOFile::Exists(const CURL& url)
141 string strFName = "\\";
142 strFName += url.GetFileName();
143 for (int i = 0; i < (int)strFName.size(); ++i )
145 if (strFName[i] == '/') strFName[i] = '\\';
147 m_hFile = m_isoReader.OpenFile((char*)strFName.c_str());
148 if (m_hFile == INVALID_HANDLE_VALUE)
149 return false;
151 m_isoReader.CloseFile(m_hFile);
152 return true;
155 int CISOFile::Stat(const CURL& url, struct __stat64* buffer)
157 string strFName = "\\";
158 strFName += url.GetFileName();
159 for (int i = 0; i < (int)strFName.size(); ++i )
161 if (strFName[i] == '/') strFName[i] = '\\';
163 m_hFile = m_isoReader.OpenFile((char*)strFName.c_str());
164 if (m_hFile != INVALID_HANDLE_VALUE)
166 memset(buffer, 0, sizeof(struct __stat64));
167 buffer->st_size = m_isoReader.GetFileSize(m_hFile);
168 buffer->st_mode = _S_IFREG;
169 m_isoReader.CloseFile(m_hFile);
170 return 0;
172 errno = ENOENT;
173 return -1;