File: file.cpp

package info (click to toggle)
freespace2 24.2.0%2Brepack-3
  • links: PTS, VCS
  • area: non-free
  • in suites: forky, sid
  • size: 43,740 kB
  • sloc: cpp: 595,005; ansic: 21,741; python: 1,174; sh: 457; makefile: 243; xml: 181
file content (347 lines) | stat: -rw-r--r-- 9,266 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
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347

#include "file.h"

#include "bytearray.h"

#include "cfile/cfilesystem.h"

extern int cfread_lua_number(double* buf, CFILE* cfile);
namespace scripting {
namespace api {

cfile_h::cfile_h(CFILE* cfp) : _cfp(cfp) {}
cfile_h::~cfile_h()
{
	if (_cfp && cf_is_valid(_cfp.get())) {
		Warning(LOCATION, "Lua cfile handle of file %s has been left open!\nYou should close the file with the object's close() method explicitly.\n", cf_get_filename(_cfp.get()));
	}
}
bool cfile_h::isValid() const { return _cfp != nullptr; }
CFILE* cfile_h::get() const { return _cfp.get(); }
void cfile_h::close()
{
	_cfp = nullptr; // Automatically closes the handle
}

//**********HANDLE: File
ADE_OBJ(l_File, cfile_h, "file", "File handle");

ADE_FUNC(isValid, l_File, NULL, "Detects whether handle is valid", "boolean", "true if valid, false if handle is invalid, nil if a syntax/type error occurs")
{
	cfile_h* cfp = nullptr;
	if (!ade_get_args(L, "o", l_File.GetPtr(&cfp)))
		return ADE_RETURN_NIL;

	if (cfp == nullptr || !cfp->isValid())
		return ADE_RETURN_FALSE;

	return ADE_RETURN_TRUE;
}

ADE_FUNC(close, l_File, NULL, "Instantly closes file and invalidates all file handles", NULL, NULL)
{
	cfile_h* cfp = nullptr;
	if (!ade_get_args(L, "o", l_File.GetPtr(&cfp)))
		return ADE_RETURN_FALSE;

	if (cfp == nullptr || !cfp->isValid())
		return ADE_RETURN_FALSE;

	cfp->close();
	return ADE_RETURN_TRUE;
}

ADE_FUNC(flush, l_File, NULL, "Flushes file buffer to disk.", "boolean", "True for success, false on failure")
{
	cfile_h* cfp = nullptr;
	if (!ade_get_args(L, "o", l_File.GetPtr(&cfp)))
		return ADE_RETURN_FALSE;

	if (cfp == nullptr || !cfp->isValid())
		return ADE_RETURN_FALSE;

	//WMC - this looks reversed, yes, it's right. Look at cflush.
	int cf_result = cflush(cfp->get());
	return ade_set_args(L, "b", cf_result == 0);
}

ADE_FUNC(getName, l_File, nullptr, "Returns the name of the given file", "string", "Name of the file handle, or an empty string if it doesn't have one, or the handle is invalid")
{
	cfile_h* cfp = nullptr;
	if (!ade_get_args(L, "o", l_File.GetPtr(&cfp)))
		return ade_set_error(L, "s", "");

	if (cfp == nullptr || !cfp->isValid())
		return ade_set_error(L, "s", "");

	auto filename = cf_get_filename(cfp->get());
	if (filename != nullptr)
		return ade_set_args(L, "s", filename);
	else
		return ade_set_args(L, "s", "");
}

ADE_FUNC(getPath, l_File, nullptr, "Determines path of the given file", "string", "Path string of the file handle, or an empty string if it doesn't have one, or the handle is invalid")
{
	cfile_h* cfp = nullptr;
	if (!ade_get_args(L, "o", l_File.GetPtr(&cfp)))
		return ade_set_error(L, "s", "");

	if (cfp == nullptr || !cfp->isValid())
		return ade_set_error(L, "s", "");

	int id = cf_get_dir_type(cfp->get());
	if (id == CF_TYPE_ANY)
		return ade_set_args(L, "s", "<any path>");
	else if (Pathtypes[id].path != nullptr)
		return ade_set_args(L, "s", Pathtypes[id].path);
	else
		return ade_set_args(L, "s", "");
}

ADE_FUNC(read,
	l_File,
	"number|string, any...",
	"Reads part of or all of a file, depending on arguments passed. Based on basic Lua file:read function."
	"Returns nil when the end of the file is reached."
	"<br><ul><li>\"*n\" - Reads a number.</li>"
	"<li>\"*a\" - Reads the rest of the file and returns it as a string.</li>"
	"<li>\"*l\" - Reads a line. Skips the end of line markers.</li>"
	"<li>(number) - Reads given number of characters, then returns them as a string.</li></ul>",
	"number|string...",
	"Requested data, or nil if the function fails")
{
	cfile_h* cfp = nullptr;
	if (!ade_get_args(L, "o", l_File.GetPtr(&cfp)))
		return ADE_RETURN_NIL;

	if (cfp == nullptr || !cfp->isValid())
		return ADE_RETURN_NIL;

	int i;
	int num_returned = 0;
	int type = LUA_TNONE;

	//WMC - Since we push stuff onto the stack, we must get
	//the original arguments NOW.
	int lastarg = lua_gettop(L);
	for (i = 2; i <= lastarg; i++)
	{
		type = lua_type(L, i);
		const char* fmt = nullptr;
		//int num = 0;
		if (type == LUA_TSTRING)
		{
			fmt = lua_tostring_nullsafe(L, i);
			if (!stricmp(fmt, "*n"))
			{
				double d = 0.0f;
				if (cfread_lua_number(&d, cfp->get()) == EOF)
					return ADE_RETURN_NIL;

				lua_pushnumber(L, d);
				num_returned++;
			}
			else if (!stricmp(fmt, "*a"))
			{
				int tell_res = cftell(cfp->get());
				if (tell_res < 0)
				{
					Error(LOCATION, "Critical error reading Lua file; could not cftell.");
				}
				int read_len = cfilelength(cfp->get()) - tell_res;

				char *buf = (char *)vm_malloc(read_len + 1);
				int final_len = cfread(buf, 1, read_len, cfp->get());
				buf[final_len] = '\0';

				lua_pushstring(L, buf);
				vm_free(buf);
				num_returned++;
			}
			else if (!stricmp(fmt, "*l"))
			{
				char buf[10240];
				size_t idx;
				if (cfgets(buf, (int)(sizeof(buf) / sizeof(char)), cfp->get()) == nullptr)
				{
					lua_pushnil(L);
				}
				else
				{
					// Strip all newlines so this works like the Lua original
					// http://www.lua.org/source/5.1/liolib.c.html#g_read
					// Note: we also strip carriage return in WMC's implementation
					for (idx = 0; idx < strlen(buf); idx++)
					{
						if (buf[idx] == '\n' || buf[idx] == '\r')
							buf[idx] = '\0';
					}

					lua_pushstring(L, buf);
				}
				num_returned++;
			}
		}
		if (type == LUA_TNUMBER || (type == LUA_TSTRING && strpbrk(fmt, "1234567890")))
		{
			int num = 0;
			if (type == LUA_TSTRING)
				num = atoi(fmt);
			else
				num = fl2i(lua_tonumber(L, i));

			if (num < 1)
			{
				if (cfeof(cfp->get()))
					lua_pushstring(L, "");
				else
					lua_pushnil(L);
			}

			char *buf = (char*)vm_malloc(num + 1);
			int total_read = cfread(buf, 1, num, cfp->get());
			if (total_read)
			{
				buf[total_read] = '\0';
				lua_pushstring(L, buf);
			}
			else
			{
				lua_pushnil(L);
			}
			vm_free(buf);
			num_returned++;
		}
		if (type != LUA_TNUMBER && type != LUA_TSTRING)
			LuaError(L, "Invalid argument passed to file:read");
	}

	return num_returned;
}

ADE_FUNC(seek, l_File, "[string Whence=\"cur\", number Offset=0]",
	"Changes position of file, or gets location."
	"Whence can be:"
	"<li>\"set\" - File start.</li>"
	"<li>\"cur\" - Current position in file.</li>"
	"<li>\"end\" - File end.</li></ul>",
	"number",
	"new offset, or false or nil on failure")
{
	const char* w = nullptr;
	int o = 0;

	cfile_h* cfp = nullptr;
	if (!ade_get_args(L, "o|si", l_File.GetPtr(&cfp), &w, &o))
		return ADE_RETURN_NIL;

	if (cfp == nullptr || !cfp->isValid())
		return ADE_RETURN_NIL;

	if (!(w == NULL || (!stricmp(w, "cur") && o != 0)))
	{
		int seek_type = CF_SEEK_CUR;
		if (!stricmp(w, "set"))
			seek_type = CF_SEEK_SET;
		else if (!stricmp(w, "cur"))
			seek_type = CF_SEEK_CUR;
		else if (!stricmp(w, "end"))
			seek_type = CF_SEEK_END;
		else
			LuaError(L, "Invalid where argument passed to seek() - '%s'", w);

		if (cfseek(cfp->get(), o, seek_type))
			return ADE_RETURN_FALSE;
	}

	int res = cftell(cfp->get());
	if (res >= 0)
		return ade_set_args(L, "i", res);
	else
		return ADE_RETURN_FALSE;
}

ADE_FUNC(write, l_File, "string|number, any...",
	"Writes a series of Lua strings or numbers to the current file.", "number", "Number of items successfully written.")
{
	cfile_h* cfp = nullptr;
	if (!ade_get_args(L, "o", l_File.GetPtr(&cfp)))
		return ade_set_error(L, "i", 0);

	if (cfp == nullptr || !cfp->isValid())
		return ade_set_error(L, "i", 0);

	int l_pos = 2;
	int type = LUA_TNONE;

	int num_successful = 0;
	while ((type = lua_type(L, l_pos)) != LUA_TNONE)
	{
		if (type == LUA_TSTRING)
		{
			auto s = lua_tostring_nullsafe(L, l_pos);
			if (cfwrite(s, (int)sizeof(char), (int)strlen(s), cfp->get()))
				num_successful++;
		}
		else if (type == LUA_TNUMBER)
		{
			double d = lua_tonumber(L, l_pos);
			char buf[32] = { 0 };
			sprintf(buf, LUA_NUMBER_FMT, d);
			if (cfwrite(buf, (int)sizeof(char), (int)strlen(buf), cfp->get()))
				num_successful++;
		}

		l_pos++;
	}

	return ade_set_args(L, "i", num_successful);
}

ADE_FUNC(writeBytes,
	l_File,
	"bytearray bytes",
	"Writes the specified data to the file",
	"number",
	"Number of bytes successfully written.")
{
	cfile_h* cfp = nullptr;
	bytearray_h* array = nullptr;
	if (!ade_get_args(L, "oo", l_File.GetPtr(&cfp), l_Bytearray.GetPtr(&array)))
		return ade_set_error(L, "i", 0);

	if (cfp == nullptr || !cfp->isValid())
		return ade_set_error(L, "i", 0);

	auto written = cfwrite(array->data().data(), 1, static_cast<int>(array->data().size()), cfp->get());

	return ade_set_args(L, "i", written);
}

ADE_FUNC(readBytes,
	l_File,
	nullptr,
	"Reads the entire contents of the file as a byte array.<br><b>Warning:</b> This may change the position inside the "
	"file.",
	"bytearray",
	"The bytes read from the file or empty array on error")
{
	cfile_h* cfp = nullptr;
	if (!ade_get_args(L, "o", l_File.GetPtr(&cfp)))
		return ade_set_error(L, "o", l_Bytearray.Set(bytearray_h()));

	if (cfp == nullptr || !cfp->isValid())
		return ade_set_error(L, "o", l_Bytearray.Set(bytearray_h()));

	cfseek(cfp->get(), 0, CF_SEEK_SET);
	SCP_vector<uint8_t> data;
	data.resize(cfilelength(cfp->get()));

	cfread(data.data(), 1, cfilelength(cfp->get()), cfp->get());

	return ade_set_error(L, "o", l_Bytearray.Set(bytearray_h(std::move(data))));
}

} // namespace api
} // namespace scripting