fileAPI.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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 "fileAPI.h"
  24. #ifdef USE_MICROSOFT_WINDOWS
  25. #include <windows.h>
  26. #else
  27. #include <unistd.h>
  28. #endif
  29. #include <fstream>
  30. #include <cstdlib>
  31. #include "bufferAPI.h"
  32. namespace dsr {
  33. // If porting to a new operating system that is not following Posix standard, list how the file system works here.
  34. // * pathSeparator is the token used to separate folders in the system, expressed as a UTF-32 string literal.
  35. // * accessFile is the function for opening a file using the UTF-32 filename, for reading or writing.
  36. // The C API is used for access, because some C++ standard library implementations don't support wide strings for MS-Windows.
  37. #ifdef USE_MICROSOFT_WINDOWS
  38. using NativeChar = wchar_t; // UTF-16
  39. static const char32_t* pathSeparator = U"\\";
  40. static const CharacterEncoding nativeEncoding = CharacterEncoding::BOM_UTF16LE;
  41. #define FILE_ACCESS_FUNCTION _wfopen
  42. #define FILE_ACCESS_SELECTION (write ? L"wb" : L"rb")
  43. List<String> file_impl_getInputArguments() {
  44. // Get a pointer to static memory with the command
  45. LPWSTR cmd = GetCommandLineW();
  46. // Split the arguments into an array of arguments
  47. int argc = 0;
  48. LPWSTR *argv = CommandLineToArgvW(cmd, &argc);
  49. // Convert the arguments into dsr::List<dsr::String>
  50. List<String> args = file_impl_convertInputArguments(argc, (void**)argv);
  51. // Free the native list of arguments
  52. LocalFree(argv);
  53. return args;
  54. }
  55. #else
  56. using NativeChar = char; // UTF-8
  57. static const char32_t* pathSeparator = U"/";
  58. static const CharacterEncoding nativeEncoding = CharacterEncoding::BOM_UTF8;
  59. #define FILE_ACCESS_FUNCTION fopen
  60. #define FILE_ACCESS_SELECTION (write ? "wb" : "rb")
  61. List<String> file_impl_getInputArguments() { return List<String>(); }
  62. #endif
  63. // Length of fixed size buffers.
  64. const int maxLength = 512;
  65. static const NativeChar* toNativeString(const ReadableString &filename, Buffer &buffer) {
  66. buffer = string_saveToMemory(filename, nativeEncoding, LineEncoding::CrLf, false, true);
  67. return (const NativeChar*)buffer_dangerous_getUnsafeData(buffer);
  68. }
  69. static String fromNativeString(const NativeChar *text) {
  70. return string_dangerous_decodeFromData(text, nativeEncoding);
  71. }
  72. List<String> file_impl_convertInputArguments(int argn, void **argv) {
  73. List<String> result;
  74. result.reserve(argn);
  75. for (int a = 0; a < argn; a++) {
  76. result.push(fromNativeString((NativeChar*)(argv[a])));
  77. }
  78. return result;
  79. }
  80. static FILE* accessFile(const ReadableString &filename, bool write) {
  81. Buffer buffer;
  82. return FILE_ACCESS_FUNCTION(toNativeString(filename, buffer), FILE_ACCESS_SELECTION);
  83. }
  84. Buffer file_loadBuffer(const ReadableString& filename, bool mustExist) {
  85. FILE *file = accessFile(filename, false);
  86. if (file != nullptr) {
  87. // Get the file's size by going to the end, measuring, and going back
  88. fseek(file, 0L, SEEK_END);
  89. int64_t fileSize = ftell(file);
  90. rewind(file);
  91. // Allocate a buffer of the file's size
  92. Buffer buffer = buffer_create(fileSize);
  93. fread((void*)buffer_dangerous_getUnsafeData(buffer), fileSize, 1, file);
  94. fclose(file);
  95. return buffer;
  96. } else {
  97. if (mustExist) {
  98. throwError(U"The file ", filename, U" could not be opened for reading.\n");
  99. }
  100. // If the file cound not be found and opened, an empty buffer is returned
  101. return Buffer();
  102. }
  103. }
  104. void file_saveBuffer(const ReadableString& filename, Buffer buffer) {
  105. if (!buffer_exists(buffer)) {
  106. throwError(U"buffer_save: Cannot save a buffer that don't exist to a file.\n");
  107. } else {
  108. FILE *file = accessFile(filename, true);
  109. if (file != nullptr) {
  110. fwrite((void*)buffer_dangerous_getUnsafeData(buffer), buffer_getSize(buffer), 1, file);
  111. fclose(file);
  112. } else {
  113. throwError("Failed to save ", filename, ".\n");
  114. }
  115. }
  116. }
  117. const char32_t* file_separator() {
  118. return pathSeparator;
  119. }
  120. inline bool isSeparator(DsrChar c) {
  121. return c == U'\\' || c == U'/';
  122. }
  123. // Returns the index of the last / or \ in path, or defaultIndex if none existed.
  124. static int64_t getLastSeparator(const ReadableString &path, int defaultIndex) {
  125. for (int64_t i = string_length(path) - 1; i >= 0; i--) {
  126. DsrChar c = path[i];
  127. if (isSeparator(c)) {
  128. return i;
  129. }
  130. }
  131. return defaultIndex;
  132. }
  133. ReadableString file_getPathlessName(const ReadableString &path) {
  134. return string_after(path, getLastSeparator(path, -1));
  135. }
  136. ReadableString file_getFolderPath(const ReadableString &path) {
  137. return string_before(path, getLastSeparator(path, string_length(path)));
  138. }
  139. bool file_hasRoot(const ReadableString &path) {
  140. #ifdef USE_MICROSOFT_WINDOWS
  141. // If a colon is found, it is a root path.
  142. return string_findFirst(path, U':') > -1;
  143. #else
  144. // If the path begins with a separator, it is the root folder in Posix systems.
  145. return path[0] == U'/';
  146. #endif
  147. }
  148. String file_getCurrentPath() {
  149. #ifdef USE_MICROSOFT_WINDOWS
  150. NativeChar resultBuffer[maxLength + 1] = {0};
  151. GetCurrentDirectoryW(maxLength, resultBuffer);
  152. return fromNativeString(resultBuffer);
  153. #else
  154. NativeChar resultBuffer[maxLength + 1] = {0};
  155. getcwd(resultBuffer, maxLength);
  156. return fromNativeString(resultBuffer);
  157. #endif
  158. }
  159. #ifdef USE_MICROSOFT_WINDOWS
  160. static String file_getApplicationFilePath() {
  161. NativeChar resultBuffer[maxLength + 1] = {0};
  162. GetModuleFileNameW(nullptr, resultBuffer, maxLength);
  163. return fromNativeString(resultBuffer);
  164. }
  165. #endif
  166. String file_getApplicationFolder(bool allowFallback) {
  167. #ifdef USE_MICROSOFT_WINDOWS
  168. return file_getFolderPath(file_getApplicationFilePath());
  169. #else
  170. #ifdef USE_LINUX
  171. // TODO: Implement using "/proc/self/exe" on Linux
  172. // https://www.wikitechy.com/tutorials/linux/how-to-find-the-location-of-the-executable-in-c
  173. NativeChar resultBuffer[maxLength + 1] = {0};
  174. //"/proc/curproc/file" on FreeBSD, which is not yet supported
  175. //"/proc/self/path/a.out" on Solaris, which is not yet supported
  176. readlink("/proc/self/exe", resultBuffer, maxLength);
  177. return file_getFolderPath(fromNativeString(resultBuffer));
  178. #else
  179. if (allowFallback) {
  180. return file_getCurrentPath();
  181. } else {
  182. throwError("file_getApplicationFolder is not implemented!\n");
  183. }
  184. #endif
  185. #endif
  186. }
  187. String file_combinePaths(const ReadableString &a, const ReadableString &b) {
  188. if (isSeparator(a[string_length(a) - 1])) {
  189. // Already ending with a separator.
  190. return string_combine(a, b);
  191. } else {
  192. // Combine using a separator.
  193. return string_combine(a, pathSeparator, b);
  194. }
  195. }
  196. String file_getAbsolutePath(const ReadableString &path) {
  197. if (file_hasRoot(path)) {
  198. return path;
  199. } else {
  200. return file_combinePaths(file_getCurrentPath(), path);
  201. }
  202. }
  203. }