plugins/include/files.inc
author David Anderson <dvander@alliedmods.net>
Fri Jan 26 02:07:24 2007 +0000 (2007-01-26)
changeset 374 39ab08027d9e
parent 345 df0b05b4f50d
child 475 bb11d7eaae0f
permissions -rw-r--r--
added license info to include files
     1 /**
     2  * ===============================================================
     3  * SourceMod (C)2004-2007 AlliedModders LLC.  All rights reserved.
     4  * ===============================================================
     5  *
     6  *  This file is part of the SourceMod/SourcePawn SDK.  This file may only be used 
     7  * or modified under the Terms and Conditions of its License Agreement, which is found 
     8  * in LICENSE.txt.  The Terms and Conditions for making SourceMod extensions/plugins 
     9  * may change at any time.  To view the latest information, see:
    10  *   http://www.sourcemod.net/license.php
    11  *
    12  * Version: $Id$
    13  */
    14 
    15 #if defined _files_included
    16  #endinput
    17 #endif
    18 #define _files_included
    19 
    20 /* @note All paths in SourceMod natives are relative to the mod folder unless otherwise noted. */
    21  
    22 enum FileType
    23 {
    24 	FileType_Unknown = 0,	/* Unknown file type (device/socket) */
    25 	FileType_Directory = 1,	/* File is a directory */
    26 	FileType_File = 2,		/* File is a file */
    27 };
    28 
    29 #define PLATFORM_MAX_PATH		256
    30 
    31 #define SEEK_SET 0
    32 #define SEEK_CUR 1
    33 #define SEEK_END 2
    34 
    35 enum PathType
    36 {
    37 	Path_SM,		/* SourceMod root folder */	
    38 };
    39 
    40 /**
    41  * @brief Builds a path relative to the SourceMod folder.
    42  *
    43  * @param type			Type of path to build as the base.
    44  * @param buffer		Buffer to store the path.
    45  * @param maxlength		Maximum length of buffer.
    46  * @param fmt			Format string.
    47  * @param ...			Format arguments.
    48  * @return				Number of bytes written to buffer (not including null terminator).
    49  */
    50 native BuildPath(PathType:type, String:buffer[], maxlength, const String:fmt[], ...);
    51 
    52 /**
    53  * @brief Opens a directory/folder for contents enumeration.
    54  * @note Directories are closed with CloseHandle().
    55  * @note Directories Handles can be cloned.
    56  *
    57  * @param path			Path to open.
    58  * @return				A Handle to the directory, INVALID_HANDLE on open error.
    59  */
    60 native Handle:OpenDirectory(const String:path[]);
    61 
    62 /**
    63  * @brief Reads the current directory entry as a local filename, then moves to the next file.
    64  * @note Contents of buffers are undefined when returning false.
    65  * @note Both the '.' and '..' automatic directory entries will be retrieved for Windows and Linux.
    66  * 
    67  * @param dir			Handle to a directory.
    68  * @param buffer		String buffer to hold directory name.
    69  * @param maxlength		Maximum size of string buffer.
    70  * @param type			Optional variable to store the file type.
    71  * @return				True on success, false if there are no more files to read.
    72  * @error				Invalid or corrupt Handle. 
    73  */
    74 native bool:ReadDirEntry(Handle:dir, String:buffer[], maxlength, &FileType:type=FileType_Unknown);
    75 
    76 /**
    77  * @brief Opens a file.
    78  * @note Files are closed with CloseHandle().
    79  * @note File Handles can be cloned.
    80  *
    81  * @param file		File to open.
    82  * @param mode		Open mode.
    83  * @return			A Handle to the file, INVALID_HANDLE on open error.
    84  */
    85 native Handle:OpenFile(const String:file[], const String:mode[]);
    86 
    87 /**
    88  * @brief Deletes a file.
    89  *
    90  * @param path		Path of the file to delete.
    91  * @return			True on success, false otherwise.
    92  */
    93 native bool:DeleteFile(const String:path[]);
    94 
    95 /**
    96  * @brief Reads a line from a text file.
    97  *
    98  * @param hndl			Handle to the file.
    99  * @param buffer		String buffer to hold the line.
   100  * @param maxlength		Maximum size of string buffer.	
   101  * @return				True on success, false otherwise.
   102  */
   103 native bool:ReadFileLine(Handle:hndl, String:buffer[], maxlength);
   104 
   105 /**
   106  * @brief Tests if the end of file has been reached.
   107  *
   108  * @param file		Handle to the file.
   109  * @return			True if end of file has been reached, false otherwise.
   110  */
   111 native bool:IsEndOfFile(Handle:file);
   112 
   113 /**
   114  * @brief Sets the file position indicator.
   115  *
   116  * @param file		Handle to the file.
   117  * @param position	Position relative to what is specified in whence.
   118  * @param whence	Look at the SEEK_* definitions.
   119  * @return			True on success, false otherwise.
   120  */
   121 native bool:FileSeek(Handle:file, position, whence);
   122 
   123 /**
   124  * @brief Get current position in the file.
   125  *
   126  * @param file		Handle to the file.
   127  * @return			Value for the file position indicator.
   128  */
   129 native FilePosition(Handle:file);
   130 
   131 /**
   132  * @brief Checks if a file exists.
   133  *
   134  * @param path		Path to the file.
   135  * @return			True if the file exists, false otherwise.
   136  */
   137 native bool:FileExists(const String:path[]);
   138 
   139 /**
   140  * @brief Renames a file.
   141  *
   142  * @param newpath		New path to the file.
   143  * @param oldpath		Path to the existing file.
   144  * @return				True on success, false otherwise.
   145  */
   146 native bool:RenameFile(const String:newpath[], const String:oldpath[]);
   147 
   148 /**
   149  * @brief Checks if a directory exists.
   150  *
   151  * @param path		Path to the directory.
   152  * @return			True if the directory exists, false otherwise.
   153  */
   154 native bool:DirExists(const String:path[]);
   155 
   156 /**
   157  * @brief Get the file size in bytes.
   158  *
   159  * @param path		Path to the file.
   160  * @return			File size in bytes, -1 if file not found.
   161  */
   162 native FileSize(const String:path[]);
   163 
   164 /**
   165  * @brief Removes a directory.
   166  * @note On most Operating Systems you cannot remove a directory which has files inside it.
   167  *
   168  * @param path		Path to the directory.
   169  * @return			True on success, false otherwise.
   170  */
   171 native bool:RemoveDir(const String:path[]);
   172 
   173 /**
   174  * @brief Writes a line of text in a file.
   175  * @note This native will append the newline character.
   176  *
   177  * @param hndl		Handle to the file.
   178  * @param format	Formatting rules.
   179  * @param ...		Variable number of format parameters.
   180  * @return			True on success, false otherwise.
   181  */
   182 native bool:WriteFileLine(Handle:hndl, const String:format[], {Handle,Float,String,_}:...);