| 1 | // LAF Base Library |
| 2 | // Copyright (c) 2020-2021 Igara Studio S.A. |
| 3 | // Copyright (c) 2001-2018 David Capello |
| 4 | // |
| 5 | // This file is released under the terms of the MIT license. |
| 6 | // Read LICENSE.txt for more information. |
| 7 | |
| 8 | #ifndef BASE_FS_H_INCLUDED |
| 9 | #define BASE_FS_H_INCLUDED |
| 10 | #pragma once |
| 11 | |
| 12 | #include <string> |
| 13 | |
| 14 | #include "base/paths.h" |
| 15 | |
| 16 | namespace base { |
| 17 | |
| 18 | class Time; |
| 19 | |
| 20 | // Default path separator (on Windows it is '\' and on Unix-like systems it is '/'). |
| 21 | extern const std::string::value_type path_separator; |
| 22 | |
| 23 | bool is_file(const std::string& path); |
| 24 | bool is_directory(const std::string& path); |
| 25 | |
| 26 | size_t file_size(const std::string& path); |
| 27 | |
| 28 | void move_file(const std::string& src, const std::string& dst); |
| 29 | void copy_file(const std::string& src, const std::string& dst, bool overwrite); |
| 30 | void delete_file(const std::string& path); |
| 31 | |
| 32 | bool has_readonly_attr(const std::string& path); |
| 33 | void remove_readonly_attr(const std::string& path); |
| 34 | |
| 35 | Time get_modification_time(const std::string& path); |
| 36 | |
| 37 | void make_directory(const std::string& path); |
| 38 | void make_all_directories(const std::string& path); |
| 39 | void remove_directory(const std::string& path); |
| 40 | |
| 41 | std::string get_current_path(); |
| 42 | std::string get_app_path(); |
| 43 | std::string get_temp_path(); |
| 44 | std::string get_user_docs_folder(); |
| 45 | #if __APPLE__ |
| 46 | std::string get_lib_app_support_path(); |
| 47 | #endif |
| 48 | |
| 49 | // If the given filename is a relative path, it converts the |
| 50 | // filename to an absolute one. |
| 51 | std::string get_canonical_path(const std::string& path); |
| 52 | |
| 53 | // TODO why get_canonical_path() is not enough? |
| 54 | std::string get_absolute_path(const std::string& filename); |
| 55 | |
| 56 | paths list_files(const std::string& path); |
| 57 | |
| 58 | // Returns true if the given character is a valud path separator |
| 59 | // (any of '\' or '/' characters). |
| 60 | bool is_path_separator(std::string::value_type chr); |
| 61 | |
| 62 | // Returns only the path (without the last trailing slash). |
| 63 | std::string get_file_path(const std::string& filename); |
| 64 | |
| 65 | // Returns the file name with its extension, removing the path. |
| 66 | std::string get_file_name(const std::string& filename); |
| 67 | |
| 68 | // Returns the extension of the file name (without the dot). |
| 69 | std::string get_file_extension(const std::string& filename); |
| 70 | |
| 71 | // Returns the whole path with another extension. |
| 72 | std::string replace_extension(const std::string& filename, const std::string& extension); |
| 73 | |
| 74 | // Returns the file name without path and without extension. |
| 75 | std::string get_file_title(const std::string& filename); |
| 76 | std::string get_file_title_with_path(const std::string& filename); |
| 77 | |
| 78 | // Joins two paths or a path and a file name with a path-separator. |
| 79 | std::string join_path(const std::string& path, const std::string& file); |
| 80 | |
| 81 | // Removes the trailing separator from the given path. |
| 82 | std::string remove_path_separator(const std::string& path); |
| 83 | |
| 84 | // Replaces all separators with the system separator. |
| 85 | std::string fix_path_separators(const std::string& filename); |
| 86 | |
| 87 | // Calls get_canonical_path() and fix_path_separators() for the |
| 88 | // given filename. |
| 89 | std::string normalize_path(const std::string& filename); |
| 90 | |
| 91 | // Returns true if the filename contains one of the specified |
| 92 | // extensions. The "extensions" parameter must be a set of possible |
| 93 | // extensions. |
| 94 | bool has_file_extension(const std::string& filename, const base::paths& extensions); |
| 95 | |
| 96 | int compare_filenames(const std::string& a, const std::string& b); |
| 97 | |
| 98 | #if LAF_WINDOWS |
| 99 | class Version; |
| 100 | Version get_file_version(const std::string& filename); |
| 101 | Version get_file_version(const wchar_t* filename); |
| 102 | #endif |
| 103 | |
| 104 | } // namespace base |
| 105 | |
| 106 | #endif |
| 107 | |