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
|
module copyfile
/*
Commandline version of a file copying program.
Run the program using the "Basic Values Only" option.
*/
import StdEnv, StdFile
Start::*World -> *File
Start world = fwrites "\nGoodbye.\n" stdinout`
where
(stdinout`,_) = accFiles (CommandLoop stdinout) world`
(stdinout ,world`) = stdio world
CommandLoop::*File *Files -> (*File,*Files)
CommandLoop stdio files = CommandLoop` stdio` files`
where
(files`,stdio`) = Copy files stdio
CommandLoop`::*File *Files -> (*File,*Files)
CommandLoop` stdio files
| answer<>'y' && answer<>'Y' = (stdio2,files)
= CommandLoop` stdio` files`
where
(files`,stdio`) = Copy files stdio2
answer = FirstChar answ
(answ ,stdio2) = freadline stdio1
stdio1 = fwrites "\nCopy another file (y/n)? " stdio
Copy::*Files *File -> (*Files,*File)
Copy files io
| source == dest = (files, fwrites "\nCopying succeeded.\n" io4)
= CopyFile (StripNewline source) (StripNewline dest) files io4
where
(dest,io4) = freadline io3
io3 = fwrites "\nDestination file: " io2
(source,io2) = freadline io1
io1 = fwrites "\nSource file: " io
CopyFile::String String *Files *File -> (*Files,*File)
CopyFile source dest files io
| not sopen = (files1,alert1)
| not dopen = (files2,alert2)
| io_error = (files4,alert3)
| not dclose = (files4,alert4)
| not sclose = (files4,alert5)
= (files4,alert6)
where
(sclose,files4) = fclose sfile` files3
(dclose,files3) = fclose dfile` files2
(io_error,sfile`,dfile`) = CopyFiles sfile dfile
(dopen,dfile,files2) = fopen dest FWriteText files1
(sopen,sfile,files1) = fopen source FReadData files
alert1 = fwrites "\nCopying failed.\nSource file could not be opened.\n" io
alert2 = fwrites "Copying failed.\nDestination file could not be opened.\n" io
alert3 = fwrites "Copying failed.\nFile I/O error.\n" io
alert4 = fwrites "Copying failed.\nDestination file could not be closed.\n" io
alert5 = fwrites "Copying failed.\nSource file could not be closed.\n" io
alert6 = fwrites "\nCopying succeeded.\n" io
CopyFiles::*File *File -> (Bool, *File, *File)
CopyFiles source dest
| srcend || wrterror = (wrterror,source1,dest1)
= CopyFiles source2 (fwritec byte dest1)
where
(_,byte,source2) = freadc source1
(srcend,source1) = fend source
(wrterror,dest1) = ferror dest
StripNewline::String -> String
StripNewline "" = ""
StripNewline str = str % (0, size str - 2)
FirstChar::String -> Char
FirstChar "" = ' '
FirstChar str = str.[0]
|