Skip to content

Commit

Permalink
MDL-36365 - lib: The csv class now reads tab separated files with tab…
Browse files Browse the repository at this point in the history
…s at the end of the file.
  • Loading branch information
abgreeve committed Nov 7, 2012
1 parent 6109f21 commit 1222dc5
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
7 changes: 6 additions & 1 deletion lib/csvlib.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,12 @@ function load_csv_content(&$content, $encoding, $delimiter_name, $column_validat
// Fix mac/dos newlines
$content = preg_replace('!\r\n?!', "\n", $content);
// Remove any spaces or new lines at the end of the file.
$content = trim($content);
if ($delimiter_name == 'tab') {
// trim() by default removes tabs from the end of content which is undesirable in a tab separated file.
$content = trim($content, chr(0x20) . chr(0x0A) . chr(0x0D) . chr(0x00) . chr(0x0B));
} else {
$content = trim($content);
}

$csv_delimiter = csv_import_reader::get_delimiter($delimiter_name);
// $csv_encode = csv_import_reader::get_encoded_delimiter($delimiter_name);
Expand Down
13 changes: 13 additions & 0 deletions lib/tests/csvclass_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ protected function setUp(){
}

public function test_csv_functions() {
global $CFG;
$csvexport = new csv_export_writer();
$csvexport->set_filename('unittest');
foreach ($this->testdata as $data) {
Expand Down Expand Up @@ -113,5 +114,17 @@ public function test_csv_functions() {
$csvimport->cleanup();
$csvimport->close();
$this->assertEquals($importerror, $errortext);

// Testing for a tab separated file.
// The tab separated file has a trailing tab and extra blank lines at the end of the file.
$filename = $CFG->dirroot . '/lib/tests/fixtures/tabfile.csv';
$fp = fopen($filename, 'r');
$tabdata = fread($fp, filesize($filename));
fclose($fp);
$iid = csv_import_reader::get_new_iid('tab');
$csvimport = new csv_import_reader($iid, 'tab');
$contentcount = $csvimport->load_csv_content($tabdata, 'utf-8', 'tab');
// This should import four rows including the headings.
$this->assertEquals($contentcount, 4);
}
}
6 changes: 6 additions & 0 deletions lib/tests/fixtures/tabfile.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
name url "Description of map" Grade
"George Fitz" "http://www.myblog.com" "This is a small map, that could be used in anything."
"Jim Brown" "http://www.blank.ne.jp" "Difficult to follow for the first five km.
Then things get easier." 1
"Tanya Keisel" "www.google.com" "No joke, it's just all burn't up. You can't ""understand"" any of it."

0 comments on commit 1222dc5

Please sign in to comment.