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