Raised This Month: $12 Target: $400
 3% 

How not to hard code paths


Post New Thread Reply   
 
Thread Tools Display Modes
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
Zenith77
Veteran Member
Join Date: Aug 2005
Old 07-06-2006 , 17:41   Re: How not to hard code paths
Reply With Quote #2

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.
__________________
Quote:
Originally Posted by phorelyph View Post
your retatred
Zenith77 is offline
TheNewt
Donor
Join Date: Jun 2006
Location: Where I live.
Old 07-06-2006 , 18:02   Re: How not to hard code paths
Reply With Quote #3

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?
__________________
Quote:
toe3_ left the chat room. (G-lined (AUTO Excessive connections from a single host.))
TheNewt is offline
Xanimos
Veteran Member
Join Date: Apr 2005
Location: Florida
Old 07-06-2006 , 18:23   Re: How not to hard code paths
Reply With Quote #4

Yes, it would.
Xanimos is offline
Send a message via AIM to Xanimos Send a message via MSN to Xanimos
jtp10181
Veteran Member
Join Date: May 2004
Location: Madison, WI
Old 07-06-2006 , 20:19   Re: How not to hard code paths
Reply With Quote #5

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
__________________
jtp10181 is offline
Send a message via ICQ to jtp10181 Send a message via AIM to jtp10181 Send a message via MSN to jtp10181 Send a message via Yahoo to jtp10181
v3x
Veteran Member
Join Date: Oct 2004
Location: US
Old 07-07-2006 , 01:19   Re: How not to hard code paths
Reply With Quote #6

Thanks, guys! I'll update it now
__________________
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.
v3x is offline
TheNewt
Donor
Join Date: Jun 2006
Location: Where I live.
Old 07-07-2006 , 09:25   Re: How not to hard code paths
Reply With Quote #7

Thanks for the info! Way better then what I did. >_<
__________________
Quote:
toe3_ left the chat room. (G-lined (AUTO Excessive connections from a single host.))
TheNewt is offline
RedRobster
Veteran Member
Join Date: Apr 2010
Location: Your Closet
Old 05-31-2010 , 11:33   Re: How not to hard code paths
Reply With Quote #8

Also, shouldn't it be
PHP Code:
  get_localinfo("amxx_configsdir"configsdir63); 
Instead of
PHP Code:
  get_localinfo("amxx_configsdir"configdir63); 
? o.0
RedRobster is offline
jim_yang
Veteran Member
Join Date: Aug 2006
Old 05-31-2010 , 11:49   Re: How not to hard code paths
Reply With Quote #9

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.
__________________
Project : CSDM all in one - 99%
<team balancer#no round end#entity remover#quake sounds#fake full#maps management menu#players punishment menu#no team flash#colored flashbang#grenade trails#HE effect#spawn protection#weapon arena#weapon upgrade#auto join#no weapon drop#one name>
jim_yang is offline
RedRobster
Veteran Member
Join Date: Apr 2010
Location: Your Closet
Old 05-31-2010 , 12:08   Re: How not to hard code paths
Reply With Quote #10

OK, thank you. Just checking.
RedRobster is offline
Old 06-08-2010, 19:11
jin123
This message has been deleted by Emp`. Reason: spam bot
Old 06-08-2010, 19:31
fysiks
This message has been deleted by Emp`. Reason: replies to spam bot
Old 06-09-2010, 03:34
SaM.ThE.MaN
This message has been deleted by Emp`. Reason: replies to spam bot
Old 06-09-2010, 12:27
fysiks
This message has been deleted by Emp`. Reason: replies to spam bot
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 00:59.


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