mike chambers | about

Config.as

Simple class that loads config files. You can view the documentation here. This is provided as is, but please post any errors, corrections or suggestions in the comments below.

#include "stringUtils.as"

/*
 Config.as v1.0

 created by mike chambers : <A href="mailto:mesh@macromedia.com">mesh@macromedia.com</A>

 requires stringUtils.as which can be downloaded from:
 http://www.mikechambers.com/files/mesh/files/stringutils/

*/
if (!String.stringUtilsDefined) {
    trace("Warning : The stringUtils.as file was not loaded. This library is required " +
        "by the Config.as file. Please make sure that the stringUtils.as file is " +
        "either in the same directory as the Config.as file, or is included in the " +
        "Flash MX include path.");
}

if (String.stringUtilsVersion & lt; 1.5) {
    trace("Warning : Config.as requires the stringUtils.as version 1.5 or higher. " +
        "You are using version : " + String.stringUtilsVersion +
        " Please upgrade your stringUtils.as file.");
}


/*
 Constructor that takes the path to the config file as an argument

 Path can be any valid relative or absolute file or url path. Although if
 running from a browser,  Flash's security restrictions apply.
*/
_global.Config = function (fileName) {
    this.fileName = fileName;
}



/*
 Method that allows you to set the path to the config file.

 Path can be any valid relative or absolute file or url path. Although if
 running from a browser,  Flash's security restrictions apply.
*/
Config.prototype.setFileName = function (fileName) {
    this.fileName = fileName;
}



/*
 Returns a string of the path to the current config file.

 If a config file has not been specified, then it returns null;
*/
Config.prototype.getFileName = function () {
    return this.fileName;
}



/*
 Method which takes a boolean value that indicates whether
 or not double quotes surrounding names and values should be removed.

 Default is false.
*/
Config.prototype.setStripQuotes(strip) {
    this.stripQuotes = strip;
}



/*
 Method which takes a boolean value that indicates whether
 or not values that contain commas should be exploded into
 an array of values.

 If set to true, calling get() on the name will return an Array.

 If set to false, calling get() on the name will return a string.

 The default is false.
*/
Config.prototype.setExplodeValues(explode) {
    this.explodeValues = explode;
}


Config.prototype.onData = function (data) {
    if (this.parse(data)) {
        this.onConfigLoad(true);
    } else {
        this.loaded = false;
        this.onConfigLoad(false);
    }

}



/*
 This is a convenience method that allows you to pass a string
 representing a config file to be parsed.

 It returns true if the parsing succeeded, and false if it did not.

 Note, since the method returns immediately, you may access
 the data in the object immediately after it has returned true.

 For example:

 var c = new Config();
 var s = "foo=bar\n";
 s += "name = mike\n";

 if(c.parse(s))
 {
  trace(c.get("foo"));    //traces "bar"
 }

 If the config object has already parsed data, then the new data will be added
 to the config object, overwriting any duplicate values that already exist.
*/
Config.prototype.parse = function (data) {

    var rows = data.split("\n");
    var rLength = rows.length;
    var tArray = new Array();

    var rString;
    var tName;
    var tString;
    var c;

    for (var i = 0; i & lt; rLength; i++) {
        rString = rows[i];

        c = rString.charAt(0);

        if (c == ";" || c == "#" ||
            c == "[" || c == "/") {
            continue;
        }

        tArray = rString.split("=");

        if (tArray.length != 2) {
            continue;
        }

        tName = tArray[0].trim();
        tValue = tArray[1].trim();

        //maybe write custom loop to strip the quotes in one pass.
        if (this.stripQuotes) {
            if (tValue.beginsWith("\"")) {
                tValue = tValue.substring(1);

                if (tValue.endsWith("\"")) {
                    tValue = tValue.substring(0, tValue.length - 1);
                }
            }

            if (tName.beginsWith("\"")) {
                tName = tName.substring(1);

                if (tName.endsWith("\"")) {
                    tName = tName.substring(0, tName.length - 1);
                }
            }
        }

        if (this.explodeValues) {
            if (tValue.indexOf(",") & gt; 0) {
                //we jsut changed tValue from a string to array;
                tValue = tValue.split(",");
            }
        }

        this.configArray[tName] = tValue;

        tValue = null;
        tName = null;
    }

    this.loaded = true;
    return true;
}



/*
 Takes a string and returns the value for that name in the config file.

 For example:

 config.ini
 foo=bar
 name=mike

 var c = new Config("config.ini");

 c.onConfigLoad = function(success)
 {
  trace(c.get("foo"));         //traces "bar"
  trace(c.get("name"));     //traces "mike"
 }
*/
Config.prototype.get = function (hash) {
    return this.configArray[hash];
}



/*
 Returns an associative array containing the name value
 pairs contained within the object.
*/
Config.prototype.getArray = function () {
    return this.configArray;
}

/*
 This method loads the config file specified in the constructor or
 setConfigFile() method, and then parses it.

 Once it has been parsed, the onConfigLoad() method will be
 called and passed a boolean value indicating whether the
 file was able to be parsed.

 The onConfigLoad method should be over ridden in order to
 allow the developer to know when the data has loaded and been parsed.
*/
Config.prototype.loadConfig = function () {
    this.loaded = false;
    this.load(this.fileName);
}

/*
 Default values for internal variables within the object.
*/
Config.prototype.configArray = new Array();
Config.prototype.stripQuotes = false;
Config.prototype.explodeValues = false;

/*
 this indicates whether or not the data has loaded. you should be
 able to register with with a watch method.
*/
Config.prototype.loaded = false;
Config.prototype.fileName = null;

/* Extending the LoadVars object. */
Config.prototype.__proto__ = LoadVars.prototype;
Config.prototype.superClass = LoadVars.prototype.constructor;

Config.prototype.constructor.configDefined = true;
Config.prototype.constructor.configVersion = 1.0;
Tags:
twitter github flickr behance