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