2 // a class to read text files automatically
3 classvar <delim = $ ; // space separated by default
4 var <stream, skipEmptyLines=false, skipBlanks=false, <delimiter;
6 *new { | pathOrFile, skipEmptyLines=false, skipBlanks=false, delimiter |
8 if (pathOrFile.isKindOf(File) ) { stream = pathOrFile } { stream = File(pathOrFile, "r") };
9 if (stream.isOpen.not) { warn(this.name ++ ": file" + pathOrFile + "not found.") ^nil };
10 ^super.newCopyArgs(stream, skipEmptyLines, skipBlanks, delimiter ? this.delim)
13 reset { stream.reset }
15 close { stream.close }
18 var c, record, string = String.new;
24 if (skipBlanks.not or: { string.size > 0 }) {
25 record = record.add(string);
29 if (c == $\n or: { c == $\r }) {
30 record = record.add(string);
32 if (skipEmptyLines.not or: { (record != [ "" ]) }) {
37 string = string.add(c);
41 if (string.notEmpty) { ^record.add(string) };
47 *read { | path, skipEmptyLines=false, skipBlanks=false, func, delimiter, startRow = 0, skipSize = 0 |
49 fr = this.new(path, skipEmptyLines, skipBlanks, delimiter) ?? { ^nil };
52 table = fr.subSample(startRow, skipSize).collect(_.collect(func)).all;
54 table = fr.subSample(startRow, skipSize).all;
60 *readInterpret { | path, skipEmptyLines=false, skipBlanks=false, delimiter, startRow = 0, skipSize = 0 |
61 ^this.read(path, skipEmptyLines, skipBlanks, _.interpret, delimiter, startRow, skipSize ) }
65 TabFileReader : FileReader {
66 classvar <delim = $\t;
69 CSVFileReader : FileReader {
73 SemiColonFileReader : FileReader {