File: Platform_Files.h

package info (click to toggle)
pose 3.0a3-3
  • links: PTS
  • area: contrib
  • in suites: potato
  • size: 15,500 kB
  • ctags: 20,548
  • sloc: ansic: 72,579; cpp: 50,198; perl: 1,336; python: 1,242; sh: 363; makefile: 290
file content (179 lines) | stat: -rw-r--r-- 4,808 bytes parent folder | download
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/* -*- mode: C++; tab-width: 4 -*- */
/* ================================================================================== */
/* Copyright (c) 1998-1999 3Com Corporation or its subsidiaries. All rights reserved. */
/* ================================================================================== */

#ifndef _PLATFORM_FILES_H_
#define _PLATFORM_FILES_H_

#include "ErrorHandling.h"		// ErrCode
#include "StreamHandle.h"		// StreamHandle


class FileReference
{
	public:
								FileReference	(void);
								FileReference	(const FileReference&);
								FileReference	(const char*);
								FileReference	(const string&);
								FileReference	(const unsigned char*);
								~FileReference	(void);

		FileReference&			operator=		(const FileReference&);

		Bool					IsSpecified		(void) const;
		Bool					Exists			(void) const;

		Bool					IsPRC			(void) const;
		Bool					IsPDB			(void) const;
		Bool					IsPQA			(void) const;
		Bool					IsPSF			(void) const;
		Bool					IsROM			(void) const;

		string					GetFileName		(void) const;

		bool					operator==		(const FileReference&) const;
		bool					operator!=		(const FileReference&) const;
		bool					operator<		(const FileReference&) const;
		bool					operator>		(const FileReference&) const;

		bool					FromPrefString	(const string&);
		string					ToPrefString	(void) const;

		FILE*					OpenAsFILE		(const char* mode) const;

		// I'm kind of opposed to making GetFilePath public.  It breaks the
		// enacapsulation of FileReference.  However, there are some places
		// where getting to this information is handy (such as in the Windows
		// code that opens the File dialogs in the same directory as a
		// previously opened file).  Until I can figure a better way for
		// handling that scenario, I'll leave GetFilePath public.
	private:
	public:
		string					GetFilePath		(void) const;

#if defined (__MACOS__)
	public:
								FileReference	(const FSSpec&);
								FileReference	(AliasHandle);

		AliasHandle				GetAlias		(void) const;
		FSSpec					GetFSSpec		(void) const;

	private:
		void					UpdateAlias		(void);
		void					UpdateSpec		(void);

		Bool					EqualAlias		(AliasHandle, AliasHandle) const;
		Bool					EqualFSSpec		(const FSSpec&, const FSSpec&) const;

		Bool					IsType			(OSType type, const char* suffix) const;

		AliasHandle				fFileAlias;
		FSSpec					fFileSpec;
#endif

#if defined (_WINDOWS) || defined (UNIX)

	private:
		string					fFilePath;
#endif
};


enum
{
	kCreateNew,			// Creates a new file; fails if it already exists.
	kCreateAlways,		// Always creates a new file, destroying any existing file.
	kOpenExisting,		// Opens the file it it exists, fails otherwise.
	kOpenAlways,		// Opens the file if it exists, creates it otherwise.
	kTruncateExisting,	// Opens and clears existing file, fails otherwise.

	kOpenTypeMask = 0x0F,

	kOpenRead	= 16,
	kOpenWrite	= 32,
	kOpenReadWrite = kOpenRead | kOpenWrite
};

class FileHandle : public StreamHandle
{
	public:
							FileHandle		(const FileReference&,
											 long openMode,
											 uae_u32 creator = 0,
											 uae_u32 fileType = 0);
		virtual				~FileHandle		(void);

		FileReference		GetReference	(void);
		Bool				IsOpen			(void);

		void				SetPos			(long offset, SeekMode);
		uae_s32				GetPos			(void);

		void				SetLength		(uae_s32);
		uae_s32				GetLength		(void);

		void				Read			(uae_s32 length, void* buffer);
		void				Write			(uae_s32 length, const void* buffer);

	protected:
		void				Open			(const FileReference&,
											 long openMode,
											 uae_u32 creator,
											 uae_u32 fileType);

#if defined (__MACOS__)
		void				CreateNew		(const FSSpec&,
											 long openMode,
											 uae_u32 creator,
											 uae_u32 fileType);
		void				CreateAlways	(const FSSpec&,
											 long openMode,
											 uae_u32 creator,
											 uae_u32 fileType);
		void				OpenExisting	(const FSSpec&,
											 long openMode,
											 uae_u32 creator,
											 uae_u32 fileType);
		void				OpenAlways		(const FSSpec&,
											 long openMode,
											 uae_u32 creator,
											 uae_u32 fileType);
		void				TruncateExisting(const FSSpec&,
											 long openMode,
											 uae_u32 creator,
											 uae_u32 fileType);
		char				GetPermission	(long openMode);
#endif

		void				Close			(void);

		void				Throw			(ErrCode);
		void				SetFileNameParameter	(void);

	private:
			// Protect the copy constructor so that we don't
			// accidentally make copies (what does it mean to
			// copy a reference to an open file?).

							FileHandle		(const FileHandle&);

		FileReference		fFileRef;

#if defined (__MACOS__)
		short				fRefNum;
#endif

#if defined (_WINDOWS)
		void*				fHandle;	// == HANDLE
#endif

#if defined (UNIX)
		FILE *				fStream;
#endif
};

#endif /* _PLATFORM_FILES_H_ */