aboutsummaryrefslogtreecommitdiff
path: root/backends/PalmOS/Src/saveslot.cpp
blob: 4cad8f2023d949c2f08eb2a1e91cec686f8f5ff9 (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
/* ScummVM - Scumm Interpreter
 * Copyright (C) 2001  Ludvig Strigeus
 * Copyright (C) 2001-2003 The ScummVM project
 *
 * 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.

 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * $Header$
 *
 */

//#include "extras.h"
#include <string.h>
#include "scummsys.h"
#include "saveload.h"

#define	MAX_BLOCK 64000

enum SaveLoadState {
	STATE_LOAD,
	STATE_SAVE,
	STATE_NONE
};

class SaveLoadData {
	public:
		SaveLoadData();

		SaveLoadState _currentState;
		byte * _readWriteData;
		uint32 _readWritePos;
		bool _needDump;
};

SaveLoadData current;

SaveLoadData::SaveLoadData()
{
	_currentState = STATE_NONE;
	_readWritePos = 0;
	_needDump = false;
}

bool SerializerStream::fopen(const char *filename, const char *mode)
{
	if (current._currentState != STATE_NONE)
		fclose();
		
	context = ::fopen(filename, mode);
	if (context != NULL) {
		current._currentState = ((strcmp(mode,"rb")==0) ? STATE_LOAD : STATE_SAVE);
		return true;
	}

	return false;
}

int SerializerStream::fread(void *buf, int size, int cnt)
{
	return ::fread(buf, size, cnt, (FILE *)context);
}

int SerializerStream::fwrite(void *buf, int size, int cnt) {

	int fullsize = size*cnt;

	if (current._currentState == STATE_SAVE && fullsize<=MAX_BLOCK)
	{
		if (!current._readWriteData)
			current._readWriteData = (byte *)malloc(MAX_BLOCK);

		if ((current._readWritePos+fullsize)>MAX_BLOCK) {
			::fwrite(current._readWriteData, current._readWritePos, 1, (FILE *)context);
			current._readWritePos = 0;
			current._needDump = false;
		}
			
		memcpy(current._readWriteData + current._readWritePos, buf, fullsize);
		current._readWritePos += fullsize;
		current._needDump = true;

		return cnt;
	}

	return ::fwrite(buf, size, cnt, (FILE *)context);
}


void SerializerStream::fclose()
{
	if (current._needDump && current._readWriteData != NULL) {
		if (current._currentState == STATE_SAVE)
			::fwrite(current._readWriteData, current._readWritePos, 1, (FILE *)context);

		free(current._readWriteData);
		current._readWriteData = NULL;
	}

	current._readWritePos = 0;
	current._needDump = false;
	current._currentState = STATE_NONE;
	::fclose((FILE *)context);
}