fileAPI.cpp 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 "fileAPI.h"
  26. #include "bufferAPI.h"
  27. namespace dsr {
  28. // TODO: Try converting to UTF-8 for file names, which would only have another chance at working
  29. static char toAscii(DsrChar c) {
  30. if (c > 127) {
  31. return '?';
  32. } else {
  33. return c;
  34. }
  35. }
  36. #define TO_RAW_ASCII(TARGET, SOURCE) \
  37. char TARGET[string_length(SOURCE) + 1]; \
  38. for (int i = 0; i < string_length(SOURCE); i++) { \
  39. TARGET[i] = toAscii(SOURCE[i]); \
  40. } \
  41. TARGET[string_length(SOURCE)] = '\0';
  42. Buffer file_loadBuffer(const ReadableString& filename, bool mustExist) {
  43. // TODO: Load files using Unicode filenames when available
  44. TO_RAW_ASCII(asciiFilename, filename);
  45. std::ifstream fileStream(asciiFilename, std::ios_base::in | std::ios_base::binary);
  46. if (fileStream.is_open()) {
  47. // Get the file's length and allocate an array for the raw encoding
  48. fileStream.seekg (0, fileStream.end);
  49. int64_t fileLength = fileStream.tellg();
  50. fileStream.seekg (0, fileStream.beg);
  51. Buffer buffer = buffer_create(fileLength);
  52. fileStream.read((char*)buffer_dangerous_getUnsafeData(buffer), fileLength);
  53. return buffer;
  54. } else {
  55. if (mustExist) {
  56. throwError(U"The file ", filename, U" could not be opened for reading.\n");
  57. }
  58. // If the file cound not be found and opened, an empty buffer is returned
  59. return Buffer();
  60. }
  61. }
  62. void file_saveBuffer(const ReadableString& filename, Buffer buffer) {
  63. // TODO: Save files using Unicode filenames
  64. if (!buffer_exists(buffer)) {
  65. throwError(U"buffer_save: Cannot save a buffer that don't exist to a file.\n");
  66. } else {
  67. TO_RAW_ASCII(asciiFilename, filename);
  68. std::ofstream fileStream(asciiFilename, std::ios_base::out | std::ios_base::binary);
  69. if (fileStream.is_open()) {
  70. fileStream.write((char*)buffer_dangerous_getUnsafeData(buffer), buffer_getSize(buffer));
  71. fileStream.close();
  72. } else {
  73. throwError("Failed to save ", filename, "\n");
  74. }
  75. }
  76. }
  77. const char32_t* file_separator() {
  78. #if defined(WIN32) || defined(_WIN32)
  79. return U"\\";
  80. #else
  81. return U"/";
  82. #endif
  83. }
  84. }