| e8555c89 Andy McCall |
1 |
#ifndef FILEOPERATIONS_HH |
| 2 |
#define FILEOPERATIONS_HH |
| 3 |
|
| 4 |
#include "Date.hh" |
| 5 |
#include "statp.hh" |
| 6 |
#include "unistdp.hh" |
| 7 |
#include "zstring_view.hh" |
| 8 |
|
| 9 |
#include <fstream> |
| 10 |
#include <memory> |
| 11 |
#include <optional> |
| 12 |
#include <string_view> |
| 13 |
#include <sys/types.h> |
| 14 |
|
| 15 |
namespace openmsx::FileOperations { |
| 16 |
|
| 17 |
struct FClose { |
| 18 |
void operator()(FILE* f) const { fclose(f); } |
| 19 |
}; |
| 20 |
using FILE_t = std::unique_ptr<FILE, FClose>; |
| 21 |
|
| 22 |
const char nativePathSeparator = |
| 23 |
#ifdef _WIN32 |
| 24 |
'\\'; |
| 25 |
#else |
| 26 |
'/'; |
| 27 |
#endif |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
[[nodiscard]] std::string expandTilde(std::string path); |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
[[nodiscard]] bool needsTildeExpansion(std::string_view path); |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
void mkdir(zstring_view path, mode_t mode); |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
void mkdirp(std::string path); |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
int unlink(zstring_view path); |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
int rmdir(zstring_view path); |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
int deleteRecursive(zstring_view path); |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
[[nodiscard]] FILE_t openFile(zstring_view filename, zstring_view mode); |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
void openOfStream(std::ofstream& stream, zstring_view filename); |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
void openOfStream(std::ofstream& stream, zstring_view filename, |
| 100 |
std::ios_base::openmode mode); |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
[[nodiscard]] std::string_view getFilename(std::string_view path); |
| 108 |
[[nodiscard]] inline zstring_view getFilename(zstring_view path) { |
| 109 |
auto v = getFilename(path.view()); |
| 110 |
return {v.data(), v.size()}; |
| 111 |
} |
| 112 |
[[nodiscard]] inline zstring_view getFilename(const std::string& path) { |
| 113 |
return getFilename(zstring_view(path)); |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
[[nodiscard]] std::string_view getDirName(std::string_view path); |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
[[nodiscard]] std::string_view getExtension(std::string_view path); |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
[[nodiscard]] std::string_view stripExtension(std::string_view path); |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
[[nodiscard]] std::string_view stem(std::string_view path); |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
[[nodiscard]] std::string cleanFilename(std::string_view filename); |
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
[[nodiscard]] std::string join(std::string_view part1, std::string_view part2); |
| 176 |
[[nodiscard]] std::string join(std::string_view part1, std::string_view part2, std::string_view part3); |
| 177 |
[[nodiscard]] std::string join(std::string_view part1, std::string_view part2, |
| 178 |
std::string_view part3, std::string_view part4); |
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
#ifdef _WIN32 |
| 188 |
[[nodiscard]] std::string getConventionalPath( std::string path); |
| 189 |
#else |
| 190 |
[[nodiscard]] inline const std::string& getConventionalPath(const std::string& path) { return path; } |
| 191 |
[[nodiscard]] inline std::string getConventionalPath( std::string&& path) { return std::move(path); } |
| 192 |
#endif |
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
#ifdef _WIN32 |
| 202 |
[[nodiscard]] std::string getNativePath(zstring_view path); |
| 203 |
#else |
| 204 |
[[nodiscard]] inline zstring_view getNativePath(zstring_view path) { return path; } |
| 205 |
#endif |
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
#ifdef _WIN32 |
| 212 |
[[nodiscard]] std::string expandCurrentDirFromDrive(std::string path); |
| 213 |
#else |
| 214 |
[[nodiscard]] inline const std::string& expandCurrentDirFromDrive(const std::string& path) { return path; } |
| 215 |
[[nodiscard]] inline std::string expandCurrentDirFromDrive( std::string&& path) { return std::move(path); } |
| 216 |
#endif |
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
[[nodiscard]] std::string getCurrentWorkingDirectory(); |
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
[[nodiscard]] std::string getAbsolutePath(std::string_view path); |
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
[[nodiscard]] bool isAbsolutePath(std::string_view path); |
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
[[nodiscard]] std::string getUserHomeDir(std::string_view username); |
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
[[nodiscard]] const std::string& getUserOpenMSXDir(); |
| 252 |
[[nodiscard]] std::string getUserOpenMSXDir(std::string_view subdir); |
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
[[nodiscard]] const std::string& getUserDataDir(); |
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
[[nodiscard]] const std::string& getSystemDataDir(); |
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
[[nodiscard]] const std::string& getSystemDocDir(); |
| 272 |
|
| 273 |
#ifdef _WIN32 |
| 274 |
|
| 275 |
typedef struct _stat64 Stat; |
| 276 |
#else |
| 277 |
using Stat = struct stat; |
| 278 |
#endif |
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
[[nodiscard]] std::optional<Stat> getStat(zstring_view filename); |
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
[[nodiscard]] bool isRegularFile(zstring_view filename); |
| 289 |
[[nodiscard]] bool isRegularFile(const Stat& st); |
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
[[nodiscard]] bool isDirectory(zstring_view directory); |
| 295 |
[[nodiscard]] bool isDirectory(const Stat& st); |
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
[[nodiscard]] bool exists(zstring_view filename); |
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
[[nodiscard]] inline time_t getModificationDate(const Stat& st) { |
| 306 |
return Date::adjustTimeT(st.st_mtime); |
| 307 |
} |
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
[[nodiscard]] std::string getNextNumberedFileName( |
| 320 |
std::string_view directory, std::string_view prefix, std::string_view extension, |
| 321 |
bool addSeparator = false); |
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
[[nodiscard]] std::string parseCommandFileArgument( |
| 332 |
std::string_view argument, std::string_view directory, |
| 333 |
std::string_view prefix, std::string_view extension); |
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
[[nodiscard]] std::string getTempDir(); |
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
[[nodiscard]] FILE_t openUniqueFile(const std::string& directory, std::string& filename); |
| 348 |
|
| 349 |
} |
| 350 |
|
| 351 |
#endif |