plugins/include/files.inc
author David Anderson <dvander@alliedmods.net>
Sun Apr 13 06:06:27 2008 +0000 (2008-04-13)
changeset 1999 63aafc6ff212
parent 1952 b7aa2b626807
child 2289 44045ead61cd
permissions -rw-r--r--
added amb1538 - adt_stack
ds@1952
     1
/**
ds@1952
     2
 * vim: set ts=4 :
ds@1952
     3
 * =============================================================================
ds@1952
     4
 * SourceMod (C)2004-2008 AlliedModders LLC.  All rights reserved.
ds@1952
     5
 * =============================================================================
ds@1952
     6
 *
ds@1952
     7
 * This file is part of the SourceMod/SourcePawn SDK.
ds@1952
     8
 *
ds@1952
     9
 * This program is free software; you can redistribute it and/or modify it under
ds@1952
    10
 * the terms of the GNU General Public License, version 3.0, as published by the
ds@1952
    11
 * Free Software Foundation.
ds@1952
    12
 * 
ds@1952
    13
 * This program is distributed in the hope that it will be useful, but WITHOUT
ds@1952
    14
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
ds@1952
    15
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
ds@1952
    16
 * details.
ds@1952
    17
 *
ds@1952
    18
 * You should have received a copy of the GNU General Public License along with
ds@1952
    19
 * this program.  If not, see <http://www.gnu.org/licenses/>.
ds@1952
    20
 *
ds@1952
    21
 * As a special exception, AlliedModders LLC gives you permission to link the
ds@1952
    22
 * code of this program (as well as its derivative works) to "Half-Life 2," the
ds@1952
    23
 * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
ds@1952
    24
 * by the Valve Corporation.  You must obey the GNU General Public License in
ds@1952
    25
 * all respects for all other code used.  Additionally, AlliedModders LLC grants
ds@1952
    26
 * this exception to all derivative works.  AlliedModders LLC defines further
ds@1952
    27
 * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
ds@1952
    28
 * or <http://www.sourcemod.net/license.php>.
ds@1952
    29
 *
ds@1952
    30
 * Version: $Id$
ds@1952
    31
 */
ds@1952
    32
ds@1952
    33
#if defined _files_included
ds@1952
    34
 #endinput
ds@1952
    35
#endif
ds@1952
    36
#define _files_included
ds@1952
    37
ds@1952
    38
/**
ds@1952
    39
 * @global All paths in SourceMod natives are relative to the mod folder 
ds@1952
    40
 * unless otherwise noted.
ds@1952
    41
 *
ds@1952
    42
 * Most functions in SourceMod (at least, ones that deal with direct 
ds@1952
    43
 * file manipulation) will support an alternate path specification.
ds@1952
    44
 *
ds@1952
    45
 * If the path starts with the string "file://" and the PathType is 
ds@1952
    46
 * not relative, then the "file://" portion is stripped off, and the 
ds@1952
    47
 * rest of the path is used without any modification (except for 
ds@1952
    48
 * correcting slashes).  This can be used to override the path 
ds@1952
    49
 * builder to supply alternate absolute paths.  Examples:
ds@1952
    50
 *
ds@1952
    51
 * file://C:/Temp/file.txt
ds@1952
    52
 * file:///tmp/file.txt
ds@1952
    53
 */
ds@1952
    54
ds@1952
    55
/**
ds@1952
    56
 * File inode types.
ds@1952
    57
 */
ds@1952
    58
enum FileType
ds@1952
    59
{
ds@1952
    60
	FileType_Unknown = 0,	/* Unknown file type (device/socket) */
ds@1952
    61
	FileType_Directory = 1,	/* File is a directory */
ds@1952
    62
	FileType_File = 2,		/* File is a file */
ds@1952
    63
};
ds@1952
    64
ds@1952
    65
/**
ds@1952
    66
 * File time modes.
ds@1952
    67
 */
ds@1952
    68
enum FileTimeMode
ds@1952
    69
{
ds@1952
    70
	FileTime_LastAccess = 0,	/* Last access (does not work on FAT) */
ds@1952
    71
	FileTime_Created = 1,		/* Creation (does not work on FAT) */
ds@1952
    72
	FileTime_LastChange = 2,	/* Last modification */
ds@1952
    73
};
ds@1952
    74
