[m-rev.] diff: .NET back-end: fix mercury_open()

Fergus Henderson fjh at cs.mu.OZ.AU
Sun Feb 16 09:38:30 AEDT 2003


Estimated hours taken: 2
Branches: main

library/io.m:
	Fix a bug in the MC++ code for mercury_open():
	use the appropriate FileAccess when opening files,
	rather than always opening them in read-write mode.
	This is necessary to ensure that we can open read-only files.
	Also, for compatibility with Unix, allow shared read/write
	access by other processes.

Workspace: /home/fjh/ws/hermes
Index: library/io.m
===================================================================
RCS file: /home/mercury1/repository/mercury/library/io.m,v
retrieving revision 1.290
diff -u -d -r1.290 io.m
--- library/io.m	14 Feb 2003 17:22:20 -0000	1.290
+++ library/io.m	15 Feb 2003 22:37:50 -0000
@@ -3773,35 +3773,40 @@
 static mercury_open(MR_String filename, MR_String openmode,
 	ML_file_encoding_kind file_encoding)
 {
-        MR_MercuryFile mf = new MR_MercuryFileStruct();
-        System::IO::FileMode fa;
+        System::IO::FileMode mode;
+        System::IO::FileAccess access;
+        System::IO::FileShare share;
         System::IO::Stream *stream = 0;
 
 	try {
-			// XXX get this right...
 		if (System::String::op_Equality(openmode, ""r"")) {
-			fa = System::IO::FileMode::Open;
-		} else if (System::String::op_Equality(openmode, ""a"")) {
-			fa = System::IO::FileMode::Append;
+			// Like '<' in Bourne shell.
+			// Read a file.  The file must exist already.
+			mode   = System::IO::FileMode::Open;
+			access = System::IO::FileAccess::Read;
 		} else if (System::String::op_Equality(openmode, ""w"")) {
-			fa = System::IO::FileMode::Truncate;
+			// Like '>' in Bourne shell.
+			// Overwrite an existing file, or create a new file.
+			mode   = System::IO::FileMode::Create;
+			access = System::IO::FileAccess::Write;
+		} else if (System::String::op_Equality(openmode, ""a"")) {
+			// Like '>>' in Bourne shell.
+			// Append to an existing file, or create a new file.
+			mode   = System::IO::FileMode::Append;
+			access = System::IO::FileAccess::Write;
 		} else {
-			MR_String msg;
-			msg = System::String::Concat(
+			mercury::runtime::Errors::SORRY(System::String::Concat(
 				""foreign code for this function, open mode:"",
-				openmode);
-			mercury::runtime::Errors::SORRY(msg);
-
-			// fa = XXX;
+				openmode));
 		}
 
-		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);
-		}
+		// For Unix compatibility, we allow files
+		// to be read or written by multiple processes
+		// simultaneously.  XXX Is this a good idea?
+		share = System::IO::FileShare::ReadWrite;
+
+		stream = System::IO::File::Open(filename, mode, access, share);
+
 	} catch (System::IO::IOException* e) {
 		MR_io_exception = e;
 	}
@@ -3809,12 +3814,11 @@
         if (!stream) {
                 return 0;
         } else {
-		stream = new System::IO::BufferedStream(stream);
-		
 		// we initialize the `reader' and `writer' fields to null;
 		// they will be filled in later if they are needed.
-                mf = mercury_file_init(stream, NULL, NULL, file_encoding);
-                return mf;
+                return mercury_file_init(
+			new System::IO::BufferedStream(stream),
+			NULL, NULL, file_encoding);
         }
 }
 

-- 
Fergus Henderson <fjh at cs.mu.oz.au>  |  "I have always known that the pursuit
The University of Melbourne         |  of excellence is a lethal habit"
WWW: <http://www.cs.mu.oz.au/~fjh>  |     -- the last words of T. S. Garp.
--------------------------------------------------------------------------
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