File: IO.k

package info (click to toggle)
kaya 0.2.0-6
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 3,012 kB
  • ctags: 1,307
  • sloc: cpp: 6,691; haskell: 4,833; sh: 2,868; yacc: 768; makefile: 700; perl: 87
file content (369 lines) | stat: -rw-r--r-- 8,256 bytes parent folder | download
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
/** -*-C-*-ish
    Kaya standard library
    Copyright (C) 2004, 2005 Edwin Brady

    This file is distributed under the terms of the GNU Lesser General
    Public Licence. See COPYING for licence.
*/

module IO;

import Prelude;

%include "stdlib.h";
%include "sys/stat.h";
%include "sys/types.h";

// Chris Morris, 18/6/05 - moved data definitions above foreign{} and
// added AccessMode type.
"Generic file handling error."
public Exception FileError = Exception("File Error",130);
"Directory handling error."
public Exception DirError = Exception("Directory Error",131);
"Invalid file mode."
public Exception InvalidMode = Exception("Invalid mode",132);

"Modes for file checking."
public data AccessMode = FExists | FExecute | FWrite | FRead;

"Modes for file opening."
public data FileMode = Read | Write | Append | Binary;

// QUESTION: Why is File primitive? Why not implement File like this instead?
"Directory Handle"
abstract data Dir(Ptr ptr);
"Directory Entry"
public data DirEntry(String name);

"File Information"
public data Stat(Mode mode, Int nlinks,
		 Int atime, Int mtime, Int ctime);
	
"File modes"  
public data Mode([Permission] ownerperms,
		 [Permission] groupperms,
		 [Permission] othersperms,
		 Bool socket,
		 Bool symlink,
		 Bool regular,
		 Bool block,
		 Bool dir,
		 Bool chardev,
		 Bool fifo,
		 Bool setUID,
		 Bool setGID,
		 Bool sticky);

"File permissions"
public data Permission = ReadP | WriteP | ExecuteP;

"Directory tree"
public data DirTree([DirInfo] files);
public data DirInfo 
    = Directory(String dname, [DirInfo] children)
    | Filename(String fname);

foreign "stdfuns.o" {
    File fopen(String name, String mode) = do_fopen;
    File freopen(String name, String mode, File f) = do_freopen;
    Bool validFile(File h) = validFile;
    String getLine(File h) = getLine;
    "Read a string (up to NULL termination) from a binary file."
    public String getString(File h) = getString;
    "Read character from file."
    public Char getChar(File h) = fgetc;
    "Write character to a file."
    Void doputChar(Int c,File h) = fputc;
    "Write a string (including NULL termination) to a binary file."
    public Void putStr(File h, String s) = putString;
    Void putLine(File h, String s) = putLine;
    "Seek to the specified byte of the file"
    public Void fseek(File h, Int p) = do_fseek;
    "Get the current byte position of the file pointer"
    public Int fpos(File h) = do_ftell;
    Bool feof(File h) = feof;
    Void fclose(File h) = fclose;
    Int do_unlink(String fn) = do_unlink;
    Int do_mkdir(String fn, Int umask) = do_mkdir;
    Int do_access(String fn, AccessMode mode) = do_access;
    "Rename a file"
    public Void rename(String old, String new) = do_rename;

    Ptr do_opendir(String name) = do_opendir;
    Int do_closedir(Ptr dirh) = do_closedir;
    Ptr do_readdir(Ptr dirh) = do_readdir;
    String dir_getname(Ptr ent) = dir_getname;

    Stat do_stat(Ptr vm, String name) = do_stat;
}

"DEPRECATED synonym for IO::putStr()"
public Void putString(File h, String s) = IO::putStr(h,s);

String getFileMode([FileMode] mode) {
  for m in mode {
    case m of {
      Read -> read = true;
      | Write -> write = true;
      | Append -> append = true;
      | Binary -> binary = true;
    }
  }
  if (read && write) { fmode = "r+"; }
  else if (read && append) { fmode = "a+"; }
  else if (read) { fmode = "r"; }
  else if (write) { fmode = "w"; }
  else if (append) { fmode = "a"; }
  else {
    throw(InvalidMode);
  }

  if (binary) { fmode+="b"; }
  return fmode;
}

"Open a file.
Throws an exception if opening fails for any reason."
public File open(String fname, [FileMode] mode)
{
    fmode = getFileMode(mode);

    h = fopen(fname,fmode);
    // Check h is valid.
    if (validFile(h)) {
	return h;
    }
    else {
	throw(FileError);
    }
}

"Re-open a file.
Throws an exception if opening fails for any reason."
public File reopen(String fname,[FileMode] mode, File f)
{
    fmode = getFileMode(mode);
    h = freopen(fname,fmode,f);
    // Check h is valid.
    if (validFile(h)) {
	return h;
    }
    else {
	throw(FileError);
    }
}

