View Single Post
Author Message
v3x
Veteran Member
Join Date: Oct 2004
Location: US
Old 07-06-2006 , 15:53   How not to hard code paths
Reply With Quote #1

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"configsdir63);
  
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!
__________________
What am I doing these days? Well, I run my own Rust server. It's heavily modded. If you'd like to join, the ip is 167.114.101.67:28116

I also created a website called Rust Tools. It will calculate and tell you the raw amounts of resources needed to craft items.

Last edited by HamletEagle; 07-08-2016 at 05:22. Reason: Fixed wrong logs dir
v3x is offline