aboutsummaryrefslogtreecommitdiff
path: root/engines/sci/engine/sys_strings.cpp
blob: f5ef4a8234475cf54942b84d861edf5711064b7f (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
/* ScummVM - Graphic Adventure Engine
 *
 * ScummVM is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the COPYRIGHT
 * file distributed with this source distribution.
 *
 * 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * $URL$
 * $Id$
 *
 */

#include "common/util.h"
#include "sci/include/sys_strings.h"
#include "sci/include/sci_memory.h"

void sys_string_acquire(sys_strings_t *strings, int index, const char *name, int max_len) {
	sys_string_t *str = strings->strings + index;

	if (index < 0 || index >= SYS_STRINGS_MAX) {
		error("[SYSSTR] Error: Attempt to acquire string #%d\n", index);
		BREAKPOINT();
	}

	if (str->name && (strcmp(name, str->name) || (str->max_size != max_len))) {
		error("[SYSSTR] Error: Attempt to re-acquire existing string #%d; was '%s', tried to claim as '%s'\n", index, str->name, name);
		BREAKPOINT();
	}

	str->name = strdup(name);
	str->max_size = max_len;
	str->value = (char*)sci_malloc(max_len + 1);
	str->value[0] = 0; // Set to empty string
}

int sys_string_set(sys_strings_t *strings, int index, const char *value) {
	sys_string_t *str = strings->strings + index;

	if (index < 0 || index >= SYS_STRINGS_MAX || !str->name) {
		error("[SYSSTR] Error: Attempt to write to invalid/unused string #%d\n", index);
		BREAKPOINT();
		return 1;
	}

	strncpy(str->value, value, str->max_size);
	str->value[str->max_size] = 0; // Make sure to terminate

	return 0;
}

void sys_string_free(sys_strings_t *strings, int index) {
	sys_string_t *str = strings->strings + index;

	free(str->name);
	str->name = NULL;

	free(str->value);
	str->value = NULL;

	str->max_size = 0;
}

void sys_string_free_all(sys_strings_t *strings) {
	int i;

	for (i = 0;i < SYS_STRINGS_MAX;i++) {
		if (strings->strings[i].name)
			sys_string_free(strings, i);
	}

}

void sys_strings_restore(sys_strings_t *new_strings, sys_strings_t *old_strings) {
	int i;

	// First, pad memory
	for (i = 0; i < SYS_STRINGS_MAX; i++) {
		sys_string_t *s = new_strings->strings + i;
		char *data = s->value;
		if (data) {
			s->value = (char *)sci_malloc(s->max_size + 1);
			strcpy(s->value, data);
			free(data);
		}
	}

	sys_string_set(new_strings, SYS_STRING_SAVEDIR, old_strings->strings[SYS_STRING_SAVEDIR].value);
}