CFLib.org – Common Function Library Project

getProfileStringUTF8(iniPath [, section] [, entry])

Last updated July 24, 2012

Version: 1 | Requires: ColdFusion 9 | Library: CFMLLib

 
Rated 2 time(s). Average Rating: 3.5

Description:
It supports to translate any unicode-based language instead of using GetProfileString which doesn't support Unicode (UTF-8). See http://help.adobe.com/en_US/ColdFusion/10.0/CFMLRef/WSc3ff6d0ea77859461172e0811cbec22c24-7c62.html

Return Values:
Returns the value of the entry if found, otherwise an empty string

Example:

view plain print about
<cfoutput>#getProfileStringUTF8("/path/to/my.ini", "section", "key")#</cfoutput>

Parameters:

Name Description Required
iniPath Full path to ini file to parse. Yes
section Section of the ini file to parse. No
entry Entry within that section to return value of. No

Full UDF Source:

view plain print about
<cfscript>
/**
 * Unicode language translator ((UTF-8))
 * version 0.1 by Pyae Phyoe Shein
 * version 1.0 by Adam Cameron - code tidy &amp; return a value rather than output it.  Added support for sections, as per getProfileString(). Make function usage analogous to getProfileString()
 * 
 * @param iniPath      Full path to ini file to parse. (Required)
 * @param section      Section of the ini file to parse. (Optional)
 * @param entry      Entry within that section to return value of. (Optional)
 * @return Returns the value of the entry if found, otherwise an empty string 
 * @author Pyae Phyoe Shein (pyaephyoeshein@gmail.com) 
 * @version 1.0, July 24, 2012 
 */

function getProfileStringUTF8(iniPath, section, entry) {
    var iniFile        = "";
    var line        = "";
    var inSection    = false;

    section        = "[#section#]";
    
    try {
        iniFile = fileOpen(iniPath, "read""UTF-8");
        line    = "";
        
        // scan the file for the section
        while (!fileIsEOF(iniFile)) {
            line = FileReadLine(iniFile);

            // keep track if we've found the correct section
            if (line == section){
                inSection = true;
                continue;
            }
            
            // if we're in the correct section and we find a match, we're done: return its value
            if (inSection && listFirst(line, "=") == entry){
                return listRest(line, "=");
            }
            
            // if we get to another section, then we didn't find the match: exit
            if (inSection && reFind("^\s*\[[^\]]+\]", line)){
                return "";
            }
        }
    }
    catch (any e){
        rethrow;
    }
    finally {
        fileClose(iniFile);
    }
    // we got to the end of the file and didn't find it
    return "";
}
</cfscript>
blog comments powered by Disqus

Search CFLib.org


Latest Additions

Troy Pullis Troy Pullis added
firstXDayOfMonth
a while ago

Henry Ho Henry Ho added
arrayMap
a while ago

Henry Ho Henry Ho added
queryGetRow
a while ago

Tony Felice Tony Felice added
getRowFromQuery
a while ago

Top Rated

Darwan Leonardo Sitepu backupDatabase
Rated 5.0, 44 time(s)

Barney Boisvert indentXml
Rated 5.0, 12 time(s)

Rachel Lehman deAccent
Rated 5.0, 9 time(s)

Markus Schneebeli                                 ListRemoveByStri...
Rated 5.0, 4 time(s)

Created by Raymond Camden / Design by Justin Johnson