1 #ifndef __GNUC__
2 #pragma once
3 #endif
4 #ifndef __XR_FILE_SYSTEM_H__
5 #define __XR_FILE_SYSTEM_H__
6 
7 #include <string>
8 #include <vector>
9 #include "xr_types.h"
10 #include "xr_reader.h"
11 #include "xr_writer.h"
12 
13 namespace xray_re {
14 
15 class xr_file_system {
16 public:
17 	enum {
18 		FSF_READ_ONLY	= 0x1,
19 	};
20 
21 			xr_file_system();
22 			~xr_file_system();
23 
24 	static xr_file_system&	instance();
25 
26 	bool		initialize(const char* fs_spec, unsigned flags = 0);
27 
28 	bool		read_only() const;
29 
30 	xr_reader*	r_open(const char* path) const;
31 	xr_reader*	r_open(const std::string& path) const;
32 	xr_reader*	r_open(const char* path, const char* name) const;
33 	xr_reader*	r_open(const char* path, const std::string& name) const;
34 	void		r_close(xr_reader*& r) const;
35 
36 	xr_writer*	w_open(const char* path, bool ignore_ro = false) const;
37 	xr_writer*	w_open(const std::string& path) const;
38 	xr_writer*	w_open(const char* path, const char* name) const;
39 	xr_writer*	w_open(const char* path, const std::string& name) const;
40 	void		w_close(xr_writer*& w) const;
41 
42 	bool		copy_file(const char* src_path, const char* src_name,
43 					const char* tgt_path, const char* tgt_name = 0) const;
44 	bool		copy_file(const char* src_path, const char* tgt_path) const;
45 	bool		copy_file(const std::string& src_path, const std::string& tgt_path) const;
46 
47 	static size_t	file_length(const char* path);
48 	static size_t	file_length(const std::string& path);
49 	size_t		file_length(const char* path, const char* name) const;
50 	size_t		file_length(const char* path, const std::string& name) const;
51 
52 	static uint32_t	file_age(const char* path);
53 	static uint32_t	file_age(const std::string& path);
54 	uint32_t	file_age(const char* path, const char* name) const;
55 	uint32_t	file_age(const char* path, const std::string& name) const;
56 
57 	static bool	file_exist(const char* path);
58 	static bool	file_exist(const std::string& path);
59 	bool		file_exist(const char* path, const char* name) const;
60 	bool		file_exist(const char* path, const std::string& name) const;
61 
62 	static bool	folder_exist(const char* path);
63 	static bool	folder_exist(const std::string& path);
64 	bool		folder_exist(const char* path, const char* name) const;
65 	bool		folder_exist(const char* path, const std::string& name) const;
66 
67 	bool		create_path(const char* path) const;
68 	bool		create_path(const std::string& path) const;
69 
70 	bool		create_folder(const char* path) const;
71 	bool		create_folder(const std::string& path) const;
72 	bool		create_folder(const char* path, const char* name) const;
73 	bool		create_folder(const char* path, const std::string& name) const;
74 
75 	const char*	resolve_path(const char* path) const;
76 	bool		resolve_path(const char* path, const char* name, std::string& full_path) const;
77 	bool		resolve_path(const char* path, const std::string& name, std::string& full_path) const;
78 
79 	void		update_path(const char* path, const std::string& root, const std::string& add);
80 	void		update_path(const char* path, const char* root, const std::string& add);
81 	void		update_path(const char* path, const char* root, const char* add);
82 
83 	static void	append_path_separator(std::string& path);
84 	static void	split_path(const std::string& path, std::string* folder = 0,
85 					std::string* name = 0, std::string* extension = 0);
86 	static void	split_path(const char* path, std::string* folder = 0,
87 					std::string* name = 0, std::string* extension = 0);
88 
89 protected:
90 	struct path_alias {
91 		std::string	path;
92 		std::string	root;
93 		std::string	filter;
94 		std::string	caption;
95 	};
96 	TYPEDEF_STD_VECTOR_PTR(path_alias)
97 
98 	const path_alias*	find_path_alias(const char* path) const;
99 	path_alias*		add_path_alias(const std::string& path,
100 					const std::string& root, const std::string& add);
101 	bool			parse_fs_spec(xr_reader& r);
102 
103 	void			working_folder(std::string& folder);
104 
105 private:
106 	path_alias_vec	m_aliases;
107 	unsigned	m_flags;
108 };
109 
110 const char PA_FS_ROOT[] = "$fs_root$";
111 const char PA_SDK_ROOT[] = "$sdk_root$";
112 const char PA_GAME_DATA[] = "$game_data$";
113 const char PA_GAME_CONFIG[] = "$game_config$";
114 const char PA_GAME_SCRIPTS[] = "$game_scripts$";
115 const char PA_GAME_MESHES[] = "$game_meshes$";
116 const char PA_GAME_TEXTURES[] = "$game_textures$";
117 const char PA_GAME_LEVELS[] = "$game_levels$";
118 const char PA_GAME_SPAWN[] = "$game_spawn$";
119 const char PA_GAME_SOUNDS[] = "$game_sounds$";
120 const char PA_LEVEL[] = "$level$";
121 const char PA_LOGS[] = "$logs$";
122 const char PA_SOUNDS[] = "$sounds$";
123 const char PA_TEXTURES[] = "$textures$";
124 const char PA_OBJECTS[] = "$objects$";
125 const char PA_CLIPS[] = "$clips$";
126 const char PA_MAPS[] = "$maps$";
127 const char PA_GROUPS[] = "$groups$";
128 const char PA_TEMP[] = "$temp$";
129 const char PA_IMPORT[] = "$import$";
130 const char PA_DETAIL_OBJECTS[] = "$detail_objects$";
131 
instance()132 inline xr_file_system& xr_file_system::instance()
133 {
134 	static xr_file_system instance0;
135 	return instance0;
136 }
137 
read_only()138 inline bool xr_file_system::read_only() const { return !!(m_flags & FSF_READ_ONLY); }
r_open(const std::string & path)139 inline xr_reader* xr_file_system::r_open(const std::string& path) const { return r_open(path.c_str()); }
r_open(const char * path,const std::string & name)140 inline xr_reader* xr_file_system::r_open(const char* path, const std::string& name) const
141 {
142 	return r_open(path, name.c_str());
143 }
w_open(const std::string & path)144 inline xr_writer* xr_file_system::w_open(const std::string& path) const { return w_open(path.c_str()); }
w_open(const char * path,const std::string & name)145 inline xr_writer* xr_file_system::w_open(const char* path, const std::string& name) const
146 {
147 	return w_open(path, name.c_str());
148 }
r_close(xr_reader * & r)149 inline void xr_file_system::r_close(xr_reader*& r) const { delete r; r = 0; }
w_close(xr_writer * & w)150 inline void xr_file_system::w_close(xr_writer*& w) const { delete w; w = 0; }
151 
file_length(const std::string & path)152 inline size_t xr_file_system::file_length(const std::string& path) { return file_length(path.c_str()); }
153 
file_age(const std::string & path)154 inline uint32_t xr_file_system::file_age(const std::string& path) { return file_age(path.c_str()); }
file_age(const char * path,const std::string & name)155 inline uint32_t xr_file_system::file_age(const char* path, const std::string& name) const
156 {
157 	return file_age(path, name.c_str());
158 }
159 
folder_exist(const std::string & path)160 inline bool xr_file_system::folder_exist(const std::string& path) { return folder_exist(path.c_str()); }
folder_exist(const char * path,const std::string & name)161 inline bool xr_file_system::folder_exist(const char* path, const std::string& name) const
162 {
163 	return folder_exist(path, name.c_str());
164 }
165 
file_exist(const std::string & path)166 inline bool xr_file_system::file_exist(const std::string& path) { return file_exist(path.c_str()); }
file_exist(const char * path,const std::string & name)167 inline bool xr_file_system::file_exist(const char* path, const std::string& name) const
168 {
169 	return file_exist(path, name.c_str());
170 }
171 
create_folder(const std::string & path)172 inline bool xr_file_system::create_folder(const std::string& path) const
173 {
174 	return create_folder(path.c_str());
175 }
create_folder(const char * path,const std::string & name)176 inline bool xr_file_system::create_folder(const char* path, const std::string& name) const
177 {
178 	return create_folder(path, name.c_str());
179 }
180 
create_path(const std::string & path)181 inline bool xr_file_system::create_path(const std::string& path) const
182 {
183 	return create_path(path.c_str());
184 }
185 
copy_file(const std::string & src_path,const std::string & tgt_path)186 inline bool xr_file_system::copy_file(const std::string& src_path, const std::string& tgt_path) const
187 {
188 	return copy_file(src_path.c_str(), tgt_path.c_str());
189 }
190 
resolve_path(const char * path,const std::string & name,std::string & full_path)191 inline bool xr_file_system::resolve_path(const char* path, const std::string& name, std::string& full_path) const
192 {
193 	return resolve_path(path, name.c_str(), full_path);
194 }
195 
split_path(const std::string & path,std::string * folder,std::string * name,std::string * extension)196 inline void xr_file_system::split_path(const std::string& path, std::string* folder,
197 		std::string* name, std::string* extension)
198 {
199 	split_path(path.c_str(), folder, name, extension);
200 }
201 
update_path(const char * path,const std::string & root,const std::string & add)202 inline void xr_file_system::update_path(const char* path, const std::string& root, const std::string& add)
203 {
204 	update_path(path, root.c_str(), add.c_str());
205 }
206 
update_path(const char * path,const char * root,const std::string & add)207 inline void xr_file_system::update_path(const char* path, const char* root, const std::string& add)
208 {
209 	update_path(path, root, add.c_str());
210 }
211 
212 } // end of namespace xray_re
213 
214 #endif
215