CFLib.org – Common Function Library Project

iniToStruct(iniFiles)

Last updated April 26, 2012

author

Dave Anderson

Version: 0 | Requires: CF9 | Library: CFMLLib

Description:
This differs from another UDF on this site in two ways: 1) will parse multiple .ini files 2) will not replace an existing struct. In other words, if app.ini has a [global] section and dev.ini does too, the result.global structure will not be wiped out, but rather will instead be augmented or have individual values overwritten

Return Values:
Returns a struct.

Example:

sample ini files:

app.ini 
--------
[global]
datasource=myDSN
appName=Monkey

dev.ini
--------
[global]
datasource=myDSN
appName=Chimp


application.config = iniToStruct(["/app.ini","/dev.ini"]);

result:
application.config.global.datasource --> "myDSN"
application.config.global.appName --> "Chimp"

Parameters:

Name Description Required
iniFiles Array of ini files. Yes

Full UDF Source:

/**
 * Converts ini file(s) to a struct.
 * 
 * @param iniFiles      Array of ini files. (Required)
 * @return Returns a struct. 
 * @author Dave Anderson (the.one.true.dave.anderson@gmail.com) 
 * @version 0, April 26, 2012 
 */
public struct function iniToStruct(array iniFiles) {
    var config = {};
    for (local.f IN iniFiles) {
        local.inifile = fileExists(f) ? f : expandPath(f);
        local.sections = getProfileSections(local.iniFile); 
        for (local.k IN sections) {
            for (local.v IN listToArray(sections[k])) {
                config[k][v] = getProfileString(local.iniFile,k,v);
            }
        }
    }
    return config;
}

Search CFLib.org


Latest Additions

Raymond Camden added
QueryDeleteRows
November 04, 2017

Leigh added
nullPad
May 11, 2016

Raymond Camden added
stripHTML
May 10, 2016

Kevin Cotton added
date2ExcelDate
May 05, 2016

Raymond Camden added
CapFirst
April 25, 2016

Created by Raymond Camden / Design by Justin Johnson