1 #ifndef __MDFN_FILE_H
2 #define __MDFN_FILE_H
3 
4 #include <mednafen/Stream.h>
5 
6 namespace Mednafen
7 {
8 
9 class MDFNFILE
10 {
11 	public:
12 
13 	MDFNFILE(VirtualFS* vfs, const char* path, const std::vector<FileExtensionSpecStruct>& known_ext, const char* purpose = nullptr);
14 	~MDFNFILE();
15 
16         void ApplyIPS(Stream*);
17 	void Close(void) throw();
18 
19         const std::string &ext;		// For file-type determination.  Leading period has been removed, and A-Z chars have been converted to a-z.
20         const std::string &fbase;	// For region detection heuristics.
21 
size(void)22 	INLINE uint64 size(void)
23 	{
24 	 return str->size();
25 	}
26 
27 	INLINE void seek(int64 offset, int whence = SEEK_SET)
28 	{
29 	 str->seek(offset, whence);
30 	}
31 
32 	INLINE uint64 read(void* ptr, uint64 count, bool error_on_eos = true)
33 	{
34 	 return str->read(ptr, count, error_on_eos);
35 	}
36 
tell(void)37 	INLINE uint64 tell(void)
38 	{
39 	 return str->tell();
40 	}
41 
rewind(void)42 	INLINE void rewind(void)
43 	{
44 	 str->rewind();
45 	}
46 
stream(void)47 	INLINE Stream* stream(void)
48 	{
49 	 return str.get();
50 	}
51 
active_vfs(void)52 	INLINE VirtualFS* active_vfs(void)
53 	{
54 	 return f_vfs;
55 	}
56 
57 	// Path of file opened from archive, in the archive.
active_dir_path(void)58 	INLINE std::string active_dir_path(void)
59 	{
60 	 return f_dir_path;
61 	}
62 
active_path(void)63 	INLINE std::string active_path(void)
64 	{
65 	 return f_path;
66 	}
67 
steal_archive_vfs(void)68 	INLINE std::unique_ptr<VirtualFS> steal_archive_vfs(void)
69 	{
70 	 f_vfs = nullptr;
71 
72 	 return std::move(archive_vfs);
73 	}
74 
75 	private:
76 
77 	std::string f_ext;
78 	std::string f_fbase;
79 
80 	std::unique_ptr<Stream> str;
81 	std::unique_ptr<VirtualFS> archive_vfs;
82 
83 	VirtualFS* f_vfs;
84 	std::string f_dir_path;
85 	std::string f_path;
86 
87 	void Open(VirtualFS* vfs, const char* path, const std::vector<FileExtensionSpecStruct>& known_ext, const char* purpose = nullptr);
88 
89 	MDFNFILE(const MDFNFILE&);
90 	MDFNFILE& operator=(const MDFNFILE&);
91 };
92 
93 class PtrLengthPair
94 {
95  public:
96 
PtrLengthPair(const void * new_data,const uint64 new_length)97  inline PtrLengthPair(const void *new_data, const uint64 new_length)
98  {
99   data = new_data;
100   length = new_length;
101  }
102 
~PtrLengthPair()103  ~PtrLengthPair()
104  {
105 
106  }
107 
GetData(void)108  INLINE const void *GetData(void) const
109  {
110   return(data);
111  }
112 
GetLength(void)113  INLINE uint64 GetLength(void) const
114  {
115   return(length);
116  }
117 
118  private:
119  const void *data;
120  uint64 length;
121 };
122 
123 // These functions should be used for data like non-volatile backup memory.
124 bool MDFN_DumpToFile(const std::string& path, const void *data, const uint64 length, bool throw_on_error = false);
125 bool MDFN_DumpToFile(const std::string& path, const std::vector<PtrLengthPair> &pearpairs, bool throw_on_error = false);
126 
127 void MDFN_BackupSavFile(const uint8 max_backup_count, const char* sav_ext);
128 
129 //
130 // Helper function to open a file in read mode, so we can stop gzip-compressing our save-game files and not have to worry so much about games
131 // that might write the gzip magic to the beginning of the save game memory area causing a problem.
132 //
133 std::unique_ptr<Stream> MDFN_AmbigGZOpenHelper(const std::string& path, std::vector<size_t> good_sizes);
134 
135 }
136 #endif
137