comparison file.rhope @ 81:dbe95bfec970

Very basic file access is now working; however, there's a bug involving assigning a literal to a named pipe inside a conditional block that needs fixing
author Mike Pavone <pavone@retrodev.com>
date Thu, 22 Jul 2010 05:39:08 +0000
parents d78613686a38
children 2e2e55fc12f9
comparison
equal deleted inserted replaced
80:d78613686a38 81:dbe95bfec970
3 //and I've finalized how File I/O will fit into that. This is just a very basic 3 //and I've finalized how File I/O will fit into that. This is just a very basic
4 //implementation to allow the compiler to be self-hosting. 4 //implementation to allow the compiler to be self-hosting.
5 5
6 Foreign C:libc 6 Foreign C:libc
7 { 7 {
8 open[name(Array,Raw Pointer),flags(Int32,Naked):filedes(Int32,Naked)] 8 open[name(Array,Raw Pointer),flags(Int32,Naked),mode(UInt32,Naked):filedes(Int32,Naked)]
9 close[filedes(Int32,Naked):status(Int32,Naked)] 9 close[filedes(Int32,Naked):status(Int32,Naked)]
10 fsync[filedes(Int32,Naked):status(Int32,Naked)] 10 fsync[filedes(Int32,Naked):status(Int32,Naked)]
11 fstat[filedes(Int32,Naked),buf(Stat,Raw Pointer):err(Int32,Naked),buf] 11 fstat[filedes(Int32,Naked),buf(Stat,Raw Pointer,Mutable):err(Int32,Naked),buf]
12 lseek[filedes(Int32,Naked),offset(Int64,Naked),whence(Int32,Naked):out(Int64,Naked)]
13 ftruncate[filedes(Int32,Naked),length(Int64,Naked):status(Int32,Naked)]
12 } 14 }
13 15
14 Blueprint Stat 16 Blueprint Stat
15 { 17 {
16 Device(UInt64,Naked) 18 Device(UInt64,Naked)
74 76
75 77
76 Open@File[file,mode:out,error] 78 Open@File[file,mode:out,error]
77 { 79 {
78 If[[mode]=["r"]] 80 If[[mode]=["r"]]
79 { 81 {
80 imode <- 0i32 82 imode <- Val[0i32]
81 type <- File Read() 83 type <- Val[File Read()]
82 out <- Val[ofile] 84 out <- Val[ofile]
83 }{ 85 }{
84 If[[mode]=["w"]] 86 If[[mode]=["w"]]
85 { 87 {
86 imode <- 65i32 88 imode <- Val[65i32]
87 type <- File Write() 89 type <- Val[File Write()]
88 out <- Val[ofile] 90 out <- Val[ofile]
91 If[[fd]!=[-1i32]]
92 { lseek[fd, 0i64, 2i32] }
89 }{ 93 }{
90 ,error <- If[[mode]=["rw"]] 94 ,error <- If[[mode]=["rw"]]
91 { 95 {
92 imode <- 66i32 96 imode <- Val[66i32]
93 type <- File ReadWrite() 97 type <- Val[File ReadWrite()]
94 out <- [ofile]Read Offset <<[0i64] 98 out <- [ofile]Read Offset <<[0i64]
95 } 99 }
96 } 100 }
97 } 101 }
98 fd <- open[[file]Name >>] 102 fd <- open[[[[file]Name >>]Flatten]Buffer >>, imode, 438u32]
99 error <- If[[fd]=[-1i32]] {} 103 error <- If[[fd]=[-1i32]] {}
100 { 104 {
101 ofile <- [[[Build[type] 105 ofile <- [[[Build[type]
102 ]Name <<[ [file]Name >> ] 106 ]Name <<[ [file]Name >> ]
103 ]Descriptor <<[fd] 107 ]Descriptor <<[fd]
121 } 125 }
122 }*/ 126 }*/
123 127
124 String@File[file:out,error] 128 String@File[file:out,error]
125 { 129 {
126 ,error <- [file]Open["w"] 130 f,error <- [file]Open["r"]
127 { 131 {
128 out,error <- String[~] 132 out,error <- String[~]
129 } 133 { Close[f] }
134 }
135 }
136
137 Truncate@File[file:out,error]
138 {
139 f,error <- [file]Open["w"]
140 {
141 out,error <- Truncate[f]
142 }
143 }
144
145 Write@File[file,data:out,error]
146 {
147 f,error <- [file]Open["w"]
148 {
149 out,error <- [f]Write[data]
150 }
151 }
152
153
154 String@File Read[file:out,error]
155 {
156 ,error <- [file]Read[[file]Length]
157 { out <- String[~] }
130 } 158 }
131 159
132 Read@File Read[file,inbytes(Int32):data,outfile,error] 160 Read@File Read[file,inbytes(Int32):data,outfile,error]
133 { 161 {
134 readbytes, mdata <- read[[file]Descriptor >>, [Array[]]Set[inbytes, 0u8], Int64[inbytes]] 162 readbytes, mdata <- read[[file]Descriptor >>, [Array[]]Set[inbytes, 0u8], Int64[inbytes]]
173 { 201 {
174 close[[file]Descriptor >>] 202 close[[file]Descriptor >>]
175 out <- File[[file]Name >>] 203 out <- File[[file]Name >>]
176 } 204 }
177 205
178 206 Length@File Write[file:out]
207 {
208 out <- Trunc Int32[[[file]Info >>]Size >>]
209 }
210
211 Truncate@File Write[file:out,err]
212 {
213 err <- If[ftruncate[[file]Descriptor >>, 0i64]] {}
214 {
215 err <- If[lseek[[file]Descriptor >>, 0i64, 0i32]] {}
216 { out <- [file]Info <<[ [[file]Info >>]Size <<[0i64] ] }
217 }
218 }
219
220 Write@File Write[file,data(Array):out,err]
221 {
222 err <- If[write[[file]Descriptor >>, data, Int64[[data]Length >>]]] {}
223 {
224 out <- file
225 }
226 }
227
228 Close@File Write[file:out]
229 {
230 fsync[[file]Descriptor >>]
231 { close[[file]Descriptor >>] }
232 out <- File[[file]Name >>]
233 }
234