1 /*
2  * Copyright (c) 2009, The MilkyTracker Team.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * - Redistributions of source code must retain the above copyright notice,
9  *   this list of conditions and the following disclaimer.
10  * - Redistributions in binary form must reproduce the above copyright
11  *   notice, this list of conditions and the following disclaimer in the
12  *   documentation and/or other materials provided with the distribution.
13  * - Neither the name of the <ORGANIZATION> nor the names of its contributors
14  *   may be used to endorse or promote products derived from this software
15  *   without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 /*
31  *  XMFile.h
32  *  MilkyPlay
33  *
34  *  Created by Peter Barth on Tue Oct 19 2004.
35  *
36  */
37 #ifndef __XMFILE_H__
38 #define __XMFILE_H__
39 
40 #include "MilkyPlayCommon.h"
41 
42 class XMFileBase
43 {
44 private:
45 	mp_dword				baseOffset;
46 
47 public:
48 							XMFileBase();
49 	virtual					~XMFileBase();
50 
51 	virtual mp_sint32		read(void* ptr,mp_sint32 size,mp_sint32 count) = 0;
52 	virtual mp_sint32		write(const void* ptr,mp_sint32 size,mp_sint32 count) = 0;
53 
54 	enum SeekOffsetTypes
55 	{
56 		SeekOffsetTypeStart,
57 		SeekOffsetTypeCurrent,
58 		SeekOffsetTypeEnd
59 	};
60 	virtual void			seek(mp_uint32 pos, SeekOffsetTypes seekOffsetType = SeekOffsetTypeStart) = 0;
61 	virtual mp_uint32		pos() = 0;
62 	virtual mp_uint32		size() = 0;
63 
isEOF()64 	virtual bool			isEOF() { return pos() >= size(); }
65 
66 	virtual	const SYSCHAR*  getFileName() = 0;
67 
68 	virtual	const char*		getFileNameASCII() = 0;
69 
70 	virtual	bool			isOpen() = 0;
71 	virtual	bool			isOpenForWriting()  = 0;
72 
73 	mp_ubyte				readByte();
74 	mp_uword				readWord();
75 	mp_dword				readDword();
76 	void					readWords(mp_uword* buffer,mp_sint32 count);
77 	void					readDwords(mp_dword* buffer,mp_sint32 count);
78 
79 	void					writeByte(mp_ubyte b);
80 	void					writeWord(mp_uword w);
81 	void					writeDword(mp_dword dw);
82 	void					writeWords(const mp_uword* buffer,mp_sint32 count);
83 	void					writeDwords(const mp_dword* buffer,mp_sint32 count);
84 
85 	void					writeString(const char* string);
86 
setBaseOffset(mp_dword baseOffset)87 	void					setBaseOffset(mp_dword baseOffset) { this->baseOffset = baseOffset; }
getBaseOffset()88 	mp_dword				getBaseOffset() const { return baseOffset; }
89 	void					seekWithBaseOffset(mp_dword pos);
90 	mp_uint32				sizeWithBaseOffset();
91 	mp_uint32				posWithBaseOffset();
92 };
93 
94 class XMFile : public XMFileBase
95 {
96 private:
97 	const SYSCHAR*  fileName;
98 
99 	char*			fileNameASCII;
100 
101 	FHANDLE			handle;
102 	mp_uint32		bytesRead;
103 
104 	bool			writeAccess;
105 
106 	mp_ubyte*		cacheBuffer;
107 	mp_ubyte*		currentCacheBufferPtr;
108 
109 	void			flush();
110 
111 public:
112 							XMFile(const SYSCHAR* fileName, bool writeAccess = false);
113 	virtual					~XMFile();
114 
115 	virtual mp_sint32		read(void* ptr,mp_sint32 size,mp_sint32 count);
116 	virtual mp_sint32		write(const void* ptr,mp_sint32 size,mp_sint32 count);
117 
118 	virtual void			seek(mp_uint32 pos, SeekOffsetTypes seekOffsetType = SeekOffsetTypeStart);
119 	virtual mp_uint32		pos();
120 	virtual mp_uint32		size();
121 
getFileName()122 	virtual const SYSCHAR*  getFileName() { return fileName; }
123 
124 	virtual const char*		getFileNameASCII();
125 
126 	virtual bool			isOpen();
isOpenForWriting()127 	virtual bool			isOpenForWriting() { return isOpen() && writeAccess; }
128 
129 	static bool				exists(const SYSCHAR* file);
130 	static bool				remove(const SYSCHAR* file);
131 };
132 
133 #endif
134