changeset 80:d78613686a38

Some progress on File
author Mike Pavone <pavone@retrodev.com>
date Wed, 21 Jul 2010 00:45:13 -0400
parents 80d8c9248f85
children dbe95bfec970
files file.rhope kernel.rhope testfileread.rhope
diffstat 3 files changed, 121 insertions(+), 16 deletions(-) [+]
line wrap: on
line diff
--- a/file.rhope	Sat Jul 10 18:02:04 2010 -0400
+++ b/file.rhope	Wed Jul 21 00:45:13 2010 -0400
@@ -1,8 +1,43 @@
 
+//This implementation will change quite a bit once the transaction system is working
+//and I've finalized how File I/O will fit into that. This is just a very basic
+//implementation to allow the compiler to be self-hosting.
+
+Foreign C:libc
+{
+	open[name(Array,Raw Pointer),flags(Int32,Naked):filedes(Int32,Naked)]
+	close[filedes(Int32,Naked):status(Int32,Naked)]
+	fsync[filedes(Int32,Naked):status(Int32,Naked)]
+	fstat[filedes(Int32,Naked),buf(Stat,Raw Pointer):err(Int32,Naked),buf]
+}
 
 Blueprint Stat
 {
-	
+	Device(UInt64,Naked)	
+	Inode(UInt64,Naked)
+	Link Count(UInt64,Naked)
+	Mode(UInt32,Naked)
+	Owner ID(UInt32,Naked)
+	Group ID(UInt32,Naked)
+	Pad0(Int32,Naked)
+	RDevice(UInt64,Naked)
+	Size(Int64,Naked)
+	Block Size(Int64,Naked)
+	Block Count(Int64,Naked)
+	Access Time(Int64,Naked)
+	Access Time Nano(UInt64,Naked)
+	Mod Time(Int64,Naked)
+	Mod Time Nano(UInt64,Naked)
+	Status Time(Int64,Naked)
+	Status Time Nano(UInt64,Naked)
+	Unused1(Int64,Naked)
+	Unused2(Int64,Naked)
+	Unused3(Int64,Naked)
+}
+
+Stat[fd:out]
+{
+	err,out <- fstat[fd, Build[Stat()]]
 }
 
 Blueprint File
@@ -15,7 +50,6 @@
 	Name
 	Descriptor(Int32,Naked)
 	Info
-	Buffer
 }
 
 Blueprint File Write
@@ -23,7 +57,6 @@
 	Name
 	Descriptor(Int32,Naked)
 	Info
-	Buffer
 }
 
 Blueprint File ReadWrite
@@ -32,7 +65,6 @@
 	Read Offset(Int64,Naked)
 	Descriptor(Int32,Naked)
 	Info
-	Buffer
 }
 
 File[name:out]
