File: FileManager.h

package info (click to toggle)
glob2 0.9.4.4-10
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 34,856 kB
  • sloc: cpp: 89,685; python: 868; ansic: 259; sh: 49; makefile: 19
file content (149 lines) | stat: -rw-r--r-- 7,093 bytes parent folder | download | duplicates (7)
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
142
143
144
145
146
147
148
149
/*
  Copyright (C) 2001-2004 Stephane Magnenat & Luc-Olivier de Charrière
  for any question or comment contact us at <stephane at magnenat dot net> or <NuageBleu at gmail dot com>

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 3 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/

#ifndef __FILEMANAGER_H
#define __FILEMANAGER_H

#include "GAGSys.h"
#include <vector>
#include <fstream>
#include <string>

//! this is the host filesystem directory separator
#ifndef DIR_SEPARATOR
#define DIR_SEPARATOR '/'
#endif
#ifndef DIR_SEPARATOR_S
#define DIR_SEPARATOR_S "/"
#endif

namespace GAGCore
{
	class StreamBackend;
	
	//! File Manager (filesystem abstraction)
	class FileManager
	{
	public:
		//! Type of Stream
		enum StreamType
		{
			//! Binary stream, content directly written in endian-safe binary
			STREAM_BINARY = 0,
			//! Compatibility stream, not human-readable but uses the metas infos for backward compatibility
			STREAM_COMPAT,
			//! Text stream, human readable and backward compatible
			STREAM_TEXT,
		};
		
	private:
		//! List of directory where to search for requested file
		std::vector<std::string> dirList;
		//! List of file relative to virtual base address after call to initDirectoryListing
		std::vector<std::string> fileList;
		//! Index in the dirFileList vector
		int fileListIndex;
	
	private:
		//! clear the list of file for directory listing
		void clearFileList(void);
		//! internal function that does the real listing job
		bool addListingForDir(const char *realDir, const char *extension=NULL, const bool dirs=false);
		//! open a file, if it is in writing, do a backup
		SDL_RWops *openWithbackup(const char *filename, const char *mode);
		//! open a file, if it is in writing, do a backup, fopen version
		FILE *openWithbackupFP(const char *filename, const char *mode);
		//! open a file, if it is in writing, do a backup, std::ofstream version
		std::ofstream *openWithbackupOFS(const char *filename, std::ofstream::openmode mode);
	
	public:
		//! FileManager constructor
		FileManager(const char *gameName);
		//! FileManager destructor
		virtual ~FileManager();
	
		//! Add a directory to the search list
		void addDir(const char *dir);
		//! Return the number of directory in the search list
		unsigned getDirCount(void) const { return dirList.size(); }
		//! Return a direction in the search list from its index
		std::string getDir(unsigned index) const { if (index < getDirCount()) return dirList[index]; else return std::string(); }
		//! Add a new subdir (create it if needed) which will be used to open file in write mode in it
		void addWriteSubdir(const char *subdir);
		void addWriteSubdir(const std::string& s) { addWriteSubdir(s.c_str()); }
	
		//! Remove a file or a directory in the virtual filesystem, const char *version
		void remove(const char *filename);
		//! Remove a file or a directory in the virtual filesystem, std::string version
		void remove(const std::string &filename) { return remove(filename.c_str()); }
		//! Returns true if filename is a directory
		bool isDir(const char *filename);
		
		//! Compress source to dest uzing gzip, returns true on success
		bool gzip(const std::string &source, const std::string &dest);
		//! Uncompress source to dest uzing gzip, returns true on success
		bool gunzip(const std::string &source, const std::string &dest);
	
		//! Open an output stream backend, use it to construct specific output streams, const char *version
		StreamBackend *openOutputStreamBackend(const char *filename);
		//! Open an output stream backend, use it to construct specific output streams, std::string version
		StreamBackend *openOutputStreamBackend(const std::string &filename) { return openOutputStreamBackend(filename.c_str()); }
		
		//! Open an input stream backend, use it to construct specific input streams, const char *version
		StreamBackend *openInputStreamBackend(const char *filename);
		//! Open an input stream backend, use it to construct specific input streams, std::string version
		StreamBackend *openInputStreamBackend(const std::string &filename) { return openInputStreamBackend(filename.c_str()); }
		
		//! Open a compressed output stream backend, use it to construct specific output streams, const char *version
		StreamBackend *openCompressedOutputStreamBackend(const char *filename);
		//! Open a compressed output stream backend, use it to construct specific output streams, std::string version
		StreamBackend *openCompressedOutputStreamBackend(const std::string &filename) { return openCompressedOutputStreamBackend(filename.c_str()); }
		
		//! Open a compressed input stream backend, use it to construct specific input streams, const char *version
		StreamBackend *openCompressedInputStreamBackend(const char *filename);
		//! Open a compressed input stream backend, use it to construct specific input streams, std::string version
		StreamBackend *openCompressedInputStreamBackend(const std::string &filename) { return openCompressedInputStreamBackend(filename.c_str()); }
		
		
		//! Open a file in the SDL_RWops format, COMPAT for GraphicContext PNG loader, can be removed on others backends, const char *version
		SDL_RWops *open(const char *filename, const char *mode="rb");
		//! Open a file in the SDL_RWops format, COMPAT for GraphicContext PNG loader, can be removed on others backends, std::string version
		SDL_RWops *open(const std::string &filename, const char *mode="rb") { return open(filename.c_str(), mode); }
		//! Open a file in the FILE* format
		FILE *openFP(const char *filename, const char *mode="rb");
		//! Open a file in the c++ stream format for reading
		std::ifstream *openIFStream(const std::string &fileName);
		//! Return the checksum of a file, const char *version
		Uint32 checksum(const char *filename);
		//! Return the checksum of a file, std::string version
		Uint32 checksum(const std::string &filename) { return checksum(filename.c_str()); }
		//! Return the modification date of a file, const char *version
		time_t mtime(const char *filename);
		//! Return the modification date of a file, std::string version
		time_t mtime(const std::string &filename) { return mtime(filename.c_str()); }
	
		// FIXME : the following functions are not thread-safe :
		//! must be call before directory listening, return true if success
		bool initDirectoryListing(const char *virtualDir, const char *extension=NULL, const bool dirs=false);
		//! get the next name, return NULL if none
		const char *getNextDirectoryEntry(void);
	};
}

#endif