/* * SPL - The SPL Programming Language * Copyright (C) 2004, 2005 Clifford Wolf * * 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; }