3 namespace CoreFramework\Filesystem\Input\Csv;
5 // Import framework stuff
6 use CoreFramework\Filesystem\Text\BaseInputTextFile;
7 use CoreFramework\Stream\Filesystem\CsvInputStreamer;
10 * A CSV file input class for writing CSV files
12 * @author Roland Haeder <webmaster@ship-simu.org>
14 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
15 * @license GNU GPL 3.0 or any newer version
16 * @link http://www.ship-simu.org
18 * This program is free software: you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation, either version 3 of the License, or
21 * (at your option) any later version.
23 * This program is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
28 * You should have received a copy of the GNU General Public License
29 * along with this program. If not, see <http://www.gnu.org/licenses/>.
31 class CsvInputFile extends BaseInputTextFile implements CsvInputStreamer {
33 * Protected constructor
37 protected function __construct () {
38 // Call parent constructor
39 parent::__construct(__CLASS__);
43 * Creates an instance of this File class and prepares it for usage
45 * @param $fileName Name of the index file
46 * @return $fileInstance An instance of this File class
48 public final static function createCsvInputFile ($fileName) {
50 $fileInstance = new CsvInputFile();
53 $fileInstance->setFileName($fileName);
55 // Init this abstract file
56 $fileInstance->initFile($fileName);
58 // Return the prepared instance
63 * Reads a line from CSV file and returns it as an indexed array. Please
64 * note that strings *must* be always in double-quotes, else any found
65 * column separators will be parsed or they may be interpreted incorrectly.
67 * @param $columnSeparator Character to use separting columns
68 * @return $lineArray An indexed array with the read line
70 public function readCsvFileLine ($columnSeparator) {
72 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] columnSeparator=%s - CALLED!', __METHOD__, __LINE__, $columnSeparator));
75 $data = $this->getPointerInstance()->readLine();
78 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] data()=%d', __METHOD__, __LINE__, strlen($data)));
81 $lineArray = $this->parseDataToIndexedArray($data, $columnSeparator);
84 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] lineArray()=%d - EXIT!', __METHOD__, __LINE__, count($lineArray)));
91 * Parses given data into an array
93 * @param $data Raw data e.g. returned from readLine()
94 * @param $columnSeparator Character to use separting columns
95 * @return $lineArray An indexed array with the read line
97 private function parseDataToIndexedArray ($data, $columnSeparator) {
99 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] data()=%d,columnSeparator=%s - CALLED!', __METHOD__, __LINE__, strlen($data), $columnSeparator));
102 $lineArray = array();
104 // Whether the parser reads a quoted string (which may contain the column separator again)
110 // Now parse the line
111 for ($idx = 0; $idx < strlen($data); $idx++) {
113 $char = substr($data, $idx, 1);
116 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] idx=%s,char=%s ...', __METHOD__, __LINE__, $idx, $char));
118 // Is the column separator found and not within quotes?
119 if (($isInQuotes === FALSE) && ($char == $columnSeparator)) {
121 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] Adding column=%s ...', __METHOD__, __LINE__, $column));
123 // Add this line to the array
124 array_push($lineArray, $column);
127 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] lineArray()=%d - After add!', __METHOD__, __LINE__, count($lineArray)));
129 // Clear variable ...
134 } elseif ($char == chr(34)) {
136 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] column=%s ...', __METHOD__, __LINE__, $column));
138 // $column must be empty at this point if we are at starting quote
139 assert(($isInQuotes === TRUE) || (empty($column)));
141 // Double-quote found, so flip variable
142 $isInQuotes = (!$isInQuotes);
145 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] isInQuotes=%d ...', __METHOD__, __LINE__, intval($isInQuotes)));
147 // Skip double-quote (escaping of them is not yet supported)
152 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] Adding char=%s ...', __METHOD__, __LINE__, $idx, $char));
154 // Add char to column
158 // Is there something outstanding?
159 if (!empty($column)) {
161 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] Adding column=%s ...', __METHOD__, __LINE__, $column));
163 // Then don't forget this. :-)
164 array_push($lineArray, $column);
167 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] lineArray()=%d - After add!', __METHOD__, __LINE__, count($lineArray)));
171 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] lineArray()=%d - EXIT!', __METHOD__, __LINE__, count($lineArray)));