[m-rev.] diff: io.setenv for C#

Peter Wang novalazy at gmail.com
Fri Oct 22 14:12:30 AEDT 2010


Branches: main

library/io.m:
        Implement `io.setenv' for C#.

diff --git a/library/io.m b/library/io.m
index f6f244a..b972e31 100644
--- a/library/io.m
+++ b/library/io.m
@@ -9833,8 +9833,7 @@ io.setenv(Var, Value) :-
 
     % io.putenv(VarString): If VarString is a string of the form "name=value",
     % sets the environment variable name to the specified value. Fails if
-    % the operation does not work. Not supported for .NET. This should only be
-    % called from io.setenv.
+    % the operation does not work. This should only be called from io.setenv.
     %
 :- impure pred io.putenv(string::in) is semidet.
 
@@ -9847,18 +9846,26 @@ io.setenv(Var, Value) :-
 ").
 
 :- pragma foreign_proc("C#",
+    io.setenv(Var::in, Value::in),
+    [will_not_call_mercury, tabled_for_io],
+"
+    try {
+        System.Environment.SetEnvironmentVariable(Var, Value);
+        SUCCESS_INDICATOR = true;
+    } catch (System.Exception) {
+        SUCCESS_INDICATOR = false;
+    }
+").
+
+:- pragma foreign_proc("C#",
     io.putenv(VarAndValue::in),
     [will_not_call_mercury, tabled_for_io],
 "
-    /*
-    ** Unfortunately there is no API in the .NET standard library for setting
-    ** environment variables. So we need to use platform-specific methods.
-    ** Currently we use the Posix function putenv(), which is also supported
-    ** on Windows.
-    */
-    // XXX C# todo
-    // SUCCESS_INDICATOR = (runtime.PInvoke._putenv(VarAndValue) == 0);
-    SUCCESS_INDICATOR = false;
+    // This procedure should never be called, as io.setenv/2 has been
+    // implemented directly for C#.
+    // This implementation is included only to suppress warnings.
+
+    throw new System.Exception(""io.putenv/1 not implemented for C#"");
 ").
 
 :- pragma foreign_proc("Java",

--------------------------------------------------------------------------
mercury-reviews mailing list
Post messages to:       mercury-reviews at csse.unimelb.edu.au
Administrative Queries: owner-mercury-reviews at csse.unimelb.edu.au
Subscriptions:          mercury-reviews-request at csse.unimelb.edu.au
--------------------------------------------------------------------------



More information about the reviews mailing list