fileAPI.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. /*
  24. TODO:
  25. * bool file_setCurrentPath(const ReadableString& path);
  26. WINBASEAPI WINBOOL WINAPI SetCurrentDirectoryW(LPCWSTR lpPathName);
  27. chdir on Posix
  28. * bool file_createFolder(const ReadableString& path);
  29. WINBASEAPI WINBOOL WINAPI CreateDirectoryW (LPCWSTR lpPathName, LPSECURITY_ATTRIBUTES lpSecurityAttributes);
  30. mkdir on Posix
  31. * int64_t file_getSize(const ReadableString& path);
  32. WINBASEAPI WINBOOL WINAPI GetFileSizeEx (HANDLE hFile, PLARGE_INTEGER lpFileSize);
  33. * bool file_remove(const ReadableString& path);
  34. WINBASEAPI WINBOOL WINAPI DeleteFileW (LPCWSTR lpFileName);
  35. * bool file_exists(const ReadableString& path);
  36. Can open a file without permissions and see if it works.
  37. * void file_getFolderContent(const ReadableString& folderPath, std::function<void(ReadableString, EntryType)> action)
  38. How to do this the safest way with Unicode as a minimum requirement?
  39. */
  40. #ifndef DFPSR_API_FILE
  41. #define DFPSR_API_FILE
  42. #include "../api/stringAPI.h"
  43. #include "bufferAPI.h"
  44. #if defined(WIN32) || defined(_WIN32)
  45. #define USE_MICROSOFT_WINDOWS
  46. #endif
  47. #if defined(__linux__)
  48. #define USE_LINUX
  49. #endif
  50. // A module for file access that exists to prevent cyclic dependencies between strings and buffers.
  51. // Buffers need a filename to be saved or loaded while strings use buffers to store their characters.
  52. namespace dsr {
  53. // Post-condition:
  54. // Returns the content of the file referred to be filename.
  55. // If mustExist is true, then failure to load will throw an exception.
  56. // If mustExist is false, then failure to load will return an empty handle (returning false for buffer_exists).
  57. Buffer file_loadBuffer(const ReadableString& filename, bool mustExist = true);
  58. // Side-effect: Saves buffer to filename as a binary file.
  59. // Pre-condition: buffer exists
  60. void file_saveBuffer(const ReadableString& filename, Buffer buffer);
  61. // Get a path separator for the target operating system.
  62. // Can be used to construct a file path that works for both forward and backward slash separators.
  63. const char32_t* file_separator();
  64. // TODO: Create regression tests for the file system.
  65. // Returns the local name of the file or folder after the last path separator, or the whole path if no separator was found.
  66. // Examples with / as the path separator:
  67. // file_getFolderPath(U"MyFolder/Cars.txt") == U"Cars.txt"
  68. // file_getFolderPath(U"MyFolder/") == U""
  69. // file_getFolderPath(U"MyFolder") == U"MyFolder"
  70. // file_getFolderPath(U"MyFolder/Folder2") == U"Folder2"
  71. ReadableString file_getPathlessName(const ReadableString &path);
  72. // Returns the file's folder path with the file removed, or path unchanged if it was already a path.
  73. // Does not include the last slash.
  74. // Examples with / as the path separator:
  75. // file_getFolderPath(U"MyFolder/Documents/Cars.txt") == U"MyFolder/Documents"
  76. // file_getFolderPath(U"MyFolder/Documents/") == U"MyFolder/Documents"
  77. // file_getFolderPath(U"MyFolder/Documents") == U"MyFolder/Documents"
  78. ReadableString file_getFolderPath(const ReadableString &path);
  79. // Combines two parts into a path and automatically adding a local separator when needed.
  80. // Can be used to get the full path of a file in a folder or add another folder to the path.
  81. // b may not begin with a separator, because only a is allowed to contain the root.
  82. // Examples with / as the path separator:
  83. // file_combinePaths(U"Folder", U"Document.txt") == U"Folder/Document.txt"
  84. // file_combinePaths(U"Folder/", U"Document.txt") == U"Folder/Document.txt"
  85. String file_combinePaths(const ReadableString &a, const ReadableString &b);
  86. // Returns true iff path contains a root, according to the local path syntax.
  87. bool file_hasRoot(const ReadableString &path);
  88. // A convenient wrapper for getting input arguments as a list of portable Unicode strings.
  89. // The actual main function gets placed in DSR_MAIN_CALLER, which calls the given function.
  90. // Example:
  91. // DSR_MAIN_CALLER(dsrMain)
  92. // int dsrMain(List<String> args) {
  93. // printText("Input arguments:\n");
  94. // for (int a = 0; a < args.length(); a++) {
  95. // printText(" args[", a, "] = ", args[a], "\n");
  96. // }
  97. // return 0;
  98. // }
  99. #ifdef USE_MICROSOFT_WINDOWS
  100. #define DSR_MAIN_CALLER(MAIN_NAME) \
  101. int MAIN_NAME(List<String> args); \
  102. int main() { \
  103. return MAIN_NAME(file_impl_getInputArguments()); \
  104. }
  105. #else
  106. #define DSR_MAIN_CALLER(MAIN_NAME) \
  107. int MAIN_NAME(List<String> args); \
  108. int main(int argc, char **argv) { return MAIN_NAME(file_impl_convertInputArguments(argc, (void**)argv)); }
  109. #endif
  110. // Helper functions have to be exposed for the macro handle your input arguments.
  111. // Do not call these yourself.
  112. List<String> file_impl_convertInputArguments(int argn, void **argv);
  113. List<String> file_impl_getInputArguments();
  114. // Get the current path, from where the application was called and relative paths start.
  115. String file_getCurrentPath();
  116. // Get the application's folder path, from where the application is stored.
  117. // If not implemented and allowFallback is true,
  118. // the current path is returned instead as a qualified guess instead of raising an exception.
  119. String file_getApplicationFolder(bool allowFallback = true);
  120. // Gets an absolute version of the path, quickly without removing redundancy.
  121. String file_getAbsolutePath(const ReadableString &path);
  122. // Returns true iff path refers to a valid file or folder.
  123. bool file_exists(const ReadableString& path);
  124. }
  125. #endif