Cogs.Core
IO.h
1#pragma once
2
3#include "FileContents.h"
4#include "FileHandle.h"
5
6#if !defined( EMSCRIPTEN )
7 #include <filesystem>
8 namespace fs = std::filesystem;
9#endif
10
11namespace Cogs {
12 namespace IO {
13#if defined( EMSCRIPTEN )
14 using RecursiveDirIterator = std::vector<std::string>;
15 using DirectoryEntry = std::string;
16
17 using FileTimeType = int64_t;
18#else
19 using RecursiveDirIterator = fs::recursive_directory_iterator;
20 using DirectoryEntry = fs::directory_entry;
21
22 using FileTimeType = fs::file_time_type;
23#endif
24
25 // offset=0 and size=0 reads whole file, size!=0 reads a range within the file, offset!=0 and size==0 is an error.
26 COGSFOUNDATION_API void readFileAsync(const std::string& fileName, const FileContents::Callback& callback, uint64_t offset = 0, uint64_t size = 0, FileContentsHints hints = FileContentsHints::None);
27 // offset=0 and size=0 reads whole file, size!=0 reads a range within the file, offset!=0 and size==0 is an error.
28 COGSFOUNDATION_API FileContents::Ptr readFileSync(const std::string& fileName, uint64_t offset = 0, uint64_t size = 0, FileContentsHints hints = FileContentsHints::None);
29 COGSFOUNDATION_API bool readTextFile(std::string& content, const std::string & fileName);
30
31 COGSFOUNDATION_API void autoExpandEnvironmentVariables(std::string& text);
32 COGSFOUNDATION_API std::string expandEnvironmentVariables(const std::string& input);
33 COGSFOUNDATION_API std::string expandEnvironmentVariable(const std::string& name);
34
35 COGSFOUNDATION_API char pathSeparator();
36 COGSFOUNDATION_API bool exists(std::string_view path);
37 COGSFOUNDATION_API bool isFile(std::string_view path);
38 COGSFOUNDATION_API bool isDirectory(std::string_view path);
39 COGSFOUNDATION_API std::string absolute(std::string_view path);
40 COGSFOUNDATION_API std::string canonical(std::string_view path);
41
42 COGSFOUNDATION_API bool remove(std::string_view path);
43
45 COGSFOUNDATION_API std::string combine(std::string_view path, std::string_view extensionPath);
46
47 COGSFOUNDATION_API std::string expandPath(std::string_view fileName);
48 COGSFOUNDATION_API std::string parentPath(std::string_view str);
49 COGSFOUNDATION_API std::string relativePath(std::string_view str);
50 COGSFOUNDATION_API std::string relativePath(std::string_view str, std::string_view base);
51
52 COGSFOUNDATION_API std::string getPathToExe();
53 COGSFOUNDATION_API std::string getPathRelativeToDll(const std::string& fileName, void* handle);
54 COGSFOUNDATION_API std::string getTempPath();
55 COGSFOUNDATION_API std::string getAppDataPath();
56 COGSFOUNDATION_API std::string getOrgLocalDataPath();
57
58 COGSFOUNDATION_API bool isAbsolute(std::string_view str);
59 COGSFOUNDATION_API bool isRelative(std::string_view str);
60
61 COGSFOUNDATION_API std::string fileName(std::string_view str);
62 COGSFOUNDATION_API std::string extension(std::string_view str);
63 COGSFOUNDATION_API std::string stem(std::string_view str);
64 COGSFOUNDATION_API void replaceAll(std::string& str, std::string_view from, std::string_view to);
65 COGSFOUNDATION_API FileTimeType getFileTime(std::string_view path);
66 COGSFOUNDATION_API uint64_t getFileSize(std::string_view path);
67
68 COGSFOUNDATION_API bool writeBinaryFile(const std::string& fileName, const void* content, size_t contentSize);
69
70 COGSFOUNDATION_API bool createDirectories(std::string_view fileName);
71 COGSFOUNDATION_API RecursiveDirIterator directoryIterator(std::string_view directory);
72 COGSFOUNDATION_API std::string getPath(const DirectoryEntry& entry);
73
74#if !defined( EMSCRIPTEN )
75 COGSFOUNDATION_API bool copyFile(std::string_view src, std::string_view dest);
76
77 COGSFOUNDATION_API void backupFile(const std::string& path, int max);
78
79 COGSFOUNDATION_API FileHandle::Ptr openFile(std::string_view path, FileHandle::OpenMode openMode, FileHandle::AccessMode accessMode);
80 COGSFOUNDATION_API void closeFile(const FileHandle::Ptr& handle);
81 COGSFOUNDATION_API size_t fileSize(const FileHandle::Ptr& handle);
82 COGSFOUNDATION_API void* mapFile(const FileHandle::Ptr& handle, FileHandle::ProtectionMode protectionMode, size_t offset, size_t size);
83 COGSFOUNDATION_API void unmapFile(const FileHandle::Ptr& handle);
84#endif
85 };
86}
Contains all Cogs related functionality.
Definition: FieldSetter.h:23
FileContentsHints
Definition: FileContents.h:11