fileAPI.cpp 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // zlib open source license
  2. //
  3. // Copyright (c) 2020 to 2022 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. // If porting to a new operating system that is not following Posix standard, list how the file system works here.
  29. // * pathSeparator is the token used to separate folders in the system, expressed as a UTF-32 string literal.
  30. // * accessFile is the function for opening a file using the UTF-32 filename, for reading or writing.
  31. // The C API is used for access, because some C++ standard library implementations don't support wide strings for MS-Windows.
  32. #if defined(WIN32) || defined(_WIN32)
  33. #include <windows.h>
  34. static const char32_t* pathSeparator = U"\\";
  35. static FILE* accessFile(const ReadableString &filename, bool write) {
  36. Buffer pathBuffer = string_saveToMemory(filename, CharacterEncoding::BOM_UTF16LE, LineEncoding::CrLf, false, true);
  37. return _wfopen((const wchar_t*)buffer_dangerous_getUnsafeData(pathBuffer), write ? L"wb" : L"rb");
  38. }
  39. #else
  40. static const char32_t* pathSeparator = U"/";
  41. static FILE* accessFile(const ReadableString &filename, bool write) {
  42. Buffer pathBuffer = string_saveToMemory(filename, CharacterEncoding::BOM_UTF8, LineEncoding::CrLf, false, true);
  43. return fopen((const char*)buffer_dangerous_getUnsafeData(pathBuffer), write ? "wb" : "rb");
  44. }
  45. #endif
  46. Buffer file_loadBuffer(const ReadableString& filename, bool mustExist) {
  47. FILE *file = accessFile(filename, false);
  48. if (file != nullptr) {
  49. // Get the file's size by going to the end, measuring, and going back
  50. fseek(file, 0L, SEEK_END);
  51. int64_t fileSize = ftell(file);
  52. rewind(file);
  53. // Allocate a buffer of the file's size
  54. Buffer buffer = buffer_create(fileSize);
  55. fread((void*)buffer_dangerous_getUnsafeData(buffer), fileSize, 1, file);
  56. fclose(file);
  57. return buffer;
  58. } else {
  59. if (mustExist) {
  60. throwError(U"The file ", filename, U" could not be opened for reading.\n");
  61. }
  62. // If the file cound not be found and opened, an empty buffer is returned
  63. return Buffer();
  64. }
  65. }
  66. void file_saveBuffer(const ReadableString& filename, Buffer buffer) {
  67. if (!buffer_exists(buffer)) {
  68. throwError(U"buffer_save: Cannot save a buffer that don't exist to a file.\n");
  69. } else {
  70. FILE *file = accessFile(filename, true);
  71. if (file != nullptr) {
  72. fwrite((void*)buffer_dangerous_getUnsafeData(buffer), buffer_getSize(buffer), 1, file);
  73. fclose(file);
  74. } else {
  75. throwError("Failed to save ", filename, ".\n");
  76. }
  77. }
  78. }
  79. const char32_t* file_separator() {
  80. return pathSeparator;
  81. }
  82. }