Merge pull request #206 from alliedmodders/tr-files

Port files.inc to transitional syntax.
This commit is contained in:
David Anderson 2014-11-18 18:56:07 -08:00
commit 0511543c76
8 changed files with 317 additions and 93 deletions

View File

@ -181,7 +181,7 @@ public:
} }
size_t Read(void *pOut, int size) KE_OVERRIDE { size_t Read(void *pOut, int size) KE_OVERRIDE {
return fread(pOut, size, 1, fp_); return fread(pOut, 1, size, fp_);
} }
char *ReadLine(char *pOut, int size) KE_OVERRIDE { char *ReadLine(char *pOut, int size) KE_OVERRIDE {
return fgets(pOut, size, fp_); return fgets(pOut, size, fp_);
@ -1102,6 +1102,35 @@ static cell_t sm_RemoveGameLogHook(IPluginContext *pContext, const cell_t *param
return 1; return 1;
} }
template <typename T>
static cell_t File_ReadTyped(IPluginContext *pContext, const cell_t *params)
{
OpenHandle<FileObject> file(pContext, params[1], g_FileType);
if (!file.Ok())
return 0;
cell_t *data;
pContext->LocalToPhysAddr(params[2], &data);
T value;
if (file->Read(&value, sizeof(value)) != sizeof(value))
return 0;
*data = value;
return 1;
}
template <typename T>
static cell_t File_WriteTyped(IPluginContext *pContext, const cell_t *params)
{
OpenHandle<FileObject> file(pContext, params[1], g_FileType);
if (!file.Ok())
return 0;
T value = (T)params[2];
return !!(file->Write(&value, sizeof(value)) == sizeof(value));
}
REGISTER_NATIVES(filesystem) REGISTER_NATIVES(filesystem)
{ {
{"OpenDirectory", sm_OpenDirectory}, {"OpenDirectory", sm_OpenDirectory},
@ -1134,5 +1163,26 @@ REGISTER_NATIVES(filesystem)
{"RemoveGameLogHook", sm_RemoveGameLogHook}, {"RemoveGameLogHook", sm_RemoveGameLogHook},
{"CreateDirectory", sm_CreateDirectory}, {"CreateDirectory", sm_CreateDirectory},
{"SetFilePermissions", sm_SetFilePermissions}, {"SetFilePermissions", sm_SetFilePermissions},
{"File.ReadLine", sm_ReadFileLine},
{"File.Read", sm_ReadFile},
{"File.ReadString", sm_ReadFileString},
{"File.Write", sm_WriteFile},
{"File.WriteString", sm_WriteFileString},
{"File.WriteLine", sm_WriteFileLine},
{"File.EndOfFile", sm_IsEndOfFile},
{"File.Seek", sm_FileSeek},
{"File.Position.get", sm_FilePosition},
{"File.ReadInt8", File_ReadTyped<int8_t>},
{"File.ReadUint8", File_ReadTyped<uint8_t>},
{"File.ReadInt16", File_ReadTyped<int16_t>},
{"File.ReadInt32", File_ReadTyped<int32_t>},
{"File.WriteInt8", File_WriteTyped<int8_t>},
{"File.WriteInt16", File_WriteTyped<int16_t>},
{"File.WriteInt32", File_WriteTyped<int32_t>},
// Transitional syntax support.
{"DirectoryListing.GetNext", sm_ReadDirEntry},
{NULL, NULL}, {NULL, NULL},
}; };

View File

@ -35,32 +35,28 @@ public ReadSimpleUsers()
{ {
BuildPath(Path_SM, g_Filename, sizeof(g_Filename), "configs/admins_simple.ini"); BuildPath(Path_SM, g_Filename, sizeof(g_Filename), "configs/admins_simple.ini");
new Handle:file = OpenFile(g_Filename, "rt"); File file = OpenFile(g_Filename, "rt");
if (file == INVALID_HANDLE) if (!file)
{ {
ParseError("Could not open file!"); ParseError("Could not open file!");
return; return;
} }
while (!IsEndOfFile(file)) while (!file.EndOfFile())
{ {
decl String:line[255]; char line[255];
if (!ReadFileLine(file, line, sizeof(line))) if (!file.ReadLine(line, sizeof(line)))
{
break; break;
}
/* Trim comments */ /* Trim comments */
new len = strlen(line); int len = strlen(line);
new bool:ignoring = false; bool ignoring = false;
for (new i=0; i<len; i++) for (int i=0; i<len; i++)
{ {
if (ignoring) if (ignoring)
{ {
if (line[i] == '"') if (line[i] == '"')
{
ignoring = false; ignoring = false;
}
} else { } else {
if (line[i] == '"') if (line[i] == '"')
{ {
@ -89,7 +85,7 @@ public ReadSimpleUsers()
ReadAdminLine(line); ReadAdminLine(line);
} }
CloseHandle(file); file.Close();
} }

View File

@ -479,12 +479,12 @@ public ParamCheck(client)
ReadPackString(outputSubmenu[Submenu_listdata], path, sizeof(path)); ReadPackString(outputSubmenu[Submenu_listdata], path, sizeof(path));
ResetPack(outputSubmenu[Submenu_listdata]); ResetPack(outputSubmenu[Submenu_listdata]);
new Handle:file = OpenFile(path, "rt"); File file = OpenFile(path, "rt");
new String:readData[128]; char readData[128];
if(file != INVALID_HANDLE) if (file)
{ {
while(!IsEndOfFile(file) && ReadFileLine(file, readData, sizeof(readData))) while (!file.EndOfFile() && file.ReadLine(readData, sizeof(readData)))
{ {
TrimString(readData); TrimString(readData);

View File

@ -1,7 +1,7 @@
/** /**
* vim: set ts=4 : * vim: set ts=4 sw=4 tw=99 noet :
* ============================================================================= * =============================================================================
* SourceMod (C)2004-2008 AlliedModders LLC. All rights reserved. * SourceMod (C)2004-2014 AlliedModders LLC. All rights reserved.
* ============================================================================= * =============================================================================
* *
* This file is part of the SourceMod/SourcePawn SDK. * This file is part of the SourceMod/SourcePawn SDK.
@ -36,7 +36,7 @@
#define _files_included #define _files_included
/** /**
* @global All paths in SourceMod natives are relative to the mod folder * @global All paths in SourceMod natives are relative to the mod folder
* unless otherwise noted. * unless otherwise noted.
* *
* Most functions in SourceMod (at least, ones that deal with direct * Most functions in SourceMod (at least, ones that deal with direct
@ -55,7 +55,7 @@
/** /**
* File inode types. * File inode types.
*/ */
enum FileType enum FileType:
{ {
FileType_Unknown = 0, /* Unknown file type (device/socket) */ FileType_Unknown = 0, /* Unknown file type (device/socket) */
FileType_Directory = 1, /* File is a directory */ FileType_Directory = 1, /* File is a directory */
@ -65,7 +65,7 @@ enum FileType
/** /**
* File time modes. * File time modes.
*/ */
enum FileTimeMode enum FileTimeMode:
{ {
FileTime_LastAccess = 0, /* Last access (does not work on FAT) */ FileTime_LastAccess = 0, /* Last access (does not work on FAT) */
FileTime_Created = 1, /* Creation (does not work on FAT) */ FileTime_Created = 1, /* Creation (does not work on FAT) */
@ -81,11 +81,164 @@ enum FileTimeMode
/** /**
* Path types. * Path types.
*/ */
enum PathType enum PathType:
{ {
Path_SM, /**< SourceMod root folder */ Path_SM, /**< SourceMod root folder */
}; };
// A DirectoryListing iterates over the contents of a directory. To obtain a
// DirectoryListing handle, call OpenDirectory().
methodmap DirectoryListing < Handle
{
// Reads the current directory entry as a local filename, then moves to the
// next file.
//
// Note: Both the '.' and '..' automatic directory entries will be retrieved.
//
// @param buffer String buffer to hold directory name.
// @param maxlength Maximum size of string buffer.
// @param type Optional variable to store the file type.
// @return True on success, false if there are no more files to read.
public native bool GetNext(char[] buffer, int maxlength, FileType &type=FileType_Unknown);
};
// A File object can be obtained by calling OpenFile(). File objects should be
// closed with delete or Close(). Note that, "delete file" does not
// actually a file, it just closes it.
methodmap File < Handle
{
// Close the file handle. This is the same as using CloseHandle) or delete.
public void Close() {
CloseHandle(this);
}
// Reads a line of text from a file.
//
// @param buffer String buffer to hold the line.
// @param maxlength Maximum size of string buffer.
// @return True on success, false otherwise.
public native bool ReadLine(char[] buffer, int maxlength);
// Reads binary data from a file.
//
// @param items Array to store each item read.
// @param num_items Number of items to read into the array.
// @param size Size of each element, in bytes, to be read.
// Valid sizes are 1, 2, or 4.
// @return Number of elements read, or -1 on error.
public native int Read(int[] items, int num_items, int size);
// Reads a UTF8 or ANSI string from a file.
//
// @param buffer Buffer to store the string.
// @param max_size Maximum size of the string buffer.
// @param read_count If -1, reads until a null terminator is encountered in
// the file. Otherwise, read_count bytes are read
// into the buffer provided. In this case the buffer
// is not explicitly null terminated, and the buffer
// will contain any null terminators read from the file.
// @return Number of characters written to the buffer, or -1
// if an error was encountered.
// @error read_count > max_size.
public native int ReadString(char[] buffer, int max_size, int read_count=-1);
// Writes binary data to a file.
//
// @param items Array of items to write. The data is read directly.
// That is, in 1 or 2-byte mode, the lower byte(s) in
// each cell are used directly, rather than performing
// any casts from a 4-byte number to a smaller number.
// @param num_items Number of items in the array.
// @param size Size of each item in the array in bytes.
// Valid sizes are 1, 2, or 4.
// @return True on success, false on error.
public native bool Write(const int[] items, int num_items, int size);
// Writes a binary string to a file.
//
// @param buffer String to write.
// @param term True to append NUL terminator, false otherwise.
// @return True on success, false on error.
public native bool WriteString(const char[] buffer, bool term);
// Writes a line of text to a text file. A newline is automatically appended.
//
// @param hndl Handle to the file.
// @param format Formatting rules.
// @param ... Variable number of format parameters.
// @return True on success, false otherwise.
public native bool WriteLine(const char[] format, any:...);
// Reads a single int8 (byte) from a file. The returned value is sign-
// extended to an int32.
//
// @param data Variable to store the data read.
// @return True on success, false on failure.
public native bool ReadInt8(int &data);
// Reads a single uint8 (unsigned byte) from a file. The returned value is
// zero-extended to an int32.
//
// @param data Variable to store the data read.
// @return True on success, false on failure.
public native bool ReadUint8(int &data);
// Reads a single int16 (short) from a file. The value is sign-extended to
// an int32.
//
// @param data Variable to store the data read.
// @return True on success, false on failure.
public native bool ReadInt16(int &data);
// Reads a single unt16 (unsigned short) from a file. The value is zero-
// extended to an int32.
//
// @param data Variable to store the data read.
// @return True on success, false on failure.
public native bool ReadUint16(int &data);
// Reads a single int32 (int/cell) from a file.
//
// @param data Variable to store the data read.
// @return True on success, false on failure.
public native bool ReadInt32(int &data);
// Writes a single int8 (byte) to a file.
//
// @param data Data to write (truncated to an int8).
// @return True on success, false on failure.
public native bool WriteInt8(int data);
// Writes a single int16 (short) to a file.
//
// @param data Data to write (truncated to an int16).
// @return True on success, false on failure.
public native bool WriteInt16(int data);
// Writes a single int32 (int/cell) to a file.
//
// @param data Data to write.
// @return True on success, false on failure.
public native bool WriteInt32(int data);
// Tests if the end of file has been reached.
//
// @return True if end of file has been reached, false otherwise.
public native bool EndOfFile();
// Sets the file position indicator.
//
// @param position Position relative to what is specified in whence.
// @param where SEEK_ constant value of where to see from.
// @return True on success, false otherwise.
public native bool Seek(int position, int where);
// Get the current position in the file; returns -1 on failure.
property int Position {
public native get();
}
}
/** /**
* Builds a path relative to the SourceMod folder. This should be used instead of * Builds a path relative to the SourceMod folder. This should be used instead of
* directly referencing addons/sourcemod, in case users change the name of their * directly referencing addons/sourcemod, in case users change the name of their
@ -98,12 +251,12 @@ enum PathType
* @param ... Format arguments. * @param ... Format arguments.
* @return Number of bytes written to buffer (not including null terminator). * @return Number of bytes written to buffer (not including null terminator).
*/ */
native BuildPath(PathType:type, String:buffer[], maxlength, const String:fmt[], any:...); native int BuildPath(PathType type, char[] buffer, int maxlength, const char[] fmt, any:...);
/** /**
* Opens a directory/folder for contents enumeration. * Opens a directory/folder for contents enumeration.
* *
* @note Directories are closed with CloseHandle(). * @note Directories are closed with CloseHandle() or delete.
* @note Directories Handles can be cloned. * @note Directories Handles can be cloned.
* @note OpenDirectory() supports the "file://" notation. * @note OpenDirectory() supports the "file://" notation.
* *
@ -113,9 +266,9 @@ native BuildPath(PathType:type, String:buffer[], maxlength, const String:fmt[],
* the Valve search paths, rather than solely files * the Valve search paths, rather than solely files
* existing directly in the gamedir. * existing directly in the gamedir.
* @param valve_path_id If use_valve_fs, a search path from gameinfo or NULL_STRING for all search paths. * @param valve_path_id If use_valve_fs, a search path from gameinfo or NULL_STRING for all search paths.
* @return A Handle to the directory, INVALID_HANDLE on open error. * @return A Handle to the directory, null on error.
*/ */
native Handle:OpenDirectory(const String:path[], bool:use_valve_fs=false, const String:valve_path_id[]="GAME"); native DirectoryListing OpenDirectory(const char[] path, bool use_valve_fs=false, const char[] valve_path_id="GAME");
/** /**
* Reads the current directory entry as a local filename, then moves to the next file. * Reads the current directory entry as a local filename, then moves to the next file.
@ -130,14 +283,32 @@ native Handle:OpenDirectory(const String:path[], bool:use_valve_fs=false, const
* @return True on success, false if there are no more files to read. * @return True on success, false if there are no more files to read.
* @error Invalid or corrupt Handle. * @error Invalid or corrupt Handle.
*/ */
native bool:ReadDirEntry(Handle:dir, String:buffer[], maxlength, &FileType:type=FileType_Unknown); native bool ReadDirEntry(Handle dir, char[] buffer, int maxlength, FileType &type=FileType_Unknown);
/** /**
* Opens a file. * Opens or creates a file, returning a File handle on success. File handles
* should be closed with delete or CloseHandle().
* *
* @note Files are closed with CloseHandle(). * The open mode may be one of the following strings:
* @note File Handles can be cloned. * "r": Open an existing file for reading.
* @note OpenFile() supports the "file://" notation. * "w": Create a file for writing, or truncate (delete the contents of) an
* existing file and then open it for writing.
* "a": Create a file for writing, or open an existing file such that writes
* will be appended to the end.
* "r+": Open an existing file for both reading and writing.
* "w+": Create a file for reading and writing, or truncate an existing file
* and then open it for reading and writing.
* "a+": Create a file for both reading and writing, or open an existing file
* such that writes will be appended to the end.
*
* The open mode may also contain an additional character after "r", "w", or "a",
* but before any "+" sign. This character may be "b" (indicating binary mode) or
* "t" (indicating text mode). By default, "text" mode is implied. On Linux and
* Mac, this has no distinction from binary mode. On Windows, it causes the '\n'
* character (0xA) to be written as "\r\n" (0xD, 0xA).
*
* Example: "rb" opens a binary file for writing; "at" opens a text file for
* appending.
* *
* @param file File to open. * @param file File to open.
* @param mode Open mode. * @param mode Open mode.
@ -146,9 +317,9 @@ native bool:ReadDirEntry(Handle:dir, String:buffer[], maxlength, &FileType:type=
* search paths, rather than solely files existing directly * search paths, rather than solely files existing directly
* in the gamedir. * in the gamedir.
* @param valve_path_id If use_valve_fs, a search path from gameinfo or NULL_STRING for all search paths. * @param valve_path_id If use_valve_fs, a search path from gameinfo or NULL_STRING for all search paths.
* @return A Handle to the file, INVALID_HANDLE on open error. * @return A File handle, or null if the file could not be opened.
*/ */
native Handle:OpenFile(const String:file[], const String:mode[], bool:use_valve_fs=false, const String:valve_path_id[]="GAME"); native File OpenFile(const char[] file, const char[] mode, bool use_valve_fs=false, const char[] valve_path_id="GAME");
/** /**
* Deletes a file. * Deletes a file.
@ -161,7 +332,7 @@ native Handle:OpenFile(const String:file[], const String:mode[], bool:use_valve_
* @param valve_path_id If use_valve_fs, a search path from gameinfo or NULL_STRING for all search paths. * @param valve_path_id If use_valve_fs, a search path from gameinfo or NULL_STRING for all search paths.
* @return True on success, false on failure or if file not immediately removed. * @return True on success, false on failure or if file not immediately removed.
*/ */
native bool:DeleteFile(const String:path[], bool:use_valve_fs=false, const String:valve_path_id[]="DEFAULT_WRITE_PATH"); native bool DeleteFile(const char[] path, bool use_valve_fs=false, const char[] valve_path_id="DEFAULT_WRITE_PATH");
/** /**
* Reads a line from a text file. * Reads a line from a text file.
@ -171,7 +342,7 @@ native bool:DeleteFile(const String:path[], bool:use_valve_fs=false, const Strin
* @param maxlength Maximum size of string buffer. * @param maxlength Maximum size of string buffer.
* @return True on success, false otherwise. * @return True on success, false otherwise.
*/ */
native bool:ReadFileLine(Handle:hndl, String:buffer[], maxlength); native bool ReadFileLine(Handle hndl, char[] buffer, int maxlength);
/** /**
* Reads binary data from a file. * Reads binary data from a file.
@ -183,7 +354,7 @@ native bool:ReadFileLine(Handle:hndl, String:buffer[], maxlength);
* Valid sizes are 1, 2, or 4. * Valid sizes are 1, 2, or 4.
* @return Number of elements read, or -1 on error. * @return Number of elements read, or -1 on error.
*/ */
native ReadFile(Handle:hndl, items[], num_items, size); native int ReadFile(Handle hndl, int[] items, int num_items, int size);
/** /**
* Reads a UTF8 or ANSI string from a file. * Reads a UTF8 or ANSI string from a file.
@ -200,7 +371,7 @@ native ReadFile(Handle:hndl, items[], num_items, size);
* if an error was encountered. * if an error was encountered.
* @error Invalid Handle, or read_count > max_size. * @error Invalid Handle, or read_count > max_size.
*/ */
native ReadFileString(Handle:hndl, String:buffer[], max_size, read_count=-1); native int ReadFileString(Handle hndl, char[] buffer, int max_size, int read_count=-1);
/** /**
* Writes binary data to a file. * Writes binary data to a file.
@ -216,7 +387,7 @@ native ReadFileString(Handle:hndl, String:buffer[], max_size, read_count=-1);
* @return True on success, false on error. * @return True on success, false on error.
* @error Invalid Handle. * @error Invalid Handle.
*/ */
native bool:WriteFile(Handle:hndl, const items[], num_items, size); native bool WriteFile(Handle hndl, const int[] items, int num_items, int size);
/** /**
* Writes a binary string to a file. * Writes a binary string to a file.
@ -227,7 +398,7 @@ native bool:WriteFile(Handle:hndl, const items[], num_items, size);
* @return True on success, false on error. * @return True on success, false on error.
* @error Invalid Handle. * @error Invalid Handle.
*/ */
native bool:WriteFileString(Handle:hndl, const String:buffer[], bool:term); native bool WriteFileString(Handle hndl, const char[] buffer, bool term);
/** /**
* Writes a line of text to a text file. A newline is automatically appended. * Writes a line of text to a text file. A newline is automatically appended.
@ -238,7 +409,7 @@ native bool:WriteFileString(Handle:hndl, const String:buffer[], bool:term);
* @return True on success, false otherwise. * @return True on success, false otherwise.
* @error Invalid Handle. * @error Invalid Handle.
*/ */
native bool:WriteFileLine(Handle:hndl, const String:format[], any:...); native bool WriteFileLine(Handle hndl, const char[] format, any:...);
/** /**
* Reads a single binary cell from a file. * Reads a single binary cell from a file.
@ -250,14 +421,13 @@ native bool:WriteFileLine(Handle:hndl, const String:format[], any:...);
* @return Number of elements read (max 1), or -1 on error. * @return Number of elements read (max 1), or -1 on error.
* @error Invalid Handle. * @error Invalid Handle.
*/ */
stock ReadFileCell(Handle:hndl, &data, size) stock int ReadFileCell(Handle hndl, int &data, int size)
{ {
new array[1], ret; int ret;
int array[1];
if ((ret = ReadFile(hndl, array, 1, size)) == 1) if ((ret = ReadFile(hndl, array, 1, size)) == 1)
{
data = array[0]; data = array[0];
}
return ret; return ret;
} }
@ -275,12 +445,11 @@ stock ReadFileCell(Handle:hndl, &data, size)
* @return True on success, false on error. * @return True on success, false on error.
* @error Invalid Handle. * @error Invalid Handle.
*/ */
stock bool:WriteFileCell(Handle:hndl, data, size) stock bool WriteFileCell(Handle hndl, int data, int size)
{ {
new array[1]; int array[1];
array[0] = data; array[0] = data;
return WriteFile(hndl, array, 1, size); return WriteFile(hndl, array, 1, size);
} }
@ -291,7 +460,7 @@ stock bool:WriteFileCell(Handle:hndl, data, size)
* @return True if end of file has been reached, false otherwise. * @return True if end of file has been reached, false otherwise.
* @error Invalid Handle. * @error Invalid Handle.
*/ */
native bool:IsEndOfFile(Handle:file); native bool IsEndOfFile(Handle file);
/** /**
* Sets the file position indicator. * Sets the file position indicator.
@ -302,7 +471,7 @@ native bool:IsEndOfFile(Handle:file);
* @return True on success, false otherwise. * @return True on success, false otherwise.
* @error Invalid Handle. * @error Invalid Handle.
*/ */
native bool:FileSeek(Handle:file, position, where); native bool FileSeek(Handle file, int position, int where);
/** /**
* Get current position in the file. * Get current position in the file.
@ -311,7 +480,7 @@ native bool:FileSeek(Handle:file, position, where);
* @return Value for the file position indicator. * @return Value for the file position indicator.
* @error Invalid Handle. * @error Invalid Handle.
*/ */
native FilePosition(Handle:file); native int FilePosition(Handle file);
/** /**
* Checks if a file exists. * Checks if a file exists.
@ -324,7 +493,7 @@ native FilePosition(Handle:file);
* @param valve_path_id If use_valve_fs, a search path from gameinfo or NULL_STRING for all search paths. * @param valve_path_id If use_valve_fs, a search path from gameinfo or NULL_STRING for all search paths.
* @return True if the file exists, false otherwise. * @return True if the file exists, false otherwise.
*/ */
native bool:FileExists(const String:path[], bool:use_valve_fs=false, const String:valve_path_id[]="GAME"); native bool FileExists(const char[] path, bool use_valve_fs=false, const char[] valve_path_id="GAME");
/** /**
* Renames a file. * Renames a file.
@ -337,7 +506,7 @@ native bool:FileExists(const String:path[], bool:use_valve_fs=false, const Strin
* @param valve_path_id If use_valve_fs, a search path from gameinfo or NULL_STRING for all search paths. * @param valve_path_id If use_valve_fs, a search path from gameinfo or NULL_STRING for all search paths.
* @return True on success or use_valve_fs specified, false otherwise. * @return True on success or use_valve_fs specified, false otherwise.
*/ */
native bool:RenameFile(const String:newpath[], const String:oldpath[], bool:use_valve_fs=false, const String:valve_path_id[]="DEFAULT_WRITE_PATH"); native bool RenameFile(const char[] newpath, const char[] oldpath, bool use_valve_fs=false, const char[] valve_path_id="DEFAULT_WRITE_PATH");
/** /**
* Checks if a directory exists. * Checks if a directory exists.
@ -350,7 +519,7 @@ native bool:RenameFile(const String:newpath[], const String:oldpath[], bool:use_
* @param valve_path_id If use_valve_fs, a search path from gameinfo or NULL_STRING for all search paths. * @param valve_path_id If use_valve_fs, a search path from gameinfo or NULL_STRING for all search paths.
* @return True if the directory exists, false otherwise. * @return True if the directory exists, false otherwise.
*/ */
native bool:DirExists(const String:path[], bool:use_valve_fs=false, const String:valve_path_id[]="GAME"); native bool DirExists(const char[] path, bool use_valve_fs=false, const char[] valve_path_id="GAME");
/** /**
* Get the file size in bytes. * Get the file size in bytes.
@ -363,7 +532,7 @@ native bool:DirExists(const String:path[], bool:use_valve_fs=false, const String
* @param valve_path_id If use_valve_fs, a search path from gameinfo or NULL_STRING for all search paths. * @param valve_path_id If use_valve_fs, a search path from gameinfo or NULL_STRING for all search paths.
* @return File size in bytes, -1 if file not found. * @return File size in bytes, -1 if file not found.
*/ */
native FileSize(const String:path[], bool:use_valve_fs=false, const String:valve_path_id[]="GAME"); native int FileSize(const char[] path, bool use_valve_fs=false, const char[] valve_path_id="GAME");
/** /**
* Flushes a file's buffered output; any buffered output * Flushes a file's buffered output; any buffered output
@ -373,7 +542,7 @@ native FileSize(const String:path[], bool:use_valve_fs=false, const String:valve
* @return True on success or use_valve_fs specified with OpenFile, * @return True on success or use_valve_fs specified with OpenFile,
* otherwise false on failure. * otherwise false on failure.
*/ */
native FlushFile(Handle:file); native bool FlushFile(Handle file);
/** /**
* Removes a directory. * Removes a directory.
@ -382,7 +551,7 @@ native FlushFile(Handle:file);
* @param path Path to the directory. * @param path Path to the directory.
* @return True on success, false otherwise. * @return True on success, false otherwise.
*/ */
native bool:RemoveDir(const String:path[]); native bool RemoveDir(const char[] path);
#define FPERM_U_READ 0x0100 /* User can read. */ #define FPERM_U_READ 0x0100 /* User can read. */
#define FPERM_U_WRITE 0x0080 /* User can write. */ #define FPERM_U_WRITE 0x0080 /* User can write. */
@ -406,7 +575,7 @@ native bool:RemoveDir(const String:path[]);
* @param valve_path_id If use_valve_fs, a search path from gameinfo or NULL_STRING for default. * @param valve_path_id If use_valve_fs, a search path from gameinfo or NULL_STRING for default.
* In this case, mode is ignored. * In this case, mode is ignored.
*/ */
native bool:CreateDirectory(const String:path[], mode, bool:use_valve_fs=false, const String:valve_path_id[]="DEFAULT_WRITE_PATH"); native bool CreateDirectory(const char[] path, int mode, bool use_valve_fs=false, const char[] valve_path_id="DEFAULT_WRITE_PATH");
/** /**
* Changes a file or directories permissions. * Changes a file or directories permissions.
@ -415,7 +584,7 @@ native bool:CreateDirectory(const String:path[], mode, bool:use_valve_fs=false,
* @param mode Permissions to set. * @param mode Permissions to set.
* @return True on success, false otherwise. * @return True on success, false otherwise.
*/ */
native bool:SetFilePermissions(const String:path[], mode); native bool SetFilePermissions(const String:path[], int mode);
/** /**
* Returns a file timestamp as a unix timestamp. * Returns a file timestamp as a unix timestamp.
@ -424,7 +593,7 @@ native bool:SetFilePermissions(const String:path[], mode);
* @param tmode Time mode. * @param tmode Time mode.
* @return Time value, or -1 on failure. * @return Time value, or -1 on failure.
*/ */
native GetFileTime(const String:file[], FileTimeMode:tmode); native GetFileTime(const char[] file, FileTimeMode tmode);
/** /**
* Same as LogToFile(), except uses an open file Handle. The file must * Same as LogToFile(), except uses an open file Handle. The file must
@ -433,10 +602,9 @@ native GetFileTime(const String:file[], FileTimeMode:tmode);
* @param hndl Handle to the file. * @param hndl Handle to the file.
* @param message Message format. * @param message Message format.
* @param ... Message format parameters. * @param ... Message format parameters.
* @noreturn
* @error Invalid Handle. * @error Invalid Handle.
*/ */
native LogToOpenFile(Handle:hndl, const String:message[], any:...); native void LogToOpenFile(Handle hndl, const char[] message, any:...);
/** /**
* Same as LogToFileEx(), except uses an open file Handle. The file must * Same as LogToFileEx(), except uses an open file Handle. The file must
@ -445,8 +613,7 @@ native LogToOpenFile(Handle:hndl, const String:message[], any:...);
* @param hndl Handle to the file. * @param hndl Handle to the file.
* @param message Message format. * @param message Message format.
* @param ... Message format parameters. * @param ... Message format parameters.
* @noreturn
* @error Invalid Handle. * @error Invalid Handle.
*/ */
native LogToOpenFileEx(Handle:hndl, const String:message[], any:...); native void LogToOpenFileEx(Handle hndl, const char[] message, any:...);

View File

@ -241,18 +241,17 @@ stock FindTarget(client, const String:target[], bool:nobots = false, bool:immuni
ClearArray(array); ClearArray(array);
new Handle:file = OpenFile(mapPath, "rt"); File file = OpenFile(mapPath, "rt");
if (file == INVALID_HANDLE) if (!file) {
{
LogError("Could not open file: %s", mapPath); LogError("Could not open file: %s", mapPath);
return 0; return 0;
} }
LogMessage("Loading maps from file: %s", mapPath); LogMessage("Loading maps from file: %s", mapPath);
decl String:buffer[64], len; int len;
while (!IsEndOfFile(file) && ReadFileLine(file, buffer, sizeof(buffer))) char buffer[64];
while (!file.IsEndOfFile() && file.ReadLine(buffer, sizeof(buffer)))
{ {
TrimString(buffer); TrimString(buffer);
@ -274,6 +273,6 @@ stock FindTarget(client, const String:target[], bool:nobots = false, bool:immuni
PushArrayString(array, buffer); PushArrayString(array, buffer);
} }
CloseHandle(file); file.Close();
return GetArraySize(array); return GetArraySize(array);
} }

View File

@ -9,37 +9,37 @@ public Plugin:myinfo =
url = "http://www.sourcemod.net/" url = "http://www.sourcemod.net/"
}; };
public OnPluginStart() public void OnPluginStart()
{ {
RegServerCmd("test_fread1", Test_ReadBinStr); RegServerCmd("test_fread1", Test_ReadBinStr);
} }
Handle:OpenFile2(const String:path[], const String:mode[]) File OpenFile2(const String:path[], const String:mode[])
{ {
new Handle:file = OpenFile(path, mode); File file = OpenFile(path, mode);
if (file == INVALID_HANDLE) if (!file)
PrintToServer("Failed to open file %s for %s", path, mode); PrintToServer("Failed to open file %s for %s", path, mode);
else else
PrintToServer("Opened file handle %x: %s", file, path); PrintToServer("Opened file handle %x: %s", file, path);
return file; return file;
} }
public Action:Test_ReadBinStr(args) public Action Test_ReadBinStr(args)
{ {
new items[] = {1, 3, 5, 7, 0, 92, 193, 26, 0, 84, 248, 2}; int items[] = {1, 3, 5, 7, 0, 92, 193, 26, 0, 84, 248, 2};
new Handle:of = OpenFile2("smbintest", "wb"); File of = OpenFile2("smbintest", "wb");
if (of == INVALID_HANDLE) if (!of)
return Plugin_Handled; return Plugin_Handled;
WriteFile(of, items, sizeof(items), 1); of.Write(items, sizeof(items), 1);
CloseHandle(of); of.Close();
new Handle:inf = OpenFile2("smbintest", "rb"); File inf = OpenFile2("smbintest", "rb");
new String:buffer[sizeof(items)]; char buffer[sizeof(items)];
ReadFileString(inf, buffer, sizeof(items), sizeof(items)); inf.ReadString(buffer, sizeof(items), sizeof(items));
FileSeek(inf, 0, SEEK_SET); inf.Seek(0, SEEK_SET);
new items2[sizeof(items)]; int items2[sizeof(items)];
ReadFile(inf, items2, sizeof(items), 1); inf.Read(items2, sizeof(items), 1);
CloseHandle(inf); inf.Close();
for (new i = 0; i < sizeof(items); i++) for (new i = 0; i < sizeof(items); i++)
{ {
@ -55,6 +55,17 @@ public Action:Test_ReadBinStr(args)
} }
} }
inf = OpenFile2("smbintest", "rb");
for (new i = 0; i < sizeof(items); i++)
{
new item;
if (!inf.ReadInt8(item) || item != items[i])
{
PrintToServer("FAILED ON INDEX %d: %d != %d", i, item, items[i]);
return Plugin_Handled;
}
}
PrintToServer("Test passed!"); PrintToServer("Test passed!");
return Plugin_Handled; return Plugin_Handled;

View File

@ -4170,7 +4170,7 @@ static void dodelete()
switch (ident) { switch (ident) {
case iFUNCTN: case iFUNCTN:
case iREFFUNC: case iREFFUNC:
error(115, "functions"); error(167, "functions");
return; return;
case iARRAY: case iARRAY:
@ -4180,7 +4180,7 @@ static void dodelete()
{ {
symbol *sym = sval.val.sym; symbol *sym = sval.val.sym;
if (!sym || sym->dim.array.level > 0) { if (!sym || sym->dim.array.level > 0) {
error(115, "arrays"); error(167, "arrays");
return; return;
} }
break; break;
@ -4188,13 +4188,13 @@ static void dodelete()
} }
if (sval.val.tag == 0) { if (sval.val.tag == 0) {
error(115, "primitive types or enums"); error(167, "integers");
return; return;
} }
methodmap_t *map = methodmap_find_by_tag(sval.val.tag); methodmap_t *map = methodmap_find_by_tag(sval.val.tag);
if (!map) { if (!map) {
error(115, pc_tagname(sval.val.tag)); error(115, "type", pc_tagname(sval.val.tag));
return; return;
} }

View File

@ -210,6 +210,7 @@ static const char *errmsg[] = {
/*164*/ "allocated array type '%s' doesn't match original type '%s'\n", /*164*/ "allocated array type '%s' doesn't match original type '%s'\n",
/*165*/ "cannot create dynamic arrays in static scope - did you mean to create a fixed-length array with brackets after the variable name?\n", /*165*/ "cannot create dynamic arrays in static scope - did you mean to create a fixed-length array with brackets after the variable name?\n",
/*166*/ "cannot use 'this' outside of a methodmap method or property\n", /*166*/ "cannot use 'this' outside of a methodmap method or property\n",
/*167*/ "cannot use delete, %s do not have destructors\n",
#else #else
"\315e\306\227\266k\217:\235\277bu\201fo\220\204\223\012", "\315e\306\227\266k\217:\235\277bu\201fo\220\204\223\012",
"\202l\224\250s\205g\346\356e\233\201(\243\315\214\267\202) \253 f\255low ea\305 \042c\353e\042\012", "\202l\224\250s\205g\346\356e\233\201(\243\315\214\267\202) \253 f\255low ea\305 \042c\353e\042\012",