plugins/include/files.inc
author Scott Ehlert <ds@alliedmods.net>
Wed Aug 15 06:19:30 2007 +0000 (2007-08-15)
changeset 1330 ab94316bb52b
parent 821 c5b4dca7f8ac
child 1370 05e9ca38e083
permissions -rw-r--r--
Updated license headers on virtually all files with extremely minor changes in the name of some sort of strange consistency.
All plugin and include file headers also have been changed to say about GPL3 instead of GPL2.

(This day shall henceforth be known as the Eighty Column Massacre of '07)
     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 #define PLATFORM_MAX_PATH		256		/**< Maximum path length. */
    51 
    52 #define SEEK_SET 0						/**< Seek from start. */
    53 #define SEEK_CUR 1						/**< Seek from current position. */
    54 #define SEEK_END 2						/**< Seek from end position. */
    55 
    56 /**
    57  * Path types.
    58  */
    59 enum PathType
    60 {
    61 	Path_SM,		/**< SourceMod root folder */	
    62 };
    63 
    64 /**
    65  * Builds a path relative to the SourceMod folder.
    66  *
    67  * @param type			Type of path to build as the base.
    68  * @param buffer		Buffer to store the path.
    69  * @param maxlength		Maximum length of buffer.
    70  * @param fmt			Format string.
    71  * @param ...			Format arguments.
    72  * @return				Number of bytes written to buffer (not including null terminator).
    73  */
    74 native BuildPath(PathType:type, String:buffer[], maxlength, const String:fmt[], any:...);
    75 
    76 /**
    77  * Opens a directory/folder for contents enumeration.
    78  * @note Directories are closed with CloseHandle().
    79  * @note Directories Handles can be cloned.
    80  *
    81  * @param path			Path to open.
    82  * @return				A Handle to the directory, INVALID_HANDLE on open error.
    83  */
    84 native Handle:OpenDirectory(const String:path[]);
    85 
    86 /**
    87  * Reads the current directory entry as a local filename, then moves to the next file.
    88  * @note Contents of buffers are undefined when returning false.
    89  * @note Both the '.' and '..' automatic directory entries will be retrieved for Windows and Linux.
    90  * 
    91  * @param dir			Handle to a directory.
    92  * @param buffer		String buffer to hold directory name.
    93  * @param maxlength		Maximum size of string buffer.
    94  * @param type			Optional variable to store the file type.
    95  * @return				True on success, false if there are no more files to read.
    96  * @error				Invalid or corrupt Handle. 
    97  */
    98 native bool:ReadDirEntry(Handle:dir, String:buffer[], maxlength, &FileType:type=FileType_Unknown);
    99 
   100 /**
   101  * Opens a file.
   102  * @note Files are closed with CloseHandle().
   103  * @note File Handles can be cloned.
   104  *
   105  * @param file			File to open.
   106  * @param mode			Open mode.
   107  * @return				A Handle to the file, INVALID_HANDLE on open error.
   108  */
   109 native Handle:OpenFile(const String:file[], const String:mode[]);
   110 
   111 /**
   112  * Deletes a file.
   113  *
   114  * @param path			Path of the file to delete.
   115  * @return				True on success, false otherwise.
   116  */
   117 native bool:DeleteFile(const String:path[]);
   118 
   119 /**
   120  * Reads a line from a text file.
   121  *
   122  * @param hndl			Handle to the file.
   123  * @param buffer		String buffer to hold the line.
   124  * @param maxlength		Maximum size of string buffer.	
   125  * @return				True on success, false otherwise.
   126  */
   127 native bool:ReadFileLine(Handle:hndl, String:buffer[], maxlength);
   128 
   129 /**
   130  * Tests if the end of file has been reached.
   131  *
   132  * @param file			Handle to the file.
   133  * @return				True if end of file has been reached, false otherwise.
   134  */
   135 native bool:IsEndOfFile(Handle:file);
   136 
   137 /**
   138  * Sets the file position indicator.
   139  *
   140  * @param file			Handle to the file.
   141  * @param position		Position relative to what is specified in whence.
   142  * @param whence		Look at the SEEK_* definitions.
   143  * @return				True on success, false otherwise.
   144  */
   145 native bool:FileSeek(Handle:file, position, whence);
   146 
   147 /**
   148  * Get current position in the file.
   149  *
   150  * @param file			Handle to the file.
   151  * @return				Value for the file position indicator.
   152  */
   153 native FilePosition(Handle:file);
   154 
   155 /**
   156  * Checks if a file exists.
   157  *
   158  * @param path			Path to the file.
   159  * @return				True if the file exists, false otherwise.
   160  */
   161 native bool:FileExists(const String:path[]);
   162 
   163 /**
   164  * Renames a file.
   165  *
   166  * @param newpath		New path to the file.
   167  * @param oldpath		Path to the existing file.
   168  * @return				True on success, false otherwise.
   169  */
   170 native bool:RenameFile(const String:newpath[], const String:oldpath[]);
   171 
   172 /**
   173  * Checks if a directory exists.
   174  *
   175  * @param path			Path to the directory.
   176  * @return				True if the directory exists, false otherwise.
   177  */
   178 native bool:DirExists(const String:path[]);
   179 
   180 /**
   181  * Get the file size in bytes.
   182  *
   183  * @param path			Path to the file.
   184  * @return				File size in bytes, -1 if file not found.
   185  */
   186 native FileSize(const String:path[]);
   187 
   188 /**
   189  * Flushes a file's buffered output; any buffered output
   190  * is immediately written to the file.
   191  *
   192  * @param file			Handle to the file.
   193  * @return				True on success, false on failure.
   194  */
   195 native FlushFile(Handle:file);
   196 
   197 /**
   198  * Removes a directory.
   199  * @note On most Operating Systems you cannot remove a directory which has files inside it.
   200  *
   201  * @param path			Path to the directory.
   202  * @return				True on success, false otherwise.
   203  */
   204 native bool:RemoveDir(const String:path[]);
   205 
   206 /**
   207  * Writes a line of text in a file.
   208  * @note This native will append the newline character.
   209  *
   210  * @param hndl			Handle to the file.
   211  * @param format		Formatting rules.
   212  * @param ...			Variable number of format parameters.
   213  * @return				True on success, false otherwise.
   214  */
   215 native bool:WriteFileLine(Handle:hndl, const String:format[], any:...);