plugins/include/files.inc
author Borja Ferrer <faluco@alliedmods.net>
Sat Sep 15 19:38:02 2007 +0000 (2007-09-15)
changeset 1428 1ceceba15864
parent 1378 115ba755bcdf
child 1729 0f60cc4ce8bb
permissions -rw-r--r--
added amb852, create entity natives
     1 /**
     2  * vim: set ts=4 :
     3  * =============================================================================
     4  * SourceMod (C)2004-2007 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 /* @global All paths in SourceMod natives are relative to the mod folder unless otherwise noted. */
    39 
    40 /**
    41  * File inode types.
    42  */
    43 enum FileType
    44 {
    45 	FileType_Unknown = 0,	/* Unknown file type (device/socket) */
    46 	FileType_Directory = 1,	/* File is a directory */
    47 	FileType_File = 2,		/* File is a file */
    48 };
    49 
    50 /**
    51  * File time modes.
    52  */
    53 enum FileTimeMode
    54 {
    55 	FileTime_LastAccess = 0,	/* Last access (does not work on FAT) */
    56 	FileTime_Created = 1,		/* Creation (does not work on FAT) */
    57 	FileTime_LastChange = 2,	/* Last modification */
    58 };
    59 
    60 #define PLATFORM_MAX_PATH		256		/**< Maximum path length. */
    61 
    62 #define SEEK_SET 0						/**< Seek from start. */
    63 #define SEEK_CUR 1						/**< Seek from current position. */
    64 #define SEEK_END 2						/**< Seek from end position. */
    65 
    66 /**
    67  * Path types.
    68  */
    69 enum PathType
    70 {
    71 	Path_SM,		/**< SourceMod root folder */	
    72 };
    73 
    74 /**
    75  * Builds a path relative to the SourceMod folder.
    76  *
    77  * @param type			Type of path to build as the base.
    78  * @param buffer		Buffer to store the path.
    79  * @param maxlength		Maximum length of buffer.
    80  * @param fmt			Format string.
    81  * @param ...			Format arguments.
    82  * @return				Number of bytes written to buffer (not including null terminator).
    83  */
    84 native BuildPath(PathType:type, String:buffer[], maxlength, const String:fmt[], any:...);
    85 
    86 /**
    87  * Opens a directory/folder for contents enumeration.
    88  * @note Directories are closed with CloseHandle().
    89  * @note Directories Handles can be cloned.
    90  *
    91  * @param path			Path to open.
    92  * @return				A Handle to the directory, INVALID_HANDLE on open error.
    93  */
    94 native Handle:OpenDirectory(const String:path[]);
    95 
    96 /**
    97  * Reads the current directory entry as a local filename, then moves to the next file.
    98  * @note Contents of buffers are undefined when returning false.
    99  * @note Both the '.' and '..' automatic directory entries will be retrieved for Windows and Linux.
   100  * 
   101  * @param dir			Handle to a directory.
   102  * @param buffer		String buffer to hold directory name.
   103  * @param maxlength		Maximum size of string buffer.
   104  * @param type			Optional variable to store the file type.
   105  * @return				True on success, false if there are no more files to read.
   106  * @error				Invalid or corrupt Handle. 
   107  */
   108 native bool:ReadDirEntry(Handle:dir, String:buffer[], maxlength, &FileType:type=FileType_Unknown);
   109 
   110 /**
   111  * Opens a file.
   112  * @note Files are closed with CloseHandle().
   113  * @note File Handles can be cloned.
   114  *
   115  * @param file			File to open.
   116  * @param mode			Open mode.
   117  * @return				A Handle to the file, INVALID_HANDLE on open error.
   118  */
   119 native Handle:OpenFile(const String:file[], const String:mode[]);
   120 
   121 /**
   122  * Deletes a file.
   123  *
   124  * @param path			Path of the file to delete.
   125  * @return				True on success, false otherwise.
   126  */
   127 native bool:DeleteFile(const String:path[]);
   128 
   129 /**
   130  * Reads a line from a text file.
   131  *
   132  * @param hndl			Handle to the file.
   133  * @param buffer		String buffer to hold the line.
   134  * @param maxlength		Maximum size of string buffer.	
   135  * @return				True on success, false otherwise.
   136  */
   137 native bool:ReadFileLine(Handle:hndl, String:buffer[], maxlength);
   138 
   139 /**
   140  * Tests if the end of file has been reached.
   141  *
   142  * @param file			Handle to the file.
   143  * @return				True if end of file has been reached, false otherwise.
   144  */
   145 native bool:IsEndOfFile(Handle:file);
   146 
   147 /**
   148  * Sets the file position indicator.
   149  *
   150  * @param file			Handle to the file.
   151  * @param position		Position relative to what is specified in whence.
   152  * @param whence		Look at the SEEK_* definitions.
   153  * @return				True on success, false otherwise.
   154  */
   155 native bool:FileSeek(Handle:file, position, whence);
   156 
   157 /**
   158  * Get current position in the file.
   159  *
   160  * @param file			Handle to the file.
   161  * @return				Value for the file position indicator.
   162  */
   163 native FilePosition(Handle:file);
   164 
   165 /**
   166  * Checks if a file exists.
   167  *
   168  * @param path			Path to the file.
   169  * @return				True if the file exists, false otherwise.
   170  */
   171 native bool:FileExists(const String:path[]);
   172 
   173 /**
   174  * Renames a file.
   175  *
   176  * @param newpath		New path to the file.
   177  * @param oldpath		Path to the existing file.
   178  * @return				True on success, false otherwise.
   179  */
   180 native bool:RenameFile(const String:newpath[], const String:oldpath[]);
   181 
   182 /**
   183  * Checks if a directory exists.
   184  *
   185  * @param path			Path to the directory.
   186  * @return				True if the directory exists, false otherwise.
   187  */
   188 native bool:DirExists(const String:path[]);
   189 
   190 /**
   191  * Get the file size in bytes.
   192  *
   193  * @param path			Path to the file.
   194  * @return				File size in bytes, -1 if file not found.
   195  */
   196 native FileSize(const String:path[]);
   197 
   198 /**
   199  * Flushes a file's buffered output; any buffered output
   200  * is immediately written to the file.
   201  *
   202  * @param file			Handle to the file.
   203  * @return				True on success, false on failure.
   204  */
   205 native FlushFile(Handle:file);
   206 
   207 /**
   208  * Removes a directory.
   209  * @note On most Operating Systems you cannot remove a directory which has files inside it.
   210  *
   211  * @param path			Path to the directory.
   212  * @return				True on success, false otherwise.
   213  */
   214 native bool:RemoveDir(const String:path[]);
   215 
   216 /**
   217  * Writes a line of text in a file.
   218  * @note This native will append the newline character.
   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  */
   225 native bool:WriteFileLine(Handle:hndl, const String:format[], any:...);
   226 
   227 /**
   228  * Returns a file timestamp as a unix timestamp.
   229  *
   230  * @param file			File name.
   231  * @param tmode			Time mode.
   232  * @return				Time value, or -1 on failure.
   233  */
   234 native GetFileTime(const String:file[], FileTimeMode:tmode);
   235 
   236 /**
   237  * Same as LogToFile(), except uses an open file Handle.  The file must 
   238  * be opened in text appending mode.
   239  *
   240  * @param hndl			Handle to the file.
   241  * @param message		Message format.
   242  * @param ...			Message format parameters.
   243  * @noreturn
   244  * @error				Invalid Handle.
   245  */
   246 native LogToOpenFile(Handle:hndl, const String:message[], any:...);
   247 
   248 /**
   249  * Same as LogToFileEx(), except uses an open file Handle.  The file must 
   250  * be opened in text appending mode.
   251  *
   252  * @param hndl			Handle to the file.
   253  * @param message		Message format.
   254  * @param ...			Message format parameters.
   255  * @noreturn
   256  * @error				Invalid Handle.
   257  */
   258 native LogToOpenFileEx(Handle:hndl, const String:message[], any:...);
   259