EzConfig

akx 12.12.03 18:48

Näppärä .cfg/.ini-lukija ja kirjoittaja

 Tekstiversio  Arvo: 0 (2 ääntä)  Äänestä: +  -
2 tiedostoa; ezconfig.php ja test.cfg

== EZCONFIG.PHP ==
<?php
/*-------------------------------------------------------------------------------------------
    EzConfig PHP Configuration Library 1.0
    Copyright (C) AKX 2003.
   
    Terms of usage:
      If you use the library, please credit me somewhere (e.g. "Includes EzConfig by AKX").
      If you modify the code, please leave this notice intact.
   
-------------------------------------------------------------------------------------------*/


  function config_load($file)
  {
   if(!file_exists($file))  // hmm, quite interesting: the file may not be found
   {
    return array("_error"=>"filenotfound");
   }
   $fc=explode("\n",file_get_contents($file))// take the file and row-by-rowize it...
   $ca=array(); // initialize our config array (no Notice whines from PHP)
   $section=""; // no section at the beginning
   for($i=0;$i<count($fc);$i++)  // iterate row by row
   {
    $rs=trim($fc[$i])// programmer convenience and something else too
    if($rs!="")
    {
     if($rs{0}=="#") // comment, no further handling.
     {

     }
     else
     {
      if($rs{0}=="[") // this is a beginning of a section
      {
       if($rs{strlen($rs)-1}=="]") // yes, our user has made no typo here
       {
        $rs=substr($rs,1,strlen($rs)-2);
        $section=$rs; // ok, section set.
       }
      }
      else  // it's a variable, not a section
      {
       $rp=explode("=",$rs,2);
       if(count($rp)>1) // yes, it's good
       {
        if($section!="")
        {
         $vname=$section.":".$rp[0]// use sectioned syntax
        }
        else
        {
         $vname=$rp[0]; // no sections here
        }
        $ca[strtolower($vname)]=$rp[1]; // set the variable, all varnames are lowercase, btw
       }
      }
     }
    }
   }
   return $ca// and for our finishing touches, return the array, too.
  }

  function config_save($file,$config,$ret_instead=0)
  {
   if(empty($config)) return 2; // umm... :)
   $secs=array();
   foreach($config as $k=>$v) // every config element is iterated through
   {
    $kx=explode(":",$k);      // explode with the section/variable separator
    if(count($kx)==1)         // no section name
    {
     $secs[""][$kx[0]]=$v;
    }
    else                      // we have a section, let's set the var according to it
    {
     $secs[$kx[0]][$kx[1]]=$v;
    }
   }

   $op=""// initialize our Write-To-File string.
   foreach($secs as $sectname=>$sectvars) // every section
   {
    if(!($sectname==""&&$op=="")) $op.="[".$sectname."]\n"; // write section header
    foreach($sectvars as $varname=>$varvalue) // every variable in there
    {
     $op.=strtolower($varname)."=".$varvalue."\n";
    }
    $op.="\n"// a bit cleaner :)
   }
   if($ret_instead==1)
   {
    return $op;
   }
   else
   {
    $f=fopen($file,"w");
    if(!$f)
    {
     return 1;
    }
    else
    {
     fwrite($f,$op);
     fclose($f);
    }
   }
   return 0;
  }
 
  if(strstr($_SERVER["PHP_SELF"],"ezconfig.php")&&empty($ezc_no_demo))
  {
   echo "<style>body{background:#f9f9f9}body,div{font-family:arial,sans-serif;font-size:10pt}.cd{background:#e0eae0;border:1px solid #000;margin:10px}</style>";
   echo "<h1>EzConfig PHP Config Library</h1><big><b>Copyright © AKX 2003</b></big><br>E-mail: <tt>rot13(\"nxk@gurnxk.gx\");</tt><p>You may use the library in any of your projects, but I'd appreciate crediting of some sort.<br>If you change the source code, you <i>must</i> leave the copyright notice intact in the source code.<hr>";
   echo "<big>You have entered the EzConfig Demo Mode. If you've reached this page in error, your URL contains ezconfig.php. You may avert this by setting \$ezc_no_demo to 1 before including this file.</big><hr>";
   echo "EzConfig is a flexible configuration file reader library in PHP. It supports multiple sections (namespaces, if you will) inside one configuration file.<p>The library consists of two functions: <tt><b>config_load(\$file)</b></tt> and <tt><b>config_save(\$file,\$config)</b></tt>.<p>Here's a demo.";
   echo "<hr>";

   /* demo code starts */
   $cfg=config_load("test.cfg");
   echo "Here's <b>test.cfg</b> as-is:<div class=cd><pre>";
   echo file_get_contents("test.cfg");
   echo "</pre></div>";
   echo "Here's the configuration file <b>test.cfg</b> parsed by EzConfig.<br><div class=cd><pre>";
   print_r($cfg);
   echo "</pre></div>";
   echo "The value of <b>is this a funky test file?</b> is <b>".$cfg["is this a funky test file?"]."</b>.";
   echo "<p>Now let's add a few values.<br>";
   $cfg["Random Value"]=rand(5000,10000);
   $cfg["Newly Created Section:Another Random Value"]=rand(50000,100000);
   echo "Here's the current (changed) configuration array...<br><div class=cd><pre>";
   print_r($cfg);
   echo "</pre></div>We may use a switch for save_config for returning (not saving) the file. We're doing this because your chmods may not be correct right now, and they don't have to be.<br>Here's the file now.<div class=cd><pre>";
   echo config_save("newtest.cfg",$cfg,1);
   echo "</pre></div>";
   /* demo code ends */
   
   echo "<p>That's it. The demo's over.<hr>";
  }
?>
== LOPPU ==

== TEST.CFG ==
# Comment.

Is This A Funky Test File?=Yes

[Section 1]
A Value=1
Another Value=Banana

[The Second Section]
Name=Austin Powers
Sex=Yes Please

[]
A Variable From A Previously Declared, Now Undeclared Empty Section=Yes, I've done that
I have the separator symbol in me=The = symbol is used to distinguish between variable and value.
== LOPPU ==

akx 18:48 12.12.03 
Noi voi periaatteessa uploadata omalle serverille ja kokeilla.
akx 18:49 12.12.03 
Jatkoa. Toi on siis tarkoitettu includettavaksi, mutta jos niin ei tee, esittelee se demon itsestään.
makeuu 10:35 13.12.03 
Ootko ite tehny tämän?
akx 17:06 13.12.03 
Olen.
DJ-Dew 17:21 14.3.05 
parse_ini_file tekee ainaki jotain samantapasta