/
file_io.h
141 lines (105 loc) · 3.33 KB
/
file_io.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/* Copyright (c) 2012-2013 Cheese and Bacon Games, LLC */
/* See the file docs/COPYING.txt for copying permission. */
#ifndef file_io_h
#define file_io_h
#include <string>
#include <sstream>
#include <SDL.h>
#ifdef GAME_OS_ANDROID
#include <vector>
#include <cstdint>
#include <dirent.h>
#include <sys/stat.h>
#else
#include <boost/filesystem.hpp>
#endif
class File_IO_Load{
private:
bool binary;
bool load_success;
bool loaded_backup;
std::string data;
std::istringstream data_stream;
public:
File_IO_Load();
File_IO_Load(std::string path,bool path_is_backup=false,bool get_binary=false,bool suppress_errors=false);
void open(std::string path,bool path_is_backup=false,bool get_binary=false,bool suppress_errors=false);
void close();
bool is_opened();
bool is_backup();
bool eof();
void getline(std::string* line);
std::string get_data();
};
class File_IO_Save{
private:
SDL_RWops* rwops;
std::string path;
public:
File_IO_Save(std::string get_path,bool append=false,bool binary=false);
void open(std::string get_path,bool append,bool binary);
bool close();
bool is_opened();
bool write(const void* ptr,size_t data_size,size_t data_count);
};
class File_IO{
private:
static bool save_file(std::string path,std::string data,bool append,bool binary);
public:
static const std::string SAVE_SUFFIX_TEMPORARY;
static const std::string SAVE_SUFFIX_BACKUP;
//First saves to a temporary file, then renames that file to the final filename
static bool save_atomic(std::string path,std::string data,bool backup=false,bool append=false,bool binary=false);
static bool exists(std::string path);
static bool is_directory(std::string path);
static bool is_regular_file(std::string path);
static bool create_directory(std::string path);
static bool rename_file(std::string old_path,std::string new_path);
//This does NOT overwrite new_path if it already exists
static bool copy_file(std::string old_path,std::string new_path);
static bool remove_file(std::string path);
static void remove_directory(std::string path);
static std::string get_file_name(std::string path);
#ifdef GAME_OS_ANDROID
static bool external_storage_available();
#endif
};
class File_IO_Directory_Iterator{
private:
#ifdef GAME_OS_ANDROID
std::string directory;
std::vector<std::string> asset_list;
std::uint32_t entry;
#else
boost::filesystem::directory_iterator it;
#endif
public:
File_IO_Directory_Iterator(std::string get_directory);
bool evaluate();
void iterate();
bool is_directory();
bool is_regular_file();
std::string get_full_path();
std::string get_file_name();
};
#ifdef GAME_OS_ANDROID
class File_IO_Directory_Iterator_User_Data{
private:
std::string directory;
DIR* dir;
struct dirent* dir_entry;
std::uint32_t entry;
std::uint32_t entries;
public:
File_IO_Directory_Iterator_User_Data(std::string get_directory);
~File_IO_Directory_Iterator_User_Data();
bool evaluate();
void iterate();
bool exists();
bool is_regular_file();
bool is_directory();
std::string get_full_path();
std::string get_file_name();
};
#endif
#endif