2 * Copyright (c) 2002 Frodo
3 * Portions Copyright (c) by the authors of ffmpeg and xvid
4 * Copyright (C) 2002-2013 Team XBMC
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)
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 // File.h: interface for the CFile class.
25 //////////////////////////////////////////////////////////////////////
27 #if !defined(AFX_FILE_H__A7ED6320_C362_49CB_8925_6C6C8CAE7B78__INCLUDED_)
28 #define AFX_FILE_H__A7ED6320_C362_49CB_8925_6C6C8CAE7B78__INCLUDED_
33 #include "utils/StdString.h"
34 #include "IFileTypes.h"
35 #include "PlatformDefs.h"
48 virtual bool OnFileCallback(void* pContext, int ipercent, float avgSpeed) = 0;
49 virtual ~IFileCallback() {};
52 /* indicate that caller can handle truncated reads, where function returns before entire buffer has been filled */
53 #define READ_TRUNCATED 0x01
55 /* indicate that that caller support read in the minimum defined chunk size, this disables internal cache then */
56 #define READ_CHUNKED 0x02
58 /* use cache to access this file */
59 #define READ_CACHED 0x04
61 /* open without caching. regardless to file type. */
62 #define READ_NO_CACHE 0x08
64 /* calcuate bitrate for file while reading */
65 #define READ_BITRATE 0x10
67 /* indicate the caller will seek between multiple streams in the file frequently */
68 #define READ_MULTI_STREAM 0x20
70 class CFileStreamBuffer;
75 auto_buffer(void) : p(NULL), s(0)
77 explicit auto_buffer(size_t size);
80 auto_buffer& allocate(size_t size);
81 auto_buffer& resize(size_t newSize);
82 auto_buffer& clear(void);
84 inline char* get(void) const { return static_cast<char*>(p); }
85 inline size_t size(void) const { return s; }
86 inline size_t length(void) const { return s; }
88 auto_buffer& attach(void* pointer, size_t size);
92 auto_buffer(const auto_buffer& other); // disallow copy constructor
93 auto_buffer& operator=(const auto_buffer& other); // disallow assignment
105 bool Open(const CStdString& strFileName, const unsigned int flags = 0);
106 bool OpenForWrite(const CStdString& strFileName, bool bOverWrite = false);
107 unsigned int Read(void* lpBuf, int64_t uiBufSize);
108 bool ReadString(char *szLine, int iLineLength);
109 int Write(const void* lpBuf, int64_t uiBufSize);
111 int64_t Seek(int64_t iFilePosition, int iWhence = SEEK_SET);
112 int Truncate(int64_t iSize);
113 int64_t GetPosition() const;
117 std::string GetContentMimeType(void);
118 std::string GetContentCharset(void);
119 unsigned int LoadFile(const std::string &filename, auto_buffer& outputBuffer);
122 // will return a size, that is aligned to chunk size
123 // but always greater or equal to the file's chunk size
124 static int GetChunkSize(int chunk, int minimum)
127 return chunk * ((minimum + chunk - 1) / chunk);
133 BitstreamStats* GetBitstreamStats() { return m_bitStreamStats; }
135 int IoControl(EIoControl request, void* param);
137 IFile *GetImplemenation() { return m_pFile; }
139 static bool Exists(const CStdString& strFileName, bool bUseCache = true);
140 static int Stat(const CStdString& strFileName, struct __stat64* buffer);
141 int Stat(struct __stat64 *buffer);
142 static bool Delete(const CStdString& strFileName);
143 static bool Rename(const CStdString& strFileName, const CStdString& strNewFileName);
144 static bool Cache(const CStdString& strFileName, const CStdString& strDest, XFILE::IFileCallback* pCallback = NULL, void* pContext = NULL);
145 static bool SetHidden(const CStdString& fileName, bool hidden);
148 unsigned int m_flags;
150 CFileStreamBuffer* m_pBuffer;
151 BitstreamStats* m_bitStreamStats;
154 // streambuf for file io, only supports buffered input currently
155 class CFileStreamBuffer
156 : public std::streambuf
159 ~CFileStreamBuffer();
160 CFileStreamBuffer(int backsize = 0);
162 void Attach(IFile *file);
166 virtual int_type underflow();
167 virtual std::streamsize showmanyc();
168 virtual pos_type seekoff(off_type, std::ios_base::seekdir,std::ios_base::openmode = std::ios_base::in | std::ios_base::out);
169 virtual pos_type seekpos(pos_type, std::ios_base::openmode = std::ios_base::in | std::ios_base::out);
177 // very basic file input stream
179 : public std::istream
182 CFileStream(int backsize = 0);
185 bool Open(const CStdString& filename);
186 bool Open(const CURL& filename);
191 CFileStreamBuffer m_buffer;
196 #endif // !defined(AFX_FILE_H__A7ED6320_C362_49CB_8925_6C6C8CAE7B78__INCLUDED_)