1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
|
/*
* Copyright (C) 2005-2013 Team XBMC
* http://xbmc.org
*
* This Program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This Program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with XBMC; see the file COPYING. If not, see
* <http://www.gnu.org/licenses/>.
*
*/
#pragma once
#include "filesystem/File.h"
#include "AddonString.h"
#include "AddonClass.h"
#include "LanguageHook.h"
#include "commons/Buffer.h"
#include <algorithm>
namespace XBMCAddon
{
namespace xbmcvfs
{
//
/// \defgroup python_file File
/// \ingroup python_xbmcvfs
/// @{
/// @brief <b>Kodi's file class.</b>
///
/// \python_class{ xbmcvfs.File(filepath, [mode]) }
///
/// @param filepath string Selected file path
/// @param mode [opt] string Additional mode options (if no mode is supplied, the default is Open for Read).
/// | Mode | Description |
/// |:------:|:--------------------------------|
/// | w | Open for write |
///
///
///--------------------------------------------------------------------------
///
/// **Example:**
/// ~~~~~~~~~~~~~{.py}
/// ..
/// f = xbmcvfs.File(file, 'w')
/// ..
/// ~~~~~~~~~~~~~
//
class File : public AddonClass
{
XFILE::CFile* file;
public:
inline File(const String& filepath, const char* mode = NULL) : file(new XFILE::CFile())
{
DelayedCallGuard dg(languageHook);
if (mode && strncmp(mode, "w", 1) == 0)
file->OpenForWrite(filepath,true);
else
file->Open(filepath, XFILE::READ_NO_CACHE);
}
inline ~File() { delete file; }
#ifdef DOXYGEN_SHOULD_USE_THIS
///
/// \ingroup python_file
/// @brief \python_func{ read([bytes]) }
///-----------------------------------------------------------------------
/// Read file parts as string.
///
/// @param bytes [opt] How many bytes to read - if not
/// set it will read the whole file
/// @return string
///
///
///-----------------------------------------------------------------------
///
/// **Example:**
/// ~~~~~~~~~~~~~{.py}
/// ..
/// f = xbmcvfs.File(file)
/// b = f.read()
/// f.close()
/// ..
/// ~~~~~~~~~~~~~
///
read(...);
#else
inline String read(unsigned long numBytes = 0)
{
XbmcCommons::Buffer b = readBytes(numBytes);
return b.getString(numBytes == 0 ? b.remaining() : std::min((unsigned long)b.remaining(),numBytes));
}
#endif
#ifdef DOXYGEN_SHOULD_USE_THIS
///
/// \ingroup python_file
/// @brief \python_func{ readBytes(numbytes) }
///-----------------------------------------------------------------------
/// Read bytes from file.
///
/// @param numbytes How many bytes to read [opt]- if not set
/// it will read the whole file
/// @return bytearray
///
///
///-----------------------------------------------------------------------
///
/// **Example:**
/// ~~~~~~~~~~~~~{.py}
/// ..
/// f = xbmcvfs.File(file)
/// b = f.read()
/// f.close()
/// ..
/// ~~~~~~~~~~~~~
///
readBytes(...);
#else
XbmcCommons::Buffer readBytes(unsigned long numBytes = 0);
#endif
#ifdef DOXYGEN_SHOULD_USE_THIS
///
/// \ingroup python_file
/// @brief \python_func{ write(buffer) }
///-----------------------------------------------------------------------
/// To write given data in file.
///
/// @param buffer Buffer to write to file
/// @return True on success.
///
///
///-----------------------------------------------------------------------
///
/// **Example:**
/// ~~~~~~~~~~~~~{.py}
/// ..
/// f = xbmcvfs.File(file, 'w')
/// result = f.write(buffer)
/// f.close()
/// ..
/// ~~~~~~~~~~~~~
///
write(...);
#else
bool write(XbmcCommons::Buffer& buffer);
#endif
#ifdef DOXYGEN_SHOULD_USE_THIS
///
/// \ingroup python_file
/// @brief \python_func{ size() }
///-----------------------------------------------------------------------
/// Get the file size.
///
/// @return The file size
///
///
///-----------------------------------------------------------------------
///
/// **Example:**
/// ~~~~~~~~~~~~~{.py}
/// ..
/// f = xbmcvfs.File(file)
/// s = f.size()
/// f.close()
/// ..
/// ~~~~~~~~~~~~~
///
size();
#else
inline long long size() { DelayedCallGuard dg(languageHook); return file->GetLength(); }
#endif
#ifdef DOXYGEN_SHOULD_USE_THIS
///
/// \ingroup python_file
/// @brief \python_func{ seek(seekBytes, iWhence) }
///-----------------------------------------------------------------------
/// Seek to position in file.
///
/// @param seekBytes position in the file
/// @param iWhence where in a file to seek from[0 begining,
/// 1 current , 2 end possition]
///
///
///-----------------------------------------------------------------------
///
/// **Example:**
/// ~~~~~~~~~~~~~{.py}
/// ..
/// f = xbmcvfs.File(file)
/// result = f.seek(8129, 0)
/// f.close()
/// ..
/// ~~~~~~~~~~~~~
///
seek(...);
#else
inline long long seek(long long seekBytes, int iWhence) { DelayedCallGuard dg(languageHook); return file->Seek(seekBytes,iWhence); }
#endif
#ifdef DOXYGEN_SHOULD_USE_THIS
///
/// \ingroup python_file
/// @brief \python_func{ close() }
///-----------------------------------------------------------------------
/// Close opened file.
///
///
///-----------------------------------------------------------------------
///
/// **Example:**
/// ~~~~~~~~~~~~~{.py}
/// ..
/// f = xbmcvfs.File(file)
/// f.close()
/// ..
/// ~~~~~~~~~~~~~
///
close();
#else
inline void close() { DelayedCallGuard dg(languageHook); file->Close(); }
#endif
#ifndef SWIG
inline const XFILE::CFile* getFile() const { return file; }
#endif
};
//@}
}
}
|