aboutsummaryrefslogtreecommitdiff
path: root/SrcShared/ChunkFile.h
blob: 0f03ddef4440674c1a43e8662af82fda1f5f2b7d (plain)
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
/* -*- mode: C++; tab-width: 4 -*- */
/* ===================================================================== *\
	Copyright (c) 1999-2001 Palm, Inc. or its subsidiaries.
	All rights reserved.

	This file is part of the Palm OS Emulator.

	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 2 of the License, or
	(at your option) any later version.
\* ===================================================================== */

#ifndef _CHUNKFILE_H_
#define _CHUNKFILE_H_

#include "EmStream.h"			// EmStream

class Chunk;


/*
	ChunkFile is a class for managing files containing tagged
	chunks of data.  It contain member functions for writing
	tagged data to a file, finding that data later, and reading
	the data back into user-specified buffers.  There are also
	member functions for writing common data types (numbers and
	strings) in a platform-independent fashion.
	
	There is no facility for *updating* chunk files.  That is,
	there is no way to modify the contents of a chunk that already
	exists in a a chunk file (regardless of whether or not the
	length of the chunk changes).
	
	The format of a chunk file is simple.  It's essentially a
	variable-length array of:
	
		tag: 4 bytes
		size: 4 bytes
		data: "size" bytes of data

	"tag", "size", and any integer values written with WriteInt
	are stored in Big Endian format.  Strings are stored without
	the NULL terminator.  Data is packed as tightly as possible;
	there's no word or longword alignment.
 */

class ChunkFile
{
	public:
								ChunkFile		(EmStream& s);
								~ChunkFile		(void);

		typedef uint32	Tag;

//		static const long		kChunkNotFound = -1;	// VC++ is a bit medieval here...
		enum { kChunkNotFound = -1 };
		long					FindChunk		(Tag tag);	// Returns chunk size

		Bool					ReadChunk		(int index, Tag& tag, Chunk&);
		Bool					ReadChunk		(Tag tag, Chunk&);
		void					ReadChunk		(uint32 size, void* data);
		Bool					ReadInt			(Tag tag, uint8&);
		Bool					ReadInt			(Tag tag, int8&);
		Bool					ReadInt			(Tag tag, uint16&);
		Bool					ReadInt			(Tag tag, int16&);
		Bool					ReadInt			(Tag tag, uint32&);
		Bool					ReadInt			(Tag tag, int32&);
		Bool					ReadString		(Tag tag, char*);
		Bool					ReadString		(Tag tag, string&);

		void					WriteChunk		(Tag tag, const Chunk&);
		void					WriteChunk		(Tag tag, uint32 size, const void* data);
		void					WriteInt		(Tag tag, uint8);
		void					WriteInt		(Tag tag, int8);
		void					WriteInt		(Tag tag, uint16);
		void					WriteInt		(Tag tag, int16);
		void					WriteInt		(Tag tag, uint32);
		void					WriteInt		(Tag tag, int32);
		void					WriteString		(Tag tag, const char*);
		void					WriteString		(Tag tag, const string&);

		EmStream&				GetStream		(void) const;

	private:
		EmStream&				fStream;
};


class Chunk
{
	public:
								Chunk			(void);
								Chunk			(long inLength);
								Chunk			(const Chunk&);
								~Chunk			(void);

		Chunk&					operator=		(const Chunk&);

		void*					GetPointer		(void) const;
		long					GetLength		(void) const;
		void					SetLength		(long inLength);

	private:
		void*					fPtr;
		long					fUsedSize;
		long					fAllocatedSize;
};


class EmStreamChunk : public EmStream
{
	public:
								EmStreamChunk	(Chunk*);
								EmStreamChunk	(Chunk&);
		virtual					~EmStreamChunk	(void);

		virtual void			SetLength		(int32			inLength);

		virtual ErrCode			PutBytes		(const void*	inBuffer,
												 int32			ioByteCount);
		virtual ErrCode			GetBytes		(void*			outBuffer,
												 int32			ioByteCount);

	private:
		Chunk&					fChunk;
		Chunk*					fOwnedChunk;
};


#endif	// _CHUNKFILE_H_