@@ -45,19 +77,19 @@
 {
 	If[[mode]=["r"]]
 	{ 
-		imode <- 
+		imode <- 0i32
 		type <- File Read()
 		out <- Val[ofile]
 	}{
 		If[[mode]=["w"]]
 		{ 
-			imode <- 
+			imode <- 65i32
 			type <- File Write()
 			out <- Val[ofile]
 		}{
 			,error <- If[[mode]=["rw"]]
 			{ 
-				imode <- 
+				imode <- 66i32
 				type <- File ReadWrite()
 				out <- [ofile]Read Offset <<[0i64]
 			}
@@ -66,11 +98,10 @@
 	fd <- open[[file]Name >>]
 	error <- If[[fd]=[-1i32]] {}
 	{
-		ofile <- [[[[Build[type]	
+		ofile <- [[[Build[type]	
 			]Name <<[ [file]Name >> ]
 			]Descriptor <<[fd]
 			]Info <<[Stat[fd]]
-			]Buffer <<[Array[]]
 	}
 }
 
@@ -81,14 +112,14 @@
 		data,outfile,error <- [~]Read[bytes]
 	}
 }
-
+/*
 Append@File[file,data:out,error]
 {
 	,error <- [file]Open["w"]
 	{
 		out,error <- [~]Append[data]
 	}
-}
+}*/
 
 String@File[file:out,error]
 {
@@ -100,6 +131,14 @@
 
 Read@File Read[file,inbytes(Int32):data,outfile,error]
 {
+	readbytes, mdata <- read[[file]Descriptor >>, [Array[]]Set[inbytes, 0u8], Int64[inbytes]]
+	error <- If[[readbytes]<[0i64]] {}
+	{
+		outfile <- file
+		data <- [mdata]Length <<[ Trunc Int32[readbytes] ]
+	}
+	/* 
+	//Skip buffering for now. Buffering approach may need to change later anyway
 	buflen <- [[file]Buffer >>]Length
 	If[buflen]
 	{
@@ -122,7 +161,18 @@
 			outfile <- file
 			data <- [mdata]Length <<[ Trunc Int32[readbytes] ]
 		}
-			
-	}
+	} */
+}
+
+Length@File Read[file:out]
+{
+	out <- Trunc Int32[[[file]Info >>]Size >>]
 }
 
+Close@File Read[file:out]
+{
+	close[[file]Descriptor >>]
+	out <- File[[file]Name >>]
+}
+
+
--- a/kernel.rhope	Sat Jul 10 18:02:04 2010 -0400
+++ b/kernel.rhope	Wed Jul 21 00:45:13 2010 -0400
@@ -1,6 +1,7 @@
 Import string.rhope
 Import list.rhope
 Import functional.rhope
+Import file.rhope
 
 Val[in:out]
 {
@@ -40,11 +41,8 @@
 
 Foreign C:libc
 {
-	open[name(Array,Raw Pointer),flags(Int32,Naked):filedes(Int32,Naked)]
 	write[filedes(Int32,Naked),buf(Array,Raw Pointer),nbyte(Int64,Naked):written(Int32,Naked)]
 	read[filedes(Int32,Naked),buf(Array,Raw Pointer,Mutable),nbyte(Int64,Naked):read(Int64,Naked),buf]
-	close[filedes(Int32,Naked):status(Int32,Naked)]
-	fsync[filedes(Int32,Naked):status(Int32,Naked)]
 }
 
 _Print Int32[n,buf:out]
@@ -361,6 +359,55 @@
 	out <- [arr]Index[index]
 }
 
+_Copy Part Naked[source,dest,srcindex,destindex:out]
+{
+	ndest <- _internal_array_copyin[dest, destindex, [source]Index[srcindex]]
+	
+	[source]Next[srcindex]
+	{
+		out <- _Copy Part Naked[source, ndest, ~, [destindex]+[1]]
+	}{
+		out <- Val[ndest]
+	}
+}
+
+_Copy Part Boxed[source,dest,srcindex,destindex:out]
+{
+	ndest <- _internal_array_setboxed[dest, destindex, [source]Index[srcindex]]
+	
+	[source]Next[srcindex]
+	{
+		out <- _Copy Part Boxed[source, ndest, ~, [destindex]+[1]]
+	}{
+		out <- Val[ndest]
+	}
+}
+
+Slice@Array[arr,slicepoint(Int32):left,right]
+{
+	If[[slicepoint]<[[arr]Length]]
+	{
+		If[[slicepoint]>[0i32]]
+		{
+			eltype <- [arr]Eltype >>
+			If[[eltype] = [Any Type()]]
+			{
+				_Copy Part Boxed[arr, _internal_array_allocboxed[[[arr]Length]-[slicepoint]], slicepoint, 0]
+			}{
+				_Copy Part Naked[arr, _internal_array_allocnaked[[[arr]Length]-[slicepoint], eltype], slicepoint, 0]
+			}
+			left <- [arr]Length <<[slicepoint]
+		}{
+			right <- arr
+			left <- Array[]
+		}
+	}{
+		left <- arr
+		right <- Array[]
+	}
+	
+}
+
 And[left,right:out]
 {
 	,out <- If[left]
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/testfileread.rhope	Wed Jul 21 00:45:13 2010 -0400
@@ -0,0 +1,8 @@
+
+Main[]
+{
+	f <- [File["test.txt"]]Open["r"]
+	,data <- [f]Read[[f]Length]
+	Print[String[data]]
+}
+