File: SeqCol-newFromFile.sc

package info (click to toggle)
supercollider-sc3-plugins 3.7.1~repack-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 14,332 kB
  • ctags: 11,704
  • sloc: cpp: 140,180; lisp: 14,746; ansic: 2,133; xml: 86; makefile: 82; haskell: 21; sh: 8
file content (64 lines) | stat: -rw-r--r-- 1,225 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
+ SequenceableCollection {

	// array[row][col]
	*newFromFile { arg path, columnSeparator=$ , rowSeparator=$\n;
		var obj, file, char, line;

		file = File.new(path, "r");
		if( file.isOpen == false, { ^nil	});

		obj = this.new;
		char = file.getChar;

		while({ char.notNil },{

			line = "";

			// skip past tabs and spaces at start of new line
			while({(char == $ ) || (char == $\t)}, {
				char = file.getChar;
			});

			// read a line in
			while({ (char!=rowSeparator) && (char!=$\r) && char.notNil }, {
				line = line ++ char;
				char = file.getChar;
			});

			char = file.getChar;
			obj = obj.add(line.split(columnSeparator));

		});

		file.close;
		^obj
	}

	writeFile{ arg path, columnSeparator=$ , rowSeparator=$\n;
		var file, line, lastRow;

		file = File.new(path, "w");
		if( file.isOpen == false, { ^nil	});

		lastRow = this.size - 1;

		this.size.do({ arg row;
			line = this[row][0].asString;

			if(this[row].size > 1, {
				for(1, this[row].size-1, { arg col;
					line = line ++ columnSeparator ++ this[row][col].asString;
				});
			});

			// the last row doesn't need a separator
			if(row != lastRow, {
				line = line ++ rowSeparator;
			});

			file.write(line);
		});

		file.close;
	}
}