CompressedData.cpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /**
  2. * Copyright (c) 2006-2017 LOVE Development Team
  3. *
  4. * This software is provided 'as-is', without any express or implied
  5. * warranty. In no event will the authors be held liable for any damages
  6. * arising from the use of this software.
  7. *
  8. * Permission is granted to anyone to use this software for any purpose,
  9. * including commercial applications, and to alter it and redistribute it
  10. * freely, subject to the following restrictions:
  11. *
  12. * 1. The origin of this software must not be misrepresented; you must not
  13. * claim that you wrote the original software. If you use this software
  14. * in a product, an acknowledgment in the product documentation would be
  15. * appreciated but is not required.
  16. * 2. Altered source versions must be plainly marked as such, and must not be
  17. * misrepresented as being the original software.
  18. * 3. This notice may not be removed or altered from any source distribution.
  19. **/
  20. // LOVE
  21. #include "CompressedData.h"
  22. namespace love
  23. {
  24. namespace math
  25. {
  26. love::Type CompressedData::type("CompressedData", &Data::type);
  27. CompressedData::CompressedData(Compressor::Format format, char *cdata, size_t compressedsize, size_t rawsize, bool own)
  28. : format(format)
  29. , data(nullptr)
  30. , dataSize(compressedsize)
  31. , originalSize(rawsize)
  32. {
  33. if (own)
  34. data = cdata;
  35. else
  36. {
  37. try
  38. {
  39. data = new char[dataSize];
  40. }
  41. catch (std::bad_alloc &)
  42. {
  43. throw love::Exception("Out of memory.");
  44. }
  45. memcpy(data, cdata, dataSize);
  46. }
  47. }
  48. CompressedData::CompressedData(const CompressedData &c)
  49. : format(c.format)
  50. , data(nullptr)
  51. , dataSize(c.dataSize)
  52. , originalSize(c.originalSize)
  53. {
  54. try
  55. {
  56. data = new char[dataSize];
  57. }
  58. catch (std::bad_alloc &)
  59. {
  60. throw love::Exception("Out of memory.");
  61. }
  62. memcpy(data, c.data, dataSize);
  63. }
  64. CompressedData::~CompressedData()
  65. {
  66. delete[] data;
  67. }
  68. Data *CompressedData::clone() const
  69. {
  70. return new CompressedData(*this);
  71. }
  72. Compressor::Format CompressedData::getFormat() const
  73. {
  74. return format;
  75. }
  76. size_t CompressedData::getDecompressedSize() const
  77. {
  78. return originalSize;
  79. }
  80. void *CompressedData::getData() const
  81. {
  82. return data;
  83. }
  84. size_t CompressedData::getSize() const
  85. {
  86. return dataSize;
  87. }
  88. } // math
  89. } // love