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"
24namespace fs {
25using namespace ghc::filesystem;
26using ifstream = ghc::filesystem::ifstream;
27using ofstream = ghc::filesystem::ofstream;
28using fstream = ghc::filesystem::fstream;
29}
30#include "gObject.h"
31
32
33class gFile : public gObject {
34public:
41
48
50 gFile(const std::string& fullPath, int fileMode = FILEMODE_READONLY, bool isBinary = true);
51 virtual ~gFile();
52
53 bool load(const std::string& fullPath, int fileMode = FILEMODE_READONLY, bool isBinary = true);
54 bool loadFile(const std::string& filePath, int fileMode = FILEMODE_READONLY, bool isBinary = true);
55 void close();
56
57 fs::path getPath();
58 int getMode();
59 bool isBinary();
60
61 void write(const std::string& content);
62 void write(std::vector<char> newBytes);
63 void write(const char* bytes, size_t length);
64
65 std::vector<char> getBytes();
66
67 const std::vector<char>& getBytesConst() {
68 return bytes;
69 }
70
71 std::string getText();
72 int getSize();
73
74 std::string getFilename();
75 std::string getDirectory();
76
77 bool exists();
78 bool isFile();
79 bool isLink();
81 bool isDevice();
82 bool isOpen();
83
84 static bool doesFileExist(const std::string& fullPath);
85 static bool doesFileExistInAssets(const std::string& filePath);
86 static bool isFile(const std::string& fullPath);
87 static bool isFileInAssets(const std::string& filePath);
88 static bool isLink(const std::string& fullPath);
89 static bool isLinkInAssets(const std::string& filePath);
90 static bool isDirectory(const std::string& fullPath);
91 static bool isDirectoryInAssets(const std::string& filePath);
92 static bool isDevice(const fs::path& path);
93 static std::string getFilename(const fs::path& path);
94 static std::string getDirectory(const fs::path& path);
95 static std::string addComplementarySlashIfNeeded(const fs::path& filePath);
96
97 //copyOptions can be one of;
98 //gFile::CopyOptions::NONE, gFile::CopyOptions::SKIP_EXISTING, gFile::CopyOptions::OVERWRITE_EXISTING
99 static void copy(const std::string& fromFullPath, const std::string& toFullPath, int copyOption = CopyOptions::NONE);
100 static bool copyFile(const std::string& fromFullPath, const std::string& toFullPath, int copyOption = CopyOptions::NONE);
101 static void copySymlink(const std::string& existingSymlinkFullPath, const std::string& newSymlinkFullPath);
102 static bool createDirectory(const std::string& fullPath);
103 static void createDirectorySymlink(const std::string& toFullPath, const std::string& symlinkFullPath);
104 static void createSymlink(const std::string& toFullPath, const std::string& symlinkFullPath);
105 static bool isEmpty(const std::string& fullPath);
106 static bool isEquivalent(const std::string& fullPath1, const std::string& fullPath2);
107 static bool isSymlink(const std::string& fullPath);
108 static bool remove(const std::string& fullPath);
109 static bool removeAll(const std::string& fullPath);
110 static void rename(const std::string& fromFullPath, const std::string& toFullPath);
111 static std::vector<std::string> getDirectoryContent(const std::string& fullPath);
112
113private:
114 fs::path path;
115 int mode;
116 bool binary;
117
118 std::vector<char> bytes;
119 int size;
120
121 fs::fstream stream;
122 bool open();
123 bool openStream(int fileMode, bool isBinary);
124 void readFile();
125
126 static fs::copy_options copyOptions[4];
127};
128
129#endif /* UTILS_GFILE_H_ */
Definition gFile.h:33
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:35
@ FILEMODE_APPEND
Definition gFile.h:39
@ FILEMODE_READWRITE
Definition gFile.h:38
@ FILEMODE_WRITEONLY
Definition gFile.h:37
@ FILEMODE_READONLY
Definition gFile.h:36
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:67
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()
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:42
@ OVERWRITE_EXISTING
Definition gFile.h:45
@ RECURSIVE
Definition gFile.h:46
@ NONE
Definition gFile.h:43
@ SKIP_EXISTING
Definition gFile.h:44
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:24
ghc::filesystem::ofstream ofstream
Definition gFile.h:27
ghc::filesystem::fstream fstream
Definition gFile.h:28
ghc::filesystem::ifstream ifstream
Definition gFile.h:26