VFS: Add more sane FileRead/Write* natives to read/write a single value

This commit is contained in:
Arkshine 2015-03-09 00:38:05 +01:00
parent a580c8c5e5
commit 71ab8d560e
2 changed files with 135 additions and 5 deletions

View File

@ -1132,11 +1132,49 @@ static cell SetFilePermissions(AMX *amx, cell *params)
}
return _chmod(realpath, mask) == 0;
#elif
#else
return chmod(realpath, params[2]) == 0;
#endif
}
template <typename T>
static cell File_ReadTyped(AMX *amx, cell *params)
{
FileObject* fp = reinterpret_cast<FileObject*>(params[1]);
if (!fp)
{
return 0;
}
cell* data = get_amxaddr(amx, params[2]);
T value;
if (fp->Read(&value, sizeof(value)) != sizeof(value))
{
return 0;
}
*data = value;
return 1;
}
template <typename T>
static cell File_WriteTyped(AMX *amx, cell *params)
{
FileObject* fp = reinterpret_cast<FileObject*>(params[1]);
if (!fp)
{
return 0;
}
T value = static_cast<T>(params[2]);
return !!(fp->Write(&value, sizeof(value)) == sizeof(value));
}
AMX_NATIVE_INFO file_Natives[] =
{
{"read_dir", read_dir},
@ -1184,7 +1222,14 @@ AMX_NATIVE_INFO file_Natives[] =
{"LoadFileForMe", LoadFileForMe},
{"GetFileTime", GetFileTime},
{"SetFilePermissions", SetFilePermissions},
{"FileReadInt8", File_ReadTyped<int8_t>},
{"FileReadUint8", File_ReadTyped<uint8_t>},
{"FileReadInt16", File_ReadTyped<int16_t>},
{"FileReadUint16", File_ReadTyped<uint16_t>},
{"FileReadInt32", File_ReadTyped<int32_t>},
{"FileWriteInt8", File_WriteTyped<int8_t>},
{"FileWriteInt16", File_WriteTyped<int16_t>},
{"FileWriteInt32", File_WriteTyped<int32_t>},
{NULL, NULL}
};

View File

@ -566,3 +566,88 @@ native GetFileTime(const file[], FileTimeType:tmode);
* @return True on success, false otherwise
*/
native bool:SetFilePermissions(const path[], mode);
/**
* Reads a single int8 (byte) from a file. The returned value is sign-
* extended to an int32.
*
* @param file Handle to the file
* @param data Variable to store the data read
*
* @return True on success, false on failure
*/
native bool:FileReadInt8(file, &any:data);
/**
* Reads a single uint8 (unsigned byte) from a file. The returned value is
* zero-extended to an int32.
*
* @param file Handle to the file
* @param data Variable to store the data read
*
* @return True on success, false on failure
*/
native bool:FileReadUint8(file, &any:data);
/**
* Reads a single int16 (short) from a file. The value is sign-extended to
* an int32.
*
* @param file Handle to the file
* @param data Variable to store the data read
*
* @return True on success, false on failure
*/
native bool:FileReadInt16(file, &any:data);
/**
* Reads a single unt16 (unsigned short) from a file. The value is zero-
* extended to an int32.
*
* @param file Handle to the file
* @param data Variable to store the data read
*
* @return True on success, false on failure
*/
native bool:FileReadUint16(file, &any:data);
/**
* Reads a single int32 (int/cell) from a file.
*
* @param file Handle to the file
* @param data Variable to store the data read
*
* @return True on success, false on failure
*/
native bool:FileReadInt32(file, &any:data);
/**
* Writes a single int8 (byte) to a file.
*
* @param file Handle to the file
* @param data Data to write (truncated to an int8)
*
* @return True on success, false on failure
*/
native bool:FileWriteInt8(file, any:data);
/**
* Writes a single int16 (short) to a file.
*
* @param file Handle to the file
* @param data Data to write (truncated to an int16)
*
* @return True on success, false on failure
*/
native bool:FileWriteInt16(file, any:data);
/**
* Writes a single int32 (int/cell) to a file.
*
* @param file Handle to the file
* @param data Data to write
*
* @return True on success, false on failure
*/
native bool:FileWriteInt32(file, any:data);