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