1 /* 2 file_utils.h MindForger thinking notebook 3 4 Copyright (C) 2016-2020 Martin Dvorak <martin.dvorak@mindforger.com> 5 6 This program is free software; you can redistribute it and/or 7 modify it under the terms of the GNU General Public License 8 as published by the Free Software Foundation; either version 2 9 of the License, or (at your option) any later version. 10 11 This program is distributed in the hope that it will be useful, 12 but WITHOUT ANY WARRANTY; without even the implied warranty of 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 GNU General Public License for more details. 15 16 You should have received a copy of the GNU General Public License 17 along with this program. If not, see <http://www.gnu.org/licenses/>. 18 */ 19 #ifndef M8R_FILE_UTILS_H_ 20 #define M8R_FILE_UTILS_H_ 21 22 #include <sys/types.h> 23 #include <sys/stat.h> 24 #include "../config/config.h" 25 26 #include <zlib.h> 27 #if defined(MSDOS) || defined(OS2) || defined(_WIN32) || defined(__CYGWIN__) 28 #include <fcntl.h> 29 #include <io.h> 30 #define SET_BINARY_MODE(file) setmode(fileno(file), O_BINARY) 31 #else 32 #define SET_BINARY_MODE(file) 33 #endif 34 35 #ifdef __APPLE__ 36 #include <mach-o/dyld.h> 37 #endif 38 39 #include <ctime> 40 #include <cstdio> 41 #include <cstring> 42 43 #include <fstream> 44 #include <sstream> 45 #include <iostream> 46 #include <iterator> 47 #include <string> 48 #include <vector> 49 50 #include "../debug.h" 51 #include "../exceptions.h" 52 #include "string_utils.h" 53 54 #ifdef __linux__ 55 constexpr const auto FILE_PATH_SEPARATOR = "/"; 56 constexpr const auto FILE_PATH_SEPARATOR_CHAR = '/'; 57 #elif _WIN32 58 constexpr const auto FILE_PATH_SEPARATOR = "\\"; 59 constexpr const auto FILE_PATH_SEPARATOR_CHAR = '\\'; 60 #else 61 constexpr const auto FILE_PATH_SEPARATOR = "/"; 62 constexpr const auto FILE_PATH_SEPARATOR_CHAR = '/'; 63 #endif 64 65 #ifdef __linux__ 66 constexpr const auto SYSTEM_TEMP_DIRECTORY = "/tmp"; 67 #elif _WIN32 68 constexpr const auto SYSTEM_TEMP_DIRECTORY = "c:\\Windows\\Temp"; 69 #else 70 constexpr const auto SYSTEM_TEMP_DIRECTORY = "/tmp"; 71 #endif 72 73 namespace m8r { 74 75 struct File 76 { 77 const std::string name; 78 FileFile79 File(const std::string& name) 80 : name(name) 81 { 82 } 83 getNameFile84 const std::string& getName() const noexcept { return name; } 85 }; 86 87 #ifdef __cplusplus 88 extern "C" { 89 #endif 90 int ungzip(const char* srcFile, const char* dstFile); 91 #ifdef __cplusplus 92 } 93 #endif 94 95 void pathToDirectoryAndFile(const std::string& path, std::string& directory, std::string& file); 96 void pathToLinuxDelimiters(const std::string& path, std::string& linuxPath); 97 bool stringToLines(const std::string* text, std::vector<std::string*>& lines); 98 bool fileToLines(const std::string* filename, std::vector<std::string*>& lines, size_t& filesize); 99 std::string* fileToString(const std::string& filename); 100 void stringToFile(const std::string& filename, const std::string& content); 101 time_t fileModificationTime(const std::string* filename); 102 bool copyFile(const std::string& from, const std::string& to); 103 bool moveFile(const std::string& from, const std::string& to); 104 void resolvePath(const std::string& path, std::string& resolvedAbsolutePath); 105 bool isDirectoryOrFileExists(const char* path); 106 bool isDirectory(const char* path); 107 bool isFile(const char* path); 108 bool isPathRelative(const std::string& path); 109 char* makeTempDirectory(char* dirNamePrefix); 110 int removeDirectoryRecursively(const char* path); 111 int copyDirectoryRecursively(const char* srcPath, const char* dstPath, bool extractGz=false); 112 bool createDirectory(const std::string& path); 113 114 /** 115 * @brief Get path to the the executable on macOS or windows. Othewise returns nullptr. 116 * 117 * Method is not reentrant - it returns pointer to the static buffer. 118 */ 119 char* getExecutablePath(); 120 } // m8r namespace 121 122 #endif /* M8R_FILE_UTILS_H_ */ 123