fileAPI.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. // zlib open source license
  2. //
  3. // Copyright (c) 2020 David Forsgren Piuva
  4. //
  5. // This software is provided 'as-is', without any express or implied
  6. // warranty. In no event will the authors be held liable for any damages
  7. // arising from the use of this software.
  8. //
  9. // Permission is granted to anyone to use this software for any purpose,
  10. // including commercial applications, and to alter it and redistribute it
  11. // freely, subject to the following restrictions:
  12. //
  13. // 1. The origin of this software must not be misrepresented; you must not
  14. // claim that you wrote the original software. If you use this software
  15. // in a product, an acknowledgment in the product documentation would be
  16. // appreciated but is not required.
  17. //
  18. // 2. Altered source versions must be plainly marked as such, and must not be
  19. // misrepresented as being the original software.
  20. //
  21. // 3. This notice may not be removed or altered from any source
  22. // distribution.
  23. #include <fstream>
  24. #include <cstdlib>
  25. #include "bufferAPI.h"
  26. #include "../math/scalar.h"
  27. #include "../base/text.h"
  28. namespace dsr {
  29. // TODO: Try converting to UTF-8 for file names, which would only have another chance at working
  30. static char toAscii(DsrChar c) {
  31. if (c > 127) {
  32. return '?';
  33. } else {
  34. return c;
  35. }
  36. }
  37. #define TO_RAW_ASCII(TARGET, SOURCE) \
  38. char TARGET[SOURCE.length() + 1]; \
  39. for (int i = 0; i < SOURCE.length(); i++) { \
  40. TARGET[i] = toAscii(SOURCE[i]); \
  41. } \
  42. TARGET[SOURCE.length()] = '\0';
  43. Buffer file_loadBuffer(const ReadableString& filename, bool mustExist) {
  44. // TODO: Load files using Unicode filenames when available
  45. TO_RAW_ASCII(asciiFilename, filename);
  46. std::ifstream fileStream(asciiFilename, std::ios_base::in | std::ios_base::binary);
  47. if (fileStream.is_open()) {
  48. // Get the file's length and allocate an array for the raw encoding
  49. fileStream.seekg (0, fileStream.end);
  50. int64_t fileLength = fileStream.tellg();
  51. fileStream.seekg (0, fileStream.beg);
  52. Buffer buffer = buffer_create(fileLength);
  53. fileStream.read((char*)buffer_dangerous_getUnsafeData(buffer), fileLength);
  54. return buffer;
  55. } else {
  56. if (mustExist) {
  57. throwError(U"The file ", filename, U" could not be opened for reading.\n");
  58. }
  59. // If the file cound not be found and opened, an empty buffer is returned
  60. return Buffer();
  61. }
  62. }
  63. void file_saveBuffer(const ReadableString& filename, Buffer buffer) {
  64. // TODO: Save files using Unicode filenames
  65. if (!buffer_exists(buffer)) {
  66. throwError(U"buffer_save: Cannot save a buffer that don't exist to a file.\n");
  67. } else {
  68. TO_RAW_ASCII(asciiFilename, filename);
  69. std::ofstream fileStream(asciiFilename, std::ios_base::out | std::ios_base::binary);
  70. if (fileStream.is_open()) {
  71. fileStream.write((char*)buffer_dangerous_getUnsafeData(buffer), buffer_getSize(buffer));
  72. fileStream.close();
  73. } else {
  74. throwError("Failed to save ", filename, "\n");
  75. }
  76. }
  77. }
  78. }