PolyData.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*
  2. Copyright (C) 2011 by Ivan Safrin
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  17. THE SOFTWARE.
  18. */
  19. #pragma once
  20. #include "PolyGlobals.h"
  21. #include "PolyString.h"
  22. namespace Polycode {
  23. /**
  24. * Stores, saves and loads data. This class can save and load arbitrary data to and from disk and convert it to strings.
  25. */
  26. class _PolyExport Data : public PolyBase {
  27. public:
  28. /**
  29. * Default constructor
  30. */
  31. Data();
  32. ~Data();
  33. /**
  34. * Loads data from a file.
  35. * @param fileName Path to the file to load data from.
  36. * @return True if susccessful, false if not
  37. */
  38. bool loadFromFile(const String& fileName);
  39. /**
  40. * Retuns data as a string with the specified encoding.
  41. * @param encoding The encoding to use. Currently only supports String::ENCODING_UTF8
  42. * @return String of the specified encoding.
  43. */
  44. String getAsString(int encoding) const;
  45. /**
  46. * Sets the data from a string with the specified encoding.
  47. * @param str The string to create the data from.
  48. * @param encoding The encoding to use. Currently only supports String::ENCODING_UTF8
  49. */
  50. void setFromString(const String& str, int encoding);
  51. /**
  52. * Saves the data to a file.
  53. * @param fileName Path to the file to save data to.
  54. * @return Returns true if successful or false if otherwise.
  55. */
  56. bool saveToFile(const String& fileName) const;
  57. /**
  58. * Returns pointer to the data.
  59. * @return Pointer to the data buffer.
  60. */
  61. char *getData() const { return data; }
  62. protected:
  63. long dataSize;
  64. char *data;
  65. };
  66. }