2
\$\begingroup\$

I load in static setting via an ini file. Ini file have the benefits

  • of NOT being server by most web servers by default.
  • easier to edit than XML
  • even simplier than JSON

Setting get loaded in Application.cfc

void function setupApplication() output="false" {
... 
application.stAdminSetting = application.loadini("admin\standard.ini");
...
}

Application.cfc has this as a function...

<cffunction name="loadini" output="false" returnType="struct">
<cfargument name="configfile" required="true" type="string" hint="reads from currentTemplate">
<cfscript>
var stResult = {};
var iniFilePath = "#GetDirectoryFromPath(GetBaseTemplatePath())##arguments.configfile#";
var stSection = getProfileSections(iniFilePath);
for(var section in stSection) {
 var CurrentSection = evaluate("stSection.#section#"); 
 var stData = {};
 for(var i = 1; i <= ListLen(CurrentSection); i++) {
 var key = ListGetAt(CurrentSection, i);
 stData[key] = getProfileString(iniFilePath, section, key);
 }
 setvariable("stResult.#section#", stData);
 }
return stResult; 
</cfscript>
</cffunction>

The ini file can have any number of setting. In particular my ini file has:

[ws]
comment=http://xxxxx.com/resource/comment.cfc?wsdl
ior=http://xxxxx.com/resource/ior.cfc?wsdl
node=http://xxxxx.com/resource/node.cfc?wsdl
pref=http://xxxxx.com/resource/pref.cfc?wsdl
traffic=http://xxxxx.com/resource/traffic.cfc?wsdl

What kinds of things can I do to improve this?

200_success
145k22 gold badges190 silver badges478 bronze badges
asked Dec 7, 2012 at 4:36
\$\endgroup\$

2 Answers 2

3
\$\begingroup\$

You don't need evaluate() here:

var CurrentSection = evaluate("stSection.#section#"); 

Simply do this:

var CurrentSection = stSection[section]); 

Similarly you don't need to use setVariable():

setvariable("stResult.#section#", stData);

Simply:

stResult[section] = stData;
answered May 4, 2013 at 7:20
\$\endgroup\$
4
\$\begingroup\$

What kind of things are you looking to improve? Your solution looks pretty straightforward. The only problem with an ini file is that ColdFusion can't instantly/natively parse it. Whereas XML/JSON can be parsed directly into a Coldfusion Struct.

<cfscript>
public struct function loadini( required string configFile) {
 var stResult = {};
 var iniFilePath = "#GetDirectoryFromPath(GetBaseTemplatePath())##arguments.configfile#";
 var stSection = getProfileSections(iniFilePath);
 // loop over the sections
 for (section in stSection) {
 // loop over the list of variables in the ini file
 for (var i=0;i<=listLen(stSection[section]);i++) {
 // result.section.variable = value 
 stResult[section][listGetAt(stSection[section],i)] = GetProfileString(iniFilePath, section, listGetAt(stSection[section],i) ) ;
 }
 }
 return stResult; 
}
</cfscript>
<cfdump var="#loadini('test.ini')#">
answered Jan 18, 2013 at 11:14
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.