Tuesday, May 14, 2002

stringUtils.as

Simple library that adds useful methods to the String object. You can view the documentation here. This is provided as is, but please post any errors, corrections or suggestions in the comments below.

/*
String Utility Component version 1.5
Mike Chambers
thanks to Branden Hall, Ben Glazer, Christian Cantrell, Nik Schramm
*/
/*
 This allows user to check from other include files whether or not the stringUtils
 library has been included.
 Example:
 if(!String.stringUtilsDefined)
 {
  trace("stringUtils.as not found");
 }
*/
String.prototype.constructor.stringUtilsDefined = true;
String.prototype.constructor.stringUtilsVersion = 1.5;
/**
* This methods trims all of the white space from the left side of a String.
*/
String.prototype.ltrim = function()
{
 var size = this.length;
 for(var i = 0; i < size; i++)
 {
  if(this.charCodeAt(i) > 32)
  {
   return this.substring(i);
  }
 }
 return "";
}
/**
* This methods trims all of the white space from the right side of a String.
*/
String.prototype.rtrim = function()
{
 var size = this.length;
 for(var i = size; i > 0; i--)
 {
  if(this.charCodeAt(i) > 32)
  {
   return this.substring(0, i + 1);
  }
 }
 return "";
}
/**
* This methods trims all of the white space from both sides of a String.
*/
String.prototype.trim = function()
{
 return this.rtrim().ltrim();
}
/**
* This methods returns true if the String begins with the string passed into
* the method. Otherwise, it returns false.
*/
String.prototype.beginsWith = function(s) {
 return (s == this.substring(0, s.length));
};
/**
* This methods returns true if the String ends with the string passed into
* the method. Otherwise, it returns false.
*/
String.prototype.endsWith = function(s) {
 return (s == this.substring(this.length - s.length));
};
 

String.prototype.remove = function(remove)
{
 return this.replace(remove, "");
}
String.prototype.replace = function(replace, replaceWith)
{
 sb = new String();
  found = false;
 for (var i = 0; i < this.length; i++)
    {
     if(this.charAt(i) == replace.charAt(0))
        {   
         found = true;
            for(var j = 0; j < replace.length; j++)
            {
             if(!(this.charAt(i + j) == replace.charAt(j)))
                {
                 found = false;
                    break;
                }
   }
            if(found)
            {
             sb += replaceWith;
                i = i + (replace.length - 1);
                continue;
            }
  }
        sb += this.charAt(i);
 }
    return sb;
}
10:54:38 AM    comment []  Google It!  

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 : mesh@macromedia.com
 requires stringUtils.as which can be downloaded from:
 http://radio.weblogs.com/0106797/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 < 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 < 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(",") > 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;
 
10:53:05 AM    comment []  Google It!  

© Copyright 2003 Mike Chambers.
 
May 2002
Sun Mon Tue Wed Thu Fri Sat
      1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31  
Apr   Jun


Macromedia MX

Resources

Flash MX

Aggregators

Books

Click here to visit the Radio UserLand website.

Subscribe to "Examples" in Radio UserLand.

Click to see the XML version of this web page.

Click here to send an email to the editor of this weblog.