GlistEngine
Loading...
Searching...
No Matches
gFile.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2016 Nitra Games Ltd.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef UTILS_GFILE_H_
18#define UTILS_GFILE_H_
19
20//#include <fstream>
21#include <ios>
22#include <iostream>
23#include "ghc/filesystem.hpp"
24
25namespace fs {
26using namespace ghc::filesystem;
27using ifstream = ghc::filesystem::ifstream;
28using ofstream = ghc::filesystem::ofstream;
29using fstream = ghc::filesystem::fstream;
30}
31#include "gObject.h"
32
33
34class gFile : public gObject {
35public:
42
49
51 gFile(const std::string& fullPath, int fileMode = FILEMODE_READONLY, bool isBinary = true);
52 virtual ~gFile();
53
54 bool load(const std::string& fullPath, int fileMode = FILEMODE_READONLY, bool isBinary = true);
55 bool loadFile(const std::string& filePath, int fileMode = FILEMODE_READONLY, bool isBinary = true);
56 void close();
57
58 fs::path getPath();
59 int getMode();
60 bool isBinary();
61
62 void write(const std::string& content);
63 void write(std::vector<char> newBytes);
64 void write(const char* bytes, size_t length);
65
66 std::vector<char> getBytes();
67
68 const std::vector<char>& getBytesConst() {
69 return bytes;
70 }
71
72 std::string getText();
73 int getSize();
74
75 std::string getFilename();
76 std::string getDirectory();
77
78 bool exists();
79 bool isFile();
80 bool isLink();
82 bool isDevice();
83 bool isOpen();
84
85 static bool doesFileExist(const std::string& fullPath);
86 static bool doesFileExistInAssets(const std::string& filePath);
87 static bool isFile(const std::string& fullPath);
88 static bool isFileInAssets(const std::string& filePath);
89 static bool isLink(const std::string& fullPath);
90 static bool isLinkInAssets(const std::string& filePath);
91 static bool isDirectory(const std::string& fullPath);
92 static bool isDirectoryInAssets(const std::string& filePath);
93 static bool isDevice(const fs::path& path);
94 static std::string getFilename(const fs::path& path);
95 static std::string getDirectory(const fs::path& path);
96 static std::string addComplementarySlashIfNeeded(const fs::path& filePath);
97
98 //copyOptions can be one of;
99 //gFile::CopyOptions::NONE, gFile::CopyOptions::SKIP_EXISTING, gFile::CopyOptions::OVERWRITE_EXISTING
100 static void copy(const std::string& fromFullPath, const std::string& toFullPath, int copyOption = CopyOptions::NONE);
101 static bool copyFile(const std::string& fromFullPath, const std::string& toFullPath, int copyOption = CopyOptions::NONE);
102 static void copySymlink(const std::string& existingSymlinkFullPath, const std::string& newSymlinkFullPath);
103 static bool createDirectory(const std::string& fullPath);
104 static void createDirectorySymlink(const std::string& toFullPath, const std::string& symlinkFullPath);
105 static void createSymlink(const std::string& toFullPath, const std::string& symlinkFullPath);
106 static bool isEmpty(const std::string& fullPath);
107 static bool isEquivalent(const std::string& fullPath1, const std::string& fullPath2);
108 static bool isSymlink(const std::string& fullPath);
109 static bool remove(const std::string& fullPath);
110 static bool removeAll(const std::string& fullPath);
111 static void rename(const std::string& fromFullPath, const std::string& toFullPath);
112 static std::vector<std::string> getDirectoryContent(const std::string& fullPath);
113 static std::string normalizePathUtf8(const std::string& p);
114
115private:
116 fs::path path;
117 int mode;
118 bool binary;
119
120 std::vector<char> bytes;
121 int size;
122
123 fs::fstream stream;
124 bool open();
125 bool openStream(int fileMode, bool isBinary);
126 void readFile();
127
128 static fs::copy_options copyOptions[4];
129};
130
131#endif /* UTILS_GFILE_H_ */
Definition gFile.h:34
void write(const std::string &content)
bool isDirectory()
void write(std::vector< char > newBytes)
static void rename(const std::string &fromFullPath, const std::string &toFullPath)
static void createSymlink(const std::string &toFullPath, const std::string &symlinkFullPath)
int getSize()
static bool createDirectory(const std::string &fullPath)
static bool doesFileExistInAssets(const std::string &filePath)
static std::string getDirectory(const fs::path &path)
static bool doesFileExist(const std::string &fullPath)
Mode
Definition gFile.h:36
@ FILEMODE_APPEND
Definition gFile.h:40
@ FILEMODE_READWRITE
Definition gFile.h:39
@ FILEMODE_WRITEONLY
Definition gFile.h:38
@ FILEMODE_READONLY
Definition gFile.h:37
fs::path getPath()
static bool isLink(const std::string &fullPath)
static bool isSymlink(const std::string &fullPath)
int getMode()
static bool removeAll(const std::string &fullPath)
static bool isDirectoryInAssets(const std::string &filePath)
bool loadFile(const std::string &filePath, int fileMode=FILEMODE_READONLY, bool isBinary=true)
static void copySymlink(const std::string &existingSymlinkFullPath, const std::string &newSymlinkFullPath)
std::string getText()
gFile(const std::string &fullPath, int fileMode=FILEMODE_READONLY, bool isBinary=true)
void write(const char *bytes, size_t length)
bool isFile()
bool isLink()
static bool remove(const std::string &fullPath)
bool exists()
static bool isEmpty(const std::string &fullPath)
static std::string addComplementarySlashIfNeeded(const fs::path &filePath)
static std::vector< std::string > getDirectoryContent(const std::string &fullPath)
static bool isDirectory(const std::string &fullPath)
const std::vector< char > & getBytesConst()
Definition gFile.h:68
static bool isDevice(const fs::path &path)
static void createDirectorySymlink(const std::string &toFullPath, const std::string &symlinkFullPath)
bool isBinary()
std::string getFilename()
virtual ~gFile()
static bool isFile(const std::string &fullPath)
void close()
static std::string normalizePathUtf8(const std::string &p)
std::vector< char > getBytes()
static bool isLinkInAssets(const std::string &filePath)
static bool isFileInAssets(const std::string &filePath)
static std::string getFilename(const fs::path &path)
static void copy(const std::string &fromFullPath, const std::string &toFullPath, int copyOption=CopyOptions::NONE)
static bool copyFile(const std::string &fromFullPath, const std::string &toFullPath, int copyOption=CopyOptions::NONE)
bool load(const std::string &fullPath, int fileMode=FILEMODE_READONLY, bool isBinary=true)
CopyOptions
Definition gFile.h:43
@ OVERWRITE_EXISTING
Definition gFile.h:46
@ RECURSIVE
Definition gFile.h:47
@ NONE
Definition gFile.h:44
@ SKIP_EXISTING
Definition gFile.h:45
std::string getDirectory()
static bool isEquivalent(const std::string &fullPath1, const std::string &fullPath2)
bool isOpen()
bool isDevice()
Definition gObject.h:33
Definition gFile.h:25
ghc::filesystem::ofstream ofstream
Definition gFile.h:28
ghc::filesystem::fstream fstream
Definition gFile.h:29
ghc::filesystem::ifstream ifstream
Definition gFile.h:27