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 /*
55 * The stream is always opened in binary mode. isBinary only decides how the
56 * contents are treated: text files get their carriage returns removed on read,
57 * binary files are kept byte for byte. Writes are never translated.
58 */
59 bool load(const std::string& fullPath, int fileMode = FILEMODE_READONLY, bool isBinary = true);
60 bool loadFile(const std::string& filePath, int fileMode = FILEMODE_READONLY, bool isBinary = true);
61 void close();
62
63 fs::path getPath();
64 int getMode();
65 bool isBinary();
66
67 void write(const std::string& content);
68 void write(std::vector<char> newBytes);
69 void write(const char* bytes, size_t length);
70
71 std::vector<char> getBytes();
72
73 const std::vector<char>& getBytesConst() {
74 return bytes;
75 }
76
77 std::string getText();
78 int getSize();
79
80 std::string getFilename();
81 std::string getDirectory();
82
83 bool exists();
84 bool isFile();
85 bool isLink();
87 bool isDevice();
88 bool isOpen();
89
90 static bool doesFileExist(const std::string& fullPath);
91 static bool doesFileExistInAssets(const std::string& filePath);
92 static bool isFile(const std::string& fullPath);
93 static bool isFileInAssets(const std::string& filePath);
94 static bool isLink(const std::string& fullPath);
95 static bool isLinkInAssets(const std::string& filePath);
96 static bool isDirectory(const std::string& fullPath);
97 static bool isDirectoryInAssets(const std::string& filePath);
98 static bool isDevice(const fs::path& path);
99 static std::string getFilename(const fs::path& path);
100 static std::string getDirectory(const fs::path& path);
101 static std::string addComplementarySlashIfNeeded(const fs::path& filePath);
102
103 //copyOptions can be one of;
104 //gFile::CopyOptions::NONE, gFile::CopyOptions::SKIP_EXISTING, gFile::CopyOptions::OVERWRITE_EXISTING
105 static void copy(const std::string& fromFullPath, const std::string& toFullPath, int copyOption = CopyOptions::NONE);
106 static bool copyFile(const std::string& fromFullPath, const std::string& toFullPath, int copyOption = CopyOptions::NONE);
107 static void copySymlink(const std::string& existingSymlinkFullPath, const std::string& newSymlinkFullPath);
108 static bool createDirectory(const std::string& fullPath);
109 static void createDirectorySymlink(const std::string& toFullPath, const std::string& symlinkFullPath);
110 static void createSymlink(const std::string& toFullPath, const std::string& symlinkFullPath);
111 static bool isEmpty(const std::string& fullPath);
112 static bool isEquivalent(const std::string& fullPath1, const std::string& fullPath2);
113 static bool isSymlink(const std::string& fullPath);
114 static bool remove(const std::string& fullPath);
115 static bool removeAll(const std::string& fullPath);
116 static void rename(const std::string& fromFullPath, const std::string& toFullPath);
117 static std::vector<std::string> getDirectoryContent(const std::string& fullPath);
118 static std::string normalizePathUtf8(const std::string& p);
119
120private:
121 fs::path path;
122 int mode;
123 bool binary;
124
125 std::vector<char> bytes;
126 int size;
127
128 fs::fstream stream;
129 bool open();
130 bool openStream(int fileMode, bool isBinary);
131 void readFile();
132
133 static fs::copy_options copyOptions[4];
134};
135
136#endif /* UTILS_GFILE_H_ */
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:73
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 gFile.h:25
ghc::filesystem::ifstream ifstream
Definition gFile.h:27
ghc::filesystem::ofstream ofstream
Definition gFile.h:28
ghc::filesystem::fstream fstream
Definition gFile.h:29