File: ChunkFile.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 (118 lines) | stat: -rw-r--r-- 3,305 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
/* -*- mode: C++; tab-width: 4 -*- */
/* ================================================================================== */
/* Copyright (c) 1998-1999 3Com Corporation or its subsidiaries. All rights reserved. */
/* ================================================================================== */

#ifndef _CHUNKFILE_H_
#define _CHUNKFILE_H_

#include "PPStream.h"


class StreamHandle;
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		(StreamHandle& s);
								~ChunkFile		(void);

		typedef uae_u32	Tag;

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

		Bool					ReadChunk		(Tag tag, Chunk&);
		void					ReadChunk		(uae_u32 size, void* data);
		Bool					ReadInt			(Tag tag, uae_u8&);
		Bool					ReadInt			(Tag tag, uae_s8&);
		Bool					ReadInt			(Tag tag, uae_u16&);
		Bool					ReadInt			(Tag tag, uae_s16&);
		Bool					ReadInt			(Tag tag, uae_u32&);
		Bool					ReadInt			(Tag tag, uae_s32&);
		Bool					ReadString		(Tag tag, char*);
		Bool					ReadString		(Tag tag, string&);

		void					WriteChunk		(Tag tag, const Chunk&);
		void					WriteChunk		(Tag tag, uae_u32 size, const void* data);
		void					WriteInt		(Tag tag, uae_u8);
		void					WriteInt		(Tag tag, uae_s8);
		void					WriteInt		(Tag tag, uae_u16);
		void					WriteInt		(Tag tag, uae_s16);
		void					WriteInt		(Tag tag, uae_u32);
		void					WriteInt		(Tag tag, uae_s32);
		void					WriteString		(Tag tag, const char*);
		void					WriteString		(Tag tag, const string&);

		StreamHandle&			GetStream		(void) const;

	private:
		StreamHandle&			fStream;
};


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

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

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

class ChunkStream : public PPStream
{
	public:
								ChunkStream		(Chunk&);
		virtual					~ChunkStream	(void);

		virtual void			SetLength		(uae_s32 inLength);

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

	private:
		Chunk&					fChunk;
};


#endif	// _CHUNKFILE_H_