xbmc
Archive.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 <cstring>
12 #include <memory>
13 #include <string>
14 #include <vector>
15 
16 #define CARCHIVE_BUFFER_MAX 4096
17 
18 namespace XFILE
19 {
20  class CFile;
21 }
22 class CVariant;
23 class IArchivable;
24 namespace KODI::TIME
25 {
26 struct SystemTime;
27 }
28 
29 class CArchive
30 {
31 public:
32  CArchive(XFILE::CFile* pFile, int mode);
33  ~CArchive();
34 
35  /* CArchive support storing and loading of all C basic integer types
36  * C basic types was chosen instead of fixed size ints (int16_t - int64_t) to support all integer typedefs
37  * For example size_t can be typedef of unsigned int, long or long long depending on platform
38  * while int32_t and int64_t are usually unsigned short, int or long long, but not long
39  * and even if int and long can have same binary representation they are different types for compiler
40  * According to section 5.2.4.2.1 of C99 http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf
41  * minimal size of short int is 16 bits
42  * minimal size of int is 16 bits (usually 32 or 64 bits, larger or equal to short int)
43  * minimal size of long int is 32 bits (larger or equal to int)
44  * minimal size of long long int is 64 bits (larger or equal to long int) */
45  // storing
46  CArchive& operator<<(float f);
47  CArchive& operator<<(double d);
48  CArchive& operator<<(short int s);
49  CArchive& operator<<(unsigned short int us);
50  CArchive& operator<<(int i);
51  CArchive& operator<<(unsigned int ui);
52  CArchive& operator<<(long int l);
53  CArchive& operator<<(unsigned long int ul);
54  CArchive& operator<<(long long int ll);
55  CArchive& operator<<(unsigned long long int ull);
56  CArchive& operator<<(bool b);
57  CArchive& operator<<(char c);
58  CArchive& operator<<(const std::string &str);
59  CArchive& operator<<(const std::wstring& wstr);
60  CArchive& operator<<(const KODI::TIME::SystemTime& time);
61  CArchive& operator<<(IArchivable& obj);
62  CArchive& operator<<(const CVariant& variant);
63  CArchive& operator<<(const std::vector<std::string>& strArray);
64  CArchive& operator<<(const std::vector<int>& iArray);
65 
66  // loading
67  inline CArchive& operator>>(float& f)
68  {
69  return streamin(&f, sizeof(f));
70  }
71 
72  inline CArchive& operator>>(double& d)
73  {
74  return streamin(&d, sizeof(d));
75  }
76 
77  inline CArchive& operator>>(short int& s)
78  {
79  return streamin(&s, sizeof(s));
80  }
81 
82  inline CArchive& operator>>(unsigned short int& us)
83  {
84  return streamin(&us, sizeof(us));
85  }
86 
87  inline CArchive& operator>>(int& i)
88  {
89  return streamin(&i, sizeof(i));
90  }
91 
92  inline CArchive& operator>>(unsigned int& ui)
93  {
94  return streamin(&ui, sizeof(ui));
95  }
96 
97  inline CArchive& operator>>(long int& l)
98  {
99  return streamin(&l, sizeof(l));
100  }
101 
102  inline CArchive& operator>>(unsigned long int& ul)
103  {
104  return streamin(&ul, sizeof(ul));
105  }
106 
107  inline CArchive& operator>>(long long int& ll)
108  {
109  return streamin(&ll, sizeof(ll));
110  }
111 
112  inline CArchive& operator>>(unsigned long long int& ull)
113  {
114  return streamin(&ull, sizeof(ull));
115  }
116 
117  inline CArchive& operator>>(bool& b)
118  {
119  return streamin(&b, sizeof(b));
120  }
121 
122  inline CArchive& operator>>(char& c)
123  {
124  return streamin(&c, sizeof(c));
125  }
126 
127  CArchive& operator>>(std::string &str);
128  CArchive& operator>>(std::wstring& wstr);
129  CArchive& operator>>(KODI::TIME::SystemTime& time);
130  CArchive& operator>>(IArchivable& obj);
131  CArchive& operator>>(CVariant& variant);
132  CArchive& operator>>(std::vector<std::string>& strArray);
133  CArchive& operator>>(std::vector<int>& iArray);
134 
135  bool IsLoading() const;
136  bool IsStoring() const;
137 
138  void Close();
139 
140  enum Mode {load = 0, store};
141 
142 protected:
143  inline CArchive &streamout(const void *dataPtr, size_t size)
144  {
145  auto ptr = static_cast<const uint8_t *>(dataPtr);
146  /* Note, the buffer is flushed as soon as it is full (m_BufferRemain == size) rather
147  * than waiting until we attempt to put more data into an already full buffer */
148  if (m_BufferRemain > size)
149  {
150  memcpy(m_BufferPos, ptr, size);
151  m_BufferPos += size;
152  m_BufferRemain -= size;
153  return *this;
154  }
155 
156  return streamout_bufferwrap(ptr, size);
157  }
158 
159  inline CArchive &streamin(void *dataPtr, size_t size)
160  {
161  auto ptr = static_cast<uint8_t *>(dataPtr);
162  /* Note, refilling the buffer is deferred until we know we need to read more from it */
163  if (m_BufferRemain >= size)
164  {
165  memcpy(ptr, m_BufferPos, size);
166  m_BufferPos += size;
167  m_BufferRemain -= size;
168  return *this;
169  }
170 
171  return streamin_bufferwrap(ptr, size);
172  }
173 
174  XFILE::CFile* m_pFile; //non-owning
175  int m_iMode;
176  std::unique_ptr<uint8_t[]> m_pBuffer;
177  uint8_t *m_BufferPos;
178  size_t m_BufferRemain;
179 
180 private:
181  void FlushBuffer();
182  CArchive &streamout_bufferwrap(const uint8_t *ptr, size_t size);
183  void FillBuffer();
184  CArchive &streamin_bufferwrap(uint8_t *ptr, size_t size);
185 };
Definition: Scraper.h:41
Definition: XTimeUtils.h:30
Definition: File.h:37
Definition: XTimeUtils.cpp:27
Definition: Variant.h:29
Definition: SimpleFS.h:27
Definition: Archive.h:29
Definition: IArchivable.h:13