plugins/include/files.inc
author Borja Ferrer <faluco@alliedmods.net>
Sun Jan 07 01:30:28 2007 +0000 (2007-01-07)
changeset 274 959c85596341
parent 257 583a23434070
child 275 81670bcf543e
permissions -rw-r--r--
Added a bunch of new file natives.
Fixed possible corruption in snprintf when input buffer was bigger than output buffer and relaying on its retval.
Fixed all cases when the above situation happened.
Fixed _PrintToHL2Log not taking va_list in.
dvander@252
     1
/**
dvander@252
     2
 * :TODO: license info
dvander@252
     3
 */
dvander@252
     4
dvander@252
     5
#if defined _files_included
dvander@252
     6
 #endinput
dvander@252
     7
#endif
dvander@252
     8
#define _files_included
dvander@252
     9
dvander@252
    10
/* @note All paths in SourceMod natives are relative to the mod folder unless otherwise noted. */
dvander@252
    11
 
dvander@252
    12
enum FileType
dvander@252
    13
{
dvander@252
    14
	FileType_Unknown = 0,	/* Unknown file type (device/socket) */
dvander@252
    15
	FileType_Directory = 1,	/* File is a directory */
dvander@252
    16
	FileType_File = 2,		/* File is a file */
dvander@252
    17
};
dvander@252
    18
dvander@257
    19
#define PLATFORM_MAX_PATH		256
dvander@252
    20
faluco@274
    21
#define SEEK_SET 0
faluco@274
    22
#define SEEK_CUR 1
faluco@274
    23
#define SEEK_END 2
faluco@274
    24
dvander@252
    25
/**
dvander@252
    26
 * @brief Opens a directory/folder for contents enumeration.
dvander@252
    27
 * @note Directories are closed with CloseHandle().
dvander@252
    28
 * @note Directories Handles can be cloned.
dvander@252
    29
 *
dvander@252
    30
 * @param path			Path to open.
dvander@252
    31
 * @return			A Handle to the directory, INVALID_HANDLE on open error.
dvander@252
    32
 */
dvander@252
    33
native Handle:OpenDirectory(const String:path[]);
dvander@252
    34
dvander@252
    35
/**
dvander@252
    36
 * @brief Reads the current directory entry as a local filename, then moves to the next file.
dvander@252
    37
 * @note Contents of buffers are undefined when returning false.
dvander@252
    38
 * @note Both the '.' and '..' automatic directory entries will be retrieved for Windows and Linux.
dvander@252
    39
 * 
dvander@252
    40
 * @param dir			Handle to a directory.
dvander@252
    41
 * @param buffer		String buffer to hold directory name.
dvander@252
    42
 * @param maxlength		Maximum size of string buffer.
dvander@252
    43
 * @param type			Optional variable to store the file type.
dvander@252
    44
 * @return			True on success, false if there are no more files to read.
dvander@252
    45
 * @error				Invalid or corrupt Handle. 
dvander@252
    46
 */
dvander@257
    47
native bool:ReadDirEntry(Handle:dir, String:buffer[], maxlength, &FileType:type=FileType_Unknown);
faluco@274
    48
faluco@274
    49
/**
faluco@274
    50
 * @brief Opens a file.
faluco@274
    51
 * @note Files are closed with CloseHandle().
faluco@274
    52
 * @note File Handles can be cloned.
faluco@274
    53
 *
faluco@274
    54
 * @param file			File to open.
faluco@274
    55
 * @param mode			Open mode.
faluco@274
    56
 * @return			A Handle to the file, INVALID_HANDLE on open error.
faluco@274
    57
 */
faluco@274
    58
native Handle:OpenFile(const String:file[], const String:mode[]);
faluco@274
    59
faluco@274
    60
/**
faluco@274
    61
 * @brief Deletes a file.
faluco@274
    62
 *
faluco@274
    63
 * @param path			Path of the file to delete.
faluco@274
    64
 * @return			True on success, false otherwise.
faluco@274
    65
 */
faluco@274
    66
native bool:DeleteFile(const String:path[]);
faluco@274
    67
faluco@274
    68
