aboutsummaryrefslogtreecommitdiff
path: root/engines/glk/archetype/misc.cpp
blob: b0e8b41eb200974c0f53d3f8118074eb283b13ab (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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/* 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.
 *
 */

#include "glk/archetype/misc.h"
#include "glk/archetype/archetype.h"
#include "glk/quetzal.h"
#include "common/debug-channels.h"

namespace Glk {
namespace Archetype {

const char *const VERSION_STUB = "Archetype version ";
const double VERSION_NUM = 1.02;

size_t Bytes;
bool KeepLooking;
bool AllErrors;

void *Prior, *NextExit;

void misc_init() {
	//NextExit = ExitProc;
	//ExitProc = @exit_prog;
	//HeapError = @HeapFunc;
	//Mark(Prior)
	Bytes = 0;
	KeepLooking = true;
	AllErrors = false;
}

/*----------------------------------------------------------------------*/

bool progfile::open(const String &name) {
	filename = name;

	if (!_file.open(name)) {
		return false;

	} else {
		file_line = 0;
		line_buffer = "";
		line_pos = 0;
		newlines = false;
		consumed = true;
		last_ch = NULL_CH;

		return true;
	}
}

void progfile::close() {
	_file.close();
}

bool progfile::readChar(char &ch) {
	if (last_ch != NULL_CH) {
		ch = last_ch;
		last_ch = NULL_CH;
	} else {
		++line_pos;
		while (line_pos >= (int)line_buffer.size()) {
			if (_file.eos()) {
				ch = NULL_CH;
				return false;
			}

			line_buffer = QuetzalReader::readString(&_file);
			line_buffer += NEWLINE_CH;
			++file_line;
			line_pos = 0;
		}

		ch = line_buffer[line_pos];
	}

	return true;
}

void progfile::unreadChar(char ch) {
	last_ch = ch;
}

void progfile::sourcePos() {
	/* With the /A switch specified, multiple source_pos messages can be called,
	 * so long as there is no fatal syntax error.Otherwise, the first error
	 * of any kind, regardless of severity, is the only error printed.This is
	 * done as a courtesy to those of us without scrolling DOS windows
	 */
	if (KeepLooking) {
		if (!AllErrors)
			KeepLooking = false;

		g_vm->writeln("Error in %s at line %d", filename.c_str(), file_line);
		g_vm->writeln(line_buffer);

		String s;
		for (int i = 0; i < line_pos; ++i)
			s += ' ';
		s += '^';
		g_vm->writeln(s);
	}
}

/*----------------------------------------------------------------------*/

void add_bytes(int delta) {
	Bytes += delta;

	if (DebugMan.isDebugChannelEnabled(DEBUG_BYTES)) {
		String line = (delta >= 0) ? "Allocated   " : "Deallocated ";
		line += String::format("%.3u bytes.  Current consumed memory: %.6u", (uint)ABS(delta), (uint)Bytes);
		debug("%s", line.c_str());
	}
}

String formatFilename(const String &name, const String &ext, bool replace) {
	String s;
	int period = 0;
	bool noExt;

	// Check for a period for an extension
	period = name.lastIndexOf('.');
	noExt = period == -1;

	if (replace || noExt) {
		return name + "." + ext;
	} else {
		return String(name.c_str(), name.c_str() + period + 1) + ext;
	}
}

void load_string(Common::ReadStream *fIn, String &the_string) {
	char buffer[257];
	size_t strSize = fIn->readByte();
	size_t strSize2 = fIn->readByte();		// Redundant second copy of the length
	assert(strSize2 == strSize);

	fIn->read(buffer, strSize);
	buffer[strSize] = '\0';

	cryptstr(buffer, strSize);
	the_string = String(buffer);
}

void dump_string(Common::WriteStream *fOut, const String &the_string) {
	assert(the_string.size() < 256);
	fOut->writeByte(the_string.size());
	fOut->writeByte(the_string.size());

	if (Encryption == NONE) {
		fOut->write(the_string.c_str(), the_string.size());

	} else {
		char buffer[257];
		strncpy(buffer, the_string.c_str(), 256);
		buffer[256] = '\0';

		cryptstr(buffer, the_string.size());
		fOut->write(buffer, the_string.size());
	}
}

StringPtr NewConstStr(const String &s) {
	return new String(s);
}

void FreeConstStr(StringPtr &sp) {
	delete sp;
}

StringPtr NewDynStr(const String &s) {
	return new String(s);
}

void FreeDynStr(StringPtr &sp) {
	delete sp;
}

} // End of namespace Archetype
} // End of namespace Glk