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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
|
/*
* SPL - The SPL Programming Language
* Copyright (C) 2004, 2005 Clifford Wolf <clifford@clifford.at>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* mod_format_init.spl: Ini file parsewr and dumper
*/
/**
* A simple INI file parser/dumper module
*
* This module implements simple INI file parser and dumper functions.
*/
/**
* This function expects the content of a INI file (inidata) and returns a two
* dimentional hash structure with the INI section names as 1st index and the
* INI variable name as 2nd index (initree).
*
* The INI file format as understood by this implementation:
*
* [foobar]
* the following entries are in the "foobar" section.
*
* key = value
* set variable "key" to "value"
*
* - everything else (including lines starting with ';' or '#' are ignored).
* - section names and variable names are limited to the regex /[a-z0-9_]+/.
* - variables declared before the first section declaration are
* defined in the "" section.
* - whitespaces are ignored.
*
* There is no mechanism for reporting parser errors (parser errors are not
* possible because all lines not recognised as section or variable declaration
* are ignored).
*/
function format_ini_parse(inidata)
{
var initree, sect;
foreach[] line (inidata =~ /[^\n]*/Ag) {
if (line =~ /^\s*\[\s*([a-z0-9_]+)\s*\]/)
sect = $1;
else
if (line =~ /^\s*([a-z0-9_]+)\s*=\s*(.*)/)
initree[sect][$1] = $2;
}
return initree;
}
/**
* This function expects a data structure such as returned by
* [[format_ini_parse()]] as parameter and returns the inidata.
*/
function format_ini_dump(initree)
{
var inidata;
foreach sect (initree) {
inidata ~= "[$sect]\n";
foreach name (initree[sect])
inidata ~= "$name = ${initree[sect][name]}\n";
inidata ~= "\n";
}
return inidata;
}
|