summaryrefslogtreecommitdiff
path: root/it_pipe.class
diff options
context:
space:
mode:
authorUrban Müller2014-01-09 14:26:18 +0100
committerUrban Müller2014-01-09 14:26:18 +0100
commit4ef1423457af1284fde904803216514a34721f91 (patch)
treeda068761d4979f5927837ab26922d1ca883c976e /it_pipe.class
parent6022d924c7ac70a13d1782f2f1199f9ef45c947c (diff)
downloaditools-4ef1423457af1284fde904803216514a34721f91.tar.gz
itools-4ef1423457af1284fde904803216514a34721f91.tar.bz2
itools-4ef1423457af1284fde904803216514a34721f91.zip
allow column name override, add tests
Diffstat (limited to 'it_pipe.class')
-rw-r--r--it_pipe.class14
1 files changed, 8 insertions, 6 deletions
diff --git a/it_pipe.class b/it_pipe.class
index 2c5a184..737908a 100644
--- a/it_pipe.class
+++ b/it_pipe.class
@@ -156,20 +156,22 @@ function cols($collist, $separator = "\t")
}
/**
- * Return contents of pipe as associative records
+ * Return contents of pipe as associative records. Column titles read from first line, separators can be \t or ; or ,
+ * @param $forceschema ignore schema in file, replace it with this comma separated column list
*/
-function csv()
+function csv($forceschema = null)
{
$counts = count_chars($this->lines[0]);
$splitchar = $counts[ord("\t")] ? "\t" : ($counts[ord(";")] > $counts[ord(",")] ? ";" : ",");
- $schema = str_getcsv(trim(array_shift($this->lines), "#\n "), $splitchar, '"'); # could do a function_exists('str_getcsv') here...
- foreach (preg_grep('/^$/', $schema) as $idx => $dummy)
- $schema[$idx] = "field$idx";
+ $csvhead = array_shift($this->lines);
+ $cols = $forceschema ? explode(",", $forceschema) : str_getcsv(trim($csvhead, "#\n "), $splitchar, '"'); # should function_exists('str_getcsv')
+ foreach (preg_grep('/^$/', $cols) as $idx => $dummy)
+ $cols[$idx] = "field$idx"; # replace empty column names
$oldlocale = setlocale(LC_CTYPE, 'de_CH.iso-8859-1'); # this works for utf-8 as well
foreach ($this->lines as $line)
- $records[] = (object)array_combine($schema, str_getcsv($line, $splitchar, '"')); # could do a function_exists('str_getcsv') here...
+ $records[] = (object)array_combine($cols, str_getcsv($line, $splitchar, '"')); # could do a function_exists('str_getcsv') here...
setlocale(LC_CTYPE, $oldlocale);