[m-rev.] Implementation of io__putback*, io__clearerr, io__ferror, io__stream_file_size in C#

Yoann Dubreuil ydu at miscrit.be
Fri Sep 7 23:44:06 AEST 2001


Hi,


===================================================================


Estimated hours taken: 2
User: ydu
Branches: main


library/io.m:
	Implementation of io__putback*, io__clearerr, io__ferror, 
	io__stream_file_size in C#

Index: io.m
===================================================================
RCS file: /home/mercury1/repository/mercury/library/io.m,v
retrieving revision 1.230
diff -u -r1.230 io.m
--- io.m	14 Aug 2001 10:18:45 -0000	1.230
+++ io.m	7 Sep 2001 13:38:58 -0000
@@ -1553,10 +1553,13 @@
 	}
 }").
 
-:- pragma foreign_proc("MC++", io__clear_err(_Stream::in, _IO0::di, _IO::uo),
+:- pragma foreign_proc("MC++", io__clear_err(_Stream::in, IO0::di, IO::uo),
 		[will_not_call_mercury, thread_safe],
 "{
-	mercury::runtime::Errors::SORRY(""foreign code for this function"");
+	// XXX no error flag to reset as in MC++ an error throws directly an
+	// exception (we should create an error indicator in MF_Mercury_file
+	// for compatibility)
+	update_io(IO0, IO);
 }").
 
 
@@ -1591,11 +1594,13 @@
 		MR_PROC_LABEL, RetStr);
 }").
 
-:- pragma foreign_proc("MC++", ferror(_Stream::in, _RetVal::out, _RetStr::out,
-		_IO0::di, _IO::uo),
+:- pragma foreign_proc("MC++", ferror(Stream::in, RetVal::out, RetStr::out,
+		IO0::di, IO::uo),
 		[will_not_call_mercury, thread_safe],
 "{
-	mercury::runtime::Errors::SORRY(""foreign code for this function"");
+	// XXX see clearerr
+	RetVal = 0;
+	update_io(IO0, IO);
 }").
 
 
@@ -1663,11 +1668,18 @@
 #endif
 }").
 
-:- pragma foreign_proc("MC++", io__stream_file_size(_Stream::in, _Size::out,
-		_IO0::di, _IO::uo),
+:- pragma foreign_proc("MC++", io__stream_file_size(Stream::in, Size::out,
+		IO0::di, IO::uo),
 		[will_not_call_mercury, thread_safe],
 "{
-	mercury::runtime::Errors::SORRY(""foreign code for this function"");
+	MR_MercuryFile mf = ML_DownCast(MR_MercuryFile, 
+		MR_word_to_c_pointer(Stream));
+	if (mf->stream->get_CanSeek()) {
+		Size = mf->stream->get_Length();
+	} else {
+	       Size = -1;
+	}
+	update_io(IO0, IO);
 }").
 
 io__file_modification_time(File, Result) -->
@@ -3197,19 +3209,28 @@
 {
         MR_MercuryFile mf = new MR_MercuryFileStruct();
         System::IO::FileMode fa;
-        System::IO::Stream *stream;
+        System::IO::Stream *stream = 0;
 
                 // XXX get this right...
-        if (type == ""r"") {
+        if (System::String::op_Equality(type, ""r"")) {
                 fa = System::IO::FileMode::Open;
-        } else if (type == ""w"") {
+        } else if (System::String::op_Equality(type, ""a"")) {
                 fa = System::IO::FileMode::Append;
-        } else {
-		mercury::runtime::Errors::SORRY(
-			""foreign code for this function"");
-                // fa = System::IO::FileMode::OpenOrCreate;
+        } else if (System::String::op_Equality(type, ""w"")) {
+		fa = System::IO::FileMode::Truncate;
+	} else {
+		MR_String msg;
+		msg = System::String::Concat(
+			""foreign code for this function, open type:"",
+			type);
+		mercury::runtime::Errors::SORRY(msg);
+
+                // fa = XXX;
         }
-        stream = System::IO::File::Open(filename, fa);
+
+	try {
+		stream = System::IO::File::Open(filename, fa);
+	} catch (System::IO::IOException* e) {}
 
         if (!stream) {
                 return 0;
@@ -3587,16 +3608,25 @@
 ").
 
 :- pragma foreign_proc("MC++", 
-	io__putback_char(_File::in, _Character::in, IO0::di, IO::uo),
+	io__putback_char(File::in, Character::in, IO0::di, IO::uo),
 		may_call_mercury, "{
-	mercury::runtime::Errors::SORRY(""foreign code for this function"");
+
+	MR_MercuryFile mf = ML_DownCast(MR_MercuryFile,
+		MR_word_to_c_pointer(File));
+	if (Character == '\\n') {
+		mf->line_number--;
+	}
+	mf->stream->Seek(-1, System::IO::SeekOrigin::Current);
 	update_io(IO0, IO);
 }").
 
 :- pragma foreign_proc("MC++",
-	io__putback_byte(_File::in, _Character::in, IO0::di, IO::uo),
+	io__putback_byte(File::in, Character::in, IO0::di, IO::uo),
 		may_call_mercury, "{
-	mercury::runtime::Errors::SORRY(""foreign code for this function"");
+
+	MR_MercuryFile mf = ML_DownCast(MR_MercuryFile, 
+		MR_word_to_c_pointer(File));
+	mf->stream->Seek(-1, System::IO::SeekOrigin::Current);
 	update_io(IO0, IO);
 }").
 


--------------------------------------------------------------------------
mercury-reviews mailing list
post:  mercury-reviews at cs.mu.oz.au
administrative address: owner-mercury-reviews at cs.mu.oz.au
unsubscribe: Address: mercury-reviews-request at cs.mu.oz.au Message: unsubscribe
subscribe:   Address: mercury-reviews-request at cs.mu.oz.au Message: subscribe
--------------------------------------------------------------------------



More information about the reviews mailing list