cvs: pear /File package.xml /File/File CSV.php /File/tests parser.php test.csv
| From: | Tomas V.V.Cox | Date: | Tue, 09 Apr 2002 03:03:15 +0000 |
| Subject: | cvs: pear /File package.xml /File/File CSV.php /File/tests parser.php test.csv | ||
| Groups: | php.pear.cvs | ||
| Request: | Send a blank email to pear-cvs+get-3159@lists.php.net to get a copy of this message | ||
cox Mon Apr 8 23:03:15 2002 EDT
Added files:
/pear/File/File CSV.php
/pear/File/tests parser.php test.csv
Modified files:
/pear/File package.xml
Log:
Initial commit of the CSV parser class
# info & explanation tomorrow, sorry it's too late
Index: pear/File/package.xml
diff -u pear/File/package.xml:1.1 pear/File/package.xml:1.2
--- pear/File/package.xml:1.1 Sun Mar 31 14:34:32 2002
+++ pear/File/package.xml Mon Apr 8 23:03:14 2002
@@ -2,7 +2,7 @@
<!DOCTYPE package SYSTEM "../package.dtd">
<!-- do not use the "Type" attribute here, that one is only for
generated package.xml files -->
-<package version="0.9">
+<package>
<name>File</name>
<summary>Common file and directory routines</summary>
@@ -24,19 +24,32 @@
<email>tal@php.net</email>
<role>lead</role>
</maintainer>
+ <maintainer>
+ <user>cox</user>
+ <name>Tomas V.V.Cox</name>
+ <email>cox@idecnet.com</email>
+ <role>developer</role>
+ </maintainer>
</maintainers>
<release>
<license>PHP</license>
- <version>0.9</version>
- <date>2002-3-30</date>
- <state>pre-release</state>
+ <version>0.9.1</version>
+ <date>2002-04-09</date>
+ <state>beta</state>
<notes>
Initial release
</notes>
<filelist>
- <dir name="/" baseinstalldir="/">
+ <dir name="/">
<file role="php">File.php</file>
+ <dir name="File">
+ <file role="php">CSV.php</file>
+ </dir>
+ <dir name="tests" role="test">
+ <file>parser.php</file>
+ <file>test.csv</file>
+ </dir>
</dir>
</filelist>
</release>
Index: pear/File/File/CSV.php
+++ pear/File/File/CSV.php
<?php
// +----------------------------------------------------------------------+
// | PHP Version 4 |
// +----------------------------------------------------------------------+
// | Copyright (c) 1997-2002 The PHP Group |
// +----------------------------------------------------------------------+
// | This source file is subject to version 2.0 of the PHP license, |
// | that is bundled with this package in the file LICENSE, and is |
// | available at through the world-wide-web at |
// | http://www.php.net/license/2_02.txt.
|
// | If you did not receive a copy of the PHP license and are unable to |
// | obtain it through the world-wide-web, please send a note to |
// | license@php.net so we can mail you a copy immediately. |
// +----------------------------------------------------------------------+
// | Authors: Tomas V.V.Cox <cox@idecnet.com> |
// | |
// +----------------------------------------------------------------------+
//
// $Id: CSV.php,v 1.1 2002/04/09 03:03:14 cox Exp $
require_once 'PEAR.php';
require_once 'File.php';
class File_CSV
{
function unquote($field, $quote)
{
if ($quote && $field{0} == $quote && $field{strlen($field)-1} == $quote) {
return substr($field, 1, -1);
}
return $field;
}
function read($file, $conf)
{
static $resources = array();
static $crlf;
if (!isset($resources[$file])) {
// check conf
if (isset($conf['sep'])) {
if (strlen($conf['sep']) != 1) {
return PEAR::raiseError('Separator can only be one char');
}
} else {
return PEAR::raiseError('Missing separator (the "sep" key)');
}
if (!isset($conf['fields']) || !is_numeric($conf['fields'])) {
return PEAR::raiseError('The number of fields must be numeric (the
"fields" key)');
}
if (isset($conf['quote'])) {
if (strlen($conf['quote']) != 1) {
return PEAR::raiseError('The quote char must be one chat (the
"quote" key)');
}
} else {
$conf['quote'] = null;
}
$fp = &File::_getFilePointer($file, FILE_MODE_READ);
if (PEAR::isError($fp)) {
return $fp;
}
$resources[$file] = $fp;
if (!empty($conf['header'])) {
File_CSV::read($file, $conf);
}
}
$fp = $resources[$file];
$buff = null;
$ret = array();
$i = 1;
$in_quote = false;
$quote = $conf['quote'];
while (($i <= $conf['fields']) && (($c = fgetc($fp)) !== false)) {
$prev = ($buff != '') ? $buff{strlen($buff) - 1} : null;
if ($quote && $c == $quote &&
($prev == $conf['sep'] || $prev == "\n" || $prev === null))
{
$in_quote = true;
}
if ($quote && $c == $conf['sep'] && $prev ==
$conf['quote']) {
$in_quote = false;
}
if (!$in_quote && $c == $conf['sep']) {
$ret[] = File_CSV::unquote($buff, $quote);
$buff = '';
$i++;
continue;
}
if ($c == "\n") {
$sub = ($prev == "\r") ? 2 : 1;
if ($buff{strlen($buff) - $sub} == $quote) {
$in_quote = false;
}
if (!$in_quote) {
if ($prev == "\r") {
$buff = substr($buff, 0, -1);
}
$ret[] = File_CSV::unquote($buff, $quote);
$buff = '';
$i++;
continue;
}
}
$buff .= $c;
}
if ($buff) {
$ret[] = File_CSV::unquote($buff, $quote);
}
return !feof($fp) ? $ret : false;
}
}
?>
Index: pear/File/tests/parser.php
+++ pear/File/tests/parser.php
<?php
$path = ini_get('include_path');
ini_set('include_path', realpath('../') . ":$path");
require_once 'File/CSV.php';
$conf = array(
'fields' => 4,
'sep' => "\t",
'quote' => '"',
'header' => true
);
ob_implicit_flush(true);
$argv = $_SERVER['argv'];
$file = $argv[1];
PEAR::setErrorHandling(PEAR_ERROR_PRINT, "warning: %s\n");
$i = 0;
while ($fields = File_CSV::read($file, $conf)) {
print_r($fields);
//if ($i++ >= 2) break;
}
echo "\n"
?>
Index: pear/File/tests/test.csv
+++ pear/File/tests/test.csv
f1 f2 f3 f4
good1 good2 good3 good4
no"quote no"qu"ote n"quote" n"q"u"o"t"
"quote1" "quote2" noquote3 noquote4
test1 "long
test2" "long test3 with seps" "I'm
silly
long"