Version 3.0.0
More functions, more options!
Class name: ini
- close ([ Resource link_identifier ])
- connect (String file)
- drop_key (String section, String key [, Resource link_identifier])
- drop_section (String section [, Resource link_identifier])
- get_keys(String section, [Resource link_identifier])
- get_section ([Resource link_identifier])
- key_exists (String section, String key, [Resource link_identifier])
- section_exists (String section, [Resource link_identifier])
- read (String section, String key, [Resource link_identifier])
- write (String section [, String key [, String value [, Resource Link_Identifier]]])
By : jorgen
<?php
///////////////////////////////////////////////////////////////////////////////
//
// INI-functions
// Jorgen Horstink
//
// Class for reading and writing to INI files
// (c) Copyright 2002 Next Avenue, The Netherlands
//
// All rights reserved.
//
///////////////////////////////////////////////////////////////////////////////
error_reporting(E_ALL);
class ini
{
var $ini_data = array (); // Array to store all ini data
var $ini_data_files = array (); // Array to store all file names
var $crlf = "\n"; // "Carriage return", SEE function connect
var $f_temp = "tempA9B4E3F4C9_"; // The temporary file, SEE function connect
var $set_waiting_limit = 10; // The maximum waiting time in seconds, SEE function connect
// Return: Link_Identifier (integer)
function connect($file)
{
$temp = $this->f_temp;
$this->f_temp .= $file;
$t = 0; // Set the counter var
// While another file is bussy... wait wait wait
while (@is_array(file($this->f_temp)))
{
$t++;
// IMPORTANT IMPORTANT IMPORTANT IMPORTANT IMPORTANT IMPORTANT IMPORTANT IMPORTANT IMPORTANT
//
// If someone does not use the function Close, the temporary file will not be deleted.
// But if you open the file for the second time, the function Connect notices that
// the temporary file already exists. So the functions 'thinks' some other file is busy. But
// that is not true, the file has not been deleted.
// To solve this problem, the function writes the timestamp in the temporary file. When the
// function notices that the file already exists, it checks whether or not the last opened
// timestamp is too old. If the timestamp is too old, the function notices that the function
// Close probably is not used. So the function deletes the temporary file and goes on...
//
// IMPORTANT IMPORTANT IMPORTANT IMPORTANT IMPORTANT IMPORTANT IMPORTANT IMPORTANT IMPORTANT
// Get the time out of the file, when the last time the script is parsed
$time = file($this->f_temp);
// If the time is too old, delete the file and break the while-statement
if ((time() - $this->set_waiting_limit) > $time[0])
{
unlink($this->f_temp);
break;
}
sleep(1); // Check every second...
if ($t > $this->set_waiting_limit) break;
}
// Create the temporary file
$fp = fopen($this->f_temp, "w+");
fwrite($fp, time());
fclose($fp);
$this->f_temp = $temp;
// If the file does not exist, return -1
if (!file_exists($file))
return (-1);
// Check whether or not there already is a connection with the selected file
for ($i = 0; $i < sizeof($this->ini_data_files); $i++)
if ($this->ini_data_files[$i] == $file)
return ($i);
// Get all ini data and store it in an Array
$ini_array = parse_ini_file($file, TRUE);
// Add the Array to ini_data[]
$this->ini_data[] = $ini_array;
// Store the filename in $ini_data_files[]
$this->ini_data_files[] = $file;
// Return the new Link_Identifier
return (sizeof($this->ini_data) - 1);
}
// Return: Boolean
function close($link = "")
{
$data_string = "";
// If the parameter $link is empty, get the last Link_Identifier
if (empty($link))
$link = sizeof($this->ini_data) - 1;
if (sizeof($this->ini_data) == 0)
return (FALSE);
// Get all ini data corresponding to the Link_Identifier
$get_data_from_array = $this->ini_data[$link];
$array_keys = array_keys($get_data_from_array);
// Build the new String...
for ($i = 0; $i < sizeof($get_data_from_array); $i++)
{
$get_data_from_key = $get_data_from_array[$array_keys[$i]];
$key_array_keys = array_keys($get_data_from_key);
$data_string .= "[" . $array_keys[$i] . "]" . $this->crlf;
for ($j = 0; $j < sizeof($key_array_keys); $j++)
$data_string .= $key_array_keys[$j] . "=" . $get_data_from_key[$key_array_keys[$j]] . $this->crlf;
}
// Get the filename
$filename = $this->ini_data_files[$link];
// If the file does not exist, return FALSE
if (!file_exists($filename))
return (FALSE);
@unlink($this->f_temp . $filename);
// Try to write all data into the file
$fp = fopen($filename, "w+");
fwrite($fp, $data_string);
fclose($fp);
// Delete the old data
unset($this->ini_data[$link]);
// Delete the old filename
unset($this->ini_data_files[$link]);
return (TRUE);
}
// Return: Boolean
function drop_key($section, $key, $link)
{
// If the parameter $link is empty, get the last Link_Identifier
if (empty($link))
$link = sizeof($this->ini_data) - 1;
// If there is no connection to any INI file, return FALSE
if (sizeof($this->ini_data) == 0)
return (FALSE);
// If the section does not exist, return FALSE
if (!$this->section_exists($section))
return (FALSE);
// If the key does not exist, return FALSE
if (!$this->key_exists($section, $key))
return (FALSE);
// Unset the key in the chosen section
unset($this->ini_data[$link][$section][$key]);
return (TRUE);
}
// Return: Boolean
function drop_section($section, $link)
{
// If the parameter $link is empty, get the last Link_Identifier
if (empty($link))
$link = sizeof($this->ini_data) - 1;
// If there is no connection to any INI file, return FALSE
if (sizeof($this->ini_data) == 0)
return (FALSE);
// If the section does not exist, return FALSE
if (!$this->section_exists($section))
return (FALSE);
// Unset the section
unset($this->ini_data[$link][$section]);
return (TRUE);
}
// Return: Array
function get_keys($section, $link = "")
{
// If the parameter $link is empty, get the last Link_Identifier
if (empty($link))
$link = sizeof($this->ini_data) - 1;
// If there is no connection to any INI file, return FALSE
if (sizeof($this->ini_data) == 0)
return (FALSE);
// If the section does not exist, return FALSE
if (!$this->section_exists($section, $link))
return (FALSE);
// Get all keys and return them
$get_data = $this->ini_data[$link][$section];
return(array_keys($get_data));
}
// Return: Array
function get_sections($link = "")
{
// If the parameter $link is empty, get the last Link_Identifier
if (empty($link))
$link = sizeof($this->ini_data) - 1;
// If there is no connection to any INI file, return FALSE
if (sizeof($this->ini_data) == 0)
return (FALSE);
$get_data = $this->ini_data[$link];
return(array_keys($get_data));
}
// Return: Boolean
function key_exists($section, $key, $link = "")
{
// If the parameter $link is empty, get the last Link_Identifier
if (empty($link))
$link = sizeof($this->ini_data) - 1;
// If there is no connection to any INI file, return FALSE
if (sizeof($this->ini_data) == 0)
return (FALSE);
// If the section does not exist, return FALSE
if (!$this->section_exists($section, $link))
return (FALSE);
// Get all keys
$keys = $this->get_keys($section, $link);
for ($i = 0; $i < sizeof($keys); $i++)
if ($keys[$i] == $key)
return (TRUE);
// If no section is found, return FALSE
return (FALSE);
}
// Return: Boolean
function section_exists($section, $link = "")
{
// If the parameter $link is empty, get the last Link_Identifier
if (empty($link))
$link = sizeof($this->ini_data) - 1;
// If there is no connection to any INI file, return FALSE
if (sizeof($this->ini_data) == 0)
return (FALSE);
// Get all sections
$sections = $this->get_sections($link);
// Check whether or not the section exists
for ($i = 0; $i < sizeof($sections); $i++)
if ($sections[$i] == $section)
return (TRUE);
// If no section is found, return FALSE
return (FALSE);
}
// Return: String
function read($section, $key, $link = "")
{
// If the parameter $link is empty, get the last Link_Identifier
if (empty($link))
$link = sizeof($this->ini_data) - 1;
// If there is no connection to any INI file, return FALSE
if (sizeof($this->ini_data) == 0)
return (FALSE);
// If the section does not exist, return FALSE
if (!$this->section_exists($section, $link))
return (FALSE);
// If the key does not exist, return FALSE
if (!$this->key_exists($section, $key, $link))
return (FALSE);
return ($this->ini_data[$link][$section][$key]);
}
// Return: Boolean
function write($section, $key = "", $value = "", $link = "")
{
// If the parameter $link is empty, get the last Link_Identifier
if (empty($link))
$link = sizeof($this->ini_data) - 1;
// If there is no connection to any INI file, return FALSE
if (sizeof($this->ini_data) == 0)
return (FALSE);
if (empty($section))
return (FALSE);
// If the section does not exist, make a new section
if (!$this->section_exists($section))
$this->ini_data[$link][$section] = array ();
// If the key is not empty...
if (!empty($key))
$this->ini_data[$link][$section][$key] = $value;
return (TRUE);
}
}
// Make a new instance of the Class...
$ini = new ini;
?>
| 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! |
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!
|
|
|
|
Learn field-tested SOA principles, methodology, technology and implementation from the global SOA market leader - in a new e-book by an IBM SOA expert. Written by IBM Certified SOA Solution Designer Bobby Woolf, "Exploring IBM SOA Technology & Practice" is the ultimate insider's guide to SOA - a PDF e-book packed cover to cover with IBM's specific advice on how to make your SOA implementation a success. FREE! Go There Now!
|
|
|
|
Building a software-as-a-service solution requires addressing a few key technical challenges. In this webcast, we'll focus on the role of IBM Tivoli Directory Server and WebSphere Portlet Factory in creating a Software as a Service solution. We will demonstrate how to use Tivoli Directory Server to prevent the user population of one tenant from accessing the virtual portal and portlet components of another tenant. We will also use the dynamic profile capability of WebSphere Portlet Factory to create multiple highly customized applications from one code base. FREE! Go There Now!
|
|
|
|
Visit IBM developerWorks to download IBM DB2 Express-C 9.5, a no-charge version of DB2 Express 9 database server. DB2 Express-C offers the same core data server base features as other DB2 Express editions and provides a solid base to build and deploy applications developed using C/C++, Java, .NET, PHP, and other programming languages. FREE! Go There Now!
|
|
|
|
Manage, govern, and share services across your organization by using WebSphere Service Registry and Repository. Follow the hands-on exercises to learn how to navigate the Web interface to publish, find, reuse, and update services. FREE! Go There Now!
|
|
|
|
As systems increase in complexity, communication between systems and software teams becomes more and more difficult. Now, there’s a way to improve product quality and communication.<br />Read the “Model Driven Systems Development” white paper to see how. Also included in this kit are more educational white papers, customer examples, tutorials, informative Webcasts, and best practices for designing, building and managing systems.<br /> FREE! Go There Now!
|
|
|
|
Learn how to do more with your reusable assets with the free Rational Asset Manager eKit. The eKit includes demos on how Rational Asset Manager tracks and audits your assets in order to utilize them for reuse. Plus you’ll find white papers and a Webcast that discuss the challenges of a Service Oriented Architecture and how Rational Asset Manager can provide quick and effective solutions. FREE! Go There Now!
|
|
|
|
Because access to government information continues to be an area of concern for many U.S. citizens with disabilities, the U.S. government enacted Section 508 of the Rehabilitation Act in 2001 to ensure that government agencies create accessible Web content, enabling all citizens to access the information they need. A fully accessible Web site makes Web content accessible to all individuals, including those with disabilities, who may be accessing Web content via a variety of user agents. Common user agents include standard Web browsers, text-only browsers, assistive devices and mobile devices such as cell phones or personal digital assistants (PDAs). FREE! Go There Now!
|
|
|
|
Get a free trial download of the latest version of IBM Rational Functional Tester V7.0.1. Rational Functional Tester is an automated functional and regression testing solution for QA teams concerned with the quality of their Java, Microsoft Visual Studio .NET, and Web-based applications. FREE! Go There Now!
|
|
|
|
Viper 2 brings a great value to developer communities including SQL, XML, PHP, Ruby, .NET and Java. You probably already know that DB2 Express-C is free for developers to develop, deploy and distribute. Viper 2 provides a variety of means that help move your application from the development stage to deployment more rapidly. This webcast shows how to best utilize the latest tools available for developing DB2 applications. FREE! Go There Now!
|
|
|
|
All FREE IBM® developerWorks Tools! |