Rework build pathname functions (#422)

* Rework build_pathname* functions

* Replace old platform defines with the new ones

* Correct usage of build_pathname_r()

* Fix inconsistencies (white spaces)

* Remove useless defines
This commit is contained in:
Karol Szuster 2017-03-11 19:26:25 +01:00 committed by Vincent Herbet
parent fa3d28872e
commit 9551c70c59
13 changed files with 688 additions and 734 deletions

View File

@ -61,8 +61,8 @@ void CPluginMngr::Finalize()
int CPluginMngr::loadPluginsFromFile(const char* filename, bool warn)
{
char file[256];
FILE *fp = fopen(build_pathname_r(file, sizeof(file) - 1, "%s", filename), "rt");
char file[PLATFORM_MAX_PATH];
FILE *fp = fopen(build_pathname_r(file, sizeof(file), "%s", filename), "rt");
if (!fp)
{
@ -276,8 +276,8 @@ CPluginMngr::CPlugin::CPlugin(int i, const char* p, const char* n, char* e, int
author = unk;
version = unk;
char file[256];
char* path = build_pathname_r(file, sizeof(file) - 1, "%s/%s", p, n);
char file[PLATFORM_MAX_PATH];
char* path = build_pathname_r(file, sizeof(file), "%s/%s", p, n);
code = 0;
memset(&amx, 0, sizeof(AMX));
int err = load_amxscript(&amx, &code, path, e, d);
@ -688,8 +688,8 @@ void CPluginMngr::CacheAndLoadModules(const char *plugin)
void CPluginMngr::CALMFromFile(const char *file)
{
char filename[256];
FILE *fp = fopen(build_pathname_r(filename, sizeof(filename) - 1, "%s", file), "rt");
char filename[PLATFORM_MAX_PATH];
FILE *fp = fopen(build_pathname_r(filename, sizeof(filename), "%s", file), "rt");
if (!fp)
{
@ -751,7 +751,7 @@ void CPluginMngr::CALMFromFile(const char *file)
continue;
}
build_pathname_r(filename, sizeof(filename)-1, "%s/%s", get_localinfo("amxx_pluginsdir", "addons/amxmodx/plugins"), pluginName);
build_pathname_r(filename, sizeof(filename), "%s/%s", get_localinfo("amxx_pluginsdir", "addons/amxmodx/plugins"), pluginName);
CacheAndLoadModules(filename);
}

View File

@ -1468,9 +1468,9 @@ static cell AMX_NATIVE_CALL amx_md5_file(AMX *amx, cell *params)
{
int len;
char *str = get_amxstring(amx, params[1], 0, len);
char file[255];
char file[PLATFORM_MAX_PATH];
build_pathname_r(file, sizeof(file)-1, "%s", str);
build_pathname_r(file, sizeof(file), "%s", str);
const char *hash = hashFile((const char *)file, Hash_Md5);
if (!hash)
@ -1502,8 +1502,8 @@ static cell AMX_NATIVE_CALL amx_hash_file(AMX *amx, cell *params)
{
int len;
char *str = get_amxstring(amx, params[1], 0, len);
char file[255];
build_pathname_r(file, sizeof(file)-1, "%s", str);
char file[PLATFORM_MAX_PATH];
build_pathname_r(file, sizeof(file), "%s", str);
HashType type = (HashType)params[2];
@ -2016,13 +2016,13 @@ static cell AMX_NATIVE_CALL log_to_file(AMX *amx, cell *params) /* 1 param */
int ilen;
char* szFile = get_amxstring(amx, params[1], 0, ilen);
FILE*fp;
char file[256];
char file[PLATFORM_MAX_PATH];
if (strchr(szFile, '/') || strchr(szFile, '\\'))
{
build_pathname_r(file, sizeof(file) - 1, "%s", szFile);
build_pathname_r(file, sizeof(file), "%s", szFile);
} else {
build_pathname_r(file, sizeof(file) - 1, "%s/%s", g_log_dir.chars(), szFile);
build_pathname_r(file, sizeof(file), "%s/%s", g_log_dir.chars(), szFile);
}
bool first_time = true;
@ -3891,8 +3891,8 @@ static cell AMX_NATIVE_CALL get_lang(AMX *amx, cell *params)
static cell AMX_NATIVE_CALL register_dictionary(AMX *amx, cell *params)
{
int len;
static char file[256];
int result = g_langMngr.MergeDefinitionFile(build_pathname_r(file, sizeof(file) - 1, "%s/lang/%s", get_localinfo("amxx_datadir", "addons/amxmodx/data"), get_amxstring(amx, params[1], 1, len)));
static char file[PLATFORM_MAX_PATH];
int result = g_langMngr.MergeDefinitionFile(build_pathname_r(file, sizeof(file), "%s/lang/%s", get_localinfo("amxx_datadir", "addons/amxmodx/data"), get_amxstring(amx, params[1], 1, len)));
return result;
}

View File

@ -10,7 +10,7 @@
#ifndef AMXMODX_H
#define AMXMODX_H
#if defined(__linux__) || defined(__APPLE__)
#if defined PLATFORM_POSIX
#include <unistd.h>
#include <stdlib.h>
#include "sclinux.h"
@ -75,7 +75,7 @@ extern AMX_NATIVE_INFO g_TextParserNatives[];
extern AMX_NATIVE_INFO g_CvarNatives[];
extern AMX_NATIVE_INFO g_GameConfigNatives[];
#if defined(_WIN32)
#if defined PLATFORM_WINDOWS
#define DLLOAD(path) (DLHANDLE)LoadLibrary(path)
#define DLPROC(m, func) GetProcAddress(m, func)
#define DLFREE(m) FreeLibrary(m)
@ -96,21 +96,13 @@ extern AMX_NATIVE_INFO g_GameConfigNatives[];
#endif
#endif
#if defined(_WIN32)
#if defined PLATFORM_WINDOWS
typedef HINSTANCE DLHANDLE;
#else
typedef void* DLHANDLE;
#define INFINITE 0xFFFFFFFF
#endif
#if defined(_WIN32)
#define PATH_SEP_CHAR '\\'
#define ALT_SEP_CHAR '/'
#else
#define PATH_SEP_CHAR '/'
#define ALT_SEP_CHAR '\\'
#endif
#ifndef GETPLAYERAUTHID
#define GETPLAYERAUTHID (*g_engfuncs.pfnGetPlayerAuthId)
#endif

View File

@ -75,14 +75,14 @@ void CLog::CreateNewFile()
time(&td);
tm *curTime = localtime(&td);
char file[256];
char file[PLATFORM_MAX_PATH];
char name[256];
int i = 0;
while (true)
{
ke::SafeSprintf(name, sizeof(name), "%s/L%02d%02d%03d.log", g_log_dir.chars(), curTime->tm_mon + 1, curTime->tm_mday, i);
build_pathname_r(file, sizeof(file)-1, "%s", name);
build_pathname_r(file, sizeof(file), "%s", name);
FILE *pTmpFile = fopen(file, "r"); // open for reading to check whether the file exists
if (!pTmpFile)
@ -108,8 +108,8 @@ void CLog::CreateNewFile()
void CLog::UseFile(const ke::AString &fileName)
{
static char file[256];
m_LogFile = build_pathname_r(file, sizeof(file) - 1, "%s/%s", g_log_dir.chars(), fileName.chars());
static char file[PLATFORM_MAX_PATH];
m_LogFile = build_pathname_r(file, sizeof(file), "%s/%s", g_log_dir.chars(), fileName.chars());
}
void CLog::SetLogType(const char* localInfo)
@ -128,11 +128,11 @@ void CLog::SetLogType(const char* localInfo)
void CLog::MapChange()
{
// create dir if not existing
char file[256];
char file[PLATFORM_MAX_PATH];
#if defined(__linux__) || defined(__APPLE__)
mkdir(build_pathname_r(file, sizeof(file)-1, "%s", g_log_dir.chars()), 0700);
mkdir(build_pathname_r(file, sizeof(file), "%s", g_log_dir.chars()), 0700);
#else
mkdir(build_pathname_r(file, sizeof(file) - 1, "%s", g_log_dir.chars()));
mkdir(build_pathname_r(file, sizeof(file), "%s", g_log_dir.chars()));
#endif
SetLogType("amxx_logging");
@ -152,7 +152,7 @@ void CLog::MapChange()
void CLog::Log(const char *fmt, ...)
{
static char file[256];
static char file[PLATFORM_MAX_PATH];
if (m_LogType == 1 || m_LogType == 2)
{
@ -189,7 +189,7 @@ void CLog::Log(const char *fmt, ...)
}
}
} else {
build_pathname_r(file, sizeof(file) - 1, "%s/L%04d%02d%02d.log", g_log_dir.chars(), (curTime->tm_year + 1900), curTime->tm_mon + 1, curTime->tm_mday);
build_pathname_r(file, sizeof(file), "%s/L%04d%02d%02d.log", g_log_dir.chars(), (curTime->tm_year + 1900), curTime->tm_mon + 1, curTime->tm_mday);
pF = fopen(file, "a+");
}
@ -218,7 +218,7 @@ void CLog::Log(const char *fmt, ...)
void CLog::LogError(const char *fmt, ...)
{
static char file[256];
static char file[PLATFORM_MAX_PATH];
static char name[256];
if (m_FoundError)
@ -244,7 +244,7 @@ void CLog::LogError(const char *fmt, ...)
FILE *pF = NULL;
ke::SafeSprintf(name, sizeof(name), "%s/error_%04d%02d%02d.log", g_log_dir.chars(), curTime->tm_year + 1900, curTime->tm_mon + 1, curTime->tm_mday);
build_pathname_r(file, sizeof(file)-1, "%s", name);
build_pathname_r(file, sizeof(file), "%s", name);
pF = fopen(file, "a+");
if (pF)
@ -266,4 +266,3 @@ void CLog::LogError(const char *fmt, ...)
// print on server console
print_srvconsole("L %s: %s\n", date, msg);
}

View File

@ -49,8 +49,8 @@ int LookupFile(AMX_DBG *amxdbg, ucell address)
bool BinLog::Open()
{
const char *data = get_localinfo("amxmodx_datadir", "addons/amxmodx/data");
char path[255];
build_pathname_r(path, sizeof(path)-1, "%s/binlogs", data);
char path[PLATFORM_MAX_PATH];
build_pathname_r(path, sizeof(path), "%s/binlogs", data);
if (!DirExists(path))
{
@ -63,8 +63,8 @@ bool BinLog::Open()
return false;
}
char file[255];
build_pathname_r(file, sizeof(file)-1, "%s/binlogs/lastlog", data);
char file[PLATFORM_MAX_PATH];
build_pathname_r(file, sizeof(file), "%s/binlogs/lastlog", data);
unsigned int lastcntr = 0;
FILE *lastlog = fopen(file, "rb");
@ -81,7 +81,7 @@ bool BinLog::Open()
fwrite(&lastcntr, sizeof(int), 1, lastlog);
fclose(lastlog);
}
build_pathname_r(file, sizeof(file)-1, "%s/binlogs/binlog%04d.blg", data, lastcntr);
build_pathname_r(file, sizeof(file), "%s/binlogs/binlog%04d.blg", data, lastcntr);
m_logfile = file;
/**

View File

@ -1015,8 +1015,8 @@ static cell AMX_NATIVE_CALL rename_file(AMX *amx, cell *params)
if (params[0] / sizeof(cell) >= 3 && params[3] > 0)
{
build_pathname_r(file_old_r, sizeof(file_old_r) - 1, "%s", file_old);
build_pathname_r(file_new_r, sizeof(file_new_r) - 1, "%s", file_new);
build_pathname_r(file_old_r, sizeof(file_old_r), "%s", file_old);
build_pathname_r(file_new_r, sizeof(file_new_r), "%s", file_new);
}
else
{

View File

@ -187,9 +187,9 @@ void ParseAndOrAdd(CStack<ke::AString *> & files, const char *name)
void BuildPluginFileList(const char *initialdir, CStack<ke::AString *> & files)
{
char path[255];
char path[PLATFORM_MAX_PATH];
#if defined WIN32
build_pathname_r(path, sizeof(path)-1, "%s/*.ini", initialdir);
build_pathname_r(path, sizeof(path), "%s/*.ini", initialdir);
_finddata_t fd;
intptr_t handle = _findfirst(path, &fd);
@ -205,7 +205,7 @@ void BuildPluginFileList(const char *initialdir, CStack<ke::AString *> & files)
_findclose(handle);
#elif defined(__linux__) || defined(__APPLE__)
build_pathname_r(path, sizeof(path)-1, "%s/", initialdir);
build_pathname_r(path, sizeof(path), "%s/", initialdir);
struct dirent *ep;
DIR *dp;
@ -463,8 +463,8 @@ int C_Spawn(edict_t *pent)
CVAR_SET_STRING(init_amxmodx_modules.name, buffer);
// ###### Load Vault
char file[255];
g_vault.setSource(build_pathname_r(file, sizeof(file) - 1, "%s", get_localinfo("amxx_vault", "addons/amxmodx/configs/vault.ini")));
char file[PLATFORM_MAX_PATH];
g_vault.setSource(build_pathname_r(file, sizeof(file), "%s", get_localinfo("amxx_vault", "addons/amxmodx/configs/vault.ini")));
g_vault.loadVault();
// ###### Init time and freeze tasks

View File

@ -31,6 +31,7 @@
#include "trie_natives.h"
#include "CDataPack.h"
#include "CGameConfigs.h"
#include <amtl/os/am-path.h>
CList<CModule, const char*> g_modules;
CList<CScript, AMX*> g_loadedscripts;
@ -671,77 +672,41 @@ void get_modname(char* buffer)
strcpy(buffer, g_mod_name.chars());
}
char* build_pathname(const char *fmt, ...)
char *build_pathname(const char *fmt, ...)
{
static char string[256];
int b;
int a = b = ke::SafeSprintf(string, sizeof(string), "%s%c", g_mod_name.chars(), PATH_SEP_CHAR);
static char string[PLATFORM_MAX_PATH];
auto len = ke::path::Format(string, sizeof(string), "%s/", g_mod_name.chars());
va_list argptr;
va_start(argptr, fmt);
a += vsnprintf (&string[a], 255 - a, fmt, argptr);
string[a] = 0;
ke::path::FormatVa(&string[len], sizeof(string) - len, fmt, argptr);
va_end(argptr);
char* path = &string[b];
while (*path)
{
if (*path == ALT_SEP_CHAR)
{
*path = PATH_SEP_CHAR;
}
++path;
}
return string;
}
char *build_pathname_r(char *buffer, size_t maxlen, const char *fmt, ...)
{
ke::SafeSprintf(buffer, maxlen, "%s%c", g_mod_name.chars(), PATH_SEP_CHAR);
size_t len = strlen(buffer);
char *ptr = buffer + len;
auto len = ke::path::Format(buffer, maxlen, "%s/", g_mod_name.chars());
va_list argptr;
va_start(argptr, fmt);
vsnprintf (ptr, maxlen-len, fmt, argptr);
ke::path::FormatVa(&buffer[len], maxlen - len, fmt, argptr);
va_end (argptr);
while (*ptr)
{
if (*ptr == ALT_SEP_CHAR)
{
*ptr = PATH_SEP_CHAR;
}
++ptr;
}
return buffer;
}
// build pathname based on addons dir
char* build_pathname_addons(const char *fmt, ...)
char *build_pathname_addons(const char *fmt, ...)
{
static char string[256];
static char string[PLATFORM_MAX_PATH];
va_list argptr;
va_start(argptr, fmt);
vsnprintf (string, 255, fmt, argptr);
ke::path::FormatVa(string, sizeof(string), fmt, argptr);
va_end(argptr);
char* path = string;
while (*path)
{
if (*path == ALT_SEP_CHAR)
{
*path = PATH_SEP_CHAR;
}
++path;
}
return string;
}
@ -762,7 +727,7 @@ bool ConvertModuleName(const char *pathString, char *path)
/* run to filename instead of dir */
char *ptr = tmpname;
ptr = tmpname + len - 1;
while (ptr >= tmpname && *ptr != PATH_SEP_CHAR)
while (ptr >= tmpname && *ptr != PLATFORM_SEP_CHAR)
ptr--;
if (ptr >= tmpname)
{
@ -822,7 +787,7 @@ bool ConvertModuleName(const char *pathString, char *path)
*ptr = '\0';
}
size_t length = ke::SafeSprintf(path, PLATFORM_MAX_PATH, "%s%c%s_amxx", orig_path, PATH_SEP_CHAR, tmpname);
auto length = ke::path::Format(path, PLATFORM_MAX_PATH, "%s/%s_amxx", orig_path, tmpname);
#if defined PLATFORM_LINUX
# if defined AMD64 || PAWN_CELL_SIZE == 64
@ -843,7 +808,7 @@ bool LoadModule(const char *shortname, PLUG_LOADTIME now, bool simplify, bool no
build_pathname_r(
pathString,
sizeof(pathString)-1,
sizeof(pathString),
"%s/%s",
get_localinfo("amxx_modulesdir", "addons/amxmodx/modules"),
shortname);

View File

@ -307,7 +307,7 @@ int ReadConfig(void)
{
char FileName[512];
MF_BuildPathnameR(FileName,sizeof(FileName)-1,"%s",get_localinfo("amxx_configsdir","addons/amxmodx/configs"));
MF_BuildPathnameR(FileName,sizeof(FileName),"%s",get_localinfo("amxx_configsdir","addons/amxmodx/configs"));
strncat(FileName,"/hamdata.ini",sizeof(FileName)-1);

View File

@ -46,7 +46,7 @@ static cell nvault_open(AMX *amx, cell *params)
int len, id=-1;
char *name = MF_GetAmxString(amx, params[1], 0, &len);
char path[255], file[255];
MF_BuildPathnameR(path, sizeof(path)-1, "%s/vault", MF_GetLocalInfo("amxx_datadir", "addons/amxmodx/data"));
MF_BuildPathnameR(path, sizeof(path), "%s/vault", MF_GetLocalInfo("amxx_datadir", "addons/amxmodx/data"));
sprintf(file, "%s/%s.vault", path, name);
for (size_t i=0; i<g_Vaults.length(); i++)
{

View File

@ -59,13 +59,13 @@ static cell AMX_NATIVE_CALL SQL_MakeDbTuple(AMX *amx, cell *params)
char path[255];
FILE *fp;
MF_BuildPathnameR(path, sizeof(path)-1, "%s", db);
MF_BuildPathnameR(path, sizeof(path), "%s", db);
if ((fp=fopen(path, "rb")))
{
fclose(fp);
sql->db = strdup(path);
} else {
MF_BuildPathnameR(path, sizeof(path)-1, "%s/sqlite3/%s.sq3",
MF_BuildPathnameR(path, sizeof(path), "%s/sqlite3/%s.sq3",
MF_GetLocalInfo("amxx_datadir", "addons/amxmodx/data"),
db);
sql->db = strdup(path);
@ -630,4 +630,3 @@ AMX_NATIVE_INFO g_BaseSqlNatives[] =
{NULL, NULL},
};

View File

@ -69,7 +69,7 @@ void OnAmxxAttach()
MF_OverrideNatives(g_OldCompatNatives, MODULE_NAME);
char path[255];
MF_BuildPathnameR(path, sizeof(path)-1, "%s/sqlite3", MF_GetLocalInfo("amxx_datadir", "addons/amxmodx/data"));
MF_BuildPathnameR(path, sizeof(path), "%s/sqlite3", MF_GetLocalInfo("amxx_datadir", "addons/amxmodx/data"));
if (!DirExists(path))
{
mkdir(path
@ -96,4 +96,3 @@ void OnPluginsUnloaded()
extern "C" void __cxa_pure_virtual(void)
{
}

View File

@ -73,14 +73,14 @@ static cell AMX_NATIVE_CALL dbi_connect(AMX *amx, cell *params)
int err;
char error[512];
/** if there is an older database, read there instead of the new path */
MF_BuildPathnameR(path, sizeof(path)-1, "%s", info.database);
MF_BuildPathnameR(path, sizeof(path), "%s", info.database);
FILE *fp = fopen(path, "rb");
if (fp)
{
fclose(fp);
info.database = path;
} else {
MF_BuildPathnameR(path, sizeof(path)-1, "%s/sqlite3/%s.sq3",
MF_BuildPathnameR(path, sizeof(path), "%s/sqlite3/%s.sq3",
MF_GetLocalInfo("amxx_datadir", "addons/amxmodx/data"),
info.database);
info.database = path;