Page 1 of 1

Reading, parsing and displaying a .cfg file

Posted: Sun Aug 23, 2009 12:25 pm
by Cherry134
Hey there, I have a plain text .cfg file that looks very similar to what is shown below:

Code: Select all

 
Admins
{
 "[ƒcƒc]± Furry Indigo"
       {
        "auth"      "steam"
        "identity"      "STEAM_0:0:1"
        "group"     "levelmax"
        "immunity"      "998"
    }
 
 "Cherry"
       {
        "auth"      "steam"
        "identity"      "STEAM_0:1:1"
        "group"     "levelmax"
        "immunity"      "997"
    }
  etc
}
 
 
How would I go about, reading each entry, and displaying the name, in this case "[ƒcƒc]± Furry Indigo" or "Cherry", the STEAMID, in this case "STEAM_0:0:1" or "STEAM_0:1:1", the Group, in this case "levelmax", and the immunity level, in this case "998" or "997", and then displaying these in a table?

Thanks and Regards,

Cherry

Re: Reading, parsing and displaying a .cfg file

Posted: Sun Aug 23, 2009 2:32 pm
by requinix
Use a state machine. It won't care about spaces and newlines like a regular expression solution would.

Code: Select all

$file = '
Admins{"[ƒcƒc]± Furry Indigo"{"auth""steam""identity""STEAM_0:0:1"
"group""levelmax""immunity""998"}"Cherry"{"auth""steam""identity"
"STEAM_0:1:1""group""levelmax""immunity""997"}}
';
 
$mode = 0; $section = $user = $key = "";
$array = array();
 
for ($i = 0; $i < strlen($file); $i++) {
    $c = $file[$i];
    switch ($mode) {
        case 0: // the section name
            if ($c == "{") {
                $section = trim($section);
                $array[$section] = array();
                $mode = 1; // look for a username
            } else {
                $section .= $c;
            }
            break;
        case 1: // user name (before the first ")
            if ($c == "}") {
                $mode = 0; // section is empty
            } else if ($c == '"') {
                $mode = 2; // get the username
            }
            break;
        case 2: // user name (inside the quotes)
            if ($c == '"') {
                $array[$section][$user] = array();
                $mode = 3;
            } else {
                $user .= $c;
            }
            break;
        case 3: // waiting for the {
            if ($c == "{") $mode = 4;
            break;
        case 4: // key (before the first ")
            if ($c == '"') $mode = 5;
            break;
        case 5: // key
            if ($c == '"') {
                $array[$section][$user][$key] = "";
                $mode = 6;
            } else {
                $key .= $c;
            }
            break;
        case 6: // value (before the first ")
            if ($c == '"') $mode = 7;
            break;
        case 7: // value
            if ($c == '"') {
                $key = "";
                $mode = 8;
            } else {
                $array[$section][$user][$key] .= $c;
            }
            break;
        case 8: // waiting for a " (key) or a }
            if ($c == '"') {
                $mode = 5;
            } else if ($c == "}") {
                $user = "";
                $mode = 9;
            }
            break;
        case 9: // waiting for a " (username) or a }
            if ($c == '"') {
                $mode = 2;
            } else if ($c == "}") {
                $section = "";
                $mode = 0;
            }
            break;
    }
}
var_dump($array);
 

Code: Select all

array(1) {
  ["Admins"]=>
  array(2) {
    ["[ƒcƒc]± Furry Indigo"]=>
    array(4) {
      ["auth"]=>
      string(5) "steam"
      ["identity"]=>
      string(11) "STEAM_0:0:1"
      ["group"]=>
      string(8) "levelmax"
      ["immunity"]=>
      string(3) "998"
    }
    ["Cherry"]=>
    array(4) {
      ["auth"]=>
      string(5) "steam"
      ["identity"]=>
      string(11) "STEAM_0:1:1"
      ["group"]=>
      string(8) "levelmax"
      ["immunity"]=>
      string(3) "997"
    }
  }
}

Re: Reading, parsing and displaying a .cfg file

Posted: Sun Aug 23, 2009 5:39 pm
by Cherry134
Thanks thats great :)

How would I then use that dumped array to make a table?

Re: Reading, parsing and displaying a .cfg file

Posted: Sun Aug 23, 2009 5:53 pm
by requinix
Cherry134 wrote:How would I then use that dumped array to make a table?
Depends what the table is supposed to look like...

Re: Reading, parsing and displaying a .cfg file

Posted: Sun Aug 23, 2009 6:28 pm
by Cherry134
A simple table with 4 columns, Name, SteamID, Group, Immunity, and then sort every record by immunity, from highest to lowest.

Re: Reading, parsing and displaying a .cfg file

Posted: Mon Aug 24, 2009 1:58 am
by requinix
What would be really nice is if you could dump all this information into a database. That would make everything very easy.

Otherwise,

Code: Select all

/* delete line 16 */
/* line 31: */ $array[$user] = array();
/* line 45: */ $array[$user][$key] = "";
/* line 59: */ $array[$user][$key] .= $c;
Then make a function that can take two user arrays (like what you see in $array[...]) and decide which one comes first - if two users can have the same "immunity" then you need to sort by something else too. Combine it with uasort and the array is sorted.
Then loop through it and print your table.

Re: Reading, parsing and displaying a .cfg file

Posted: Mon Aug 24, 2009 9:55 am
by Cherry134
tasairis wrote:What would be really nice is if you could dump all this information into a database. That would make everything very easy.

Otherwise,

Code: Select all

/* delete line 16 */
/* line 31: */ $array[$user] = array();
/* line 45: */ $array[$user][$key] = "";
/* line 59: */ $array[$user][$key] .= $c;
Then make a function that can take two user arrays (like what you see in $array[...]) and decide which one comes first - if two users can have the same "immunity" then you need to sort by something else too. Combine it with uasort and the array is sorted.
Then loop through it and print your table.
Thanks, one more question. How would I convert a 1 digit string to and 3 digit integer, and a 2 digit string to a 3 digit integer?

Re: Reading, parsing and displaying a .cfg file

Posted: Mon Aug 24, 2009 3:49 pm
by requinix
You mean zero-padding? 5 becomes 005?

sprintf and printf are the easiest.

Code: Select all

echo sprintf("%03u", 5); // 005

Re: Reading, parsing and displaying a .cfg file

Posted: Mon Aug 24, 2009 5:37 pm
by Cherry134
tasairis wrote:You mean zero-padding? 5 becomes 005?

sprintf and printf are the easiest.

Code: Select all

echo sprintf("%03u", 5); // 005
Thanks, that worked perfectly