summaryrefslogtreecommitdiffstats
path: root/xbmc/interfaces/legacy/File.h
blob: 01a757a318d754cf2521e4083300896f0f17d61e (plain)
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/*
 *  Copyright (C) 2005-2018 Team Kodi
 *  This file is part of Kodi - https://kodi.tv
 *
 *  SPDX-License-Identifier: GPL-2.0-or-later
 *  See LICENSES/README.md for more information.
 */

#pragma once

#include "AddonClass.h"
#include "AddonString.h"
#include "LanguageHook.h"
#include "commons/Buffer.h"
#include "filesystem/File.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                  |
    ///
    ///
    ///--------------------------------------------------------------------------
    /// @python_v19 Added context manager support
    ///
    /// **Example:**
    /// ~~~~~~~~~~~~~{.py}
    /// ..
    /// f = xbmcvfs.File(file, 'w')
    /// ..
    /// ~~~~~~~~~~~~~
    ///
    /// **Example (v19 and up):**
    /// ~~~~~~~~~~~~~{.py}
    /// ..
    /// with xbmcvfs.File(file, 'w') as f:
    ///   ..
    /// ..
    /// ~~~~~~~~~~~~~
    //
    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() override { delete file; }

#if !defined(DOXYGEN_SHOULD_USE_THIS)
      inline File* __enter__() { return this; }
      inline void __exit__() { close(); }
#endif

#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()
      /// ..
      /// ~~~~~~~~~~~~~
      ///
      /// **Example (v19 and up):**
      /// ~~~~~~~~~~~~~{.py}
      /// ..
      /// with xbmcvfs.File(file) as file:
      ///   b = f.read()
      /// ..
      /// ~~~~~~~~~~~~~
      ///
      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.readBytes()
      /// f.close()
      /// ..
      /// ~~~~~~~~~~~~~
      ///
      /// **Example (v19 and up):**
      /// ~~~~~~~~~~~~~{.py}
      /// ..
      /// with xbmcvfs.File(file) as f:
      ///   b = f.readBytes()
      /// ..
      /// ~~~~~~~~~~~~~
      ///
      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()
      /// ..
      /// ~~~~~~~~~~~~~
      ///
      /// **Example (v19 and up):**
      /// ~~~~~~~~~~~~~{.py}
      /// ..
      /// with xbmcvfs.File(file, 'w') as f:
      ///   result = f.write(buffer)
      /// ..
      /// ~~~~~~~~~~~~~
      ///
      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()
      /// ..
      /// ~~~~~~~~~~~~~
      ///
      /// **Example (v19 and up):**
      /// ~~~~~~~~~~~~~{.py}
      /// ..
      /// with xbmcvfs.File(file) as f:
      ///   s = f.size()
      /// ..
      /// ~~~~~~~~~~~~~
      ///
      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            [opt] where in a file to seek from[0 beginning,
      ///                           1 current , 2 end position]
      ///
      ///
      ///-----------------------------------------------------------------------
      /// @python_v19 Function changed. param **iWhence** is now optional.
      ///
      /// **Example:**
      /// ~~~~~~~~~~~~~{.py}
      /// ..
      /// f = xbmcvfs.File(file)
      /// result = f.seek(8129, 0)
      /// f.close()
      /// ..
      /// ~~~~~~~~~~~~~
      ///
      /// **Example (v19 and up):**
      /// ~~~~~~~~~~~~~{.py}
      /// ..
      /// with xbmcvfs.File(file) as f:
      ///   result = f.seek(8129, 0)
      /// ..
      /// ~~~~~~~~~~~~~
      ///
      seek(...);
#else
      inline long long seek(long long seekBytes, int iWhence = SEEK_SET) { DelayedCallGuard dg(languageHook); return file->Seek(seekBytes,iWhence); }
#endif

#ifdef DOXYGEN_SHOULD_USE_THIS
      ///
      /// \ingroup python_file
      /// @brief \python_func{ tell() }
      /// Get the current position in the file.
      ///
      /// @return                       The file position
      ///
      ///
      ///-----------------------------------------------------------------------
      /// @python_v19 New function added
      ///
      /// **Example:**
      /// ~~~~~~~~~~~~~{.py}
      /// ..
      /// f = xbmcvfs.File(file)
      /// s = f.tell()
      /// f.close()
      /// ..
      /// ~~~~~~~~~~~~~
      ///
      /// **Example (v19 and up):**
      /// ~~~~~~~~~~~~~{.py}
      /// ..
      /// with xbmcvfs.File(file) as f:
      ///   s = f.tell()
      /// ..
      /// ~~~~~~~~~~~~~
      ///
      tell();
#else
      inline long long tell() { DelayedCallGuard dg(languageHook); return file->GetPosition(); }
#endif

#ifdef DOXYGEN_SHOULD_USE_THIS
      ///
      /// \ingroup python_file
      /// @brief \python_func{ close() }
      /// Close opened file.
      ///
      ///
      ///-----------------------------------------------------------------------
      ///
      /// **Example:**
      /// ~~~~~~~~~~~~~{.py}
      /// ..
      /// f = xbmcvfs.File(file)
      /// f.close()
      /// ..
      /// ~~~~~~~~~~~~~
      ///
      /// **Example (v19 and up):**
      /// ~~~~~~~~~~~~~{.py}
      /// ..
      /// with xbmcvfs.File(file) as f:
      ///   ..
      /// ..
      /// ~~~~~~~~~~~~~
      ///
      close();
#else
      inline void close() { DelayedCallGuard dg(languageHook); file->Close(); }
#endif

#ifndef SWIG
      inline const XFILE::CFile* getFile() const { return file; }
#endif

    };
    //@}
  }
}