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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
|
/**
* vim: set ts=4 :
* =============================================================================
* SourceMod (C)2004-2008 AlliedModders LLC. All rights reserved.
* =============================================================================
*
* This file is part of the SourceMod/SourcePawn SDK.
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, version 3.0, as published by the
* Free Software Foundation.
*
* 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, see <http://www.gnu.org/licenses/>.
*
* As a special exception, AlliedModders LLC gives you permission to link the
* code of this program (as well as its derivative works) to "Half-Life 2," the
* "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
* by the Valve Corporation. You must obey the GNU General Public License in
* all respects for all other code used. Additionally, AlliedModders LLC grants
* this exception to all derivative works. AlliedModders LLC defines further
* exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
* or <http://www.sourcemod.net/license.php>.
*
* Version: $Id$
*/
#if defined _files_included
#endinput
#endif
#define _files_included
/**
* @global All paths in SourceMod natives are relative to the mod folder
* unless otherwise noted.
*
* Most functions in SourceMod (at least, ones that deal with direct
* file manipulation) will support an alternate path specification.
*
* If the path starts with the string "file://" and the PathType is
* not relative, then the "file://" portion is stripped off, and the
* rest of the path is used without any modification (except for
* correcting slashes). This can be used to override the path
* builder to supply alternate absolute paths. Examples:
*
* file://C:/Temp/file.txt
* file:///tmp/file.txt
*/
/**
* File inode types.
*/
enum FileType
{
FileType_Unknown = 0, /* Unknown file type (device/socket) */
FileType_Directory = 1, /* File is a directory */
FileType_File = 2, /* File is a file */
};
/**
* File time modes.
*/
enum FileTimeMode
{
FileTime_LastAccess = 0, /* Last access (does not work on FAT) */
FileTime_Created = 1, /* Creation (does not work on FAT) */
FileTime_LastChange = 2, /* Last modification */
};
#define PLATFORM_MAX_PATH 256 /**< Maximum path length. */
#define SEEK_SET 0 /**< Seek from start. */
#define SEEK_CUR 1 /**< Seek from current position. */
#define SEEK_END 2 /**< Seek from end position. */
/**
* Path types.
*/
enum PathType
{
Path_SM, /**< SourceMod root folder */
};
/**
* Builds a path relative to the SourceMod folder. This should be used instead of
* directly referencing addons/sourcemod, in case users change the name of their
* folder layout.
*
* @param type Type of path to build as the base.
* @param buffer Buffer to store the path.
* @param maxlength Maximum length of buffer.
* @param fmt Format string.
* @param ... Format arguments.
* @return Number of bytes written to buffer (not including null terminator).
*/
native BuildPath(PathType:type, String:buffer[], maxlength, const String:fmt[], any:...);
/**
* Opens a directory/folder for contents enumeration.
*
* @note Directories are closed with CloseHandle().
* @note Directories Handles can be cloned.
* @note OpenDirectory() supports the "file://" notation.
*
* @param path Path to open.
* @return A Handle to the directory, INVALID_HANDLE on open error.
*/
native Handle:OpenDirectory(const String:path[]);
/**
* Reads the current directory entry as a local filename, then moves to the next file.
*
* @note Contents of buffers are undefined when returning false.
* @note Both the '.' and '..' automatic directory entries will be retrieved for Windows and Linux.
*
* @param dir Handle to a directory.
* @param buffer String buffer to hold directory name.
* @param maxlength Maximum size of string buffer.
* @param type Optional variable to store the file type.
* @return True on success, false if there are no more files to read.
* @error Invalid or corrupt Handle.
*/
native bool:ReadDirEntry(Handle:dir, String:buffer[], maxlength, &FileType:type=FileType_Unknown);
/**
* Opens a file.
*
* @note Files are closed with CloseHandle().
* @note File Handles can be cloned.
* @note OpenFile() supports the "file://" notation.
*
* @param file File to open.
* @param mode Open mode.
* @return A Handle to the file, INVALID_HANDLE on open error.
*/
native Handle:OpenFile(const String:file[], const String:mode[]);
/**
* Deletes a file.
*
* @param path Path of the file to delete.
* @return True on success, false otherwise.
*/
native bool:DeleteFile(const String:path[]);
/**
* Reads a line from a text file.
*
* @param hndl Handle to the file.
* @param buffer String buffer to hold the line.
* @param maxlength Maximum size of string buffer.
* @return True on success, false otherwise.
*/
native bool:ReadFileLine(Handle:hndl, String:buffer[], maxlength);
/**
* Reads binary data from a file.
*
* @param hndl Handle to the file.
* @param items Array to store each item read.
* @param num_items Number of items to read into the array.
* @param size Size of each element, in bytes, to be read.
* Valid sizes are 1, 2, or 4.
* @return Number of elements read, or -1 on error.
*/
native ReadFile(Handle:hndl, items[], num_items, size);
/**
* Reads a UTF8 or ANSI string from a file.
*
* @param hndl Handle to the file.
* @param buffer Buffer to store the string.
* @param max_size Maximum size of the string buffer.
* @param read_count If -1, reads until a null terminator is encountered in
* the file. Otherwise, read_count bytes are read
* into the buffer provided. In this case the buffer
* is not explicitly null terminated, and the buffer
* will contain any null terminators read from the file.
* @return Number of characters written to the buffer, or -1
* if an error was encountered.
* @error Invalid Handle, or read_count > max_size.
*/
native ReadFileString(Handle:hndl, String:buffer[], max_size, read_count=-1);
/**
* Writes binary data to a file.
*
* @param hndl Handle to the file.
* @param items Array of items to write. The data is read directly.
* That is, in 1 or 2-byte mode, the lower byte(s) in
* each cell are used directly, rather than performing
* any casts from a 4-byte number to a smaller number.
* @param num_items Number of items in the array.
* @param size Size of each item in the array in bytes.
* Valid sizes are 1, 2, or 4.
* @return True on success, false on error.
* @error Invalid Handle.
*/
native bool:WriteFile(Handle:hndl, const items[], num_items, size);
/**
* Writes a binary string to a file.
*
* @param hndl Handle to th efile.
* @param buffer String to write.
* @param term True to append NUL terminator, false otherwise.
* @return True on success, false on error.
* @error Invalid Handle.
*/
native bool:WriteFileString(Handle:hndl, const String:buffer[], bool:term);
/**
* Writes a line of text to a text file. A newline is automatically appended.
*
* @param hndl Handle to the file.
* @param format Formatting rules.
* @param ... Variable number of format parameters.
* @return True on success, false otherwise.
* @error Invalid Handle.
*/
native bool:WriteFileLine(Handle:hndl, const String:format[], any:...);
/**
* Reads a single binary cell from a file.
*
* @param hndl Handle to the file.
* @param data Variable to store the data read.
* @param size Size of the data to read in bytes. Valid
* sizes are 1, 2, or 4 bytes.
* @return Number of elements read (max 1), or -1 on error.
* @error Invalid Handle.
*/
stock ReadFileCell(Handle:hndl, &data, size)
{
new array[1], ret;
if ((ret = ReadFile(hndl, array, 1, size)) == 1)
{
data = array[0];
}
return ret;
}
/**
* Writes a single binary cell to a file.
*
* @param hndl Handle to the file.
* @param data Cell to write to the file.
* @param size Size of the data to read in bytes. Valid
* sizes are 1, 2, or 4 bytes. If the size
* is less than 4 bytes, the data is truncated
* rather than casted. That is, only the lower
* bits will be read.
* @return True on success, false on error.
* @error Invalid Handle.
*/
stock bool:WriteFileCell(Handle:hndl, data, size)
{
new array[1];
array[0] = data;
return WriteFile(hndl, array, 1, size);
}
/**
* Tests if the end of file has been reached.
*
* @param file Handle to the file.
* @return True if end of file has been reached, false otherwise.
* @error Invalid Handle.
*/
native bool:IsEndOfFile(Handle:file);
/**
* Sets the file position indicator.
*
* @param file Handle to the file.
* @param position Position relative to what is specified in whence.
* @param where SEEK_ constant value of where to see from.
* @return True on success, false otherwise.
* @error Invalid Handle.
*/
native bool:FileSeek(Handle:file, position, where);
/**
* Get current position in the file.
*
* @param file Handle to the file.
* @return Value for the file position indicator.
* @error Invalid Handle.
*/
native FilePosition(Handle:file);
/**
* Checks if a file exists.
*
* @param path Path to the file.
* @param use_valve_fs If true, the Valve file system will be used instead.
* This can be used to find files existing in any of
* the GAME search paths, rather than solely files
* existing directly in the gamedir.
* @return True if the file exists, false otherwise.
*/
native bool:FileExists(const String:path[], bool:use_valve_fs=false);
/**
* Renames a file.
*
* @param newpath New path to the file.
* @param oldpath Path to the existing file.
* @return True on success, false otherwise.
*/
native bool:RenameFile(const String:newpath[], const String:oldpath[]);
/**
* Checks if a directory exists.
*
* @param path Path to the directory.
* @return True if the directory exists, false otherwise.
*/
native bool:DirExists(const String:path[]);
/**
* Get the file size in bytes.
*
* @param path Path to the file.
* @param use_valve_fs If true, the Valve file system will be used instead.
* This can be used to find files existing in any of
* the GAME search paths, rather than solely files
* existing directly in the gamedir.
* @return File size in bytes, -1 if file not found.
*/
native FileSize(const String:path[], bool:use_valve_fs=false);
/**
* Flushes a file's buffered output; any buffered output
* is immediately written to the file.
*
* @param file Handle to the file.
* @return True on success, false on failure.
*/
native FlushFile(Handle:file);
/**
* Removes a directory.
* @note On most Operating Systems you cannot remove a directory which has files inside it.
*
* @param path Path to the directory.
* @return True on success, false otherwise.
*/
native bool:RemoveDir(const String:path[]);
#define FPERM_U_READ 0x0100 /* User can read. */
#define FPERM_U_WRITE 0x0080 /* User can write. */
#define FPERM_U_EXEC 0x0040 /* User can exec. */
#define FPERM_G_READ 0x0020 /* Group can read. */
#define FPERM_G_WRITE 0x0010 /* Group can write. */
#define FPERM_G_EXEC 0x0008 /* Group can exec. */
#define FPERM_O_READ 0x0004 /* Anyone can read. */
#define FPERM_O_WRITE 0x0002 /* Anyone can write. */
#define FPERM_O_EXEC 0x0001 /* Anyone can exec. */
/**
* Creates a directory.
*
* @param path Path to create.
* @param mode Permissions (default is o=rx,g=rx,u=rwx). Note that folders must have
* the execute bit set on Linux. On Windows, the mode is ignored.
*/
native bool:CreateDirectory(const String:path[], mode);
/**
* Returns a file timestamp as a unix timestamp.
*
* @param file File name.
* @param tmode Time mode.
* @return Time value, or -1 on failure.
*/
native GetFileTime(const String:file[], FileTimeMode:tmode);
/**
* Same as LogToFile(), except uses an open file Handle. The file must
* be opened in text appending mode.
*
* @param hndl Handle to the file.
* @param message Message format.
* @param ... Message format parameters.
* @noreturn
* @error Invalid Handle.
*/
native LogToOpenFile(Handle:hndl, const String:message[], any:...);
/**
* Same as LogToFileEx(), except uses an open file Handle. The file must
* be opened in text appending mode.
*
* @param hndl Handle to the file.
* @param message Message format.
* @param ... Message format parameters.
* @noreturn
* @error Invalid Handle.
*/
native LogToOpenFileEx(Handle:hndl, const String:message[], any:...);
|