INIToArray.inc -- generalized parser for an INI to an array

A forum for macro code snippets to be used in writing other macros. Post routines or .inc files here only, completed macros go to the Macro Depot.

Moderator: MacroQuest Developers

Gumby
a ghoul
a ghoul
Posts: 99
Joined: Sat Jan 24, 2004 5:27 pm

INIToArray.inc -- generalized parser for an INI to an array

Post by Gumby » Fri Jan 30, 2004 7:22 pm

Created another generic function out of my buffbitch.mac v1.1 code; this I hope, will be more useful to others who are using INI files for configuration, especially ones that become as unwieldy as mine is. The script can be used to grab Headers, Keynames, or individual values based upon the parameters passed to it; it can either use an uninitialized array, or append the new entries at the back of an already initialized array.

Problems / complaints, reply here or toss em in a PM.

EDIT: Updates:

INIToArray will now return the index of the resultant array upon successful completion.

Updated error code returns to correct a duplicate return; subroutine returns -1 if the ini request is valid but there were no entries (headers, keys, or values) to parse into the array.

G

Code: Select all

| -- INIToArray.inc 
| Code by Gumby 1/31/04 
|

Sub INIToArray(ArrayName,INIFileName,INIHeaderName,INIKeyName)

| INIToArray is a generalized parse routine to split entries (Headers, Keys, or Values)
| from a given INI file and place them into an array.  If the array is uninitialized, the
| routine will start at index 0; if the array is already initialized, the routine
| will scan to the first unitialized element and add the new entries starting
| at that index.  The return if valid is the last index of the resultant array.
|
| *** NOTE: Headers and Keys are already returned by the $ini flag in |-delimited format;
| However, the delimiter of the values can be changed by the /varset Delimiter entry that
| is found under the /varset ParseIndex directly under the declares under the documenation.
| A future revision may switch this to a passed parameter; however, error checking would
| be difficult in my estimation.  Any ideas for it, feel free to PM me.
|
| Return Codes:
|
|   * $return>=0   INIToArray completed successfully, return is last index of new Array
|   * $return==-1  The INI request was valid but returned no headers / keys / values
|   * $return==-2  An ArrayName was not passed to INIToArray
|   * $return==-3  An INIFileName was not passed to INIToArray
|   * $return==-4  A valid array was not passed to INIToArray
|   * $return==-5  The INI File/Header/Key combination was not valid.
|
|
| CAVEATS: Because of the aliasing error checking a bogus array is 
|          frankly odd.  The creative "solution" I'm using has one 
|          flaw: Do not set the 0 index element of the array you're
|          passing to INIToArray to be '(0)'.  I have no idea why
|          anyone would have an array with this as an element in MQ,
|          let alone the first one... but if you do, IndexFind will
|          will erroneously return -3 for the valid array. If someone
|          figures out a better way to check for array validity, please
|          PM me on the forums.
|
| USAGE:  This sub is called with the following syntax:
| /call INIToArray <ArrayName> "<INIFileName>" to get Headers
| /call INIToArray <ArrayName> "<INIFileName>" "<INIHeaderName>" to get keys
| /call INIToArray <ArrayName> "<INIFileName>" "<INIHeaderName>" "<INIKeyName>" to get values
|
| *** NOTE: This sub makes use of aliasing (@@ArrayName), for ArrayName *only* 
| pass the name of the array, do not attempt to pass the actual array itself.


   /declare ParseVar           local
   /declare ParseCounter       local
   /declare ParseValueName     local
   /declare ParseIndex         local
   /declare DebugCounter       local
   /declare Delimiter          local
   /declare LocalDebugFlag     local

   /varset ParseIndex          0
   /varset Delimiter           |

| LocalDebugFlag: Change this varset to 1 to enable ArrayDump at end for Debugging
   /varset LocalDebugFlag      0


| Up Front Error Checking

| Check that the Array (name) was passed
   /if "$defined(ArrayName)"=="FALSE" {
      /return -2
   }

| Check that the Ini filename was passed
   /if "$defined(INIFileName)"=="FALSE" {
      /return -3
   }

| Check that a valid array was passed
   /if "$right(3,"@@ArrayName(0)")"=="(0)" {
      /return -4
   }

| Find first undefined element

   /if "@@ArrayName(0)"!="UNDEFINED-ARRAY-ELEMENT" {
      :StartUndefFind
         /varadd ParseIndex 1
         /if "@@ArrayName(@ParseIndex)"=="UNDEFINED-ARRAY-ELEMENT" /goto :EndUndefFind
         /goto :StartUndefFind
      :EndUndefFind
   }

| Build ParseVar

   /if "$defined(INIHeaderName)"=="TRUE" {
      /if "$defined(INIKeyName)"=="TRUE" {       
         /varset ParseVar "$ini("@INIFileName","@INIHeaderName","@INIKeyName")"
      } else {
         /varset ParseVar "$ini("@INIFileName","@INIHeaderName")"
         /varset Delimiter |
      }
   } else {
      /varset ParseVar "$ini("@INIFileName")"
      /varset Delimiter |
   }

| Error checking if INI filename / header / key are invalid

   /if "@ParseVar"=="NOTFOUND" {
      /return -5
   }

| Build Array

   /varset ParseCounter 1
   :StartParseINI
      /varset ParseValueName "$arg(@ParseCounter,"@ParseVar",@Delimiter)"
      /if "@ParseValueName"=="" {
         /goto :EndParseINI
      }
      /varset @ArrayName(@ParseIndex) "@ParseValueName"
      /varadd ParseCounter 1
      /varadd ParseIndex 1
   /goto :StartParseINI

   :EndParseINI
   /varsub ParseIndex 1

| Debug Code if you turn it on, dumps resultant array out after build

   /if n @LocalDebugFlag==1 {
      /echo @ArrayName built
      /echo ******* Full Dump of @ArrayName Array *******
      /for DebugCounter 0 to @ParseIndex
         /echo @ArrayName entry @DebugCounter: @@ArrayName(@DebugCounter)
      /next DebugCounter
   }
/return @ParseIndex