SCDoc: Use proper static string constants instead of comparing string literals.
[supercollider.git] / SCClassLibrary / Common / Streams / TabFileReader.sc
blob78f9a8da6b9d5362a41cf451a83eced1064aec8c
1 FileReader : Stream {
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 |
7                 var stream;
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)
11         }
13         reset { stream.reset }
15         close { stream.close }
17         next {
18                 var c, record, string = String.new;
19                 while {
20                         c = stream.getChar;
21                         c.notNil
22                 } {
23                         if (c == delimiter) {
24                                 if (skipBlanks.not or: { string.size > 0 }) {
25                                         record = record.add(string);
26                                         string = String.new;
27                                 }
28                         } {
29                                 if (c == $\n or: { c == $\r }) {
30                                         record = record.add(string);
31                                         string = String.new;
32                                         if (skipEmptyLines.not or: { (record != [ "" ]) }) {
33                                                 ^record
34                                         };
35                                         record = nil;
36                                 }{
37                                         string = string.add(c);
38                                 }
39                         }
40                 };
41                 if (string.notEmpty) { ^record.add(string) };
42                 ^record;
43         }
45         read { ^this.all }
47         *read { | path, skipEmptyLines=false, skipBlanks=false, func, delimiter, startRow = 0, skipSize = 0 |
48                 var fr, table;
49                 fr = this.new(path, skipEmptyLines, skipBlanks,  delimiter) ?? { ^nil };
51                 if (func.notNil) {
52                         table = fr.subSample(startRow, skipSize).collect(_.collect(func)).all;
53                 } {
54                         table = fr.subSample(startRow, skipSize).all;
55                 };
56                 fr.close;
57                 ^table
58         }
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 {
70         classvar <delim = $,;
73 SemiColonFileReader : FileReader {
74         classvar <delim = $;;