lib/environ: more documentation and nitunit tests
[nit.git] / lib / standard / environ.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2006 Floréal Morandat <morandat@lirmm.fr>
4 # Copyright 2008 Jean Privat <jean@pryen.org>
5 #
6 # This file is free software, which comes along with NIT. This software is
7 # distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
8 # without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
9 # PARTICULAR PURPOSE. You can modify it is you want, provided this header
10 # is kept unaltered, and a notification of the changes is added.
11 # You are allowed to redistribute it and sell it, alone or is a part of
12 # another product.
13
14 # Access to the environment variables of the process
15 module environ
16
17 import string
18
19 # TODO prevoir une structure pour recup tout un environ, le modifier et le passer a process
20
21 redef class String
22 # Return environment value for the symbol.
23 # If there is no such environment variable, then return ""
24 #
25 # assert "PATH".environ != ""
26 # assert "NIT %\n".environ == ""
27 fun environ: String
28 do
29 var res = self.to_cstring.get_environ
30 # FIXME: There is no proper way to handle NULL C string yet. What a pitty.
31 var nulstr = once ("".to_cstring.get_environ)
32 if res != nulstr then
33 return res.to_s
34 else
35 return ""
36 end
37 end
38
39 # Set the enviroment value of a variable.
40 #
41 # "NITis".setenv("fun")
42 # assert "NITis".environ == "fun"
43 fun setenv(v: String) do to_cstring.setenv( v.to_cstring )
44 end
45
46 redef class NativeString
47 private fun get_environ: NativeString is extern "string_NativeString_NativeString_get_environ_0"
48 private fun setenv( v : NativeString ) is extern "string_NativeString_NativeString_setenv_1"
49 end