The IniHandler can read and write ini-style files (and strings). Many options can be set.
example: http://www.blueshoes.org/en/framework/util/ini_handler/
source: http://www.blueshoes.org/download/Bs_IniHandler.class.phps
manual: http://developer.blueshoes.org/phpdoc/Bs_IniHandler.html
this class is part of the blueshoes php application framework, see http://www.blueshoes.org/
By : blueshoes
<?php
define('BS_INIHANDLER_VERSION', '4.0.$x$');
define('BS_INIHANDLER_UNQUOTE_NONE', 0);
define('BS_INIHANDLER_UNQUOTE_DOUBLE', 1);
define('BS_INIHANDLER_UNQUOTE_SINGLE', 2);
define('BS_INIHANDLER_UNQUOTE_ALL', 3);
/*************************************************************************
* This class provides methods to work with ini-style files.
*
*
*
* E.g.
* # Comment
* [Some test data]
* one = hallo
* two = "hallo"
* food = "Tom's Pizza = 'good stuff'"
* more food = Sam's Pizza's = 'best stuff'
* empty = ""
* noVal =
* [more test data]
* one = hi
* two = 'hi'
* food = 'Pizza = "good"'
* empty = ''
* noVal
*
* no dependencies here.
*
* @author andrej arn <andrej at blueshoes dot org>, Sam Blum <sam at blueshoes dot org>
* @copyright blueshoes.org
* @version 4.0.$id$
* @package util
* @access public
*/
class Bs_IniHandler extends Bs_Object {
/**
* specifies which chars at the start of a line define a comment line.
* the line is left-trimmed before the comparison is made.
*
* default: '#', '/', ';'
*
* note: you can only specify chars, not strings. so if you want '//' as
* comment char, you need to define '/'. (which is done by default)
*
* @access public
* @var array $commentChars (vector)
*/
var $commentChars = array('#', '/', ';');
/**
* should quoted values be unquoted?
*
* 0 = no
* BS_INIHANDLER_UNQUOTE_SINGLE = only single-quotes 'like this'
* BS_INIHANDLER_UNQUOTE_DOUBLE = only double-quotes "like this"
* BS_INIHANDLER_UNQUOTE_ALL = single and double quotes (default)
*
* @access public
* @var int $unQuote
*/
var $unQuote = BS_INIHANDLER_UNQUOTE_ALL;
/**
* vector with the sections as strings.
*
* note: since this var is not really needed, and all information is available in
* $this->_params, this internal var may disappear in the future.
*
* @access private
* @var array $_sections
*/
var $_sections;
/**
* 2-dim hash where the first dim is a hash with the section names, the 2nd
* is the key/value pair hash.
* @access private
* @var array $_params
*/
var $_params;
/**
* @todo make private
*/
var $comments;
/**
* the fullpath to the currently used file.
* @var string $_fileFullPath
*/
var $_fileFullPath;
/**
* the last error message of the error that occured. not set = no error.
* @access private
* @var string $_lastError
*/
var $_lastError;
/**
* Constructor.
* WARNING: please do not use the param $fileFullPath here, better call loadFile() yourself
* because otherwise you won't know if it worked or not.
* @param string $fileFullPath
*/
function Bs_IniHandler($fileFullPath='') {
parent::Bs_Object(__FILE__); //call parent constructor.
if (!empty($fileFullPath)) {
$this->loadFile($fileFullPath);
}
}
/**
* Loads the given file (read in and parse).
* @access public
* @param string $fileFullPath (a fullpath to the desired file.)
* @return bool (see getLastError())
*/
function loadFile($fileFullPath) {
$this->reset();
if (!file_exists($fileFullPath)) {
$this->_lastError = "File doesn't exists: '{$fileFullPath}'";
return FALSE;
}
if (!is_readable($fileFullPath)) {
$this->_lastError = "File is not readable: '{$fileFullPath}'";
return FALSE;
}
$this->_fileFullPath = $fileFullPath;
$fileContent = file($fileFullPath);
$this->_parseFromArray($fileContent);
return TRUE;
}
/**
* loads the ini stuff from the given string instead of a file (read in and parse).
* @access public
* @param string $str
* @return bool (see getLastError())
*/
function loadString($str) {
$this->reset();
$arr = explode("\n", $str);
$this->_parseFromArray($arr);
return TRUE;
}
/**
* sets the quote handling.
* @access public
* @param int $mode (see constants)
* @return void
*/
function setQuoteHandling($mode=BS_INIHANDLER_UNQUOTE_ALL) {
$this->unQuote = $mode;
}
/**
* gets called from loadFile() and loadString() to parse the data.
* @access private
* @param array (vector filled with strings (lines))
* @return void
*/
function _parseFromArray($arr) {
$this->comments = array();
$comment = array();
$section = '';
foreach($arr as $line) {
$sectionFound = $valueFound = FALSE;
$param = array('key'=>'', 'val'=>'');
do { // try
$line = trim($line);
# Skip empty lines
if (empty($line)) break; // try
# Comment
if (in_array($line[0], $this->commentChars)) {
$comment[] = $line;
break; // try
}
# Section
if (preg_match('/\[(.*)\]/', $line, $ar)) {
$section = $ar[1];
$sectionFound = TRUE;
break; // try
}
# Parameter
// split 1x at first '='
$tmp = explode('=', $line);
if (!is_array($tmp)) break; // try
if (sizeOf($tmp) < 2) {
//invalid comment line, whatever.
//no good if we arrive here. that's some crappy line that should not be in the file.
//we could issue a warning here.
$comment[] = @$tmp[0];
break;
}
$param['key'] = trim($tmp[0]);
array_shift($tmp);
if (sizeOf($tmp)>1) $tmp[0] = implode('=', $tmp);
$param['val'] = isSet($tmp[0]) ? trim($tmp[0]) : '';
if (empty($param['val'])) {
$valueFound = TRUE;
break; // try
}
$unQuote = '';
if ($this->unQuote & BS_INIHANDLER_UNQUOTE_DOUBLE) $unQuote .= '"';
if ($this->unQuote & BS_INIHANDLER_UNQUOTE_SINGLE) $unQuote .= "'";
if (empty($unQuote)) {
$valueFound = TRUE;
break; // try
}
// trim quote
$regEx = '/^(['.$unQuote.']?)(.*)\1$/';
if (preg_match($regEx, $param['val'], $ar)) {
$param['val'] = $ar[2];
$valueFound = TRUE;
break; // try
} else {
//the value had unmatching quotes, like "here' or 'here"
break; // try
}
} while(FALSE);
if ($sectionFound) {
$this->_sections[] = $section;
if (!empty($comment)) $this->comments[$section] = $comment;
$comment = array();
} else if ($valueFound) {
$this->_params[$section][$param['key']] = $param['val'];
if (!empty($comment)) $this->comments[$section .'__'. $param['key']] = $comment;
$comment = array();
}
} // foreach
if (!empty($comment)) $this->comments['__LastComment__'] = $comment;
}
/**
*
*/
function toString() {
$outStr = "# Bs_IniHandler File generated by www.blueshoes.org\n\n";
foreach ($this->_params as $section => $params) {
if (isSet($this->comments[$section])) {
foreach ($this->comments[$section] as $comment) $outStr .= "{$comment}\n";
}
$outStr .= "[".$section."]\n";
foreach ($params as $key => $value) {
if (isSet($this->comments[$section .'__'. $key])) {
foreach ($this->comments[$section .'__'. $key] as $comment) $outStr .= " {$comment}\n";
}
$outStr .= " " .$key. " = " .$value. "\n";
}
$outStr .= "\n";
}
if (isSet($this->comments['__LastComment__'])) {
foreach ($this->comments['__LastComment__'] as $comment) $outStr .= "{$comment}\n";
}
return $outStr;
}
/**
* saves the ini settings to the file specified.
* @access public
* @param string $fileFullPath (see above)
* @return bool (see getLastError())
* @see saveString()
*/
function saveFile($fileFullPath) {
$outStr = $this->toString();
if (!$fp = fopen($this->_fileFullPath, 'wb')) {
$this->_lastError = "Failed open the file for writing: '{$fileFullPath}'";
return FALSE;
}
if (!fwrite($fp, $outStr)){
$this->_lastError = "Failed to write (but was able to open) the file: '{$fileFullPath}'";
return FALSE;
}
@fclose($fp);
return TRUE;
}
/**
* resets this object so we can re-use it for something else.
* some setting vars are not reset.
*
* resets:
* _sections
* _params
* _fileFullPath
* _lastError
*
* keeps:
* commentChars
* unQuote
*
* @access public
* @return void
*/
function reset() {
unset($this->_sections);
unset($this->_params);
unset($this->_fileFullPath);
unset($this->_lastError);
}
/**
* returns [all parameters|parameter] [for the given section].
*
* examples:
* get() => returns all sections with all params as 2-D hash.
* array of [<section>][<key>] => <string>
* get('section') => returns all params for the section specified as 1-D hash.
* array of [<key>] => <string>
* get('section', 'key') => returns the param specified of the section specified as string.
*
* note: if a param is defined in the 'global scope', use an empty string for the
* $section name. example: get('', 'key')
*
* @access public
* @param string $section if not given returns all sections
* @param string $key if not given returns all keys
* @return mixed (see above)
* @throws null (if the given section or key does not exist)
*/
function get($section=NULL, $key=NULL) {
if (is_null($section)) return $this->_params;
if (!isSet($this->_params[$section])) return NULL; //throw
if (is_null($key)) return $this->_params[$section];
if (!isSet($this->_params[$section][$key])) return NULL; //throw
return $this->_params[$section][$key];
}
/**
* tells if the section or key specified is set.
*
* examples:
* has('mySection') => tells if 'mySection' is set
* has('mySection', 'myKey' => tells if myKey in mySection is set.
*
* note: case matters!
*
* @access public
* @param string $section
* @param string $key (default is NULL)
* @return bool
*/
function has($section, $key=NULL) {
if (is_null($key)) {
return (isSet($this->_params[$section])); //using _params instead of _sections cause it's a hash. in_array is slower.
} else {
return (isSet($this->_params[$section]) && isSet($this->_params[$section][$key]));
}
}
/**
* returns the text message of the last error that occured.
*
* call this function if something failed, for example after getting
* bool FALSE back from loadFile().
*
* @access public
* @return mixed (string last error, or NULL if no error occured.)
*/
function getLastError() {
if (is_null($this->_lastError)) return NULL;
return $this->_lastError;
}
}
#####################################################
# SELF - Test
#####################################################
if (basename($_SERVER['PHP_SELF']) == 'Bs_IniHandler.class.php') {
$testData =<<<EOD
# Comment 1
# comment 2
[]
globalData = foo
# comment A
[Some test data]
# comment B
one = hallo
two = "hallo"
# comment C
food = "Tom's Pizza = 'good stuff'"
more food = Sam's Pizza's = 'best stuff'
empty = ""
noVal =
# comment D
[more test data]
one = hi
two = 'hi'
food = 'Pizza = "good"'
empty = ''
noVal
# comment E
EOD;
$iniHandler = new Bs_IniHandler();
$iniHandler->loadString($testData);
#XR_dump($iniHandler->comments, __LINE__, '', __FILE__);
XR_dump($iniHandler->toString(), __LINE__, '', __FILE__);
}
?>
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |
More File Manipulation Code Articles
More By Codewalkers
developerWorks - FREE Tools! |
The IBM DB2 Deep Compression ROI tool is designed for DBA’s and IT management personnel to perform a clinical analysis of the cost savings gained from the Storage Optimization feature of DB2 9 for Linux, UNIX and Windows. The feature, also known as Deep Compression, compresses data that lies within a database by up to 80% at times. FREE! Go There Now!
|
|
|
|
Join this webcast, to learn how the Rational Process Library can help with compliance issues, drive process improvement, and assist in service-oriented architecture (SOA) or Agile development. We will take a peek into the Rational Process Library with content around software and systems engineering (including RUP), operations and systems management, program and portfolio management, and asset and SOA governance. FREE! Go There Now!
|
|
|
|
WebSphere Process Server delivers a unique integration framework that simplifies existing IT resources. Often, as IT assets grow to support business demand, so too does their complexity and manageability. In this webcast, we’ll discuss how WebSphere Process Server helps deliver an SOA infrastructure that provides a common model to orchestrate, mediate, connect, map, and execute the underlying IT functions. Discover how WebSphere Process Server simplifies integration of business processes by leveraging existing IT assets as reusable services without the complexities of traditional integration methodologies. FREE! Go There Now!
|
|
|
|
Download a free trial version of IBM Rational Developer for System z, software that can help you deliver core development capabilities; the power of Java Platform, Enterprise Edition (Java EE); and rapid application development support to diverse enterprise application development teams. With comprehensive development tools to help create, deploy and maintain traditional enterprise and composite applications, Rational Developer for System z enables developers with different technical backgrounds to easily participate in important technology projects. FREE! Go There Now!
|
|
|
|
Download the IBM WebSphere Portal V6.1 beta code and learn more about the rich features and enhancements in IBM WebSphere Portal V6.1. WebSphere Portal provides a composite application or business mashup framework and the advanced tooling needed to build flexible, SOA-based solutions, and scalability to meet the needs of any size organization. FREE! Go There Now!
|
|
|
|
Join us for this web seminar to learn how you can defend your web applications from attack. Learn about the 3 most common web application attacks, including how they occur and what can be done to prevent them. We’ll also discuss manual versus automated approaches for scanning and identifying web application vulnerabilities and how IBM Rational AppScan, an automated vulnerability scanner, can help you automate more of what you are doing manually today. FREE! Go There Now!
|
|
|
|
Join this webcast to see how IBM Data Studio Developer and pureQuery can take the pain out of Java data access. uApplications developed using both Java and SQL have become a common requirement. Database connectivity using Java Database Connectivity (JDBC) to create an application is a multi-step tedious process, and tooling that covers both SQL and Java has been unavailable, until now. IBM Data Studio introduces the pureQuery platform: a high-performance, Java data access platform focused on simplifying the tasks of developing, managing, and optimizing database applications and services. FREE! Go There Now!
|
|
|
|
Discover how Rational tools and best practices for testing can make your job easier. The new Rational Testing eKits provide you with valuable resources – including demos, webcasts, tutorials, and articles – that help you address your specific testing needs across the software lifecycle. Five new eKits are available covering the topics of Requirements and Test Management, Functional Testing, Performance Testing, Code Quality and Embedded Systems, and SOA and Web Services Testing. FREE! Go There Now!
|
|
|
|
Informix Dynamic Server (IDS) Express Edition offers outstanding online transaction processing (OLTP) database performance, while helping to simplify and automate many of the tasks associated with deploying databases for small business applications. IDS 11 further extends the ease of management and applications integration with the Admin API and Scheduler, high availability with Continuous Log Restore for backup server recovery in case of a primary server failure, and column level encryption to protect personal and company private data. FREE! Go There Now!
|
|
|
|
Join the IBM Watchfire team for an informative discussion on techniques and best practices to proactively manage Web application security and how to effectively build application security testing into the software development lifecycle (SDLC). In this Software Delivery Platform webcast you will learn: How to better understand potential web application security vulnerabilities, best practices and how to effectively integrate application security testing into the software development lifecycle, the importance of detecting and removing software vulnerabilities during application development. FREE! Go There Now!
|
|
|
|
All FREE IBM® developerWorks Tools! |