Skip to content

Commit fbe01e8

Browse files
i-n-g-oneilcsmith-net
authored andcommitted
add Utility Functions, GlibUtils and GlibUtilsTest (#106)
* add Utility Functions, GlubUtils and GlibUtilsTest * rename to GLib and GLibTest
1 parent 90b1586 commit fbe01e8

File tree

3 files changed

+121
-0
lines changed

3 files changed

+121
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* This file is part of gstreamer-java.
3+
*
4+
* gstreamer-java is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU Lesser General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* gstreamer-java is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU Lesser General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU Lesser General Public License
15+
* along with gstreamer-java. If not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
package org.freedesktop.gstreamer.glib;
18+
19+
import org.freedesktop.gstreamer.lowlevel.GlibAPI;
20+
21+
public class GLib {
22+
23+
public static boolean setEnv(String variable, final String value, boolean overwrite) {
24+
return GlibAPI.GLIB_API.g_setenv(variable, value, overwrite);
25+
}
26+
27+
public static String getEnv(String variable) {
28+
return GlibAPI.GLIB_API.g_getenv(variable);
29+
}
30+
31+
public static void unsetEnv(String variable) {
32+
GlibAPI.GLIB_API.g_unsetenv(variable);
33+
}
34+
}

src/org/freedesktop/gstreamer/lowlevel/GlibAPI.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,4 +176,45 @@ protected List<String> getFieldOrder() {
176176
});
177177
}
178178
}
179+
180+
/*
181+
* Miscellaneous Utility Functions
182+
*/
183+
184+
/**
185+
* Returns the value of an environment variable.
186+
*
187+
* On UNIX, the name and value are byte strings which might or might not be
188+
* in some consistent character set and encoding. On Windows, they are in UTF-8.
189+
* On Windows, in case the environment variable's value contains references
190+
* to other environment variables, they are expanded.
191+
*
192+
* @param variable the environment variable to get.
193+
* @return the value of the environment variable, or NULL if the
194+
* environment variable is not found.
195+
*/
196+
String g_getenv(String variable);
197+
198+
199+
/**
200+
* Sets an environment variable.
201+
*
202+
* On UNIX, both the variable's name and value can be arbitrary byte strings,
203+
* except that the variable's name cannot contain '='.
204+
* On Windows, they should be in UTF-8.
205+
*
206+
* @param variable the environment variable to set, must not contain '='.
207+
* @param value the value for to set the variable to.
208+
* @param overwrite whether to change the variable if it already exists.
209+
* @return FALSE if the environment variable couldn't be set.
210+
*/
211+
boolean g_setenv(String variable, String value, boolean overwrite);
212+
213+
214+
/**
215+
* Removes an environment variable from the environment.
216+
*
217+
* @param variable the environment variable to remove, must not contain '='.
218+
*/
219+
void g_unsetenv(String variable);
179220
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* This file is part of gstreamer-java.
3+
*
4+
* gstreamer-java is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU Lesser General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* gstreamer-java is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU Lesser General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU Lesser General Public License
15+
* along with gstreamer-java. If not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
package org.freedesktop.gstreamer;
18+
19+
import org.freedesktop.gstreamer.glib.GLib;
20+
import static org.junit.Assert.assertEquals;
21+
import org.junit.Test;
22+
23+
public class GLibTest {
24+
25+
@Test
26+
public void getEnv() {
27+
String user = GLib.getEnv("USER");
28+
String pwd = GLib.getEnv("PWD");
29+
System.out.println("user: " + user);
30+
System.out.println("path: " + pwd);
31+
}
32+
33+
@Test
34+
public void setUnsetEnv() {
35+
36+
// set environment
37+
GLib.setEnv("TESTVAR", "foo", true);
38+
39+
// get environment
40+
assertEquals("could not set TESTVAR!", GLib.getEnv("TESTVAR"), "foo");
41+
42+
// unset
43+
GLib.unsetEnv("TESTVAR");
44+
assertEquals("could not unset TESTVAR!", GLib.getEnv("TESTVAR"), null);
45+
}
46+
}

0 commit comments

Comments
 (0)