3 namespace Org\Mxchange\CoreFramework\Filesystem\Input\Csv;
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Filesystem\Text\BaseInputTextFile;
7 use Org\Mxchange\CoreFramework\Stream\Filesystem\CsvInputStreamer;
10 * A CSV file input class for writing CSV files
12 * @author Roland Haeder <webmaster@ship-simu.org>
14 <<<<<<< HEAD:framework/main/classes/file_directories/text/input/csv/class_CsvInputFile.php
15 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
17 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2016 Core Developer Team
18 >>>>>>> Some updates::inc/main/classes/file_directories/text/input/csv/class_CsvInputFile.php
19 * @license GNU GPL 3.0 or any newer version
20 * @link http://www.ship-simu.org
22 * This program is free software: you can redistribute it and/or modify
23 * it under the terms of the GNU General Public License as published by
24 * the Free Software Foundation, either version 3 of the License, or
25 * (at your option) any later version.
27 * This program is distributed in the hope that it will be useful,
28 * but WITHOUT ANY WARRANTY; without even the implied warranty of
29 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 * GNU General Public License for more details.
32 * You should have received a copy of the GNU General Public License
33 * along with this program. If not, see <http://www.gnu.org/licenses/>.
35 class CsvInputFile extends BaseInputTextFile implements CsvInputStreamer {
37 * Protected constructor
41 protected function __construct () {
42 // Call parent constructor
43 parent::__construct(__CLASS__);
47 * Creates an instance of this File class and prepares it for usage
49 * @param $fileName Name of the index file
50 * @return $fileInstance An instance of this File class
52 public final static function createCsvInputFile ($fileName) {
54 $fileInstance = new CsvInputFile();
56 // Init this abstract file
57 $fileInstance->initFile($fileName);
59 // Return the prepared instance
64 * Reads a line from CSV file and returns it as an indexed array. Please
65 * note that strings *must* be always in double-quotes, else any found
66 * column separators will be parsed or they may be interpreted incorrectly.
68 * @param $columnSeparator Character to use separting columns
69 * @return $lineArray An indexed array with the read line
71 public function readCsvFileLine ($columnSeparator) {
73 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] columnSeparator=%s - CALLED!', __METHOD__, __LINE__, $columnSeparator));
76 $data = $this->getPointerInstance()->readLine();
79 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] data()=%d', __METHOD__, __LINE__, strlen($data)));
82 $lineArray = $this->parseDataToIndexedArray($data, $columnSeparator);
85 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] lineArray()=%d - EXIT!', __METHOD__, __LINE__, count($lineArray)));
92 * Parses given data into an array
94 * @param $data Raw data e.g. returned from readLine()
95 * @param $columnSeparator Character to use separting columns
96 * @return $lineArray An indexed array with the read line
98 private function parseDataToIndexedArray ($data, $columnSeparator) {
100 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] data()=%d,columnSeparator=%s - CALLED!', __METHOD__, __LINE__, strlen($data), $columnSeparator));
103 $lineArray = array();
105 // Whether the parser reads a quoted string (which may contain the column separator again)
111 // Now parse the line
112 for ($idx = 0; $idx < strlen($data); $idx++) {
114 $char = substr($data, $idx, 1);
117 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] idx=%s,char=%s ...', __METHOD__, __LINE__, $idx, $char));
119 // Is the column separator found and not within quotes?
120 if (($isInQuotes === false) && ($char == $columnSeparator)) {
122 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] Adding column=%s ...', __METHOD__, __LINE__, $column));
124 // Add this line to the array
125 array_push($lineArray, $column);
128 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] lineArray()=%d - After add!', __METHOD__, __LINE__, count($lineArray)));
130 // Clear variable ...
135 } elseif ($char == chr(34)) {
137 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] column=%s ...', __METHOD__, __LINE__, $column));
139 // $column must be empty at this point if we are at starting quote
140 assert(($isInQuotes === true) || (empty($column)));
142 // Double-quote found, so flip variable
143 $isInQuotes = (!$isInQuotes);
146 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] isInQuotes=%d ...', __METHOD__, __LINE__, intval($isInQuotes)));
148 // Skip double-quote (escaping of them is not yet supported)
153 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] Adding char=%s ...', __METHOD__, __LINE__, $idx, $char));
155 // Add char to column
159 // Is there something outstanding?
160 if (!empty($column)) {
162 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] Adding column=%s ...', __METHOD__, __LINE__, $column));
164 // Then don't forget this. :-)
165 array_push($lineArray, $column);
168 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] lineArray()=%d - After add!', __METHOD__, __LINE__, count($lineArray)));
172 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] lineArray()=%d - EXIT!', __METHOD__, __LINE__, count($lineArray)));