[m-rev.] diff: fix mercury_open(filename, "w") on IL backend

Peter Ross peter.ross at miscrit.be
Sat Dec 15 02:50:57 AEDT 2001


Hi,


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


Estimated hours taken: 1
Branches: main

library/io.m:
    Get mercury_open() working on the IL backend when we trying to open
    a file for writing and that file doesn't exist.

Index: io.m
===================================================================
RCS file: /home/mercury1/repository/mercury/library/io.m,v
retrieving revision 1.235
diff -u -r1.235 io.m
--- io.m	8 Nov 2001 15:30:36 -0000	1.235
+++ io.m	14 Dec 2001 15:48:46 -0000
@@ -3205,26 +3205,34 @@
         System::IO::FileMode fa;
         System::IO::Stream *stream = 0;
 
-                // XXX get this right...
-        if (System::String::op_Equality(type, ""r"")) {
-                fa = System::IO::FileMode::Open;
-        } else if (System::String::op_Equality(type, ""a"")) {
-                fa = System::IO::FileMode::Append;
-        } 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);
+	try {
+			// XXX get this right...
+		if (System::String::op_Equality(type, ""r"")) {
+			fa = System::IO::FileMode::Open;
+		} else if (System::String::op_Equality(type, ""a"")) {
+			fa = System::IO::FileMode::Append;
+		} 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;
-        }
+			// fa = XXX;
+		}
 
-	try {
-		stream = System::IO::File::Open(filename, fa);
-	} catch (System::IO::IOException* e) {}
+		if (fa == System::IO::FileMode::Truncate &&
+				!System::IO::File::Exists(filename))
+		{
+			stream = System::IO::File::Create(filename);
+		} else {
+			stream = System::IO::File::Open(filename, fa);
+		}
+	} catch (System::IO::IOException* e) {
+		System::Console::WriteLine(e->Message);
+	}
 
         if (!stream) {
                 return 0;

--------------------------------------------------------------------------
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