"Read a line from a file."
public String get(File handle)
{
    return getLine(handle);
}

"Write a line to a file."
public Void put(File handle, String val)
{
    putLine(handle,val);
}

"Write a character to a file."
public Void putChar(File handle, Char c)
{
    doputChar(Int(c),handle);
}

"Write an integer to a file (as binary)"
public Void putInt(File handle, Int i)
{
    putChar(handle, Char(i>>24));
    putChar(handle, Char((i>>16) & 255));
    putChar(handle, Char((i>>8) & 255));
    putChar(handle, Char(i & 255));
}

"Get a binary integer from a file."
public Int getInt(File handle)
{
    c1 = Int(getChar(handle));
    c2 = Int(getChar(handle));
    c3 = Int(getChar(handle));
    c4 = Int(getChar(handle));
    return (c1<<24)+(c2<<16)+(c3<<8)+c4;
}

"Check for end of file."
public Bool eof(File handle)
{
    return feof(handle);
}

"Close a file."
public Void close(File handle)
{
    return fclose(handle);
}

"Read a file.
Reads the entire contents of <em>fname</em> into a String."
public String readFile(String fname)
{
    contents = "";
    f = open(fname,[Read]);
    while(!eof(f)) {
	contents = contents+get(f);
    }
    close(f);
    return contents;
}

"Write a String to a File.
Creates a file called <em>fname</em>, containing the given <em>content</em>."
public Void writeFile(String fname, String content) 
{
    f = open(fname,[Write]);
    put(f,content);
    close(f);
}

"Unlinks a File.
Throws a FileError exception on failure"
public Void unlink(String fname)
{
  r = do_unlink(fname);
  if (r != 0) {
      throw(FileError);
  }
}

"Makes a directory.
Throws a FileError exception if creation fails."
public Void mkdir(String dname, Int umask) 
{
  r = do_mkdir(dname,umask);
  if (r != 0) {
      throw(FileError);
  }
}

"Checks a file exists"
public Bool fileExists(String fname) {
  if (do_access(fname,FExists) == 0) {
    return true;
  } else {
    return false;
  }
}

"Checks a file is readable"
public Bool fileReadable(String fname) {
  if (do_access(fname,FRead) == 0) {
    return true;
  } else {
    return false;
  }
}

"Checks a file is writable"
public Bool fileWritable(String fname) {
  if (do_access(fname,FWrite) == 0) {
    return true;
  } else {
    return false;
  }
}

"Checks a file is executable"
public Bool fileExecutable(String fname) {
  if (do_access(fname,FExecute) == 0) {
    return true;
  } else {
    return false;
  }
}

"Checks a file has the given mode"
public Bool fileMode(String fname, AccessMode m) {
    return (do_access(fname,m)==0);	
}

"Open a directory for reading.
Throws a DirError on error."
public Dir openDir(String fname) {
    ptr = do_opendir(fname);
    if (null(ptr)) {
	throw(DirError);
    }
    return Dir(ptr);
}

"Close a directory handle.
Throws a DirError on error."
public Void closeDir(Dir h) {
    if (do_closedir(h.ptr)!=0) {
	throw(DirError);
    }
}

"Get the next directory entry.
Throws a DirError on error, or on reaching the end of the directory."
public DirEntry readDir(Dir h) {
    Ptr ent = do_readdir(h.ptr);
    if (null(ent)) {
	throw(DirError);
    }
    name = dir_getname(ent);
    return DirEntry(name);
}

"Return all files in a directory."
public [String] listDir(String dirname) {
    dir = openDir(dirname);
    fs = [];
    try {
	while(true) {
	    ent = readDir(dir); // Throws at the end. Yeuch.
	    push(fs,ent.name);
	}
    }
    catch(e) {
	if (e==DirError) {
	    closeDir(dir);
	    return fs;
	}
	else {
	    throw(e);
	}
    }
    return fs;
}

"Get file information."
public Stat stat(String fname)
{
//    putStrLn("statting "+fname);
    return do_stat(getVM(),fname);
}

"Return the entire directory tree, starting from <em>fname</em>."
public DirTree dirinfo(String fname)
{
    return DirTree(getDirInfo(fname));
}

public [DirInfo] getDirInfo(String fname) {
    fs = listDir(fname);
    inf = [];
    for f in fs {
	realf = fname+"/"+f;
	stats = stat(realf);
	if (stats.mode.dir) {
	    if (f!="." && f!="..") {
		children = getDirInfo(realf);
		push(inf, Directory(f, children));
	    }
	}
	else {
	    push(inf, Filename(f));
	}
    }
    return inf;
}