File: cfile.cpp

package info (click to toggle)
freespace2 24.0.2%2Brepack-1
  • links: PTS, VCS
  • area: non-free
  • in suites: trixie
  • size: 43,188 kB
  • sloc: cpp: 583,107; ansic: 21,729; python: 1,174; sh: 464; makefile: 248; xml: 181
file content (166 lines) | stat: -rw-r--r-- 5,788 bytes parent folder | download | duplicates (4)
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
//
//

#include "cfile.h"
#include "cfile/cfile.h"
#include "cfile/cfilesystem.h"
#include "scripting/api/objs/file.h"
#include "scripting/lua/LuaTable.h"

namespace scripting {
namespace api {

//**********LIBRARY: CFILE
//WMC - It's on my to-do list! (Well, if I had one anyway)
//WMC - Did it. I had to invent a to-do list first, though.
//Ironically, I never actually put this on it.
ADE_LIB(l_CFile, "CFile", "cf", "CFile FS2 filesystem access");

ADE_FUNC(deleteFile, l_CFile, "string Filename, string Path", "Deletes given file. Path must be specified. Use a slash for the root directory.", "boolean", "True if deleted, false")
{
	const char* n_filename = nullptr;
	const char *n_path = "";
	if(!ade_get_args(L, "ss", &n_filename, &n_path))
		return ade_set_error(L, "b", false);

	int path = CF_TYPE_INVALID;
	if(n_path != NULL && strlen(n_path))
		path = cfile_get_path_type(n_path);

	if(path == CF_TYPE_INVALID)
		return ade_set_error(L, "b", false);

	return ade_set_args(L, "b", cf_delete(n_filename, path) != 0);
}

ADE_FUNC(fileExists, l_CFile, "string Filename, [string Path = \"\", boolean CheckVPs = false]", "Checks if a file exists. Use a blank string for path for any directory, or a slash for the root directory.", "boolean", "True if file exists, false or nil otherwise")
{
	const char* n_filename = nullptr;
	const char *n_path = "";
	bool check_vps = false;
	if(!ade_get_args(L, "s|sb", &n_filename, &n_path, &check_vps))
		return ADE_RETURN_NIL;

	int path = CF_TYPE_ANY;
	if(n_path != NULL && strlen(n_path))
		path = cfile_get_path_type(n_path);

	if(path == CF_TYPE_INVALID)
		return ade_set_error(L, "b", false);

	if(!check_vps)
		return ade_set_args(L, "b", cf_exists(n_filename, path) != 0);
	else
		return ade_set_args(L, "b", cf_exists_full(n_filename, path ) != 0);
}

ADE_FUNC(listFiles, l_CFile, "string directory, string filter",
         "Lists all the files in the specified directory matching a filter. The filter must have the format "
         "\"*<rest>\" (the wildcard has to appear at the start), \"<subfolder>/*<rest>\" (to check subfolder(s)) "
         "or \"*/*<rest>\" (for a glob search).",
         "string[]", "A table with all matching files or nil on error")
{
	using namespace luacpp;

	const char* dir;
	const char* filter;
	if (!ade_get_args(L, "ss", &dir, &filter)) {
		return ADE_RETURN_NIL;
	}

	SCP_string filter_str = filter;
	if (filter_str.empty()) {
		LuaError(L, "The filter \"%s\" is not valid! It may not be empty.", filter);
		return ADE_RETURN_NIL;
	}

	// allow subpath in filter, but it can't be the first part of the filter
	bool has_subpath = ((filter_str.find("/*") % SCP_string::npos) > 0);
	if ((filter_str[0] != '*') && !has_subpath) {
		LuaError(L, "The filter \"%s\" is not valid! The first character must be a '*', or it must follow a path.", filter);
		return ADE_RETURN_NIL;
	}

	SCP_string ext; // Extension with '.' if it exists
	auto dot_pos = filter_str.find('.');
	if (dot_pos != SCP_string::npos) {
		ext = filter_str.substr(dot_pos);
	}

	auto path_type = cfile_get_path_type(dir);
	if (path_type == CF_TYPE_INVALID) {
		LuaError(L, "The directory \"%s\" is not valid!", dir);
		return ADE_RETURN_NIL;
	}

	SCP_vector<SCP_string> files;
	cf_get_file_list(files, path_type, filter, CF_SORT_NAME);

	LuaTable table = LuaTable::create(L);
	for (size_t i = 0; i < files.size(); ++i) {
		// Add the extension since cf_get_file_list removes it. We use the filter without the preceeding '*' here
		table.addValue(i + 1, files[i] + ext);
	}

	return ade_set_args(L, "t", &table);
}

ADE_FUNC(openFile, l_CFile, "string Filename, [string Mode=\"r\", string Path = \"\"]",
		 "Opens a file. 'Mode' uses standard C fopen arguments. In read mode use a blank string for path for any directory, "
			"or a slash for the root directory. When using write mode a valid path must be specified. "
			"Be EXTREMELY CAREFUL when using this function, as you may PERMANENTLY delete any file by accident",
		 "file",
		 "File handle, or invalid file handle if the specified file couldn't be opened")
{
	const char* n_filename = nullptr;
	const char *n_mode = "r";
	const char *n_path = "";
	if(!ade_get_args(L, "s|ss", &n_filename, &n_mode, &n_path))
		return ade_set_error(L, "o", l_File.Set(cfile_h()));

	int type = CFILE_NORMAL;

	int path = CF_TYPE_ANY;
	if(n_path != NULL && strlen(n_path))
		path = cfile_get_path_type(n_path);

	if(path == CF_TYPE_INVALID)
		return ade_set_error(L, "o", l_File.Set(cfile_h()));

	if((path == CF_TYPE_ANY) && (strchr(n_mode,'w') || strchr(n_mode,'+') || strchr(n_mode,'a')))
		return ade_set_error(L, "o", l_File.Set(cfile_h()));

	CFILE *cfp = cfopen(n_filename, n_mode, type, path);

	if(!cf_is_valid(cfp))
		return ade_set_error(L, "o", l_File.Set(cfile_h()));

	return ade_set_args(L, "o", l_File.Set(cfile_h(cfp)));
}

ADE_FUNC(openTempFile, l_CFile, NULL, "Opens a temp file that is automatically deleted when closed", "file", "File handle, or invalid file handle if tempfile couldn't be created")
{
	return ade_set_args(L, "o", l_File.Set(cfile_h(ctmpfile())));
}

ADE_FUNC(renameFile, l_CFile, "string CurrentFilename, string NewFilename, string Path", "Renames given file. Path must be specified. Use a slash for the root directory.", "boolean", "True if file was renamed, otherwise false")
{
	const char* n_filename     = nullptr;
	const char* n_new_filename = nullptr;
	const char *n_path = "";
	if(!ade_get_args(L, "ss|s", &n_filename, &n_new_filename, &n_path))
		return ade_set_error(L, "b", false);

	int path = CF_TYPE_INVALID;
	if(n_path != NULL && strlen(n_path))
		path = cfile_get_path_type(n_path);

	if(path == CF_TYPE_INVALID)
		return ade_set_error(L, "b", false);

	return ade_set_args(L, "b", cf_rename(n_filename, n_new_filename, path) != 0);
}

}
}