ds@1952
    75
#define PLATFORM_MAX_PATH		256		/**< Maximum path length. */
ds@1952
    76
ds@1952
    77
#define SEEK_SET 0						/**< Seek from start. */
ds@1952
    78
#define SEEK_CUR 1						/**< Seek from current position. */
ds@1952
    79
#define SEEK_END 2						/**< Seek from end position. */
ds@1952
    80
ds@1952
    81
/**
ds@1952
    82
 * Path types.
ds@1952
    83
 */
ds@1952
    84
enum PathType
ds@1952
    85
{
ds@1952
    86
	Path_SM,		/**< SourceMod root folder */	
ds@1952
    87
};
ds@1952
    88
ds@1952
    89
/**
ds@1952
    90
 * Builds a path relative to the SourceMod folder.  This should be used instead of 
ds@1952
    91
 * directly referencing addons/sourcemod, in case users change the name of their 
ds@1952
    92
 * folder layout.
ds@1952
    93
 *
ds@1952
    94
 * @param type			Type of path to build as the base.
ds@1952
    95
 * @param buffer		Buffer to store the path.
ds@1952
    96
 * @param maxlength		Maximum length of buffer.
ds@1952
    97
 * @param fmt			Format string.
ds@1952
    98
 * @param ...			Format arguments.
ds@1952
    99
 * @return				Number of bytes written to buffer (not including null terminator).
ds@1952
   100
 */
ds@1952
   101
native BuildPath(PathType:type, String:buffer[], maxlength, const String:fmt[], any:...);
ds@1952
   102
ds@1952
   103
/**
ds@1952
   104
 * Opens a directory/folder for contents enumeration.
ds@1952
   105
 *
ds@1952
   106
 * @note Directories are closed with CloseHandle().
ds@1952
   107
 * @note Directories Handles can be cloned.
ds@1952
   108
 * @note OpenDirectory() supports the "file://" notation.
ds@1952
   109
 *
ds@1952
   110
 * @param path			Path to open.
ds@1952
   111
 * @return				A Handle to the directory, INVALID_HANDLE on open error.
ds@1952
   112
 */
ds@1952
   113
native Handle:OpenDirectory(const String:path[]);
ds@1952
   114
ds@1952
   115
/**
ds@1952
   116
 * Reads the current directory entry as a local filename, then moves to the next file.
ds@1952
   117
 *
ds@1952
   118
 * @note Contents of buffers are undefined when returning false.
ds@1952
   119
 * @note Both the '.' and '..' automatic directory entries will be retrieved for Windows and Linux.
ds@1952
   120
 * 
ds@1952
   121
 * @param dir			Handle to a directory.
ds@1952
   122
 * @param buffer		String buffer to hold directory name.
ds@1952
   123
 * @param maxlength		Maximum size of string buffer.
ds@1952
   124
 * @param type			Optional variable to store the file type.
ds@1952
   125
 * @return				True on success, false if there are no more files to read.
ds@1952
   126
 * @error				Invalid or corrupt Handle. 
ds@1952
   127
 */
ds@1952
   128
native bool:ReadDirEntry(Handle:dir, String:buffer[], maxlength, &FileType:type=FileType_Unknown);
ds@1952
   129
ds@1952
   130
/**
ds@1952
   131
 * Opens a file.
ds@1952
   132
 *
ds@1952
   133
 * @note Files are closed with CloseHandle().
ds@1952
   134
 * @note File Handles can be cloned.
ds@1952
   135
 * @note OpenFile() supports the "file://" notation.
ds@1952
   136
 *
ds@1952
   137
 * @param file			File to open.
ds@1952
   138
 * @param mode			Open mode.
ds@1952
   139
 * @return				A Handle to the file, INVALID_HANDLE on open error.
ds@1952
   140
 */
ds@1952
   141
native Handle:OpenFile(const String:file[], const String:mode[]);
ds@1952
   142
ds@1952
   143
/**
ds@1952
   144
 * Deletes a file.
ds@1952
   145
 *
ds@1952
   146
 * @param path			Path of the file to delete.
ds@1952
   147
 * @return				True on success, false otherwise.
ds@1952
   148
 */
ds@1952
   149
