xbmc
File.h
1 /*
2  * Copyright (C) 2005-2018 Team Kodi
3  * This file is part of Kodi - https://kodi.tv
4  *
5  * SPDX-License-Identifier: GPL-2.0-or-later
6  * See LICENSES/README.md for more information.
7  */
8 
9 #pragma once
10 
11 #include "AddonClass.h"
12 #include "AddonString.h"
13 #include "LanguageHook.h"
14 #include "commons/Buffer.h"
15 #include "filesystem/File.h"
16 
17 #include <algorithm>
18 
19 namespace XBMCAddon
20 {
21 
22  namespace xbmcvfs
23  {
24 
25  //
57  //
58  class File : public AddonClass
59  {
60  XFILE::CFile* file;
61  public:
62  inline File(const String& filepath, const char* mode = NULL) : file(new XFILE::CFile())
63  {
64  DelayedCallGuard dg(languageHook);
65  if (mode && strncmp(mode, "w", 1) == 0)
66  file->OpenForWrite(filepath,true);
67  else
68  file->Open(filepath, XFILE::READ_NO_CACHE);
69  }
70 
71  inline ~File() override { delete file; }
72 
73 #if !defined(DOXYGEN_SHOULD_USE_THIS)
74  inline File* __enter__() { return this; }
75  inline void __exit__() { close(); }
76 #endif
77 
78 #ifdef DOXYGEN_SHOULD_USE_THIS
79  read(...);
109 #else
110  inline String read(unsigned long numBytes = 0)
111  {
112  XbmcCommons::Buffer b = readBytes(numBytes);
113  return b.getString(numBytes == 0 ? b.remaining() : std::min((unsigned long)b.remaining(),numBytes));
114  }
115 #endif
116 
117 #ifdef DOXYGEN_SHOULD_USE_THIS
118  readBytes(...);
148 #else
149  XbmcCommons::Buffer readBytes(unsigned long numBytes = 0);
150 #endif
151 
152 #ifdef DOXYGEN_SHOULD_USE_THIS
153  write(...);
182 #else
183  bool write(XbmcCommons::Buffer& buffer);
184 #endif
185 
186 #ifdef DOXYGEN_SHOULD_USE_THIS
187  size();
215 #else
216  inline long long size() { DelayedCallGuard dg(languageHook); return file->GetLength(); }
217 #endif
218 
219 #ifdef DOXYGEN_SHOULD_USE_THIS
220  seek(...);
251 #else
252  inline long long seek(long long seekBytes, int iWhence = SEEK_SET) { DelayedCallGuard dg(languageHook); return file->Seek(seekBytes,iWhence); }
253 #endif
254 
255 #ifdef DOXYGEN_SHOULD_USE_THIS
256  tell();
285 #else
286  inline long long tell() { DelayedCallGuard dg(languageHook); return file->GetPosition(); }
287 #endif
288 
289 #ifdef DOXYGEN_SHOULD_USE_THIS
290  close();
315 #else
316  inline void close() { DelayedCallGuard dg(languageHook); file->Close(); }
317 #endif
318 
319 #ifndef SWIG
320  inline const XFILE::CFile* getFile() const { return file; }
321 #endif
322 
323  };
325  }
326 }
size_t remaining() const
This method provides for the remaining number of bytes that can be read out of the buffer or written ...
Definition: Buffer.h:207
Definition: File.h:37
Defining LOG_LIFECYCLE_EVENTS will log all instantiations, deletions and also reference countings (in...
Definition: Addon.cpp:25
Definition: File.h:58
bool Open(const CURL &file, const unsigned int flags=0)
Attempt to open an IFile instance.
Definition: File.cpp:245
This class is based on the java java.nio.Buffer class however, it does not implement the &#39;mark&#39; funct...
Definition: Buffer.h:79
This class can be used to access the language hook&#39;s DelayedCallOpen and DelayedCallClose.
Definition: LanguageHook.h:122
This class is the superclass for all reference counted classes in the api.
Definition: AddonClass.h:57