How not to hard code paths
You may have heard the term "hard coding" a few times before. Here's a quote from Wikipedia.org.
Quote:
To hard code or hard coding (also, hard-code/hard-coding, hardcode/hardcoding) refers to the software development practice of embedding output data directly into the source code of a program or other executable object, instead of obtaining that data from external sources or generating it by the program itself with the given input.
Considered an anti-pattern or "Bad Thing?", hard coding requires the program's source code to be changed any time the input data changes, when it might be more convenient to the end user to change the detail by some means outside the program.
|
With the help of a few functions, we can stop using these hardcoded paths.
Example of a hard coded path
PHP Code:
new const g_file[] = "addons/amxmodx/configs/file.cfg";
Example of a non-hard-coded path
PHP Code:
new const g_file[84];
public plugin_init()
{
// ...
static configsdir[64];
get_localinfo("amxx_configsdir", configsdir, 63);
format(g_file , 83 , "%s/file.cfg" , configsdir);
}
Now the path and file name are stored in the
g_file variable. The variable is global so you can easily use it in other functions.
Other info values for getting directory paths
Note: All are followed by an example. Use only the first part of each one!
Quote:
amxx_logs addons/amxmodx/logs
amxx_configsdir addons/amxmodx/configs
amxx_datadir addons/amxmodx/data
amxx_modules addons/amxmodx/configs/modules.ini
amxx_plugins addons/amxmodx/configs/plugins.ini
amxx_pluginsdir addons/amxmodx/plugins
amxx_modulesdir addons/amxmodx/modules
amxx_vault addons/amxmodx/data/vault.ini
csstats_score addons/amxmodx/data/csstats.amxx
csstats addons/amxmodx/data/csstats.dat
|
SO STOP HARD CODING PATHS!
__________________