AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Code Snippets/Tutorials (https://forums.alliedmods.net/forumdisplay.php?f=83)
-   -   How not to hard code paths (https://forums.alliedmods.net/showthread.php?t=40946)

v3x 07-06-2006 15:53

How not to hard code paths
 
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! :P

Zenith77 07-06-2006 17:41

Re: How not to hard code paths
 
You can also use get_localinfo() to find the above dir. This is really just a more complicated method. I thought I would just post for reference.

TheNewt 07-06-2006 18:02

Re: How not to hard code paths
 
Code:
  new configsdir[64];   get_configsdir(g_configsdir, 63);   format(g_file , 83 , "%s/file.cfg" , configsdir);
wouldn't it be get_configsdir(configsdir, 63) not g_configsdir?

Xanimos 07-06-2006 18:23

Re: How not to hard code paths
 
Yes, it would.

jtp10181 07-06-2006 20:19

Re: How not to hard code paths
 
Just some FYI, get_localinfo() will get you any of the paths defined in the core.ini, here is an example of a stock core.ini

Code:

; Configuration file for AMX Mod X
amxx_logdir 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


v3x 07-07-2006 01:19

Re: How not to hard code paths
 
Thanks, guys! I'll update it now :)

TheNewt 07-07-2006 09:25

Re: How not to hard code paths
 
Thanks for the info! Way better then what I did. >_<

RedRobster 05-31-2010 11:33

Re: How not to hard code paths
 
Also, shouldn't it be
PHP Code:

  get_localinfo("amxx_configsdir"configsdir63); 

Instead of
PHP Code:

  get_localinfo("amxx_configsdir"configdir63); 

? o.0

jim_yang 05-31-2010 11:49

Re: How not to hard code paths
 
your eyes are sharp, just a typo and I think everyone will notice that when they use it. and I don't think the author is here lately.

RedRobster 05-31-2010 12:08

Re: How not to hard code paths
 
OK, thank you. :) Just checking.


All times are GMT -4. The time now is 15:43.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.