/**
faluco@274
    69
 * @brief Reads a line from a text file.
faluco@274
    70
 *
faluco@274
    71
 * @param hndl			Handle to the file.
faluco@274
    72
 * @param buffer		String buffer to hold the line.
faluco@274
    73
 * @param maxlength		Maximum size of string buffer.	
faluco@274
    74
 * @return			True on success, false otherwise.
faluco@274
    75
 */
faluco@274
    76
native bool:ReadFileLine(Handle:hndl, String:buffer[], maxlength);
faluco@274
    77
faluco@274
    78
/**
faluco@274
    79
 * @brief Tests if the end of file has been reached.
faluco@274
    80
 *
faluco@274
    81
 * @param file			Handle to the file.
faluco@274
    82
 * @return			True if end of file has been reached, false otherwise.
faluco@274
    83
 */
faluco@274
    84
native bool:IsEndOfFile(Handle:file);
faluco@274
    85
faluco@274
    86
/**
faluco@274
    87
 * @brief Sets the file position indicator.
faluco@274
    88
 *
faluco@274
    89
 * @param file			Handle to the file.
faluco@274
    90
 * @param position		Position relative to what is specified in whence.
faluco@274
    91
 * @param whence		Look at the SEEK_* definitions.
faluco@274
    92
 * @return			True on success, false otherwise.
faluco@274
    93
 */
faluco@274
    94
native bool:FileSeek(Handle:file, position, whence);
faluco@274
    95
faluco@274
    96
/**
faluco@274
    97
 * @brief Get current position in the file.
faluco@274
    98
 *
faluco@274
    99
 * @param file			Handle to the file.
faluco@274
   100
 * @return			Value for the file position indicator.
faluco@274
   101
 */
faluco@274
   102
native FilePosition(Handle:file);
faluco@274
   103
faluco@274
   104
/**
faluco@274
   105
 * @brief Checks if a file exists.
faluco@274
   106
 *
faluco@274
   107
 * @param path			Path to the file.
faluco@274
   108
 * @return			True if the file exists, false otherwise.
faluco@274
   109
 */
faluco@274
   110
native bool:FileExists(const String:path[]);
faluco@274
   111
faluco@274
   112
/**
faluco@274
   113
 * @brief Renames a file.
faluco@274
   114
 *
faluco@274
   115
 * @param newpath		New path to the file.
faluco@274
   116
 * @param oldpath		Path to the existing file.
faluco@274
   117
 * @return			True on success, false otherwise.
faluco@274
   118
 */
faluco@274
   119
native bool:RenameFile(const String:newpath[], const String:oldpath[]);
faluco@274
   120
faluco@274
   121
/**
faluco@274
   122
 * @brief Checks if a directory exists.
faluco@274
   123
 *
faluco@274
   124
 * @param path			Path to the directory.
faluco@274
   125
 * @return			True if the directory exists, false otherwise.
faluco@274
   126
 */
faluco@274
   127
native bool:DirExists(const String:path[]);
faluco@274
   128
faluco@274
   129
/**
faluco@274
   130
 * @brief Get the file size in bytes.
faluco@274
   131
 *
faluco@274
   132
 * @param path			Path to the file.
faluco@274
   133
 * @return			File size in bytes, -1 if file not found.
faluco@274
   134
 */
faluco@274
   135
native FileSize(const String:path[]);
faluco@274
   136
faluco@274
   137
/**
faluco@274
   138
 * @brief Removes a directory.
faluco@274
   139
 * @note On most Operating Systems you cannot remove a directory which has files inside it.
faluco@274
   140
 *
faluco@274
   141
 * @param path			Path to the directory.
faluco@274
   142
 * @return			True on success, false otherwise.
faluco@274
   143
 */
faluco@274
   144
native bool:RemoveDir(const String:path[]);
faluco@274
   145
faluco@274
   146
/**
faluco@274
   147
 * @brief Writes a line of text in a file.
faluco@274
   148
 * @note This native will append the newline character.
faluco@274
   149
 *
faluco@274
   150
 * @param hndl			Handle to the file.
faluco@274
   151
 * @param format		Formatting rules.
faluco@274
   152
 * @param ...			Variable number of format parameters.
faluco@274
   153
 * @return			True on success, false otherwise.
faluco@274
   154
 */
faluco@274
   155
native bool:WriteFileLine(Handle:hndl, const String:format[], ...);