Merge pull request #3562 from Karlson2k/vfs_update_small_02
[vuplus_xbmc] / xbmc / filesystem / File.h
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
6  *
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.
11  *
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.
16  *
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/>.
20  *
21  */
22
23 // File.h: interface for the CFile class.
24 //
25 //////////////////////////////////////////////////////////////////////
26
27 #if !defined(AFX_FILE_H__A7ED6320_C362_49CB_8925_6C6C8CAE7B78__INCLUDED_)
28 #define AFX_FILE_H__A7ED6320_C362_49CB_8925_6C6C8CAE7B78__INCLUDED_
29
30 #pragma once
31
32 #include <iostream>
33 #include "utils/StdString.h"
34 #include "IFileTypes.h"
35 #include "PlatformDefs.h"
36
37 class BitstreamStats;
38 class CURL;
39
40 namespace XFILE
41 {
42
43 class IFile;
44
45 class IFileCallback
46 {
47 public:
48   virtual bool OnFileCallback(void* pContext, int ipercent, float avgSpeed) = 0;
49   virtual ~IFileCallback() {};
50 };
51
52 /* indicate that caller can handle truncated reads, where function returns before entire buffer has been filled */
53 #define READ_TRUNCATED 0x01
54
55 /* indicate that that caller support read in the minimum defined chunk size, this disables internal cache then */
56 #define READ_CHUNKED   0x02
57
58 /* use cache to access this file */
59 #define READ_CACHED     0x04
60
61 /* open without caching. regardless to file type. */
62 #define READ_NO_CACHE  0x08
63
64 /* calcuate bitrate for file while reading */
65 #define READ_BITRATE   0x10
66
67 /* indicate the caller will seek between multiple streams in the file frequently */
68 #define READ_MULTI_STREAM 0x20
69
70 class CFileStreamBuffer;
71
72 class auto_buffer
73 {
74 public:
75   auto_buffer(void) : p(NULL), s(0)
76   { }
77   explicit auto_buffer(size_t size);
78   ~auto_buffer();
79
80   auto_buffer& allocate(size_t size);
81   auto_buffer& resize(size_t newSize);
82   auto_buffer& clear(void);
83
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; }
87
88   auto_buffer& attach(void* pointer, size_t size);
89   void* detach(void);
90
91 private:
92   auto_buffer(const auto_buffer& other); // disallow copy constructor
93   auto_buffer& operator=(const auto_buffer& other); // disallow assignment
94
95   void* p;
96   size_t s;
97 };
98
99 class CFile
100 {
101 public:
102   CFile();
103   ~CFile();
104
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);
110   void Flush();
111   int64_t Seek(int64_t iFilePosition, int iWhence = SEEK_SET);
112   int Truncate(int64_t iSize);
113   int64_t GetPosition() const;
114   int64_t GetLength();
115   void Close();
116   int GetChunkSize();
117   std::string GetContentMimeType(void);
118   std::string GetContentCharset(void);
119   unsigned int LoadFile(const std::string &filename, auto_buffer& outputBuffer);
120
121
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)
125   {
126     if(chunk)
127       return chunk * ((minimum + chunk - 1) / chunk);
128     else
129       return minimum;
130   }
131
132   bool SkipNext();
133   BitstreamStats* GetBitstreamStats() { return m_bitStreamStats; }
134
135   int IoControl(EIoControl request, void* param);
136
137   IFile *GetImplemenation() { return m_pFile; }
138
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);
146
147 private:
148   unsigned int m_flags;
149   IFile* m_pFile;
150   CFileStreamBuffer* m_pBuffer;
151   BitstreamStats* m_bitStreamStats;
152 };
153
154 // streambuf for file io, only supports buffered input currently
155 class CFileStreamBuffer
156   : public std::streambuf
157 {
158 public:
159   ~CFileStreamBuffer();
160   CFileStreamBuffer(int backsize = 0);
161
162   void Attach(IFile *file);
163   void Detach();
164
165 private:
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);
170
171   IFile* m_file;
172   char*  m_buffer;
173   int    m_backsize;
174   int    m_frontsize;
175 };
176
177 // very basic file input stream
178 class CFileStream
179   : public std::istream
180 {
181 public:
182   CFileStream(int backsize = 0);
183   ~CFileStream();
184
185   bool Open(const CStdString& filename);
186   bool Open(const CURL& filename);
187   void Close();
188
189   int64_t GetLength();
190 private:
191   CFileStreamBuffer m_buffer;
192   IFile*            m_file;
193 };
194
195 }
196 #endif // !defined(AFX_FILE_H__A7ED6320_C362_49CB_8925_6C6C8CAE7B78__INCLUDED_)