package.xml 100666 0 0 45220 10630107705 6251
File
pear.php.net
Common file and directory routines, also CSV handling
Provides easy access to read/write to files along with
some common routines to deal with paths.
Also provides interface for handling CSV files.
Richard Heyes
richard
richard@php.net
no
Tal Peer
tal
tal@php.net
no
Michael Wallner
mike
mike@php.net
no
Helgi Þormar
dufuz
helgi@php.net
yes
Tomas V.V. Cox
cox
cox@idecnet.com
no
Cipriano Groenendal
cipri
cipri@php.net
yes
2007-06-01
1.3.0
1.3.0
stable
stable
PHP
No code changes in this release
4.3.0
1.4.0b1
PEAR
pear.php.net
1.5.3
pcre
2007-05-20
1.3.0RC1
1.3.0RC1
beta
stable
PHP
No code changes in this release
2007-05-13
1.3.0a1
1.3.0a1
alpha
stable
PHP
* Fixed Bug #5045 File::stripTrailingSeparators should not rtrim the path "/" on linux (mike)
* Fixed Bug #5464 fwrite returns false but file checks for -1 (mike)
* Request #6316 File_CSV doesn't allow to read data from a file and after write data to the same file (helgi)
* Request #7559 File_Util::isIncludable() (file exists within include_path) (mike)
* Fixed Bug #7789 File::_getFilePointer() cannot return a reference (mike)
* Added more CSV tests (helgi)
* Moving the PHP dep to 4.3 to follow PEAR, adding dep on PEAR 1.5.0 to be sure people run proper PEAR version (helgi)
* File_CSV: add b mode to fopen in disoverFormat for portability (http://www.php.net/fopen)
* Fixed Bug #6447 Package will not install (helgi)
* Fixed Bug #5553 File_CSV should detect an occurance of delimeter in field and escape (helgi)
* Fixed Bug #4792 File_CSV: Last column keeps line breaks (helgi)
* Fixed Bug #10721 File_CSV: discoverFormat() can't handle large files (ieure)
* Fixed Bug #10883 Test 019 mislabeled
* Fixed Bug #10882 Tests fail (helgi / cipri)
* File_CSV: discoverFormat now uses the first 30 lines of the file. (helgi)
* File_CSV: supports reading ="" quoting, i.e. Excel only way of being smart with items starting with 00 and spaces and such (helgi)
* File_CSV: Now handles headers, pass header = true in the config for it to process it, we recommend reading like this:
$data = array();
File_CSV::read($file, $conf);
while ($res = File_CSV::read($file, $conf)) {
$data[] = $res;
}
Note the first read call outside the while loop, it's because we don't want to include the header in the final data since
the first read gives that back just in case someone needs to do something else with those headers.
Passing this csv and the correct config:
header,body,foot
I'm a little header,this is my teapot,can't be!
Will give you this:
Array
(
[0] => Array
(
[header] => I'm a little header
[body] => this is my teapot
[foot] => can't be!
)
)
detection of headers will never be added since CSV has no standard for defining what is a header and what is not (helgi)
1.2.2
1.2.2
stable
stable
2005-08-12
PHP
* Fixed bug #5071 install File throws XML error (helgi)
1.2.1
1.2.1
stable
stable
2005-08-00
PHP
* Fixed bug in File::close() on Windows (mike)
* Fixed bug #4835 - File::readLine() causes memory exhaustion (mike)
* Fixed bug #4911 - Bad test in File_Util::isAbsolute() (mike)
* Fixed bug #5032 - Reverted File::buildPath() to original implementation (mike)
* Fixed bug #4334 - File_CSV::discoverFormat does not detect quotes in single column (helgi) (thanks to luke_richards_99 at yahoo dot co dot uk)
* Fixed bug #4559 - Wrong PHP version check in File_CSV (helgi)
* Fixed bug #4295 - resetPointer method added to allow for reseting the file pointer to the beginning (helgi)
+ added test for bug #4334 (helgi)
1.2.0
1.2.0
stable
stable
2005-03-30
PHP
* Fixed package.xml for PEAR 1.4 (helgi)
+ added FILE_SORT_RANDOM mode (mike)
+ added File_Util::switchExt() (mike)
1.1.0
1.1.0
stable
stable
2005-02-21
PHP
* Fixed bug in File_Util::sortFiles() (mike, Demian Turner)
* Fixed infinity loop in readQuoted (File_CSV) when fields are more then expected (helgi)
* Fixed File_CSV tests (helgi)
1.1.0RC5
1.1.0RC5
beta
beta
2005-02-02
PHP
* Bug #3364 fixed, typo (helgi)
1.1.0RC4
1.1.0RC4
beta
beta
2005-02-02
PHP
* Required PHP dep now 4.2.0 because of PEAR (helgi)
* Patch from Firman Wandayandi for File_CSV (helgi)
- Fixed bugs: Fields count less nor more than expected handling
- Added Mac EOL support (Only loaded on PHP 4.3.0 and higher)
- Added few tests
* added kind of a filter callback for File_Util::listDir() (mike)
* Fixed Bug #3355 (missing delimiter of preg_quote() in File_Util::buildPath()) (mike)
* Fixed Bug #3357 (infinite loop in File_Util::realPath()) (mike)
1.1.0RC3
1.1.0RC3
beta
beta
2005-01-13
PHP
* now really containing the fix for File_CSV
1.1.0RC2
1.1.0RC2
beta
beta
2005-01-12
PHP
* added File_Util containing all methods not handling file I/O (mike)
* deprecated methods are now available in File_Util (still in File for BC) (mike)
* fixed bug #2827 (File_CSV::discoverFormat() is unable to discover format in
one column CSV file), allows 1 field per line in discoverFormat as well as
the config overall, with no separator (which is the standard), removed
one error check to fix this issue as well as moving error checking around
in _conf, might give some people issues (can't see how tho), also added a
new param to discoverFormat so one can inject a check for $ as a separator
or something like that (helgi)
1.1.0RC1
1.1.0RC1
beta
beta
2004-12-17
PHP
* Fixed Bug #2810 (Can not call readAll two times)
* Fixed file locking
- Code cleanup (vastly)
+ Implemented Request #1542 (File::relativePath(), File::realPath())
1.0.3
1.0.3
stable
stable
2003-01-28
PHP
Fixed handling of paths containing '..' and '~' in File::isAbsolute().
1.0.2
1.0.2
stable
stable
2002-05-26
PHP
Revert to mode specification instead of using _checkAppend() function
1.0.1
1.0.1
stable
stable
2002-05-03
PHP
Bugfix in _checkAppend() usage
1.0.0
1.0.0
stable
stable
2002-05-02
PHP
Stable release
0.9.2
0.9.2
beta
beta
2002-04-24
PHP
Fixed bug apparent when using fopen wrappers
0.9.1
0.9.1
beta
beta
2002-04-09
PHP
Initial release
File-1.3.0/File/CSV.php 100666 0 0 52064 10630107705 7477
* @author Helgi Þormar
* @copyright 2004-2005 The Authors
* @license http://www.php.net/license/3_0.txt PHP License 3.0
* @version CVS: $Id: CSV.php,v 1.41 2007/05/20 12:25:14 dufuz Exp $
* @link http://pear.php.net/package/File
*/
require_once 'PEAR.php';
require_once 'File.php';
/**
* File class for handling CSV files (Comma Separated Values), a common format
* for exchanging data.
*
* TODO:
* - Usage example and Doc
* - Use getPointer() in discoverFormat
* - Add a line counter for being able to output better error reports
* - Store the last error in GLOBALS and add File_CSV::getLastError()
*
* Wish:
* - Other methods like readAll(), writeAll(), numFields(), numRows()
* - Try to detect if a CSV has header or not in discoverFormat() (not possible with CSV)
*
* Known Bugs:
* (they has been analyzed but for the moment the impact in the speed for
* properly handle this uncommon cases is too high and won't be supported)
* - A field which is composed only by a single quoted separator (ie -> ;";";)
* is not handled properly
* - When there is exactly one field minus than the expected number and there
* is a field with a separator inside, the parser will throw the "wrong count" error
*
* Info about CSV and links to other sources
* http://www.shaftek.org/publications/drafts/mime-csv/draft-shafranovich-mime-csv-00.html#appendix
*
* @author Tomas V.V.Cox
* @author Helgi Þormar
* @package File
*/
class File_CSV
{
/**
* This raiseError method works in a different way. It will always return
* false (an error occurred) but it will call PEAR::raiseError() before
* it. If no default PEAR global handler is set, will trigger an error.
*
* @param string $error The error message
* @return bool always false
*/
function raiseError($error)
{
// If a default PEAR Error handler is not set trigger the error
// XXX Add a PEAR::isSetHandler() method?
if ($GLOBALS['_PEAR_default_error_mode'] == PEAR_ERROR_RETURN) {
PEAR::raiseError($error, null, PEAR_ERROR_TRIGGER, E_USER_WARNING);
} else {
PEAR::raiseError($error);
}
return false;
}
/**
* Checks the configuration given by the user
*
* @access private
* @param string &$error The error will be written here if any
* @param array &$conf The configuration assoc array
* @return string error Returns a error message
*/
function _conf(&$error, &$conf)
{
// check conf
if (!is_array($conf)) {
return $error = 'Invalid configuration';
}
if (!isset($conf['fields']) || !(int)$conf['fields']) {
return $error = 'The number of fields must be numeric (the "fields" key)';
}
if (isset($conf['sep'])) {
if (strlen($conf['sep']) != 1) {
return $error = 'Separator can only be one char';
}
} elseif ($conf['fields'] > 1) {
return $error = 'Missing separator (the "sep" key)';
}
if (isset($conf['quote'])) {
if (strlen($conf['quote']) != 1) {
return $error = 'The quote char must be one char (the "quote" key)';
}
} else {
$conf['quote'] = null;
}
if (!isset($conf['crlf'])) {
$conf['crlf'] = "\n";
}
if (!isset($conf['eol2unix'])) {
$conf['eol2unix'] = true;
}
}
/**
* Return or create the file descriptor associated with a file
*
* @param string $file The name of the file
* @param array &$conf The configuration
* @param string $mode The open node (ex: FILE_MODE_READ or FILE_MODE_WRITE)
* @param boolean $reset if passed as true and resource for the file exists
* than the file pointer will be moved to the beginning
*
* @return mixed A file resource or false
*/
function getPointer($file, &$conf, $mode = FILE_MODE_READ, $reset = false)
{
static $resources = array();
static $config;
if (isset($resources[$file][$mode])) {
$conf = $config;
if ($reset) {
fseek($resources[$file][$mode], 0);
}
return $resources[$file][$mode];
}
File_CSV::_conf($error, $conf);
if ($error) {
return File_CSV::raiseError($error);
}
$config = $conf;
PEAR::pushErrorHandling(PEAR_ERROR_RETURN);
$fp = File::_getFilePointer($file, $mode);
PEAR::popErrorHandling();
if (PEAR::isError($fp)) {
return File_CSV::raiseError($fp);
}
$resources[$file][$mode] = $fp;
return $fp;
}
/**
* Unquote data
*
* @param string $field The data to unquote
* @param string $quote The quote char
* @return string the unquoted data
*/
function unquote($field, $quote)
{
// Trim first the string.
$field = trim($field);
$quote = trim($quote);
// Incase null fields (form: ;;)
if (!strlen($field)) {
return $field;
}
// excel compat
if ($field[0] == '=' && $field[1] == '"') {
$field = str_replace('="', '"', $field);
}
$field_len = strlen($field);
if ($quote && $field[0] == $quote && $field[$field_len - 1] == $quote) {
// Get rid of escaping quotes
$new = $prev = $c = '';
for ($i = 0; $i < $field_len; ++$i) {
$prev = $c;
$c = $field[$i];
// Deal with escaping quotes
if ($c == $quote && $prev == $quote) {
$c = '';
}
$new .= $c;
}
$field = substr($new, 1, -1);
}
return $field;
}
/**
* Reads a row of data as an array from a CSV file. It's able to
* read memo fields with multiline data.
*
* @param string $file The filename where to write the data
* @param array &$conf The configuration of the dest CSV
*
* @return mixed Array with the data read or false on error/no more data
*/
function readQuoted($file, &$conf)
{
if (!$fp = File_CSV::getPointer($file, $conf, FILE_MODE_READ)) {
return false;
}
$buff = $old = $prev = $c = '';
$ret = array();
$i = 1;
$in_quote = false;
$quote = $conf['quote'];
$f = $conf['fields'];
$sep = $conf['sep'];
while (false !== $ch = fgetc($fp)) {
$old = $prev;
$prev = $c;
$c = $ch;
// Common case
if ($c != $quote && $c != $sep && $c != "\n" && $c != "\r") {
$buff .= $c;
continue;
}
// Start quote.
if (
$in_quote === false &&
$quote && $c == $quote &&
(
$prev == $sep || $prev == "\n" || $prev === null ||
$prev == "\r" || $prev == '' || $prev == ' '
|| $prev == '=' //excel compat
)
) {
$in_quote = true;
// excel compat, removing the = part but only if we are in a quote
if ($prev == '=') {
$buff{strlen($buff) - 1} = '';
}
}
if ($in_quote) {
// When does the quote end, make sure it's not double quoted
if ($c == $sep && $prev == $quote && $old != $quote) {
$in_quote = false;
} elseif ($c == $sep && $buff == $quote.$quote) {
// In case we are dealing with double quote but empty value
$in_quote = false;
} elseif ($c == "\n" || $c == "\r") {
$sub = ($prev == "\r") ? 2 : 1;
$buff_len = strlen($buff);
if (
$buff_len >= $sub &&
$buff[$buff_len - $sub] == $quote
) {
$in_quote = false;
}
}
}
if (!$in_quote && ($c == $sep || $c == "\n" || $c == "\r") && $prev != '') {
// More fields than expected
if ($c == $sep && (count($ret) + 1) == $f) {
// Seek the pointer into linebreak character.
while (true) {
$c = fgetc($fp);
if ($c == "\n" || $c == "\r" || $c == '') {
break;
}
}
// Insert last field value.
$ret[] = File_CSV::unquote($buff, $quote);
return $ret;
}
// Less fields than expected
if (($c == "\n" || $c == "\r") && $i != $f) {
// Insert last field value.
$ret[] = File_CSV::unquote($buff, $quote);
if (count($ret) == 1 && empty($ret[0])) {
return array();
}
// Pair the array elements to fields count. - inserting empty values
$ret_count = count($ret);
$sum = ($f - 1) - ($ret_count - 1);
$data = array_merge($ret, array_fill($ret_count, $sum, ''));
return $data;
}
if ($prev == "\r") {
$buff = substr($buff, 0, -1);
}
// Convert EOL character to Unix EOL (LF).
if ($conf['eol2unix']) {
$buff = preg_replace('/(\r\n|\r)$/', "\n", $buff);
}
$ret[] = File_CSV::unquote($buff, $quote);
if (count($ret) == $f) {
return $ret;
}
$buff = '';
++$i;
continue;
}
$buff .= $c;
}
/* If it's the end of the file and we still have something in buffer
* then we process it since files can have no CL/FR at the end
*/
$feof = feof($fp);
if ($feof && !in_array($buff, array("\r", "\n", "\r\n")) && strlen($buff) > 0) {
$ret[] = File_CSV::unquote($buff, $quote);
if (count($ret) == $f) {
return $ret;
}
}
return !$feof ? $ret : false;
}
/**
* Reads a "row" from a CSV file and return it as an array
*
* @param string $file The CSV file
* @param array &$conf The configuration of the dest CSV
*
* @return mixed Array or false
*/
function read($file, &$conf)
{
static $headers = array();
if (!$fp = File_CSV::getPointer($file, $conf, FILE_MODE_READ)) {
return false;
}
// The size is limited to 4K
if (!$line = fgets($fp, 4096)) {
return false;
}
$fields = $conf['fields'] == 1 ? array($line) : explode($conf['sep'], $line);
$nl = array("\n", "\r", "\r\n");
if (in_array($fields[count($fields) - 1], $nl)) {
array_pop($fields);
}
$field_count = count($fields);
$last =& $fields[$field_count - 1];
$len = strlen($last);
if (
$field_count != $conf['fields'] ||
$conf['quote'] &&
(
$len !== 0 && $last[$len - 1] == "\n"
&&
(
($last[0] == $conf['quote']
&& $last[strlen(rtrim($last)) - 1] != $conf['quote'])
||
// excel support
($last[0] == '=' && $last[1] == $conf['quote'])
||
// if the row has spaces before the quote
preg_match('|^\s+'.preg_quote($conf['quote']) .'|Ums', $last, $match)
)
)
// XXX perhaps there is a separator inside a quoted field
//preg_match("|{$conf['quote']}.*{$conf['sep']}.*{$conf['quote']}|U", $line)
) {
fseek($fp, -1 * strlen($line), SEEK_CUR);
return File_CSV::readQuoted($file, $conf);
} else {
foreach ($fields as $k => $v) {
$fields[$k] = File_CSV::unquote($v, $conf['quote']);
}
}
if (isset($conf['header']) && empty($headers)) {
// read the first row and assign to $headers
$headers = $fields;
return $headers;
}
if ($field_count != $conf['fields']) {
File_CSV::raiseError("Read wrong fields number count: '". $field_count .
"' expected ".$conf['fields']);
return true;
}
if (!empty($headers)) {
$tmp = array();
foreach ($fields as $k => $v) {
$tmp[$headers[$k]] = $v;
}
$fields = $tmp;
}
return $fields;
}
/**
* Internal use only, will be removed in the future
*
* @param string $str The string to debug
* @access private
*/
function _dbgBuff($str)
{
if (strpos($str, "\r") !== false) {
$str = str_replace("\r", "_r_", $str);
}
if (strpos($str, "\n") !== false) {
$str = str_replace("\n", "_n_", $str);
}
if (strpos($str, "\t") !== false) {
$str = str_replace("\t", "_t_", $str);
}
if ($str === null) {
$str = '_NULL_';
}
if ($str === '') {
$str = 'Empty string';
}
echo "buff: ($str)\n";
}
/**
* Writes a struc (array) in a file as CSV
*
* @param string $file The filename where to write the data
* @param array $fields Ordered array with the data
* @param array &$conf The configuration of the dest CSV
*
* @return bool True on success false otherwise
*/
function write($file, $fields, &$conf)
{
if (!$fp = File_CSV::getPointer($file, $conf, FILE_MODE_WRITE)) {
return false;
}
$field_count = count($fields);
if ($field_count != $conf['fields']) {
File_CSV::raiseError("Wrong fields number count: '". $field_count .
"' expected ".$conf['fields']);
return true;
}
$write = '';
for ($i = 0; $i < $field_count; ++$i) {
// only quote if the field contains a sep
if (!is_numeric($fields[$i]) && $conf['quote']
&& isset($conf['sep']) && strpos($fields[$i], $conf['sep'])
) {
$write .= $conf['quote'] . $fields[$i] . $conf['quote'];
} else {
$write .= $fields[$i];
}
$write .= ($i < ($field_count - 1)) ? $conf['sep']: $conf['crlf'];
}
if (!fwrite($fp, $write, strlen($write))) {
return File_CSV::raiseError('Can not write to file');
}
return true;
}
/**
* Discover the format of a CSV file (the number of fields, the separator
* and if it quote string fields)
*
* @param string the CSV file name
* @param array extra separators that should be checked for.
* @return mixed Assoc array or false
*/
function discoverFormat($file, $extraSeps = array())
{
if (!$fp = @fopen($file, 'rb')) {
return File_CSV::raiseError("Could not open file: $file");
}
// Set auto detect line ending for Mac EOL support
$oldini = ini_get('auto_detect_line_endings');
if ($oldini != '1') {
ini_set('auto_detect_line_endings', '1');
}
// Take the first 30 lines and store the number of ocurrences
// for each separator in each line
$lines = '';
for ($i = 0; $i < 30 && $line = fgets($fp, 4096); $i++) {
$lines .= $line;
}
fclose($fp);
if ($oldini != '1') {
ini_set('auto_detect_line_endings', $oldini);
}
$seps = array("\t", ';', ':', ',');
$seps = array_merge($seps, $extraSeps);
$matches = array();
$quotes = '"\'';
$lines = str_replace('""', '', $lines);
while ($lines != ($newLines = preg_replace('|((["\'])[^"]*(\2))|', '\2_\2', $lines))){
$lines = $newLines;
}
$eol = strpos($lines, "\r") ? "\r" : "\n";
$lines = explode($eol, $lines);
foreach ($lines as $line) {
$orgLine = $line;
foreach ($seps as $sep) {
$line = preg_replace("|^[^$quotes$sep]*$sep*([$quotes][^$quotes]*[$quotes])|sm", '_', $orgLine);
// Find all seps that are within qoutes
///FIXME ... counts legitimit lines as bad ones
// In case there's a whitespace infront the field
$regex = '|\s*?';
// Match the first quote (optional), also optionally match = since it's excel stuff
$regex.= "(?:\=?[$quotes])";
$regex.= '(.*';
// Don't match a sep if we are inside a quote
// also don't accept the sep if it has a quote on the either side
///FIXME has to be possible if we are inside a quote! (tests fail because of this)
$regex.= "(?:[^$quotes])$sep(?:[^$quotes])";
$regex.= '.*)';
// Close quote (if it's present) and the sep (optional, could be end of line)
$regex.= "(?:[$quotes](?:$sep?))|Ums";
preg_match_all($regex, $line, $match);
// Finding all seps, within quotes or not
$sep_count = substr_count($line, $sep);
// Real count
$matches[$sep][] = $sep_count - count($match[0]);
}
}
$final = array();
// Group the results by amount of equal ocurrences
foreach ($matches as $sep => $res) {
$times = array();
$times[0] = 0;
foreach ($res as $k => $num) {
if ($num > 0) {
$times[$num] = (isset($times[$num])) ? $times[$num] + 1 : 1;
}
}
arsort($times);
// Use max fields count.
$fields[$sep] = max(array_flip($times));
$amount[$sep] = $times[key($times)];
}
arsort($amount);
$sep = key($amount);
$conf['fields'] = $fields[$sep] + 1;
$conf['sep'] = $sep;
// Test if there are fields with quotes around in the first 30 lines
$quote = null;
$string = implode('', $lines);
foreach (array('"', '\'') as $q) {
if (preg_match_all("|$sep(?:\s*?)(\=?[$q]).*([$q])$sep|Us", $string, $match)) {
if ($match[1][0] == $match[2][0]) {
$quote = $match[1][0];
break;
}
}
if (
preg_match_all("|^(\=?[$q]).*([$q])$sep{0,1}|Ums", $string, $match)
|| preg_match_all("|(\=?[$q]).*([$q])$sep\s$|Ums", $string, $match)
) {
if ($match[1][0] == $match[2][0]) {
$quote = $match[1][0];
break;
}
}
}
$conf['quote'] = $quote;
return $conf;
}
/**
* Front to call getPointer and moving the resource to the
* beginning of the file
* Reset it if you like.
*
* @param string $file The name of the file
* @param array &$conf The configuration
* @param string $mode The open node (ex: FILE_MODE_READ or FILE_MODE_WRITE)
*
* @return boolean true on success false on failure
*/
function resetPointer($file, &$conf, $mode)
{
if (!File_CSV::getPointer($file, $conf, $mode, true)) {
return false;
}
return true;
}
} File-1.3.0/File/Util.php 100666 0 0 33530 10630107705 7756
* @copyright 2004-2005 Michael Wallner
* @license http://www.php.net/license/3_0.txt PHP License 3.0
* @version CVS: $Id: Util.php,v 1.25 2007/02/20 14:19:08 mike Exp $
* @link http://pear.php.net/package/File
*/
/**#@+
* Sorting Constants
*/
define('FILE_SORT_NONE', 0);
define('FILE_SORT_REVERSE', 1);
define('FILE_SORT_NAME', 2);
define('FILE_SORT_SIZE', 4);
define('FILE_SORT_DATE', 8);
define('FILE_SORT_RANDOM', 16);
/**#@-*/
/**#@+
* Listing Constants
*/
define('FILE_LIST_FILES', 1);
define('FILE_LIST_DIRS', 2);
define('FILE_LIST_DOTS', 4);
define('FILE_LIST_ALL', FILE_LIST_FILES | FILE_LIST_DIRS | FILE_LIST_DOTS);
/**#@-*/
/**
* @ignore
*/
define('FILE_WIN32', defined('OS_WINDOWS') ? OS_WINDOWS : !strncasecmp(PHP_OS, 'win', 3));
/**
* File_Util
*
* File and directory utility functions.
*
* @access public
* @static
*/
class File_Util
{
/**
* Returns a string path built from the array $pathParts. Where a join
* occurs multiple separators are removed. Joins using the optional
* separator, defaulting to the PHP DIRECTORY_SEPARATOR constant.
*
* @static
* @access public
* @param array $parts Array containing the parts to be joined
* @param string $separator The directory seperator
*/
function buildPath($parts, $separator = DIRECTORY_SEPARATOR)
{
$qs = '/^'. preg_quote($separator, '/') .'+$/';
for ($i = 0, $c = count($parts); $i < $c; $i++) {
if (!strlen($parts[$i]) || preg_match($qs, $parts[$i])) {
unset($parts[$i]);
} elseif (0 == $i) {
$parts[$i] = rtrim($parts[$i], $separator);
} elseif ($c - 1 == $i) {
$parts[$i] = ltrim($parts[$i], $separator);
} else {
$parts[$i] = trim($parts[$i], $separator);
}
}
return implode($separator, $parts);
}
/**
* Returns a path without leading / or C:\. If this is not
* present the path is returned as is.
*
* @static
* @access public
* @param string $path The path to be processed
* @return string The processed path or the path as is
*/
function skipRoot($path)
{
if (File_Util::isAbsolute($path)) {
if (FILE_WIN32) {
return substr($path, $path{3} == '\\' ? 4 : 3);
}
return ltrim($path, '/');
}
return $path;
}
/**
* Returns the temp directory according to either the TMP, TMPDIR, or
* TEMP env variables. If these are not set it will also check for the
* existence of /tmp, %WINDIR%\temp
*
* @static
* @access public
* @return string The system tmp directory
*/
function tmpDir()
{
if (FILE_WIN32) {
if (isset($_ENV['TEMP'])) {
return $_ENV['TEMP'];
}
if (isset($_ENV['TMP'])) {
return $_ENV['TMP'];
}
if (isset($_ENV['windir'])) {
return $_ENV['windir'] . '\\temp';
}
if (isset($_ENV['SystemRoot'])) {
return $_ENV['SystemRoot'] . '\\temp';
}
if (isset($_SERVER['TEMP'])) {
return $_SERVER['TEMP'];
}
if (isset($_SERVER['TMP'])) {
return $_SERVER['TMP'];
}
if (isset($_SERVER['windir'])) {
return $_SERVER['windir'] . '\\temp';
}
if (isset($_SERVER['SystemRoot'])) {
return $_SERVER['SystemRoot'] . '\\temp';
}
return '\temp';
}
if (isset($_ENV['TMPDIR'])) {
return $_ENV['TMPDIR'];
}
if (isset($_SERVER['TMPDIR'])) {
return $_SERVER['TMPDIR'];
}
return '/tmp';
}
/**
* Returns a temporary filename using tempnam() and File::tmpDir().
*
* @static
* @access public
* @param string $dirname Optional directory name for the tmp file
* @return string Filename and path of the tmp file
*/
function tmpFile($dirname = null)
{
if (!isset($dirname)) {
$dirname = File_Util::tmpDir();
}
return tempnam($dirname, 'temp.');
}
/**
* Returns boolean based on whether given path is absolute or not.
*
* @static
* @access public
* @param string $path Given path
* @return boolean True if the path is absolute, false if it is not
*/
function isAbsolute($path)
{
if (preg_match('/(?:\/|\\\)\.\.(?=\/|$)/', $path)) {
return false;
}
if (FILE_WIN32) {
return preg_match('/^[a-zA-Z]:(\\\|\/)/', $path);
}
return ($path{0} == '/') || ($path{0} == '~');
}
/**
* Checks for a file's existence, taking the current include path
* into consideration
*
* This method can be called statically
* (e.g., File_Util::isIncludable('config.php'))
*
* @param string $file
* @param string $sep the directory separator (optional)
* @return string the includable path
* @access public
* @static
*/
function isIncludable($file, $sep = DIRECTORY_SEPARATOR)
{
foreach ((array) explode(PATH_SEPARATOR, ini_get('include_path')) as $path) {
if (file_exists($path .= $sep . $file)) {
return $path;
}
}
if (file_exists($file)) {
return $file;
}
return NULL;
}
/**
* Get path relative to another path
*
* @static
* @access public
* @return string
* @param string $path
* @param string $root
* @param string $separator
*/
function relativePath($path, $root, $separator = DIRECTORY_SEPARATOR)
{
$path = File_Util::realpath($path, $separator);
$root = File_Util::realpath($root, $separator);
$dirs = explode($separator, $path);
$comp = explode($separator, $root);
if (FILE_WIN32) {
if (strcasecmp($dirs[0], $comp[0])) {
return $path;
}
unset($dirs[0], $comp[0]);
}
foreach ($comp as $i => $part) {
if (isset($dirs[$i]) && $part == $dirs[$i]) {
unset($dirs[$i], $comp[$i]);
} else {
break;
}
}
return str_repeat('..' . $separator, count($comp)) . implode($separator, $dirs);
}
/**
* Get real path (works with non-existant paths)
*
* @static
* @access public
* @return string
* @param string $path
* @param string $separator
*/
function realPath($path, $separator = DIRECTORY_SEPARATOR)
{
if (!strlen($path)) {
return $separator;
}
$drive = '';
if (FILE_WIN32) {
$path = preg_replace('/[\\\\\/]/', $separator, $path);
if (preg_match('/([a-zA-Z]\:)(.*)/', $path, $matches)) {
$drive = $matches[1];
$path = $matches[2];
} else {
$cwd = getcwd();
$drive = substr($cwd, 0, 2);
if ($path{0} !== $separator{0}) {
$path = substr($cwd, 3) . $separator . $path;
}
}
} elseif ($path{0} !== $separator) {
$path = getcwd() . $separator . $path;
}
$dirStack = array();
foreach (explode($separator, $path) as $dir) {
if (strlen($dir) && $dir !== '.') {
if ($dir == '..') {
array_pop($dirStack);
} else {
$dirStack[] = $dir;
}
}
}
return $drive . $separator . implode($separator, $dirStack);
}
/**
* Check whether path is in root path
*
* @static
* @access public
* @return bool
* @param string $path
* @param string $root
*/
function pathInRoot($path, $root)
{
static $realPaths = array();
if (!isset($realPaths[$root])) {
$realPaths[$root] = File_Util::realPath($root);
}
return false !== strstr(File_Util::realPath($path), $realPaths[$root]);
}
/**
* List Directory
*
* The final argument, $cb, is a callback that either evaluates to true or
* false and performs a filter operation, or it can also modify the
* directory/file names returned. To achieve the latter effect use as
* follows:
*
*
* name, "\n";
* }
* ?>
*
*
* @static
* @access public
* @return array
* @param string $path
* @param int $list
* @param int $sort
* @param mixed $cb
*/
function listDir($path, $list = FILE_LIST_ALL, $sort = FILE_SORT_NONE, $cb = null)
{
if (!strlen($path) || !is_dir($path)) {
return null;
}
$entries = array();
for ($dir = dir($path); false !== $entry = $dir->read(); ) {
if ($list & FILE_LIST_DOTS || $entry{0} !== '.') {
$isRef = ($entry === '.' || $entry === '..');
$isDir = $isRef || is_dir($path .'/'. $entry);
if ( ((!$isDir && $list & FILE_LIST_FILES) ||
($isDir && $list & FILE_LIST_DIRS)) &&
(!is_callable($cb) ||
call_user_func_array($cb, array(&$entry)))) {
$entries[] = (object) array(
'name' => $entry,
'size' => $isDir ? null : filesize($path .'/'. $entry),
'date' => filemtime($path .'/'. $entry),
);
}
}
}
$dir->close();
if ($sort) {
$entries = File_Util::sortFiles($entries, $sort);
}
return $entries;
}
/**
* Sort Files
*
* @static
* @access public
* @return array
* @param array $files
* @param int $sort
*/
function sortFiles($files, $sort)
{
if (!$files) {
return array();
}
if (!$sort) {
return $files;
}
if ($sort === 1) {
return array_reverse($files);
}
if ($sort & FILE_SORT_RANDOM) {
shuffle($files);
return $files;
}
$names = array();
$sizes = array();
$dates = array();
if ($sort & FILE_SORT_NAME) {
$r = &$names;
} elseif ($sort & FILE_SORT_DATE) {
$r = &$dates;
} elseif ($sort & FILE_SORT_SIZE) {
$r = &$sizes;
} else {
asort($files, SORT_REGULAR);
return $files;
}
$sortFlags = array(
FILE_SORT_NAME => SORT_STRING,
FILE_SORT_DATE => SORT_NUMERIC,
FILE_SORT_SIZE => SORT_NUMERIC,
);
foreach ($files as $file) {
$names[] = $file->name;
$sizes[] = $file->size;
$dates[] = $file->date;
}
if ($sort & FILE_SORT_REVERSE) {
arsort($r, $sortFlags[$sort & ~1]);
} else {
asort($r, $sortFlags[$sort]);
}
$result = array();
foreach ($r as $i => $f) {
$result[] = $files[$i];
}
return $result;
}
/**
* Switch File Extension
*
* @static
* @access public
* @return string|array
* @param string|array $filename
* @param string $to new file extension
* @param string $from change only files with this extension
* @param bool $reverse change only files not having $from extension
*/
function switchExt($filename, $to, $from = null, $reverse = false)
{
if (is_array($filename)) {
foreach ($filename as $key => $file) {
$filename[$key] = File_Util::switchExt($file, $to, $from);
}
return $filename;
}
if ($len = strlen($from)) {
$ext = substr($filename, -$len - 1);
$cfn = FILE_WIN32 ? 'strcasecmp' : 'strcmp';
if (!$reverse == $cfn($ext, '.'. $from)) {
return $filename;
}
return substr($filename, 0, -$len - 1) .'.'. $to;
}
if ($pos = strpos($filename, '.')) {
return substr($filename, 0, $pos) .'.'. $to;
}
return $filename .'.'. $to;
}
}
?>
File-1.3.0/tests/CSV/001.csv 100666 0 0 202 10630107705 10211 "Field 1-1", "Field 1-2", "Field 1-3", "Field 1-4"
"Field 2-1", "Field 2-2", "Field 2-3"
"Field 3-1", "Field 3-2"
"Field 4-1"
File-1.3.0/tests/CSV/001.phpt 100666 0 0 2310 10630107705 10413 --TEST--
File_CSV Test Case 001: Fields count less than expected
--FILE--
--EXPECT--
Format:
Array
(
[fields] => 4
[sep] => ,
[quote] => "
)
Data:
Array
(
[0] => Array
(
[0] => Field 1-1
[1] => Field 1-2
[2] => Field 1-3
[3] => Field 1-4
)
[1] => Array
(
[0] => Field 2-1
[1] => Field 2-2
[2] => Field 2-3
[3] =>
)
[2] => Array
(
[0] => Field 3-1
[1] => Field 3-2
[2] =>
[3] =>
)
[3] => Array
(
[0] => Field 4-1
[1] =>
[2] =>
[3] =>
)
)
File-1.3.0/tests/CSV/002.csv 100666 0 0 236 10630107705 10221 "Field 1-1", "Field 1-2", "Field 1-3", "Field 1-4"
"Field 2-1", "Field 2-2", "Field 2-3", "Field 2-4", "Extra Field"
"Field 3-1", "Field 3-2"
"Field 4-1"
File-1.3.0/tests/CSV/002.phpt 100666 0 0 2321 10630107705 10416 --TEST--
File_CSV Test Case 002: Fields count more than expected
--FILE--
--EXPECT--
Format:
Array
(
[fields] => 4
[sep] => ,
[quote] => "
)
Data:
Array
(
[0] => Array
(
[0] => Field 1-1
[1] => Field 1-2
[2] => Field 1-3
[3] => Field 1-4
)
[1] => Array
(
[0] => Field 2-1
[1] => Field 2-2
[2] => Field 2-3
[3] => Field 2-4
)
[2] => Array
(
[0] => Field 3-1
[1] => Field 3-2
[2] =>
[3] =>
)
[3] => Array
(
[0] => Field 4-1
[1] =>
[2] =>
[3] =>
)
)
File-1.3.0/tests/CSV/003.csv 100666 0 0 224 10630107705 10217 "Field 1-1","Field 1-2","Field 1-3","Field 1-4"
"Field 2-1","Field 2-2","Field 2-3","I'm multiline
Field"
"Field 3-1","Field 3-2","Field 3-3"
File-1.3.0/tests/CSV/003.phpt 100666 0 0 2112 10630107705 10415 --TEST--
File_CSV Test Case 003: Windows EOL
--FILE--
--EXPECT--
Format:
Array
(
[fields] => 4
[sep] => ,
[quote] => "
)
Data:
Array
(
[0] => Array
(
[0] => Field 1-1
[1] => Field 1-2
[2] => Field 1-3
[3] => Field 1-4
)
[1] => Array
(
[0] => Field 2-1
[1] => Field 2-2
[2] => Field 2-3
[3] => I'm multiline
Field
)
[2] => Array
(
[0] => Field 3-1
[1] => Field 3-2
[2] => Field 3-3
[3] =>
)
)
File-1.3.0/tests/CSV/004.csv 100666 0 0 217 10630107705 10222 "Field 1-1","Field 1-2","Field 1-3","Field 1-4"
"Field 2-1","Field 2-2","Field 2-3","I'm multiline
Field"
"Field 3-1","Field 3-2","Field 3-3"
File-1.3.0/tests/CSV/004.phpt 100666 0 0 2107 10630107705 10422 --TEST--
File_CSV Test Case 004: Unix EOL
--FILE--
--EXPECT--
Format:
Array
(
[fields] => 4
[sep] => ,
[quote] => "
)
Data:
Array
(
[0] => Array
(
[0] => Field 1-1
[1] => Field 1-2
[2] => Field 1-3
[3] => Field 1-4
)
[1] => Array
(
[0] => Field 2-1
[1] => Field 2-2
[2] => Field 2-3
[3] => I'm multiline
Field
)
[2] => Array
(
[0] => Field 3-1
[1] => Field 3-2
[2] => Field 3-3
[3] =>
)
)
File-1.3.0/tests/CSV/005.csv 100666 0 0 216 10630107705 10222 "Field 1-1","Field 1-2","Field 1-3","Field 1-4"
"Field 2-1","Field 2-2","Field 2-3","I'm multiline
Field"
"Field 3-1","Field 3-2","Field 3-3"
File-1.3.0/tests/CSV/005.phpt 100666 0 0 3064 10630107705 10426 --TEST--
File_CSV Test Case 005: Mac EOL
--FILE--
$row) {
if (strpos($row, "\r") !== false) {
$row = str_replace("\r", "_r_", $row);
}
if (strpos($row, "\n") !== false) {
$str = str_replace("\n", "_n_", $row);
}
if (strpos($row, "\t") !== false) {
$row = str_replace("\t", "_t_", $row);
}
$data[$key] = $row;
}
return $data;
}
$data = array_map('_dbgBuff', $data);
print "Data:\n";
print_r($data);
?>
--EXPECT--
Format:
Array
(
[fields] => 4
[sep] => ,
[quote] => "
)
Data:
Array
(
[0] => Array
(
[0] => Field 1-1
[1] => Field 1-2
[2] => Field 1-3
[3] => Field 1-4
)
[1] => Array
(
[0] => Field 2-1
[1] => Field 2-2
[2] => Field 2-3
[3] => I'm multiline_r_Field
)
[2] => Array
(
[0] => Field 3-1
[1] => Field 3-2
[2] => Field 3-3
[3] =>
)
)
File-1.3.0/tests/CSV/006.csv 100666 0 0 154 10630107705 10224 "Sample Data Line 1"
"Sample Data Line 2"
"Sample Data Line 3"
"Sample Data Line 4"
"Sample Data Line 5" File-1.3.0/tests/CSV/006.phpt 100666 0 0 707 10630107705 10410 --TEST--
File_CSV Test Case 006: One field quote autodiscovery
--FILE--
--EXPECT--
Format:
Array
(
[fields] => 1
[sep] => ,
[quote] => "
)
File-1.3.0/tests/CSV/007.csv 100666 0 0 516 10630107705 10227 John,Doe,120 jefferson st.,Riverside, NJ, 08075
Jack,McGinnis,220 hobo Av.,Phila, PA,09119
"John ""Da Man""",Repici,120 Jefferson St.,Riverside, NJ,08075
Stephen,Tyler,"7452 Terrace ""At the Plaza"" road",SomeTown,SD, 91234
,Blankman,,SomeTown, SD, 00298
"Joan ""the bone"", Anne",Jet,"9th, at Terrace plc",Desert City,CO,00123
File-1.3.0/tests/CSV/007.phpt 100666 0 0 3544 10630107705 10433 --TEST--
File_CSV Test Case 007: Various different lines.
--FILE--
--EXPECT--
Data:
Array
(
[0] => Array
(
[0] => John
[1] => Doe
[2] => 120 jefferson st.
[3] => Riverside
[4] => NJ
[5] => 08075
)
[1] => Array
(
[0] => Jack
[1] => McGinnis
[2] => 220 hobo Av.
[3] => Phila
[4] => PA
[5] => 09119
)
[2] => Array
(
[0] => John "Da Man"
[1] => Repici
[2] => 120 Jefferson St.
[3] => Riverside
[4] => NJ
[5] => 08075
)
[3] => Array
(
[0] => Stephen
[1] => Tyler
[2] => 7452 Terrace "At the Plaza" road
[3] => SomeTown
[4] => SD
[5] => 91234
)
[4] => Array
(
[0] =>
[1] => Blankman
[2] =>
[3] => SomeTown
[4] => SD
[5] => 00298
)
[5] => Array
(
[0] => Joan "the bone", Anne
[1] => Jet
[2] => 9th, at Terrace plc
[3] => Desert City
[4] => CO
[5] => 00123
)
)
File-1.3.0/tests/CSV/008.csv 100666 0 0 104 10630107705 10221 "Joan the bone, Anne",Jet,"9th, at Terrace plc",Desert City,CO,00123 File-1.3.0/tests/CSV/008.phpt 100666 0 0 1432 10630107705 10426 --TEST--
File_CSV Test Case 008: Various different lines.
--FILE--
--EXPECT--
Data:
Array
(
[0] => Array
(
[0] => Joan the bone, Anne
[1] => Jet
[2] => 9th, at Terrace plc
[3] => Desert City
[4] => CO
[5] => 00123
)
)
File-1.3.0/tests/CSV/009.csv 100666 0 0 405 10630107705 10226 "John ""Da Man""",Repici,120 Jefferson St.,Riverside, NJ,08075
Stephen,Tyler,"7452 Terrace ""At the Plaza"" road",SomeTown,SD, 91234
"John ""Da Man""",Repici,120 Jefferson St.,Riverside, NJ,08075
Repici,"John ""Da Man""",120 Jefferson St.,Riverside, NJ,08075 File-1.3.0/tests/CSV/009.phpt 100666 0 0 2745 10630107705 10437 --TEST--
File_CSV Test Case 009: Escaping of quotes within quotes.
--FILE--
--EXPECT--
Data:
Array
(
[0] => Array
(
[0] => John "Da Man"
[1] => Repici
[2] => 120 Jefferson St.
[3] => Riverside
[4] => NJ
[5] => 08075
)
[1] => Array
(
[0] => Stephen
[1] => Tyler
[2] => 7452 Terrace "At the Plaza" road
[3] => SomeTown
[4] => SD
[5] => 91234
)
[2] => Array
(
[0] => John "Da Man"
[1] => Repici
[2] => 120 Jefferson St.
[3] => Riverside
[4] => NJ
[5] => 08075
)
[3] => Array
(
[0] => Repici
[1] => John "Da Man"
[2] => 120 Jefferson St.
[3] => Riverside
[4] => NJ
[5] => 08075
)
)
File-1.3.0/tests/CSV/010.csv 100666 0 0 36 10630107705 10176 ,Blankman,,SomeTown, SD, 00298 File-1.3.0/tests/CSV/010.phpt 100666 0 0 1517 10630107705 10423 --TEST--
File_CSV Test Case 010: Empty first field and no quoting but some fields have extra spacing
--FILE--
--EXPECT--
Data:
Array
(
[0] => Array
(
[0] =>
[1] => Blankman
[2] =>
[3] => SomeTown
[4] => SD
[5] => 00298
)
)
File-1.3.0/tests/CSV/011.csv 100666 0 0 26 10630107705 10176 "I'm multiline
Field" File-1.3.0/tests/CSV/011.phpt 100666 0 0 1563 10630107705 10425 --TEST--
File_CSV Test Case 011: Only one field that's multiline with out a EOL and a potential quote inside quotes
--FILE--
--EXPECT--
Format:
Array
(
[fields] => 1
[sep] => ,
[quote] => "
)
Data:
Array
(
[0] => Array
(
[0] => I'm multiline
Field
)
)
File-1.3.0/tests/CSV/012.csv 100666 0 0 101 10630107705 10211 header,body,foot
I'm a little header,this is my teapot,can't be! File-1.3.0/tests/CSV/012.phpt 100666 0 0 1252 10630107705 10421 --TEST--
File_CSV Test Case 012: Read headers
--FILE--
--EXPECT--
Data:
Array
(
[0] => Array
(
[header] => I'm a little header
[body] => this is my teapot
[foot] => can't be!
)
)
File-1.3.0/tests/CSV/013.csv 100666 0 0 65 10630107705 10203 ="004343",=" Helgi",=" Project
multiline
stuff"
File-1.3.0/tests/CSV/013.phpt 100666 0 0 1451 10630107705 10423 --TEST--
File_CSV Test Case 013: Reading ="" excel only fields
--FILE--
--EXPECT--
Format:
Array
(
[fields] => 3
[sep] => ,
[quote] => "
)
Data:
Array
(
[0] => Array
(
[0] => 004343
[1] => Helgi
[2] => Project
multiline
stuff
)
)
File-1.3.0/tests/CSV/014.csv 100666 0 0 20 10630107705 10173 Foo,Bar,Foobar
File-1.3.0/tests/CSV/014.phpt 100666 0 0 1713 10630107705 10425 --TEST--
File_CSV Test Case 014: Use 2 modes, first read the file then write to it.
--FILE--
--EXPECT--
Data:
Array
(
[0] => Array
(
[0] => Foo
[1] => Bar
[2] => Foobar
)
)
Write:
bool(true)
File-1.3.0/tests/CSV/015.csv 100666 0 0 404 10630107705 10222 John,Doe,120 jefferson st.,Riverside, NJ, 08075
Jack,McGinnis,220 hobo Av.,Phila, PA,09119
"John ""Da Man""",Repici,120 Jefferson St.,Riverside, NJ,08075
Stephen,Tyler,"7452 Terrace ""At the Plaza"" road",SomeTown,SD, 91234
,Blankman,,SomeTown, SD, 00298
File-1.3.0/tests/CSV/015.phpt 100666 0 0 3177 10630107705 10434 --TEST--
File_CSV Test Case 015: Various different lines.
--FILE--
--EXPECT--
Data:
Array
(
[0] => Array
(
[0] => John
[1] => Doe
[2] => 120 jefferson st.
[3] => Riverside
[4] => NJ
[5] => 08075
)
[1] => Array
(
[0] => Jack
[1] => McGinnis
[2] => 220 hobo Av.
[3] => Phila
[4] => PA
[5] => 09119
)
[2] => Array
(
[0] => John "Da Man"
[1] => Repici
[2] => 120 Jefferson St.
[3] => Riverside
[4] => NJ
[5] => 08075
)
[3] => Array
(
[0] => Stephen
[1] => Tyler
[2] => 7452 Terrace "At the Plaza" road
[3] => SomeTown
[4] => SD
[5] => 91234
)
[4] => Array
(
[0] =>
[1] => Blankman
[2] =>
[3] => SomeTown
[4] => SD
[5] => 00298
)
)
File-1.3.0/tests/CSV/016.csv 100666 0 0 110 10630107705 10215 "Joan ""the bone"", Anne",Jet,"9th, at Terrace plc",Desert City,CO,00123 File-1.3.0/tests/CSV/016.phpt 100666 0 0 1646 10630107705 10434 --TEST--
File_CSV Test Case 016: Various different lines.
--FILE--
--EXPECT--
Format:
Array
(
[fields] => 6
[sep] => ,
[quote] => "
)
Data:
Array
(
[0] => Array
(
[0] => Joan "the bone", Anne
[1] => Jet
[2] => 9th, at Terrace plc
[3] => Desert City
[4] => CO
[5] => 00123
)
)
File-1.3.0/tests/CSV/017.csv 100666 0 0 110 10630107705 10216 "Joan ""the bone"", Anne",Jet,"9th, at Terrace plc",Desert City,CO,00123 File-1.3.0/tests/CSV/017.phpt 100666 0 0 1644 10630107705 10433 --TEST--
File_CSV Test Case 017: Various different lines.
--FILE--
6,
'sep' => ',',
'quote' => '"'
);
$data = array();
while ($res = File_CSV::read($file, $conf)) {
$data[] = $res;
}
print "Data:\n";
print_r($data);
print "\n";
?>
--EXPECT--
Data:
Array
(
[0] => Array
(
[0] => Joan "the bone", Anne
[1] => Jet
[2] => 9th, at Terrace plc
[3] => Desert City
[4] => CO
[5] => 00123
)
)
File-1.3.0/tests/CSV/018.csv 100666 0 0 107 10630107705 10225 "Joan ""the bone"" Anne",Jet,"9th, at Terrace plc",Desert City,CO,00123 File-1.3.0/tests/CSV/018.phpt 100666 0 0 1727 10630107705 10436 --TEST--
File_CSV Test Case 018: Various different lines.
--FILE--
6,
'sep' => ',',
'quote' => '"'
);
$data = array();
while ($res = File_CSV::read($file, $conf)) {
$data[] = $res;
}
print "Data:\n";
print_r($data);
print "\n";
?>
--EXPECT--
Data:
Array
(
[0] => Array
(
[0] => Joan "the bone" Anne
[1] => Jet
[2] => 9th, at Terrace plc
[3] => Desert City
[4] => CO
[5] => 00123
)
)
File-1.3.0/tests/CSV/019.csv 100666 0 0 107 10630107705 10226 "Joan ""the bone"" Anne",Jet,"9th, at Terrace plc",Desert City,CO,00123 File-1.3.0/tests/CSV/019.phpt 100666 0 0 1763 10630107705 10437 --TEST--
File_CSV Test Case 019: Various different lines.
--FILE--
--EXPECT--
Format:
Array
(
[fields] => 6
[sep] => ,
[quote] => "
)
Data:
Array
(
[0] => Array
(
[0] => Joan "the bone" Anne
[1] => Jet
[2] => 9th, at Terrace plc
[3] => Desert City
[4] => CO
[5] => 00123
)
)
File-1.3.0/tests/CSV/020.csv 100666 0 0 44 10630107705 10176 "004343",Helgi,Project,"007","008"
File-1.3.0/tests/CSV/020.phpt 100666 0 0 2303 10630107705 10416 --TEST--
File_CSV Test Case 020: First field quoted = last field being excluded
--FILE--
--EXPECT--
Format:
Array
(
[fields] => 5
[sep] => ,
[quote] => "
)
Data:
Array
(
[0] => Array
(
[0] => 004343
[1] => Helgi
[2] => Project
[3] => 007
[4] => 008
)
)
File-1.3.0/tests/CSV/021.csv 100666 0 0 1474 10630107705 10247 Axxxfs 02xxx0 00xxxxx84 11 10/04/06 2163,53
10/04/06 VIREMENT RECU TIERS VIR ACOMPTE PAIEMENT FC 904 11/04/06 700,00
10/04/06 CHEQUE N° 646 08/04/06 -33,61
07/04/06 REMISE CHEQUES BORDEREAU 05242 11/04/06 1237,93
05/04/06 PRELEVEMENT PL PROVINCE 05.04.0 04/04/06 -78,00
04/04/06 COMMISSION TTC FACTURE NUMERO 038 10/04/06 -29,90
27/03/06 CHEQUE N° 645 25/03/06 -34,01
24/03/06 CHEQUE N° 614 22/03/06 -32,75
22/03/06 CHEQUE N° 643 20/03/06 -273,00
21/03/06 FACTURE CARTE DU 190306 TOTAL CART 21/03/06 -32,00
20/03/06 PRELEVEMENT URSSAF 19/03/06 -75,00
16/03/06 VIRT CPTE A CPTE EMIS VT 17H18 02 15/03/06 -1000,00
15/03/06 CHEQUE N° 641 13/03/06 -100,07
14/03/06 PRELEVEMENT TELECOM 06 REF 13/03/06 -29,99
14/03/06 CHEQUE N° 642 12/03/06 -30,00
File-1.3.0/tests/CSV/021.phpt 100666 0 0 6504 10630107705 10426 --TEST--
File_CSV Test Case 021: Tabs as separators and no quotes
--FILE--
--EXPECT--
Format:
fields: 4
sep:
buff: (_t_)
quote:
buff: (_NULL_)
Data:
Array
(
[0] => Array
(
[0] => Axxxfs
[1] => 02xxx0 00xxxxx84 11
[2] => 10/04/06
[3] => 2163,53
)
[1] => Array
(
[0] => 10/04/06
[1] => VIREMENT RECU TIERS VIR ACOMPTE PAIEMENT FC 904
[2] => 11/04/06
[3] => 700,00
)
[2] => Array
(
[0] => 10/04/06
[1] => CHEQUE N° 646
[2] => 08/04/06
[3] => -33,61
)
[3] => Array
(
[0] => 07/04/06
[1] => REMISE CHEQUES BORDEREAU 05242
[2] => 11/04/06
[3] => 1237,93
)
[4] => Array
(
[0] => 05/04/06
[1] => PRELEVEMENT PL PROVINCE 05.04.0
[2] => 04/04/06
[3] => -78,00
)
[5] => Array
(
[0] => 04/04/06
[1] => COMMISSION TTC FACTURE NUMERO 038
[2] => 10/04/06
[3] => -29,90
)
[6] => Array
(
[0] => 27/03/06
[1] => CHEQUE N° 645
[2] => 25/03/06
[3] => -34,01
)
[7] => Array
(
[0] => 24/03/06
[1] => CHEQUE N° 614
[2] => 22/03/06
[3] => -32,75
)
[8] => Array
(
[0] => 22/03/06
[1] => CHEQUE N° 643
[2] => 20/03/06
[3] => -273,00
)
[9] => Array
(
[0] => 21/03/06
[1] => FACTURE CARTE DU 190306 TOTAL CART
[2] => 21/03/06
[3] => -32,00
)
[10] => Array
(
[0] => 20/03/06
[1] => PRELEVEMENT URSSAF
[2] => 19/03/06
[3] => -75,00
)
[11] => Array
(
[0] => 16/03/06
[1] => VIRT CPTE A CPTE EMIS VT 17H18 02
[2] => 15/03/06
[3] => -1000,00
)
[12] => Array
(
[0] => 15/03/06
[1] => CHEQUE N° 641
[2] => 13/03/06
[3] => -100,07
)
[13] => Array
(
[0] => 14/03/06
[1] => PRELEVEMENT TELECOM 06 REF
[2] => 13/03/06
[3] => -29,99
)
[14] => Array
(
[0] => 14/03/06
[1] => CHEQUE N° 642
[2] => 12/03/06
[3] => -30,00
)
)
File-1.3.0/tests/CSV/022.csv 100666 0 0 27 10630107705 10201 "I am multiline
Field" File-1.3.0/tests/CSV/022.phpt 100666 0 0 1571 10630107705 10426 --TEST--
File_CSV Test Case 022: Only one field that's multiline with out a EOL
--FILE--
--EXPECT--
Format:
Array
(
[fields] => 1
[sep] => ,
[quote] => "
)
Data:
Array
(
[0] => Array
(
[0] => I am multiline
Field
)
)
File-1.3.0/tests/CSV/023.csv 100666 0 0 26 10630107705 10201 'I"m multiline
Field' File-1.3.0/tests/CSV/023.phpt 100666 0 0 1614 10630107705 10425 --TEST--
File_CSV Test Case 023: Only one field that's multiline with out a EOL and a potential quote inside quotes
--FILE--
--EXPECT--
Format:
Array
(
[fields] => 1
[sep] => ,
[quote] => '
)
Data:
Array
(
[0] => Array
(
[0] => I"m multiline
Field
)
)
File-1.3.0/tests/CSV/024.csv 100666 0 0 130 10630107705 10216 "I'm multiline
Field
that spans
over couple
of
rather odd
sections
of the
world" File-1.3.0/tests/CSV/024.phpt 100666 0 0 1575 10630107705 10434 --TEST--
File_CSV Test Case 024: Only one field that's multiline with out a EOL and spans couple of lines
--FILE--
--EXPECT--
Format:
Array
(
[fields] => 1
[sep] => ,
[quote] => "
)
Data:
Array
(
[0] => Array
(
[0] => I'm multiline
Field
that spans
over couple
of
rather odd
sections
of the
world
)
)
File-1.3.0/tests/CSV/025.csv 100666 0 0 275 10630107705 10231 "I'm multiline
Field
that spans
over couple
of
rather odd
sections
of the
world", Helgi, foobar, "And I'm another
multiline beast
that spans
couple of
lines
in
this
document" File-1.3.0/tests/CSV/025.phpt 100666 0 0 2040 10630107705 10421 --TEST--
File_CSV Test Case 025: One row, four fields that are multiline with out a EOL and span couple of lines
--FILE--
--EXPECT--
Format:
Array
(
[fields] => 4
[sep] => ,
[quote] => "
)
Data:
Array
(
[0] => Array
(
[0] => I'm multiline
Field
that spans
over couple
of
rather odd
sections
of the
world
[1] => Helgi
[2] => foobar
[3] => And I'm another
multiline beast
that spans
couple of
lines
in
this
document
)
)
File-1.3.0/tests/CSV/026.csv 100666 0 0 133 10630107705 10223 Conference room 1, "John,
Please bring the M. Mathers file for review
-J.L.
",10/18/2002 File-1.3.0/tests/CSV/026.phpt 100666 0 0 1656 10630107705 10436 --TEST--
File_CSV Test Case 026: One row, four fields that are multiline with out a EOL and span couple of lines
--FILE--
--EXPECT--
Format:
Array
(
[fields] => 3
[sep] => ,
[quote] => "
)
Data:
Array
(
[0] => Array
(
[0] => Conference room 1
[1] => John,
Please bring the M. Mathers file for review
-J.L.
[2] => 10/18/2002
)
)
File-1.3.0/tests/CSV/027.csv 100666 0 0 45 10630107705 10206 John , Doe , Bob
John,Doe,Bob File-1.3.0/tests/CSV/027.phpt 100666 0 0 1757 10630107705 10441 --TEST--
File_CSV Test Case 027: Records with spaces around them but no quotes
--FILE--
--EXPECT--
Format:
fields: 3
sep:
buff: (,)
quote:
buff: (_NULL_)
Data:
Array
(
[0] => Array
(
[0] => John
[1] => Doe
[2] => Bob
)
[1] => Array
(
[0] => John
[1] => Doe
[2] => Bob
)
)
File-1.3.0/tests/CSV/028.csv 100666 0 0 107 10630107705 10226 "Joan ""the bone"" Anne",Jet,"9th, at Terrace plc",Desert City,CO,00123 File-1.3.0/tests/CSV/028.phpt 100666 0 0 1747 10630107705 10441 --TEST--
File_CSV Test Case 028: Variant of 016 with out the sep after the double quotes
--FILE--
--EXPECT--
Format:
Array
(
[fields] => 6
[sep] => ,
[quote] => "
)
Data:
Array
(
[0] => Array
(
[0] => Joan "the bone" Anne
[1] => Jet
[2] => 9th, at Terrace plc
[3] => Desert City
[4] => CO
[5] => 00123
)
)
File-1.3.0/tests/CSV/029.csv 100666 0 0 34 10630107705 10206 "Joan """"the bone"""" Anne" File-1.3.0/tests/CSV/029.phpt 100666 0 0 1115 10630107705 10427 --TEST--
File_CSV Test Case 029: Output double quotes.
--FILE--
1,
'quote' => '"'
);
$data = array();
while ($res = File_CSV::read($file, $conf)) {
$data[] = $res;
}
print "Data:\n";
print_r($data);
print "\n";
?>
--EXPECT--
Data:
Array
(
[0] => Array
(
[0] => Joan ""the bone"" Anne
)
)
File-1.3.0/tests/CSV/030.csv 100666 0 0 275 10630107705 10225 "I'm multiline
Field
that spans
over couple
of
rather odd
sections
of the
world", Helgi, foobar, "And I'm another
multiline beast
that spans
couple of
lines
in
this
document" File-1.3.0/tests/CSV/030.phpt 100666 0 0 1735 10630107705 10427 --TEST--
File_CSV Test Case 030: One row, four fields that are multiline with out a EOL and span couple of lines
--FILE--
4,
'sep' => ',',
'quote' => '"'
);
$data = array();
while ($res = File_CSV::read($file, $conf)) {
$data[] = $res;
}
print "Data:\n";
print_r($data);
print "\n";
?>
--EXPECT--
Data:
Array
(
[0] => Array
(
[0] => I'm multiline
Field
that spans
over couple
of
rather odd
sections
of the
world
[1] => Helgi
[2] => foobar
[3] => And I'm another
multiline beast
that spans
couple of
lines
in
this
document
)
)
File-1.3.0/tests/CSV/031.csv 100666 0 0 133 10630107705 10217 Conference room 1, "John,
Please bring the M. Mathers file for review
-J.L.
",10/18/2002 File-1.3.0/tests/CSV/031.phpt 100666 0 0 1553 10630107705 10426 --TEST--
File_CSV Test Case 031: One row, four fields that are multiline with out a EOL and span couple of lines
--FILE--
3,
'sep' => ',',
'quote' => '"'
);
$data = array();
while ($res = File_CSV::read($file, $conf)) {
$data[] = $res;
}
print "Data:\n";
print_r($data);
print "\n";
?>
--EXPECT--
Data:
Array
(
[0] => Array
(
[0] => Conference room 1
[1] => John,
Please bring the M. Mathers file for review
-J.L.
[2] => 10/18/2002
)
)
File-1.3.0/tests/CSV/032.csv 100666 0 0 167 10630107705 10227 "I'm multiline
Field
that spans
over couple
of
rather odd
sections
of the
world", Helgi, foobar,And I'm another File-1.3.0/tests/CSV/032.phpt 100666 0 0 1735 10630107705 10431 --TEST--
File_CSV Test Case 032: One row, four fields that are multiline with out a EOL and span couple of lines
--FILE--
--EXPECT--
Format:
Array
(
[fields] => 4
[sep] => ,
[quote] => "
)
Data:
Array
(
[0] => Array
(
[0] => I'm multiline
Field
that spans
over couple
of
rather odd
sections
of the
world
[1] => Helgi
[2] => foobar
[3] => And I'm another
)
)
File-1.3.0/tests/CSV/bug4792_part1.csv 100666 0 0 113 10630107705 12124 Name Last Email
Manuel Razzar razzar@gmail.com
Demo User email@email.com File-1.3.0/tests/CSV/bug4792_part1.phpt 100666 0 0 1540 10630107705 12331 --TEST--
File_CSV Test Case bug4792_part1: All lines but last end with a whitespace Part 1
--FILE--
--EXPECT--
Data:
Email- Ends OK
razzar@gmail.com- Ends OK
email@email.com- Ends OK
File-1.3.0/tests/CSV/bug4792_part2.csv 100666 0 0 115 10630107705 12127 Name Last Email
Manuel Razzar razzar@gmail.com
Demo User email@email.com File-1.3.0/tests/CSV/bug4792_part2.phpt 100666 0 0 1534 10630107705 12335 --TEST--
File_CSV Test Case bug4792_part2: All lines but last end with a whitespace Part 2
--FILE--
--EXPECT--
Data:
Email- Ends OK
razzar@gmail.com- Ends OK
email@email.com- Ends OK
File-1.3.0/tests/CSV/bug5257.csv 100666 0 0 42 10630107705 10773 "","foo"
"foo",""
,foo
foo,
File-1.3.0/tests/CSV/bug5257.phpt 100666 0 0 1375 10630107705 11225 --TEST--
File_CSV Test Case bug5257: Delimiter problem if first field is empty
--FILE--
--EXPECT--
Format:
Array
(
[0] =>
[1] => foo
)
Array
(
[0] => foo
[1] =>
)
Array
(
[0] =>
[1] => foo
)
Array
(
[0] => foo
[1] =>
)
File-1.3.0/tests/CSV/bug5553.phpt 100666 0 0 1733 10630107705 11222 --TEST--
File_CSV Test Case bug5553: Write a file with delimiter inside quotes and don't pass the quote option
--FILE--
--EXPECT--
Write:
bool(true)
Data:
Array
(
[0] => Array
(
[0] => Hi
[1] => Hello
[2] => Hi,world
)
)
File-1.3.0/tests/CSV/tests.txt 100666 0 0 1122 10630107705 11121 File_CSV Test Case
==================
Case Covered Notes
--------------------------------------------------------------------------------
001 Fields count less than expected [x]
002 Fields count more than expected [x] High risk possibility
discovered fields count
wrong
003 Windows EOL [x]
004 Unix EOL [x]
005 Mac EOL [x]
File-1.3.0/tests/FileTest.php 100666 0 0 21452 10630107705 11043 PHPUnit_TestCase($name);
}
function getTestString()
{
static $str;
isset($str) or $str = str_repeat(str_repeat("0123456789", 1000)."\n", 100);
return $str;
}
function getTestLine()
{
static $str;
isset($str) or $str = str_repeat("0123456789", 1000);
return $str;
}
function setUp()
{
$this->tearDown();
if (PEAR::isError($e = File::write('test.txt', $this->getTestString(), FILE_MODE_WRITE))) {
die("Cannot start test: ". str_replace($this->getTestString(),'...', $e->getMessage()));
}
}
function tearDown()
{
File::closeAll();
file_exists('test.txt') and unlink('test.txt');
}
function testlocking()
{
$this->assertFalse(PEAR::isError(File::write('test.txt', 'abc', FILE_MODE_APPEND, true)));
$this->assertTrue(PEAR::isError(File::write('test.txt', 'def', FILE_MODE_WRITE, true)));
$this->assertFalse(PEAR::isError(File::unlock('test.txt', FILE_MODE_APPEND)));
$this->assertFalse(PEAR::isError(File::unlock('test.txt', FILE_MODE_WRITE)));
}
function testclose()
{
$this->assertFalse(PEAR::isError(File::close('test.txt', FILE_MODE_WRITE)));
$this->assertFalse(PEAR::isError(File::close('test.txt', FILE_MODE_APPEND)));
$this->assertFalse(PEAR::isError(File::close('test.txt', FILE_MODE_READ)));
}
function testreadAll()
{
$this->assertEquals($this->getTestString(), File::readAll('test.txt'));
$this->assertEquals($this->getTestString(), File::readAll('test.txt'));
$this->assertEquals($this->getTestString(), File::readAll('test.txt'));
}
function testread()
{
$this->assertEquals($this->getTestLine(), File::read('test.txt', 10000));
$this->assertEquals("\n", File::read('test.txt', 1));
$this->assertEquals('0123456789', File::read('test.txt', 10));
}
function testwrite()
{
$this->assertFalse(PEAR::isError($bytes = File::write('test.txt', '0123456789')));
$this->assertEquals(10, $bytes);
}
function testreadChar()
{
$this->assertFalse(PEAR::isError(File::rewind('test.txt', FILE_MODE_READ)));
$this->assertEquals('0', File::readChar('test.txt'));
$this->assertEquals('1', File::readChar('test.txt'));
$this->assertEquals('2', File::readChar('test.txt'));
$this->assertEquals('3', File::readChar('test.txt'));
$this->assertEquals('4', File::readChar('test.txt'));
$this->assertEquals('5', File::readChar('test.txt'));
$this->assertEquals('6', File::readChar('test.txt'));
$this->assertEquals('7', File::readChar('test.txt'));
$this->assertEquals('8', File::readChar('test.txt'));
$this->assertEquals('9', File::readChar('test.txt'));
$this->assertEquals('0', File::readChar('test.txt'));
}
function testwriteChar()
{
$this->assertEquals(1, File::writeChar('test.txt', 'a'));
$this->assertEquals(1, File::writeChar('test.txt', 'b'));
$this->assertEquals(1, File::writeChar('test.txt', 'c'));
$this->assertEquals(1, File::writeChar('test.txt', 'd'));
$this->assertEquals(1, File::writeChar('test.txt', 'e'));
$this->assertEquals(1, File::writeChar('test.txt', 'f'));
$this->assertEquals(1, File::writeChar('test.txt', 'g'));
$this->assertEquals(1, File::writeChar('test.txt', 'h'));
$this->assertEquals(1, File::writeChar('test.txt', 'i'));
$this->assertEquals(1, File::writeChar('test.txt', 'j'));
}
function testreadLine()
{
$this->assertFalse(PEAR::isError(File::rewind('test.txt', FILE_MODE_READ)));
$this->assertEquals($this->getTestLine(), File::readLine('test.txt'));
$this->assertEquals($this->getTestLine(), File::readLine('test.txt'));
$this->assertEquals($this->getTestLine(), File::readLine('test.txt'));
$this->assertEquals($this->getTestLine(), File::readLine('test.txt'));
$this->assertEquals($this->getTestLine(), File::readLine('test.txt'));
$this->assertEquals($this->getTestLine(), File::readLine('test.txt'));
$this->assertEquals($this->getTestLine(), File::readLine('test.txt'));
$this->assertEquals($this->getTestLine(), File::readLine('test.txt'));
$this->assertEquals($this->getTestLine(), File::readLine('test.txt'));
$this->assertEquals($this->getTestLine(), File::readLine('test.txt'));
}
function testwriteLine()
{
$line = $this->getTestLine();
$length = strlen($line) + 1;
$this->assertEquals($length, File::writeLine('test.txt', $line));
$this->assertEquals($length, File::writeLine('test.txt', $line));
$this->assertEquals($length, File::writeLine('test.txt', $line));
$this->assertEquals($length, File::writeLine('test.txt', $line));
$this->assertEquals($length, File::writeLine('test.txt', $line));
$this->assertEquals($length, File::writeLine('test.txt', $line));
$this->assertEquals($length, File::writeLine('test.txt', $line));
$this->assertEquals($length, File::writeLine('test.txt', $line));
$this->assertEquals($length, File::writeLine('test.txt', $line));
$this->assertEquals($length, File::writeLine('test.txt', $line));
}
function testrewind()
{
$this->assertFalse(PEAR::isError(File::rewind('test.txt', FILE_MODE_WRITE)));
$this->assertFalse(PEAR::isError(File::rewind('test.txt', FILE_MODE_READ)));
}
function testbuildPath()
{
$path = array(
'some',
DIRECTORY_SEPARATOR,
DIRECTORY_SEPARATOR,
DIRECTORY_SEPARATOR,
DIRECTORY_SEPARATOR,
'weird'.DIRECTORY_SEPARATOR,
DIRECTORY_SEPARATOR,
DIRECTORY_SEPARATOR,
DIRECTORY_SEPARATOR,
DIRECTORY_SEPARATOR.'path'.DIRECTORY_SEPARATOR.DIRECTORY_SEPARATOR,
);
$built = implode(DIRECTORY_SEPARATOR, array('some','weird','path','',''));
$this->assertEquals($built, File::buildPath($path));
}
function testskipRoot()
{
if (OS_WINDOWS) {
$this->assertEquals('WINDOWS', File::skipRoot('C:\\WINDOWS'));
$this->assertEquals('WINDOWS', File::skipRoot('C:\\\\WINDOWS'));
$this->assertEquals('WINDOWS', File::skipRoot('C:/WINDOWS'));
} else {
$this->assertEquals('usr/share/pear', File::skipRoot('/usr/share/pear'));
}
}
function testgetTempDir()
{
$dir = File::getTempDir();
$this->assertTrue(is_dir($dir), "is_dir($dir)");
}
function testgetTempFile()
{
$tmp = File::getTempFile();
$this->assertTrue(file_exists($tmp));
}
function testisAbsolute()
{
$this->assertFalse(File::isAbsolute('abra/../cadabra'));
$this->assertFalse(File::isAbsolute('data/dir'));
if (OS_WINDOWS) {
$this->assertTrue(File::isAbsolute('C:\\\\data'));
$this->assertTrue(File::isAbsolute('d:/data'));
$this->assertFalse(File::isAbsolute('\\'));
} else {
$this->assertTrue(File::isAbsolute('/'));
$this->assertFalse(File::isAbsolute('\\'));
$this->assertTrue(File::isAbsolute('~mike/bin'));
}
}
function testrelativePath()
{
$this->assertEquals('tests/File', File::relativePath('/usr/share/pear/tests/File', '/usr/share/pear', '/'));
$this->assertEquals('../etc', File::relativePath('/etc', '/usr', '/'));
$this->assertEquals('D:\\Data', File::relativePath('D:\\Data', 'C:\\Data', '\\'));
if (OS_WINDOWS) {
$this->assertEquals('data\\dir', File::relativePath('/var/data/dir', '/var'));
} else {
$this->assertEquals('data/dir', File::relativePath('/var/data/dir', '/var'));
}
$this->assertEquals('../', File::relativePath('data', 'data/dir', '/'));
}
function testrealpath()
{
$drive = OS_WINDOWS ? substr(getcwd(),0, 2) :'';
$this->assertEquals($drive . '/a/weird/path/is', File::realpath('/a\\weird//path\is/that/./../', '/'));
$this->assertEquals($drive . '/a/weird/path/is/that', File::realpath('/a\\weird//path\is/that/./../that/.', '/'));
}
}
$result = &PHPUnit::run(new PHPUnit_TestSuite('FileTest'));
echo $result->toString();
?> File-1.3.0/tests/parser.php 100666 0 0 1236 10630107705 10576 4,
'sep' => "\t",
'quote' => '"',
'header' => false
);
//*/
ob_implicit_flush(true);
$argv = $_SERVER['argv'];
$file = $argv[1];
$write = (isset($argv[2])) ? $argv[2] : false;
PEAR::setErrorHandling(PEAR_ERROR_PRINT, "warning: %s\n");
$conf = File_CSV::discoverFormat($file);
while ($fields = File_CSV::read($file, $conf)) {
if ($write) {
File_CSV::write($write, $fields, $conf);
}
print_r($fields);
}
var_dump($conf);
echo "\n"
?> File-1.3.0/tests/test.csv 100666 0 0 1053 10630107705 10262 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"
less1 "less2 ." less3
isvalid1 isvalid2 isvalid3 isvalid4
more21 "more22 ." more23 more24 more25 more26
isvalid21 isvalid22 isvalid23 isvalid24
lessnoq31 lessnoq32 lessnoq33
isvalid31 isvalid32 isvalid33 isvalid34
morenoq41 morenoq42 morenoq43 morenoq44 morenoq45
isvalid41 isvalid42 isvalid43 isvalid44
File-1.3.0/File.php 100666 0 0 40204 10630107705 7035
* @author Tal Peer
* @author Michael Wallner
* @copyright 2002-2005 The Authors
* @license http://www.php.net/license/3_0.txt PHP License 3.0
* @version CVS: $Id: File.php,v 1.38 2007/03/24 16:38:56 dufuz Exp $
* @link http://pear.php.net/package/File
*/
/**
* Requires PEAR
*/
require_once 'PEAR.php';
/**
* The default number of bytes for reading
*/
if (!defined('FILE_DEFAULT_READSIZE')) {
define('FILE_DEFAULT_READSIZE', 1024, true);
}
/**
* The maximum number of bytes for reading lines
*/
if (!defined('FILE_MAX_LINE_READSIZE')) {
define('FILE_MAX_LINE_READSIZE', 40960, true);
}
/**
* Whether file locks should block
*/
if (!defined('FILE_LOCKS_BLOCK')) {
define('FILE_LOCKS_BLOCK', true, true);
}
/**
* Mode to use for reading from files
*/
define('FILE_MODE_READ', 'rb', true);
/**
* Mode to use for truncating files, then writing
*/
define('FILE_MODE_WRITE', 'wb', true);
/**
* Mode to use for appending to files
*/
define('FILE_MODE_APPEND', 'ab', true);
/**
* Use this when a shared (read) lock is required
*/
define('FILE_LOCK_SHARED', LOCK_SH | (FILE_LOCKS_BLOCK ? 0 : LOCK_NB), true);
/**
* Use this when an exclusive (write) lock is required
*/
define('FILE_LOCK_EXCLUSIVE', LOCK_EX | (FILE_LOCKS_BLOCK ? 0 : LOCK_NB), true);
/**
* Class for handling files
*
* A class with common functions for writing,
* reading and handling files and directories
*
* @author Richard Heyes
* @author Tal Peer
* @author Michael Wallner
* @access public
* @package File
*
* @static
*/
class File extends PEAR
{
/**
* Destructor
*
* Unlocks any locked file pointers and closes all filepointers
*
* @access private
*/
function _File()
{
File::closeAll();
}
/**
* Handles file pointers. If a file pointer needs to be opened,
* it will be. If it already exists (based on filename and mode)
* then the existing one will be returned.
*
* @access private
* @param string $filename Filename to be used
* @param string $mode Mode to open the file in
* @param mixed $lock Type of lock to use
* @return mixed PEAR_Error on error or file pointer resource on success
*/
function _getFilePointer($filename, $mode, $lock = false)
{
$filePointers = &PEAR::getStaticProperty('File', 'filePointers');
// Win32 is case-insensitive
if (OS_WINDOWS) {
$filename = strtolower($filename);
}
// check if file pointer already exists
if (!isset($filePointers[$filename][$mode]) ||
!is_resource($filePointers[$filename][$mode])) {
// check if we can open the file in the desired mode
switch ($mode)
{
case FILE_MODE_READ:
if (!preg_match('/^.+(? $modes) {
foreach (array_keys($modes) as $mode) {
if (is_resource($filePointers[$fname][$mode])) {
@fclose($filePointers[$fname][$mode]);
}
unset($filePointers[$fname][$mode]);
}
}
}
}
/**
* This closes an open file pointer
*
* @access public
* @param string $filename The filename that was opened
* @param string $mode Mode the file was opened in
* @return mixed PEAR Error on error, true otherwise
*/
function close($filename, $mode)
{
$filePointers = &PEAR::getStaticProperty('File', 'filePointers');
if (OS_WINDOWS) {
$filename = strToLower($filename);
}
if (!isset($filePointers[$filename][$mode])) {
return true;
}
$fp = $filePointers[$filename][$mode];
unset($filePointers[$filename][$mode]);
if (is_resource($fp)) {
// unlock file
@flock($fp, LOCK_UN);
// close file
if (!@fclose($fp)) {
return PEAR::raiseError("Cannot close file: $filename");
}
}
return true;
}
/**
* This unlocks a locked file pointer.
*
* @access public
* @param string $filename The filename that was opened
* @param string $mode Mode the file was opened in
* @return mixed PEAR Error on error, true otherwise
*/
function unlock($filename, $mode)
{
$fp = File::_getFilePointer($filename, $mode);
if (PEAR::isError($fp)) {
return $fp;
}
if (!@flock($fp, LOCK_UN)) {
return PEAR::raiseError("Cacnnot unlock file: $filename");
}
return true;
}
/**
* @deprecated
*/
function stripTrailingSeparators($path, $separator = DIRECTORY_SEPARATOR)
{
if ($path === $separator) {
return $path;
}
return rtrim($path, $separator);
}
/**
* @deprecated
*/
function stripLeadingSeparators($path, $separator = DIRECTORY_SEPARATOR)
{
if ($path === $separator) {
return $path;
}
return ltrim($path, $separator);
}
/**
* @deprecated Use File_Util::buildPath() instead.
*/
function buildPath($parts, $separator = DIRECTORY_SEPARATOR)
{
require_once 'File/Util.php';
return File_Util::buildPath($parts, $separator);
}
/**
* @deprecated Use File_Util::skipRoot() instead.
*/
function skipRoot($path)
{
require_once 'File/Util.php';
return File_Util::skipRoot($path);
}
/**
* @deprecated Use File_Util::tmpDir() instead.
*/
function getTempDir()
{
require_once 'File/Util.php';
return File_Util::tmpDir();
}
/**
* @deprecated Use File_Util::tmpFile() instead.
*/
function getTempFile($dirname = null)
{
require_once 'File/Util.php';
return File_Util::tmpFile($dirname);
}
/**
* @deprecated Use File_Util::isAbsolute() instead.
*/
function isAbsolute($path)
{
require_once 'File/Util.php';
return File_Util::isAbsolute($path);
}
/**
* @deprecated Use File_Util::relativePath() instead.
*/
function relativePath($path, $root, $separator = DIRECTORY_SEPARATOR)
{
require_once 'File/Util.php';
return File_Util::relativePath($path, $root, $separator);
}
/**
* @deprecated Use File_Util::realpath() instead.
*/
function realpath($path, $separator = DIRECTORY_SEPARATOR)
{
require_once 'File/Util.php';
return File_Util::realpath($path, $separator);
}
}
PEAR::registerShutdownFunc(array('File', '_File'));
?>