Cogs.Foundation
Loading...
Searching...
No Matches
IO.h
Go to the documentation of this file.
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
32 COGSFOUNDATION_API std::string expandEnvironmentVariables(const std::string& input);
33 COGSFOUNDATION_API std::string expandEnvironmentVariable(const std::string& name);
34
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();
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);
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
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);
84#endif
85 };
86}
#define COGSFOUNDATION_API
Definition: FoundationBase.h:31
std::shared_ptr< FileHandle > Ptr
Definition: FileHandle.h:17
AccessMode
Definition: FileHandle.h:26
ProtectionMode
Definition: FileHandle.h:31
OpenMode
Definition: FileHandle.h:19
COGSFOUNDATION_API std::string getPathToExe()
Definition: IO.Emscripten.cpp:252
COGSFOUNDATION_API std::string expandEnvironmentVariable(const std::string &name)
Definition: IO.Emscripten.cpp:167
fs::directory_entry DirectoryEntry
Definition: IO.h:20
COGSFOUNDATION_API bool readTextFile(std::string &content, const std::string &fileName)
Definition: IO.Emscripten.cpp:156
COGSFOUNDATION_API bool writeBinaryFile(const std::string &fileName, const void *content, size_t contentSize)
Definition: IO.Emscripten.cpp:325
COGSFOUNDATION_API std::string getTempPath()
Definition: IO.Emscripten.cpp:262
COGSFOUNDATION_API FileContents::Ptr readFileSync(const std::string &fileName, uint64_t offset=0, uint64_t size=0, FileContentsHints hints=FileContentsHints::None)
Definition: IO.Emscripten.cpp:151
COGSFOUNDATION_API std::string expandEnvironmentVariables(const std::string &input)
Definition: IO.Emscripten.cpp:163
COGSFOUNDATION_API std::string stem(std::string_view str)
Definition: IO.Emscripten.cpp:301
COGSFOUNDATION_API void closeFile(const FileHandle::Ptr &handle)
Definition: IO.Linux.cpp:388
COGSFOUNDATION_API bool isRelative(std::string_view str)
Definition: IO.Emscripten.cpp:279
COGSFOUNDATION_API char pathSeparator()
Definition: IO.Emscripten.cpp:171
COGSFOUNDATION_API uint64_t getFileSize(std::string_view path)
Definition: IO.Emscripten.cpp:321
COGSFOUNDATION_API FileTimeType getFileTime(std::string_view path)
Definition: IO.Emscripten.cpp:317
COGSFOUNDATION_API void replaceAll(std::string &str, std::string_view from, std::string_view to)
Definition: IO.Emscripten.cpp:307
fs::file_time_type FileTimeType
Definition: IO.h:22
COGSFOUNDATION_API std::string combine(std::string_view path, std::string_view extensionPath)
Combine the two paths with a directory separator.
Definition: IO.Emscripten.cpp:199
COGSFOUNDATION_API std::string getPath(const DirectoryEntry &entry)
Definition: IO.Emscripten.cpp:337
COGSFOUNDATION_API std::string fileName(std::string_view str)
Definition: IO.Emscripten.cpp:284
COGSFOUNDATION_API size_t fileSize(const FileHandle::Ptr &handle)
Definition: IO.Linux.cpp:392
fs::recursive_directory_iterator RecursiveDirIterator
Definition: IO.h:19
COGSFOUNDATION_API bool isAbsolute(std::string_view str)
Definition: IO.Emscripten.cpp:274
COGSFOUNDATION_API bool copyFile(std::string_view src, std::string_view dest)
Definition: IO.Linux.cpp:442
COGSFOUNDATION_API std::string canonical(std::string_view path)
Definition: IO.Emscripten.cpp:191
COGSFOUNDATION_API void backupFile(const std::string &path, int max)
Definition: IO.cpp:14
COGSFOUNDATION_API std::string absolute(std::string_view path)
Definition: IO.Emscripten.cpp:187
COGSFOUNDATION_API std::string relativePath(std::string_view str)
Definition: IO.Emscripten.cpp:244
COGSFOUNDATION_API void * mapFile(const FileHandle::Ptr &handle, FileHandle::ProtectionMode protectionMode, size_t offset, size_t size)
Definition: IO.Linux.cpp:396
COGSFOUNDATION_API bool isDirectory(std::string_view path)
Definition: IO.Emscripten.cpp:183
COGSFOUNDATION_API void autoExpandEnvironmentVariables(std::string &text)
Definition: IO.Emscripten.cpp:160
COGSFOUNDATION_API bool isFile(std::string_view path)
Definition: IO.Emscripten.cpp:179
COGSFOUNDATION_API std::string getOrgLocalDataPath()
Definition: IO.Emscripten.cpp:270
COGSFOUNDATION_API bool remove(std::string_view path)
Definition: IO.Emscripten.cpp:195
COGSFOUNDATION_API std::string extension(std::string_view str)
Definition: IO.Emscripten.cpp:289
COGSFOUNDATION_API std::string expandPath(std::string_view fileName)
Definition: IO.Emscripten.cpp:233
COGSFOUNDATION_API bool exists(std::string_view path)
Definition: IO.Emscripten.cpp:175
COGSFOUNDATION_API RecursiveDirIterator directoryIterator(std::string_view directory)
Definition: IO.Emscripten.cpp:333
COGSFOUNDATION_API FileHandle::Ptr openFile(std::string_view path, FileHandle::OpenMode openMode, FileHandle::AccessMode accessMode)
Definition: IO.Linux.cpp:384
COGSFOUNDATION_API bool createDirectories(std::string_view fileName)
Definition: IO.Emscripten.cpp:329
COGSFOUNDATION_API std::string parentPath(std::string_view str)
Definition: IO.Emscripten.cpp:237
COGSFOUNDATION_API void unmapFile(const FileHandle::Ptr &handle)
Definition: IO.Linux.cpp:400
COGSFOUNDATION_API std::string getPathRelativeToDll(const std::string &fileName, void *handle)
Definition: IO.Emscripten.cpp:257
COGSFOUNDATION_API std::string getAppDataPath()
Definition: IO.Emscripten.cpp:266
COGSFOUNDATION_API void readFileAsync(const std::string &fileName, const FileContents::Callback &callback, uint64_t offset=0, uint64_t size=0, FileContentsHints hints=FileContentsHints::None)
Definition: IO.Emscripten.cpp:102
Main Cogs namespace.
Definition: MortonCode.h:5
FileContentsHints
Definition: FileContents.h:11
std::unique_ptr< FileContents > Ptr
Definition: FileContents.h:23
std::function< void(Ptr &&)> Callback
Definition: FileContents.h:24