首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在CFML中创建INI文件

在CFML中创建INI文件
EN

Stack Overflow用户
提问于 2009-10-28 03:23:14
回答 3查看 594关注 0票数 3

有人能找到一种方法来改进一下这段代码吗?我想一下子读入一个INI文件,并创建一个相应的数据结构。

代码语言:javascript
复制
<cfset INIfile = expandPath(".") & "\jobs.ini">
<cfset profile = GetProfileSections(INIfile)>
<cfloop collection="#profile#" item="section">
    <cfloop list="#profile[section]#" index="entry">
        <cfset app.workflow[section][entry]=GetProfileString(INIfile, section, entry) >
    </cfloop>
</cfloop>
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2009-10-28 16:53:09

我不相信你可以使用CFML power来改善这一点。你需要解析巨大的ini文件吗?如果不是,为什么你想要改进你的代码,这对我来说很简单。

其他可能的解决方案(虽然对于CF很常见)是尝试纯Java。请参阅此SO线程以了解纯Java examples

附注:顺便说一句,如果您有特殊的性能需求,您应该考虑使用其他存储进行配置。对于大型数据集,对旧的好的MySQL进行简单的SELECT查询可能要快得多。

票数 4
EN

Stack Overflow用户

发布于 2009-10-29 00:09:57

要扩展ryber的评论,您可以考虑使用这种方法。我假设您使用的是CF8.01或更高版本,因为我使用了嵌套的隐式结构表示法。这可以很容易地转换为CF7/6/etc语法,但不会那么干净或简洁。

同样,这仅适用于您的ini文件未被任何其他应用程序或人员使用的情况,并且不需要为ini格式。

settings.cfm:

代码语言:javascript
复制
<cfset variables.settings = {
    fooSection = {
        fooKey = 'fooVal',
        fooNumber = 2,
    },
    fooSection2 = {
        //...
    },
    fooSection3 = {
        //...
    }
} />

Application.cfc: (仅限onApplicationStart方法)

代码语言:javascript
复制
<cffunction name="onApplicationStart">
    <cfinclude template="settings.cfm" />
    <cfset application.workflow = variables.settings />
    <cfreturn true />
</cffunction>

此外,我还使用了CFEncode应用程序对settings.cfm的内容进行加密。它不会保护你免受那些想要查看其加密内容的人的影响(加密并不是那么强,有办法在不解密的情况下查看内容),但是如果你只是想把一些爱管闲事的人拒之门外,它会增加一些额外的进入障碍,这可能会阻止一些人进入。

更新:因为你刚刚留下了一个评论,说你在CF7上,所以这里是原生的CF7语法:

settings.cfm:

代码语言:javascript
复制
<cfset variables.settings = StructNew() />
<cfset variables.settings.fooSection = StructNew() />
<cfset variables.settings.fooSection.fooKey = 'fooVal' />
<cfset variables.settings.fooSection.fooNumber = 2 />
<!--- ... --->

或者,您可以使用JSONUtil和CFSaveContent继续使用类似于JSON的方法(类似于我最初的语法),但使用的是CF7:

代码语言:javascript
复制
<cfsavecontent variable="variables.jsonSettings">
{
    fooSection = {
        fooKey = 'fooVal',
        fooNumber = 2,
    },
    fooSection2 = {
        //...
    },
    fooSection3 = {
        //...
    }
};
</cfsavecontent>
<cfset variables.settings = jsonUtil.deserializeFromJSON(variables.jsonSettings) />
票数 1
EN

Stack Overflow用户

发布于 2009-12-18 22:53:49

我创建了一个在许多应用程序中使用的CFC。当你初始化它的时候,你给它一个ini文件路径,它就会基于这个ini文件创建一个结构。它还可以选择使结构保持平坦或基于ini文件中的节创建子结构。然后,您可以使用它的getSetting()方法获取单个方法,也可以使用getAllSettings()返回整个结构。你可能会发现它很有帮助。

代码语言:javascript
复制
<cfcomponent hint="converts INI file to a structure">
    <cfset variables.settings=structNew() />
    <cffunction name="init" access="public" output="false" returntype="any">
        <cfargument name="configurationFile" type="string" required="yes" />
        <cfargument name="useSections" default="false" type="boolean" />
        <cfset var local=structNew() />
        <cfif fileExists(arguments.configurationFile)>
            <!--- Get the [sections] in the .INI file --->
            <cfset local.sectionStruct=getProfileSections(arguments.configurationFile) />
            <!--- Loop over each of these sections in turn --->
            <cfloop collection="#local.sectionStruct#" item="local.item">
                <cfset local.workingStruct=structNew() />
                <cfloop list="#local.sectionStruct[local.item]#" index="local.key">
                    <!--- Loop over the keys in the current section and add the key/value to a temporary structure --->
                    <cfset local.workingStruct[local.key]=getProfileString(arguments.configurationFile,local.item,local.key) />
                </cfloop>
                <cfif arguments.useSections>
                    <!--- Copy the temporary structure to a key in the setting structure for the current section --->
                    <cfset variables.settings[local.item]=duplicate(local.workingStruct) />
                <cfelse>
                    <!--- Append the temporary structure to the setting structure --->
                    <cfset structAppend(variables.settings,local.workingStruct,"yes") />
                </cfif>
            </cfloop>
        <cfelse>
            <cfthrow
                message="Configuration file not found. Must use fully-qualified path."
                extendedinfo="#arguments.configurationFile#"
            />
        </cfif>
        <cfreturn this>
    </cffunction>

    <cffunction name="getAllSettings" access="public" output="false" returntype="struct">
        <cfreturn variables.settings>
    </cffunction>

    <cffunction name="getSetting" access="public" output="false" returntype="string">
        <cfargument name="settingName" required="yes" type="string" />
        <cfset var returnValue="" />

        <cfif structKeyExists(variables.settings,arguments.settingName)>
            <cfset returnValue=variables.settings[arguments.settingName] />
        <cfelse>
            <cfthrow
                message="No such setting '#arguments.settingName#'."
            />
        </cfif>
        <cfreturn returnValue>
    </cffunction>
</cfcomponent>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1633097

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档