[PVR][Estuary] Timer settings dialog: Show client name in timer type selection dialog...
[xbmc.git] / xbmc / filesystem / File.h
bloba5f991af301fc7c8ea9cf494d5bfede35b255526
1 /*
2 * Copyright (c) 2002 Frodo
3 * Portions Copyright (c) by the authors of ffmpeg and xvid
4 * Copyright (C) 2002-2018 Team Kodi
5 * This file is part of Kodi - https://kodi.tv
7 * SPDX-License-Identifier: GPL-2.0-or-later
8 * See LICENSES/README.md for more information.
9 */
11 #pragma once
13 // File.h: interface for the CFile class.
15 //////////////////////////////////////////////////////////////////////
17 #include "IFileTypes.h"
18 #include "URL.h"
20 #include <iostream>
21 #include <memory>
22 #include <stdio.h>
23 #include <string>
24 #include <vector>
26 #include "PlatformDefs.h"
28 class BitstreamStats;
30 namespace XFILE
33 class IFile;
35 class CFileStreamBuffer;
37 class CFile
39 public:
40 CFile();
41 ~CFile();
43 bool CURLCreate(const std::string &url);
44 bool CURLAddOption(XFILE::CURLOPTIONTYPE type, const char* name, const char * value);
45 bool CURLOpen(unsigned int flags);
47 /**
48 * Attempt to open an IFile instance.
49 * @param file reference to CCurl file description
50 * @param flags see IFileTypes.h
51 * @return true on success, false otherwise
53 * Remarks: Open can only be called once. Calling
54 * Open() on an already opened file will fail
55 * except if flag READ_REOPEN is set and the underlying
56 * file has an implementation of ReOpen().
58 bool Open(const CURL& file, const unsigned int flags = 0);
59 bool Open(const std::string& strFileName, const unsigned int flags = 0);
61 bool OpenForWrite(const CURL& file, bool bOverWrite = false);
62 bool OpenForWrite(const std::string& strFileName, bool bOverWrite = false);
64 ssize_t LoadFile(const CURL& file, std::vector<uint8_t>& outputBuffer);
66 /**
67 * Attempt to read bufSize bytes from currently opened file into buffer bufPtr.
68 * @param bufPtr pointer to buffer
69 * @param bufSize size of the buffer
70 * @return number of successfully read bytes if any bytes were read and stored in
71 * buffer, zero if no bytes are available to read (end of file was reached)
72 * or undetectable error occur, -1 in case of any explicit error
74 ssize_t Read(void* bufPtr, size_t bufSize);
75 bool ReadString(char *szLine, int iLineLength);
76 /**
77 * Attempt to write bufSize bytes from buffer bufPtr into currently opened file.
78 * @param bufPtr pointer to buffer
79 * @param bufSize size of the buffer
80 * @return number of successfully written bytes if any bytes were written,
81 * zero if no bytes were written and no detectable error occur,
82 * -1 in case of any explicit error
84 ssize_t Write(const void* bufPtr, size_t bufSize);
85 void Flush();
86 int64_t Seek(int64_t iFilePosition, int iWhence = SEEK_SET);
87 int Truncate(int64_t iSize);
88 int64_t GetPosition() const;
89 int64_t GetLength();
90 void Close();
91 int GetChunkSize();
92 const std::string GetProperty(XFILE::FileProperty type, const std::string &name = "") const;
93 const std::vector<std::string> GetPropertyValues(XFILE::FileProperty type, const std::string &name = "") const;
94 ssize_t LoadFile(const std::string& filename, std::vector<uint8_t>& outputBuffer);
96 static int DetermineChunkSize(const int srcChunkSize, const int reqChunkSize);
98 const std::unique_ptr<BitstreamStats>& GetBitstreamStats() const { return m_bitStreamStats; }
100 int IoControl(EIoControl request, void* param);
102 IFile* GetImplementation() const { return m_pFile.get(); }
104 // CURL interface
105 static bool Exists(const CURL& file, bool bUseCache = true);
106 static bool Delete(const CURL& file);
108 * Fills struct __stat64 with information about file specified by filename
109 * For st_mode function will set correctly _S_IFDIR (directory) flag and may set
110 * _S_IREAD (read permission), _S_IWRITE (write permission) flags if such
111 * information is available. Function may set st_size (file size), st_atime,
112 * st_mtime, st_ctime (access, modification, creation times).
113 * Any other flags and members of __stat64 that didn't updated with actual file
114 * information will be set to zero (st_nlink can be set ether to 1 or zero).
115 * @param file specifies requested file
116 * @param buffer pointer to __stat64 buffer to receive information about file
117 * @return zero of success, -1 otherwise.
119 static int Stat(const CURL& file, struct __stat64* buffer);
120 static bool Rename(const CURL& file, const CURL& urlNew);
121 static bool Copy(const CURL& file, const CURL& dest, XFILE::IFileCallback* pCallback = NULL, void* pContext = NULL);
122 static bool SetHidden(const CURL& file, bool hidden);
124 // string interface
125 static bool Exists(const std::string& strFileName, bool bUseCache = true);
127 * Fills struct __stat64 with information about file specified by filename
128 * For st_mode function will set correctly _S_IFDIR (directory) flag and may set
129 * _S_IREAD (read permission), _S_IWRITE (write permission) flags if such
130 * information is available. Function may set st_size (file size), st_atime,
131 * st_mtime, st_ctime (access, modification, creation times).
132 * Any other flags and members of __stat64 that didn't updated with actual file
133 * information will be set to zero (st_nlink can be set ether to 1 or zero).
134 * @param strFileName specifies requested file
135 * @param buffer pointer to __stat64 buffer to receive information about file
136 * @return zero of success, -1 otherwise.
138 static int Stat(const std::string& strFileName, struct __stat64* buffer);
140 * Fills struct __stat64 with information about currently open file
141 * For st_mode function will set correctly _S_IFDIR (directory) flag and may set
142 * _S_IREAD (read permission), _S_IWRITE (write permission) flags if such
143 * information is available. Function may set st_size (file size), st_atime,
144 * st_mtime, st_ctime (access, modification, creation times).
145 * Any other flags and members of __stat64 that didn't updated with actual file
146 * information will be set to zero (st_nlink can be set ether to 1 or zero).
147 * @param buffer pointer to __stat64 buffer to receive information about file
148 * @return zero of success, -1 otherwise.
150 int Stat(struct __stat64 *buffer);
151 static bool Delete(const std::string& strFileName);
152 static bool Rename(const std::string& strFileName, const std::string& strNewFileName);
153 static bool Copy(const std::string& strFileName, const std::string& strDest, XFILE::IFileCallback* pCallback = NULL, void* pContext = NULL);
154 static bool SetHidden(const std::string& fileName, bool hidden);
155 double GetDownloadSpeed();
157 private:
158 unsigned int m_flags = 0;
159 CURL m_curl;
160 std::unique_ptr<IFile> m_pFile;
161 std::unique_ptr<CFileStreamBuffer> m_pBuffer;
162 std::unique_ptr<BitstreamStats> m_bitStreamStats;
165 // streambuf for file io, only supports buffered input currently
166 class CFileStreamBuffer
167 : public std::streambuf
169 public:
170 ~CFileStreamBuffer() override;
171 explicit CFileStreamBuffer(int backsize = 0);
173 void Attach(IFile *file);
174 void Detach();
176 private:
177 int_type underflow() override;
178 std::streamsize showmanyc() override;
179 pos_type seekoff(off_type, std::ios_base::seekdir,std::ios_base::openmode = std::ios_base::in | std::ios_base::out) override;
180 pos_type seekpos(pos_type, std::ios_base::openmode = std::ios_base::in | std::ios_base::out) override;
182 IFile* m_file;
183 char* m_buffer;
184 int m_backsize;
185 int m_frontsize = 0;
188 // very basic file input stream
189 class CFileStream
190 : public std::istream
192 public:
193 explicit CFileStream(int backsize = 0);
194 ~CFileStream() override;
196 bool Open(const std::string& filename);
197 bool Open(const CURL& filename);
198 void Close();
200 int64_t GetLength();
201 private:
202 CFileStreamBuffer m_buffer;
203 std::unique_ptr<IFile> m_file;