3 * A CSV file input class for writing CSV files
5 * @author Roland Haeder <webmaster@ship-simu.org>
7 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
8 * @license GNU GPL 3.0 or any newer version
9 * @link http://www.ship-simu.org
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 class CsvInputFile extends BaseInputTextFile implements CsvInputStreamer {
26 * Protected constructor
30 protected function __construct () {
31 // Call parent constructor
32 parent::__construct(__CLASS__);
36 * Creates an instance of this File class and prepares it for usage
38 * @param $fileName Name of the index file
39 * @return $fileInstance An instance of this File class
41 public final static function createCsvInputFile ($fileName) {
43 $fileInstance = new CsvInputFile();
46 $fileInstance->setFileName($fileName);
48 // Init this abstract file
49 $fileInstance->initFile($fileName);
51 // Return the prepared instance
56 * Reads a line from CSV file and returns it as an indexed array. Please
57 * note that strings *must* be always in double-quotes, else any found
58 * column separators will be parsed or they may be interpreted incorrectly.
60 * @param $columnSeparator Character to use separting columns
61 * @return $lineArray An indexed array with the read line
63 public function readCsvFileLine ($columnSeparator) {
65 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] columnSeparator=%s - CALLED!', __METHOD__, __LINE__, $columnSeparator));
68 $data = $this->getPointerInstance()->readLine();
71 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] data()=%d', __METHOD__, __LINE__, strlen($data)));
74 $lineArray = $this->parseDataToIndexedArray($data, $columnSeparator);
77 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] lineArray()=%d - EXIT!', __METHOD__, __LINE__, count($lineArray)));
84 * Parses given data into an array
86 * @param $data Raw data e.g. returned from readLine()
87 * @param $columnSeparator Character to use separting columns
88 * @return $lineArray An indexed array with the read line
90 private function parseDataToIndexedArray ($data, $columnSeparator) {
92 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] data()=%d,columnSeparator=%s - CALLED!', __METHOD__, __LINE__, strlen($data), $columnSeparator));
97 // Whether the parser reads a quoted string (which may contain the column separator again)
103 // Now parse the line
104 for ($idx = 0; $idx < strlen($data); $idx++) {
106 $char = substr($data, $idx, 1);
109 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] idx=%s,char=%s ...', __METHOD__, __LINE__, $idx, $char));
111 // Is the column separator found and not within quotes?
112 if (($isInQuotes === FALSE) && ($char == $columnSeparator)) {
114 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] Adding column=%s ...', __METHOD__, __LINE__, $column));
116 // Add this line to the array
117 array_push($lineArray, $column);
120 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] lineArray()=%d - After add!', __METHOD__, __LINE__, count($lineArray)));
122 // Clear variable ...
127 } elseif ($char == chr(34)) {
129 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] column=%s ...', __METHOD__, __LINE__, $column));
131 // $column must be empty at this point if we are at starting quote
132 assert(($isInQuotes === TRUE) || (empty($column)));
134 // Double-quote found, so flip variable
135 $isInQuotes = (!$isInQuotes);
138 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] isInQuotes=%d ...', __METHOD__, __LINE__, intval($isInQuotes)));
140 // Skip double-quote (escaping of them is not yet supported)
145 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] Adding char=%s ...', __METHOD__, __LINE__, $idx, $char));
147 // Add char to column
151 // Is there something outstanding?
152 if (!empty($column)) {
154 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] Adding column=%s ...', __METHOD__, __LINE__, $column));
156 // Then don't forget this. :-)
157 array_push($lineArray, $column);
160 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] lineArray()=%d - After add!', __METHOD__, __LINE__, count($lineArray)));
164 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] lineArray()=%d - EXIT!', __METHOD__, __LINE__, count($lineArray)));