native bool:DeleteFile(const String:path[]);
ds@1952
   150
ds@1952
   151
/**
ds@1952
   152
 * Reads a line from a text file.
ds@1952
   153
 *
ds@1952
   154
 * @param hndl			Handle to the file.
ds@1952
   155
 * @param buffer		String buffer to hold the line.
ds@1952
   156
 * @param maxlength		Maximum size of string buffer.	
ds@1952
   157
 * @return				True on success, false otherwise.
ds@1952
   158
 */
ds@1952
   159
native bool:ReadFileLine(Handle:hndl, String:buffer[], maxlength);
ds@1952
   160
ds@1952
   161
/**
ds@1952
   162
 * Reads binary data from a file.
ds@1952
   163
 *
ds@1952
   164
 * @param hndl			Handle to the file.
ds@1952
   165
 * @param items			Array to store each item read.  
ds@1952
   166
 * @param num_items		Number of items to read into the array.
ds@1952
   167
 * @param size			Size of each element, in bytes, to be read.
ds@1952
   168
 *						Valid sizes are 1, 2, or 4.
ds@1952
   169
 * @return				Number of elements read, or -1 on error.
ds@1952
   170
 */
ds@1952
   171
native ReadFile(Handle:hndl, items[], num_items, size);
ds@1952
   172
ds@1952
   173
/**
ds@1952
   174
 * Reads a UTF8 or ANSI string from a file.
ds@1952
   175
 *
ds@1952
   176
 * @param hndl			Handle to the file.
ds@1952
   177
 * @param buffer		Buffer to store the string.
ds@1952
   178
 * @param max_size		Maximum size of the string buffer.
ds@1952
   179
 * @param stop			If true, reading will stop once max_size-1 bytes have 
ds@1952
   180
 *						been read.  If false, reading will stop once a NUL 
ds@1952
   181
 *						terminator is reached.  The buffer will simply be 
ds@1952
   182
 *						terminated in either case, the difference is in how 
ds@1952
   183
 *						the far the file position is changed.
ds@1952
   184
 * @return				Number of characters written to the buffer, or -1 
ds@1952
   185
 *						if an error was encountered.
ds@1952
   186
 * @error				Invalid Handle, or read_count > max_size.
ds@1952
   187
 */
ds@1952
   188
native ReadFileString(Handle:hndl, String:buffer[], max_size, read_count=-1);
ds@1952
   189
ds@1952
   190
/**
ds@1952
   191
 * Writes binary data to a file.
ds@1952
   192
 *
ds@1952
   193
 * @param hndl			Handle to the file.
ds@1952
   194
 * @param items			Array of items to write.  The data is read directly. 
ds@1952
   195
 *						That is, in 1 or 2-byte mode, the lower byte(s) in 
ds@1952
   196
 *						each cell are used directly, rather than performing 
ds@1952
   197
 *						any casts from a 4-byte number to a smaller number.
ds@1952
   198
 * @param num_items		Number of items in the array.
ds@1952
   199
 * @param size			Size of each item in the array in bytes.  
ds@1952
   200
 *						Valid sizes are 1, 2, or 4.
ds@1952
   201
 * @return				True on success, false on error.
ds@1952
   202
 * @error				Invalid Handle.
ds@1952
   203
 */
ds@1952
   204
native bool:WriteFile(Handle:hndl, const items[], num_items, size);
ds@1952
   205
ds@1952
   206
/**
ds@1952
   207
 * Writes a binary string to a file.
ds@1952
   208
 *
ds@1952
   209
 * @param hndl			Handle to th efile.
ds@1952
   210
 * @param buffer		String to write.
ds@1952
   211
 * @param term			True to append NUL terminator, false otherwise.
ds@1952
   212
 * @return				True on success, false on error.
ds@1952
   213
 * @error				Invalid Handle.
ds@1952
   214
 */
ds@1952
   215
native bool:WriteFileString(Handle:hndl, const String:buffer[], bool:term);
ds@1952
   216
ds@1952
   217
