_g_getenv_nomalloc()
- From: Tim Janik <timj gtk org>
- To: Tor Lillqvist <tml iki fi>
- Cc: Gtk+ Developers <gtk-devel-list gnome org>, Hans Breuer <hans breuer org>
- Subject: _g_getenv_nomalloc()
- Date: Mon, 23 Jan 2006 17:13:28 +0100 (CET)
hi tml,
as discussed on irc, i've added _g_getenv_nomalloc(), please review the
windows branch. i've modelled it after g_getenv(), but can't test-compile,
let alone test it.
cvs server: Diffing .
Index: gutils.c
===================================================================
RCS file: /cvs/gnome/glib/glib/gutils.c,v
retrieving revision 1.186
diff -u -p -r1.186 gutils.c
--- gutils.c 30 Dec 2005 02:08:02 -0000 1.186
+++ gutils.c 23 Jan 2006 14:44:30 -0000
@@ -1155,6 +1155,64 @@ g_getenv (const gchar *variable)
#endif /* G_OS_WIN32 */
}
+/* _g_getenv_nomalloc
+ * this function does a getenv() without doing any kind of allocation
+ * through glib. it's suitable for chars <= 127 only (both, for the
+ * variable name and the contents) and for contents < 1024 chars in
+ * length. also, it aliases "" to a NULL return value.
+ **/
+const gchar*
+_g_getenv_nomalloc (const gchar *variable,
+ gchar buffer[1024])
+{
+#ifndef G_OS_WIN32
+ const gchar *retval = getenv (variable);
+ if (retval && retval[0])
+ {
+ gint l = strlen (retval);
+ if (l < 1024)
+ {
+ strncpy (buffer, retval, l);
+ buffer[l] = 0;
+ return buffer;
+ }
+ }
+ return NULL;
+#else /* G_OS_WIN32 */
+ if (G_WIN32_HAVE_WIDECHAR_API ())
+ {
+ /* convert variable name to wchar_t without malloc */
+ wchar_t wname[256], result[1024];
+ gint i, l = strlen (variable);
+ if (l >= 256)
+ return NULL;
+ for (i = 0; variable[i]; i++)
+ wname[i] = variable[i];
+ wname[i] = 0;
+ l = GetEnvironmentVariableW (wname, result, 1024);
+ if (l > 0 && l < 1024 && result[0])
+ {
+ /* convert variable contents from wchar_t without malloc */
+ for (i = 0; i < l; i++)
+ buffer[i] = result[i];
+ buffer[i] = 0;
+ return buffer;
+ }
+ return NULL;
+ }
+ else
+ {
+ gint l = GetEnvironmentVariableW (variable, buffer, 1024);
+ if (l > 0 && l < 1024 && buffer[0])
+ {
+ buffer[l] = 0;
+ return buffer;
+ }
+ return NULL;
+ }
+#endif /* G_OS_WIN32 */
+}
+
/**
* g_setenv:
* @variable: the environment variable to set, must not contain '='.
Index: gutils.h
===================================================================
RCS file: /cvs/gnome/glib/glib/gutils.h,v
retrieving revision 1.38
diff -u -p -r1.38 gutils.h
--- gutils.h 31 Aug 2005 21:02:47 -0000 1.38
+++ gutils.h 23 Jan 2006 14:44:30 -0000
@@ -211,6 +211,8 @@ gboolean g_setenv
gboolean overwrite);
void g_unsetenv (const gchar *variable);
gchar** g_listenv (void);
+const gchar* _g_getenv_nomalloc (const gchar *variable,
+ gchar buffer[1024]);
/* we try to provide a usefull equivalent for ATEXIT if it is
* not defined, but use is actually abandoned. people should
---
ciaoTJ
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]