Merge pull request #506 from andrewcsmith/patch-2
[supercollider.git] / SCClassLibrary / Common / Files / File.sc
blob3455ef6c872e78a7fbd47c878fea902adb20f6c2
1 File : UnixFILE {
3         classvar <openDialogs, <systemIsCaseSensitive;
5         *initClass {
6                 var f = this.filenameSymbol.asString;
7                 systemIsCaseSensitive = not(File.exists(f.toLower) and: {File.exists(f.toUpper)});
8         }
10         *new { arg pathName, mode;
11                 ^super.new.open(pathName, mode);
12         }
13         *open { arg pathName, mode;
14                 ^super.new.open(pathName, mode);
15         }
16         *use { arg pathName, mode, function;
17                 var file;
18                 file = this.new(pathName, mode);
19                 ^{ function.value(file) }.protect({ file.close });
20         }
21         *delete { arg pathName;
22                 _FileDelete
23                 ^this.primitiveFailed
24         }
25         *mtime { arg pathName;
26                 _FileMTime
27                 ^this.primitiveFailed
28         }
29         *exists { arg pathName;
30                 _FileExists
31                 ^this.primitiveFailed
32         }
33         *existsCaseSensitive { arg pathName;
34                 ^if(systemIsCaseSensitive) {
35                         this.exists(pathName)
36                 } {
37                         (pathName.dirname+/+"*").pathMatch.detect{|x|x.compare(pathName)==0}.notNil
38                 }
39         }
40         *realpath { arg pathName;
41                 _FileRealPath
42                 ^this.primitiveFailed
43         }
44         *mkdir { arg pathName;
45                 _FileMkDir
46                 ^this.primitiveFailed
47         }
48         *copy { arg pathNameFrom, pathNameTo;
49                 _FileCopy
50                 ^this.primitiveFailed
51         }
52         *type { arg pathName;
53                 var sym = #[error, not_found, regular, directory, symlink, block, character, fifo, socket, unknown];
54                 ^sym.clipAt(this.prType(pathName))
55         }
56         *prType { arg pathName;
57                 _FileType
58                 ^this.primitiveFailed
59         }
60         *fileSize { arg pathName;
61                 _FileSize
62                 ^this.primitiveFailed
63         }
65         *getcwd {
66                 var string;
67                 this.prGetcwd(string = String.new(256));
68                 ^string
69         }
70         open { arg pathName, mode;
71                 /* open the file. mode is a string passed
72                         to fopen, so should be one of:
73                         "r","w","a","rb","wb","ab","r+","w+","a+",
74                         "rb+","wb+","ab+"
75                 */
76                 if (this.prOpen(pathName, mode), {
77                         this.addOpenFile;
78                 });
79         }
80         close { // close the file
81                 // the GC will not call this for you
82                 this.prClose;
83                 fileptr = nil;
84                 openFiles.remove(this);
85         }
86         length { // returns the length of the file
87                 _FileLength;
88                 ^this.primitiveFailed;
89         }
90         pos { // current file position
91                 _FilePos
92                 ^this.primitiveFailed;
93         }
94         pos_ { arg toPos;
95                 toPos = toPos.clip(0, this.length - 1);
96                 this.seek(toPos, 0);
97         }
98         seek { arg offset = 0, origin = 0;
99                 // origin is an integer, one of:
100                 // 0 - from beginning of file
101                 // 1 - from current position
102                 // 2 - from end of file
104                 _FileSeek
105                 ^this.primitiveFailed;
106         }
108         readAllString {
109                 var string;
110                 string = String.newClear(this.length);
111                 this.read(string);
112                 ^string
113         }
114         readAllStringRTF {
115                 ^this.readAllString.stripRTF;
116         }
117         readAllStringHTML {
118                 ^this.readAllString.stripHTML;
119         }
120         readAllSignal {
121                 var signal;
122                 signal = Signal.newClear(this.length);
123                 this.read(signal);
124                 ^signal
125         }
127         // PRIVATE
128         prOpen { arg pathName, mode;
129                 /* open the file. mode is a string passed
130                         to fopen, so should be one of:
131                         "r","w","a","rb","wb","ab","r+","w+","a+",
132                         "rb+","wb+","ab+"
133                 */
134                 _FileOpen
135                 ^this.primitiveFailed;
136         }
137         prClose {
138                 _FileClose
139                 ^this.primitiveFailed;
140         }
142         *prGetcwd { arg string;
143                 _File_getcwd
144                 ^this.primitiveFailed;
145         }
149 Pipe : UnixFILE {
150         var pid;
152         // pipe stdin to, or stdout from, a unix shell command.
153         *new { arg commandLine, mode;
154                 ^super.new.open(commandLine, mode);
155         }
156         open { arg commandLine, mode;
157                 /* open the file. mode is a string passed
158                         to popen, so should be one of:
159                         "r","w"
160                 */
161                 if ((pid = this.prOpen(commandLine, mode)).notNil) {
162                         this.addOpenFile;
163                 }
164         }
166         close {
167                 var res = this.prClose(pid);
168                 fileptr = nil;
169                 pid = nil;
170                 openFiles.remove(this);
171                 ^res;
172         }
174         // close the file
175         prClose { arg pid;
176                 // the GC will not call this for you
177                 _PipeClose
178                 ^this.primitiveFailed;
179         }
182         // PRIVATE
183         prOpen { arg pathName, mode;
184                 /* open the file. mode is a string passed
185                         to popen, so should be one of:
186                         "r","w"
187                 */
188                 _PipeOpen
189                 ^this.primitiveFailed;
190         }