2 * Copyright (C) 2005-2013 Team XBMC
5 * This Program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2, or (at your option)
10 * This Program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with XBMC; see the file COPYING. If not, see
17 * <http://www.gnu.org/licenses/>.
23 #include "filesystem/File.h"
25 #include "utils/log.h"
28 #pragma GCC diagnostic ignored "-Wlong-long"
31 using namespace XFILE;
33 #define BUFFER_MAX 4096
35 CArchive::CArchive(CFile* pFile, int mode)
40 m_pBuffer = new uint8_t[BUFFER_MAX];
41 memset(m_pBuffer, 0, BUFFER_MAX);
53 void CArchive::Close()
58 bool CArchive::IsLoading()
60 return (m_iMode == load);
63 bool CArchive::IsStoring()
65 return (m_iMode == store);
68 CArchive& CArchive::operator<<(float f)
70 return streamout(&f, sizeof(f));
73 CArchive& CArchive::operator<<(double d)
75 return streamout(&d, sizeof(d));
78 CArchive& CArchive::operator<<(short int s)
80 return streamout(&s, sizeof(s));
83 CArchive& CArchive::operator<<(unsigned short int us)
85 return streamout(&us, sizeof(us));
88 CArchive& CArchive::operator<<(int i)
90 return streamout(&i, sizeof(i));
93 CArchive& CArchive::operator<<(unsigned int ui)
95 return streamout(&ui, sizeof(ui));
98 CArchive& CArchive::operator<<(long int l)
100 return streamout(&l, sizeof(l));
103 CArchive& CArchive::operator<<(unsigned long int ul)
105 return streamout(&ul, sizeof(ul));
108 CArchive& CArchive::operator<<(long long int ll)
110 return streamout(&ll, sizeof(ll));
113 CArchive& CArchive::operator<<(unsigned long long int ull)
115 return streamout(&ull, sizeof(ull));
118 CArchive& CArchive::operator<<(bool b)
120 return streamout(&b, sizeof(b));
123 CArchive& CArchive::operator<<(char c)
125 return streamout(&c, sizeof(c));
128 CArchive& CArchive::operator<<(const std::string& str)
132 return streamout(str.data(), str.size() * sizeof(char));
135 CArchive& CArchive::operator<<(const std::wstring& wstr)
137 *this << wstr.size();
139 return streamout(wstr.data(), wstr.size() * sizeof(wchar_t));
142 CArchive& CArchive::operator<<(const SYSTEMTIME& time)
144 return streamout(&time, sizeof(SYSTEMTIME));
147 CArchive& CArchive::operator<<(IArchivable& obj)
154 CArchive& CArchive::operator<<(const CVariant& variant)
156 *this << (int)variant.type();
157 switch (variant.type())
159 case CVariant::VariantTypeInteger:
160 *this << variant.asInteger();
162 case CVariant::VariantTypeUnsignedInteger:
163 *this << variant.asUnsignedInteger();
165 case CVariant::VariantTypeBoolean:
166 *this << variant.asBoolean();
168 case CVariant::VariantTypeString:
169 *this << variant.asString();
171 case CVariant::VariantTypeWideString:
172 *this << variant.asWideString();
174 case CVariant::VariantTypeDouble:
175 *this << variant.asDouble();
177 case CVariant::VariantTypeArray:
178 *this << variant.size();
179 for (unsigned int index = 0; index < variant.size(); index++)
180 *this << variant[index];
182 case CVariant::VariantTypeObject:
183 *this << variant.size();
184 for (CVariant::const_iterator_map itr = variant.begin_map(); itr != variant.end_map(); itr++)
187 *this << itr->second;
190 case CVariant::VariantTypeNull:
191 case CVariant::VariantTypeConstNull:
199 CArchive& CArchive::operator<<(const std::vector<std::string>& strArray)
201 *this << strArray.size();
202 for (size_t index = 0; index < strArray.size(); index++)
203 *this << strArray.at(index);
208 CArchive& CArchive::operator<<(const std::vector<int>& iArray)
210 *this << iArray.size();
211 for (size_t index = 0; index < iArray.size(); index++)
212 *this << iArray.at(index);
217 inline CArchive& CArchive::streamout(const void* dataPtr, size_t size)
219 const uint8_t* ptr = (const uint8_t*)dataPtr;
221 if (size + m_BufferPos >= BUFFER_MAX)
224 while (size >= BUFFER_MAX)
226 memcpy(m_pBuffer, ptr, BUFFER_MAX);
227 m_BufferPos = BUFFER_MAX;
234 memcpy(m_pBuffer + m_BufferPos, ptr, size);
240 CArchive& CArchive::operator>>(float& f)
242 return streamin(&f, sizeof(f));
245 CArchive& CArchive::operator>>(double& d)
247 return streamin(&d, sizeof(d));
250 CArchive& CArchive::operator>>(short int& s)
252 return streamin(&s, sizeof(s));
255 CArchive& CArchive::operator>>(unsigned short int& us)
257 return streamin(&us, sizeof(us));
260 CArchive& CArchive::operator>>(int& i)
262 return streamin(&i, sizeof(i));
265 CArchive& CArchive::operator>>(unsigned int& ui)
267 return streamin(&ui, sizeof(ui));
270 CArchive& CArchive::operator>>(long int& l)
272 return streamin(&l, sizeof(l));
275 CArchive& CArchive::operator>>(unsigned long int& ul)
277 return streamin(&ul, sizeof(ul));
280 CArchive& CArchive::operator>>(long long int& ll)
282 return streamin(&ll, sizeof(ll));
285 CArchive& CArchive::operator>>(unsigned long long int& ull)
287 return streamin(&ull, sizeof(ull));
290 CArchive& CArchive::operator>>(bool& b)
292 return streamin(&b, sizeof(b));
295 CArchive& CArchive::operator>>(char& c)
297 return streamin(&c, sizeof(c));
300 CArchive& CArchive::operator>>(std::string& str)
305 char *s = new char[iLength];
306 streamin(s, iLength * sizeof(char));
307 str.assign(s, iLength);
313 CArchive& CArchive::operator>>(std::wstring& wstr)
318 wchar_t * const p = new wchar_t[iLength];
319 streamin(p, iLength * sizeof(wchar_t));
320 wstr.assign(p, iLength);
326 CArchive& CArchive::operator>>(SYSTEMTIME& time)
328 return streamin(&time, sizeof(SYSTEMTIME));
331 CArchive& CArchive::operator>>(IArchivable& obj)
338 CArchive& CArchive::operator>>(CVariant& variant)
342 variant = CVariant((CVariant::VariantType)type);
344 switch (variant.type())
346 case CVariant::VariantTypeInteger:
353 case CVariant::VariantTypeUnsignedInteger:
360 case CVariant::VariantTypeBoolean:
367 case CVariant::VariantTypeString:
374 case CVariant::VariantTypeWideString:
381 case CVariant::VariantTypeDouble:
388 case CVariant::VariantTypeArray:
392 for (; size > 0; size--)
396 variant.append(value);
400 case CVariant::VariantTypeObject:
404 for (; size > 0; size--)
410 variant[name] = value;
414 case CVariant::VariantTypeNull:
415 case CVariant::VariantTypeConstNull:
423 CArchive& CArchive::operator>>(std::vector<std::string>& strArray)
428 for (size_t index = 0; index < size; index++)
432 strArray.push_back(str);
438 CArchive& CArchive::operator>>(std::vector<int>& iArray)
443 for (size_t index = 0; index < size; index++)
453 inline CArchive& CArchive::streamin(void* dataPtr, const size_t size)
455 size_t read = m_pFile->Read(dataPtr, size);
458 CLog::Log(LOGERROR, "%s: can't stream out: requested %lu bytes, was read %lu bytes", __FUNCTION__, (unsigned long)size, (unsigned long)read);
459 memset(dataPtr, 0, size);
465 void CArchive::FlushBuffer()
469 m_pFile->Write(m_pBuffer, m_BufferPos);