/**
ds@1952
   218
 * Writes a line of text to a text file.  A newline is automatically appended.
ds@1952
   219
 *
ds@1952
   220
 * @param hndl			Handle to the file.
ds@1952
   221
 * @param format		Formatting rules.
ds@1952
   222
 * @param ...			Variable number of format parameters.
ds@1952
   223
 * @return				True on success, false otherwise.
ds@1952
   224
 * @error				Invalid Handle.
ds@1952
   225
 */
ds@1952
   226
native bool:WriteFileLine(Handle:hndl, const String:format[], any:...);
ds@1952
   227
ds@1952
   228
/**
ds@1952
   229
 * Reads a single binary cell from a file.
ds@1952
   230
 *
ds@1952
   231
 * @param hndl			Handle to the file.
ds@1952
   232
 * @param data			Variable to store the data read.
ds@1952
   233
 * @param size			Size of the data to read in bytes.  Valid 
ds@1952
   234
 *						sizes are 1, 2, or 4 bytes.
ds@1952
   235
 * @return				Number of elements read (max 1), or -1 on error.
ds@1952
   236
 * @error				Invalid Handle.
ds@1952
   237
 */
ds@1952
   238
stock ReadFileCell(Handle:hndl, &data, size)
ds@1952
   239
{
ds@1952
   240
	new array[1], ret;
ds@1952
   241
	
ds@1952
   242
	if ((ret = ReadFile(hndl, array, 1, size)) == 1)
ds@1952
   243
	{
ds@1952
   244
		data = array[0];
ds@1952
   245
	}
ds@1952
   246
	
ds@1952
   247
	return ret;
ds@1952
   248
}
ds@1952
   249
ds@1952
   250
/**
ds@1952
   251
 * Writes a single binary cell to a file.
ds@1952
   252
 *
ds@1952
   253
 * @param hndl			Handle to the file.
ds@1952
   254
 * @param data			Cell to write to the file.
ds@1952
   255
 * @param size			Size of the data to read in bytes.  Valid 
ds@1952
   256
 *						sizes are 1, 2, or 4 bytes.  If the size 
ds@1952
   257
 *						is less than 4 bytes, the data is truncated 
ds@1952
   258
 *						rather than casted.  That is, only the lower 
ds@1952
   259
 *						bits will be read.
ds@1952
   260
 * @return				True on success, false on error.
ds@1952
   261
 * @error				Invalid Handle.
ds@1952
   262
 */
ds@1952
   263
stock bool:WriteFileCell(Handle:hndl, data, size)
ds@1952
   264
{
ds@1952
   265
	new array[1];
ds@1952
   266
	
ds@1952
   267
	array[0] = data;
ds@1952
   268
	
ds@1952
   269
	return WriteFile(hndl, array, 1, size);
ds@1952
   270
}
ds@1952
   271
 
ds@1952
   272
/**
ds@1952
   273
 * Tests if the end of file has been reached.
ds@1952
   274
 *
ds@1952
   275
 * @param file			Handle to the file.
ds@1952
   276
 * @return				True if end of file has been reached, false otherwise.
ds@1952
   277
 * @error				Invalid Handle.
ds@1952
   278
 */
ds@1952
   279
native bool:IsEndOfFile(Handle:file);
ds@1952
   280
ds@1952
   281
/**
ds@1952
   282
 * Sets the file position indicator.
ds@1952
   283
 *
ds@1952
   284
 * @param file			Handle to the file.
ds@1952
   285
 * @param position		Position relative to what is specified in whence.
ds@1952
   286
 * @param where			SEEK_ constant value of where to see from.
ds@1952
   287
 * @return				True on success, false otherwise.
ds@1952
   288
 * @error				Invalid Handle.
ds@1952
   289
 */
ds@1952
   290
native bool:FileSeek(Handle:file, position, where);
ds@1952
   291
ds@1952
   292
/**
ds@1952
   293
 * Get current position in the file.
ds@1952
   294
 *
ds@1952
   295
 * @param file			Handle to the file.
ds@1952
   296
 * @return				Value for the file position indicator.
ds@1952
   297
 * @error				Invalid Handle.
ds@1952
   298
 */
ds@1952
   299
native FilePosition(Handle:file);
ds@1952
   300
ds@1952
   301
