Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

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?

share|improve this question
add comment

2 Answers

up vote 0 down vote accepted

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;
share|improve this answer
add comment

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')#">
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.