fileAPI.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. #ifndef DFPSR_API_FILE
  24. #define DFPSR_API_FILE
  25. #include "../api/stringAPI.h"
  26. #include "bufferAPI.h"
  27. #include <version>
  28. // A module for file access that exists to prevent cyclic dependencies between strings and buffers.
  29. // Buffers need a filename to be saved or loaded while strings use buffers to store their characters.
  30. namespace dsr {
  31. // NativeChar is defined as whatever type the native system uses for expressing Unicode
  32. // Use for input arguments in the main function to allow using file_convertInputArguments.
  33. #if defined(WIN32) || defined(_WIN32)
  34. using NativeChar = wchar_t; // UTF-16
  35. #else
  36. using NativeChar = char; // UTF-8
  37. #endif
  38. // Post-condition:
  39. // Returns the content of the file referred to be filename.
  40. // If mustExist is true, then failure to load will throw an exception.
  41. // If mustExist is false, then failure to load will return an empty handle (returning false for buffer_exists).
  42. Buffer file_loadBuffer(const ReadableString& filename, bool mustExist = true);
  43. // Side-effect: Saves buffer to filename as a binary file.
  44. // Pre-condition: buffer exists
  45. void file_saveBuffer(const ReadableString& filename, Buffer buffer);
  46. // Get a path separator for the target operating system.
  47. // Can be used to construct a file path that works for both forward and backward slash separators.
  48. const char32_t* file_separator();
  49. // Returns the local name after the last path separator, or the whole path if no separator was found.
  50. ReadableString file_getPathlessName(const ReadableString &path);
  51. // Convert input arguments from main into a list of dsr::String.
  52. // argv must be declared as wchar_t** on MS-Windows to allow using Unicode, which can be done by using the dsr::NativeChar alias:
  53. // int main(int argn, NativeChar **argv) {
  54. List<String> file_convertInputArguments(int argn, NativeChar **argv);
  55. // Portable wrapper over the STD filesystem library, so that you get Unicode support with one type of string on all platforms.
  56. // To enable this wrapper, just compile everything with C++17 or a newer version that still has the feature.
  57. // Using more STD functionality can make it harder to port your project when C++ compilers are no longer maintained,
  58. // but exploring the filesystem is necessary for selecting files to load or save in editors.
  59. #ifdef __cpp_lib_filesystem
  60. enum class EntryType {
  61. Unknown, Folder, File, SymbolicLink
  62. };
  63. // Get the current path, from where the application was called and relative paths start.
  64. String file_getCurrentPath();
  65. // Get the path's absolute form to know the exact location.
  66. String file_getAbsolutePath(const ReadableString& path);
  67. // Get the path's absolute form without redundancy.
  68. String file_getCanonicalPath(const ReadableString& path);
  69. // Gets a callback for each file and folder directly inside of folderPath.
  70. // Calls back with the entry's path and an integer representing the entry type.
  71. void file_getFolderContent(const ReadableString& folderPath, std::function<void(ReadableString, EntryType)> action);
  72. // Returns true iff path refers to a valid file or folder.
  73. bool file_exists(const ReadableString& path);
  74. // Returns the file's size.
  75. int64_t file_getSize(const ReadableString& path);
  76. // Removes the file or empty folder.
  77. // Returns true iff path existed.
  78. bool file_remove(const ReadableString& path);
  79. // Removes the file or folder including any content.
  80. // Returns true iff path existed.
  81. bool file_removeRecursively(const ReadableString& path);
  82. // Returns true on success.
  83. bool file_createFolder(const ReadableString& path);
  84. #endif
  85. }
  86. #endif