From 468e6fb9dd2c5b7085a8422f616a4ec99626e3a4 Mon Sep 17 00:00:00 2001 From: David Anderson Date: Mon, 22 Oct 2007 19:26:45 +0000 Subject: [PATCH] added amb909 - LoadFileForMe() --- amxmodx/file.cpp | 32 ++++++++++++++++++++++++++++++++ plugins/include/file.inc | 17 +++++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/amxmodx/file.cpp b/amxmodx/file.cpp index 412d8e37..cc8236e4 100755 --- a/amxmodx/file.cpp +++ b/amxmodx/file.cpp @@ -824,6 +824,37 @@ static cell AMX_NATIVE_CALL amx_rename(AMX *amx, cell *params) #endif } +static cell LoadFileForMe(AMX *amx, cell *params) +{ + int len; + char *file = get_amxstring(amx, params[1], 0, len); + char path[256]; + + build_pathname_r(path, sizeof(path), "%s", file); + + byte *addr = LOAD_FILE_FOR_ME(path, &len); + if (addr == NULL) + { + return -1; + } + + cell *buffer = get_amxaddr(amx, params[2]); + cell maxlength = params[3]; + cell *bytes_avail = get_amxaddr(amx, params[4]); + + *bytes_avail = len; + + cell count; + for (count = 0; count < len && count < maxlength; count++) + { + buffer[count] = addr[count]; + } + + FREE_FILE(addr); + + return count; +} + AMX_NATIVE_INFO file_Natives[] = { {"delete_file", delete_file}, @@ -859,5 +890,6 @@ AMX_NATIVE_INFO file_Natives[] = {"rmdir", amx_rmdir}, {"fputs", amx_fputs}, {"rename_file", amx_rename}, + {"LoadFileForMe", LoadFileForMe}, {NULL, NULL} }; diff --git a/plugins/include/file.inc b/plugins/include/file.inc index fedd2c2d..c284787f 100755 --- a/plugins/include/file.inc +++ b/plugins/include/file.inc @@ -115,3 +115,20 @@ native unlink(const filename[]); native open_dir(dir[], firstfile[], length); native next_file(dirh, buffer[], length); native close_dir(dirh); + +/** + * Loads a file using the LoadFileForMe engine function. + * + * The data is truncated if there is not enough space. No null-terminator + * is applied; the data is the raw contents of the file. + * + * @param file File to load (may be a file from the GCF). + * @param buffer Buffer to store file contents. + * @param maxlength Maximum size of the file buffer. + * @param length Variable to store the file length. This may return + * a number larger than the buffer size. + * @return -1 if the file could not be loaded. Otherwise, + * the number of cells actually written to the buffer + * are returned. + */ +native LoadFileForMe(const file[], buffer[], maxlength, &length=0);