/**
ds@1952
   302
 * Checks if a file exists.
ds@1952
   303
 *
ds@1952
   304
 * @param path			Path to the file.
ds@1952
   305
 * @param use_valve_fs	If true, the Valve file system will be used instead.
ds@1952
   306
 *						This can be used to check for the existance of files 
ds@1952
   307
 *						inside GCFs or the game cache, rather than solely files 
ds@1952
   308
 *						that are on disk.
ds@1952
   309
 * @return				True if the file exists, false otherwise.
ds@1952
   310
 */
ds@1952
   311
native bool:FileExists(const String:path[], bool:use_valve_fs=false);
ds@1952
   312
ds@1952
   313
/**
ds@1952
   314
 * Renames a file.
ds@1952
   315
 *
ds@1952
   316
 * @param newpath		New path to the file.
ds@1952
   317
 * @param oldpath		Path to the existing file.
ds@1952
   318
 * @return				True on success, false otherwise.
ds@1952
   319
 */
ds@1952
   320
native bool:RenameFile(const String:newpath[], const String:oldpath[]);
ds@1952
   321
ds@1952
   322
/**
ds@1952
   323
 * Checks if a directory exists.
ds@1952
   324
 *
ds@1952
   325
 * @param path			Path to the directory.
ds@1952
   326
 * @return				True if the directory exists, false otherwise.
ds@1952
   327
 */
ds@1952
   328
native bool:DirExists(const String:path[]);
ds@1952
   329
ds@1952
   330
/**
ds@1952
   331
 * Get the file size in bytes.
ds@1952
   332
 *
ds@1952
   333
 * @param path			Path to the file.
ds@1952
   334
 * @return				File size in bytes, -1 if file not found.
ds@1952
   335
 */
ds@1952
   336
native FileSize(const String:path[]);
ds@1952
   337
ds@1952
   338
/**
ds@1952
   339
 * Flushes a file's buffered output; any buffered output
ds@1952
   340
 * is immediately written to the file.
ds@1952
   341
 *
ds@1952
   342
 * @param file			Handle to the file.
ds@1952
   343
 * @return				True on success, false on failure.
ds@1952
   344
 */
ds@1952
   345
native FlushFile(Handle:file);
ds@1952
   346
ds@1952
   347
/**
ds@1952
   348
 * Removes a directory.
ds@1952
   349
 * @note On most Operating Systems you cannot remove a directory which has files inside it.
ds@1952
   350
 *
ds@1952
   351
 * @param path			Path to the directory.
ds@1952
   352
 * @return				True on success, false otherwise.
ds@1952
   353
 */
ds@1952
   354
native bool:RemoveDir(const String:path[]);
ds@1952
   355
ds@1952
   356
/**
ds@1952
   357
 * Returns a file timestamp as a unix timestamp.
ds@1952
   358
 *
ds@1952
   359
 * @param file			File name.
ds@1952
   360
 * @param tmode			Time mode.
ds@1952
   361
 * @return				Time value, or -1 on failure.
ds@1952
   362
 */
ds@1952
   363
native GetFileTime(const String:file[], FileTimeMode:tmode);
ds@1952
   364
ds@1952
   365
/**
ds@1952
   366
 * Same as LogToFile(), except uses an open file Handle.  The file must 
ds@1952
   367
 * be opened in text appending mode.
ds@1952
   368
 *
ds@1952
   369
 * @param hndl			Handle to the file.
ds@1952
   370
 * @param message		Message format.
ds@1952
   371
 * @param ...			Message format parameters.
ds@1952
   372
 * @noreturn
ds@1952
   373
 * @error				Invalid Handle.
ds@1952
   374
 */
ds@1952
   375
native LogToOpenFile(Handle:hndl, const String:message[], any:...);
ds@1952
   376
ds@1952
   377
/**
ds@1952
   378
 * Same as LogToFileEx(), except uses an open file Handle.  The file must 
ds@1952
   379
 * be opened in text appending mode.
ds@1952
   380
 *
ds@1952
   381
 * @param hndl			Handle to the file.
ds@1952
   382
 * @param message		Message format.
ds@1952
   383
 * @param ...			Message format parameters.
ds@1952
   384
 * @noreturn
ds@1952
   385
 * @error				Invalid Handle.
ds@1952
   386
 */
ds@1952
   387
native LogToOpenFileEx(Handle:hndl, const String:message[], any:...);
ds@1952
   388