2008-10-10 Miguel de Icaza <miguel@novell.com>
authorMiguel de Icaza <miguel@gnome.org>
Fri, 10 Oct 2008 22:47:47 +0000 (22:47 -0000)
committerMiguel de Icaza <miguel@gnome.org>
Fri, 10 Oct 2008 22:47:47 +0000 (22:47 -0000)
* Split functionality that is operating system specific into
-win32.c, -unix.c and -posix.c

* Use g_malloc everywhere, and g_free, so that these can be easily
overwritten consistently.

svn path=/trunk/mono/; revision=115497

17 files changed:
eglib/ChangeLog
eglib/configure.ac
eglib/src/Makefile.am
eglib/src/gdate-unix.c [new file with mode: 0644]
eglib/src/gdate-win32.c [new file with mode: 0644]
eglib/src/gdate.c [deleted file]
eglib/src/gdir-unix.c [new file with mode: 0644]
eglib/src/gdir-win32.c [new file with mode: 0644]
eglib/src/gfile-posix.c [new file with mode: 0644]
eglib/src/gfile-unix.c [new file with mode: 0644]
eglib/src/gfile-win32.c [new file with mode: 0644]
eglib/src/gfile.c
eglib/src/gmarkup.c
eglib/src/gpath.c
eglib/src/gstr.c
eglib/src/gstring.c
eglib/src/vasprintf.c

index 03fea0be0c07a58539df5bd4c39d6984eb2dd657..bef4d9cf6e4790f2f698aff8964f3e831daf0d5f 100644 (file)
@@ -1,3 +1,11 @@
+2008-10-10  Miguel de Icaza  <miguel@novell.com>
+
+       * Split functionality that is operating system specific into
+       -win32.c, -unix.c and -posix.c 
+
+       * Use g_malloc everywhere, and g_free, so that these can be easily
+       overwritten consistently.
+
 2008-09-16  Bill Holmes  <billholmes54@gmail.com>
 
        * src/gunicode.c : Fix a warning.
index 75f201a583972c91eb29312cb3401cb682ee8f23..325a4fbf056b5f8cc818f1e8c2df4ff5021a9e96 100644 (file)
@@ -2,7 +2,7 @@ AC_INIT(README)
 AC_CANONICAL_SYSTEM
 
 AM_CONFIG_HEADER(config.h)
-AM_INIT_AUTOMAKE(eglib,0.1)
+AM_INIT_AUTOMAKE(eglib,0.2)
 AM_MAINTAINER_MODE
 
 AC_PROG_CC
index b26b78f15440295c315dc0ae3c317dc5e9765fb4..586f6007307889e9acd4894ea7c411ddca07003c 100644 (file)
@@ -1,5 +1,14 @@
 noinst_LTLIBRARIES = libeglib.la
 
+win_files  = gdate-win32.c gdir-win32.c gfile-win32.c
+unix_files = gdate-unix.c  gdir-unix.c  gfile-unix.c
+
+if PLATFORM_WIN32
+os_files = $(win_files)
+else
+os_files = $(unix_files)
+endif
+
 libeglib_la_SOURCES = \
        eglib-config.h  \
        sort.frag.h     \
@@ -22,13 +31,14 @@ libeglib_la_SOURCES = \
        gshell.c        \
        gspawn.c        \
        gtimer.c        \
-       gdate.c         \
        gfile.c         \
+       gfile-posix.c   \
        gpattern.c      \
        gdir.c          \
        gmarkup.c       \
        gutf8.c         \
-       gunicode.c
+       gunicode.c      \
+       $(os_files)
 
 libeglib_la_CFLAGS = -Wall -D_FORTIFY_SOURCE=2
 
@@ -42,4 +52,4 @@ endif
 
 MAINTAINERCLEANFILES = Makefile.in
 
-EXTRA_DIST = eglib-config.h.in
+EXTRA_DIST = eglib-config.h.in $(win_files) $(unix_files)
diff --git a/eglib/src/gdate-unix.c b/eglib/src/gdate-unix.c
new file mode 100644 (file)
index 0000000..9b0e084
--- /dev/null
@@ -0,0 +1,43 @@
+/*
+ * gdate-unix.c: Date and time utility functions.
+ *
+ * Author:
+ *   Gonzalo Paniagua Javier (gonzalo@novell.com
+ *
+ * (C) 2006 Novell, Inc.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+ * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+ * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+#include <stdio.h>
+#include <glib.h>
+
+#include <sys/time.h>
+
+void
+g_get_current_time (GTimeVal *result)
+{
+       g_return_if_fail (result != NULL);
+       struct timeval tv;
+
+       g_return_if_fail (result != NULL);
+       gettimeofday (&tv, NULL);
+       result->tv_sec = tv.tv_sec;
+       result->tv_usec = tv.tv_usec;
+}
diff --git a/eglib/src/gdate-win32.c b/eglib/src/gdate-win32.c
new file mode 100644 (file)
index 0000000..40efee9
--- /dev/null
@@ -0,0 +1,43 @@
+/*
+ * gdate-win32.c: Date and time utility functions.
+ *
+ * Author:
+ *   Gonzalo Paniagua Javier (gonzalo@novell.com
+ *
+ * (C) 2006 Novell, Inc.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+ * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+ * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+#include <stdio.h>
+#include <glib.h>
+
+#include <winsock2.h>
+
+void
+g_get_current_time (GTimeVal *result)
+{
+       g_return_if_fail (result != NULL);
+       long int l = GetTickCount();
+
+       g_return_if_fail (result != NULL);
+
+       result->tv_sec = l / 1000;
+       result->tv_usec = (l % 1000) * 1000;
+}
diff --git a/eglib/src/gdate.c b/eglib/src/gdate.c
deleted file mode 100644 (file)
index 82d63b1..0000000
+++ /dev/null
@@ -1,55 +0,0 @@
-/*
- * Date and time utility functions.
- *
- * Author:
- *   Gonzalo Paniagua Javier (gonzalo@novell.com
- *
- * (C) 2006 Novell, Inc.
- *
- * Permission is hereby granted, free of charge, to any person obtaining
- * a copy of this software and associated documentation files (the
- * "Software"), to deal in the Software without restriction, including
- * without limitation the rights to use, copy, modify, merge, publish,
- * distribute, sublicense, and/or sell copies of the Software, and to
- * permit persons to whom the Software is furnished to do so, subject to
- * the following conditions:
- *
- * The above copyright notice and this permission notice shall be
- * included in all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
- * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
- * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
- * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
- * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
- * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
- * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- */
-#include <stdio.h>
-#include <glib.h>
-
-#ifdef G_OS_WIN32
-#include <winsock2.h>
-#else
-#include <sys/time.h>
-#endif
-
-void
-g_get_current_time (GTimeVal *result)
-{
-#ifdef G_OS_WIN32
-       long int l = GetTickCount();
-
-       g_return_if_fail (result != NULL);
-
-       result->tv_sec = l / 1000;
-       result->tv_usec = (l % 1000) * 1000;
-#else
-       struct timeval tv;
-
-       g_return_if_fail (result != NULL);
-       gettimeofday (&tv, NULL);
-       result->tv_sec = tv.tv_sec;
-       result->tv_usec = tv.tv_usec;
-#endif
-}
diff --git a/eglib/src/gdir-unix.c b/eglib/src/gdir-unix.c
new file mode 100644 (file)
index 0000000..aa375d8
--- /dev/null
@@ -0,0 +1,94 @@
+/*
+ * Directory utility functions.
+ *
+ * Author:
+ *   Gonzalo Paniagua Javier (gonzalo@novell.com)
+ *
+ * (C) 2006 Novell, Inc.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+ * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+ * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+#include <glib.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <errno.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <unistd.h>
+#include <dirent.h>
+
+struct _GDir {
+       DIR *dir;
+};
+
+GDir *
+g_dir_open (const gchar *path, guint flags, GError **error)
+{
+       GDir *dir;
+
+       g_return_val_if_fail (path != NULL, NULL);
+       g_return_val_if_fail (error == NULL || *error == NULL, NULL);
+
+       (void) flags; /* this is not used */
+       dir = g_new (GDir, 1);
+       dir->dir = opendir (path);
+       if (dir->dir == NULL) {
+               if (error) {
+                       gint err = errno;
+                       *error = g_error_new (G_LOG_DOMAIN, g_file_error_from_errno (err), strerror (err));
+               }
+               g_free (dir);
+               return NULL;
+       }
+       return dir;
+}
+
+const gchar *
+g_dir_read_name (GDir *dir)
+{
+       struct dirent *entry;
+
+       g_return_val_if_fail (dir != NULL && dir->dir != NULL, NULL);
+       do {
+               entry = readdir (dir->dir);
+               if (entry == NULL)
+                       return NULL;
+       } while ((strcmp (entry->d_name, ".") == 0) || (strcmp (entry->d_name, "..") == 0));
+
+       return entry->d_name;
+}
+
+void
+g_dir_rewind (GDir *dir)
+{
+       g_return_if_fail (dir != NULL && dir->dir != NULL);
+       rewinddir (dir->dir);
+}
+
+void
+g_dir_close (GDir *dir)
+{
+       g_return_if_fail (dir != NULL && dir->dir != 0);
+       closedir (dir->dir);
+       dir->dir = NULL;
+       g_free (dir);
+}
+
+
diff --git a/eglib/src/gdir-win32.c b/eglib/src/gdir-win32.c
new file mode 100644 (file)
index 0000000..839f0b1
--- /dev/null
@@ -0,0 +1,147 @@
+/*
+ * Directory utility functions.
+ *
+ * Author:
+ *   Gonzalo Paniagua Javier (gonzalo@novell.com)
+ *
+ * (C) 2006 Novell, Inc.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+ * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+ * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+#include <glib.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <errno.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <io.h>
+
+#include <winsock2.h>
+
+struct _GDir {
+       HANDLE handle;
+       gchar* current;
+       gchar* next;
+};
+
+GDir *
+g_dir_open (const gchar *path, guint flags, GError **error)
+{
+       GDir *dir;
+       gunichar2* path_utf16;
+       gunichar2* path_utf16_search;
+       WIN32_FIND_DATA find_data;
+
+       g_return_val_if_fail (path != NULL, NULL);
+       g_return_val_if_fail (error == NULL || *error == NULL, NULL);
+       dir = g_new0 (GDir, 1);
+
+       path_utf16 = u8to16 (path);
+
+       dir->handle = FindFirstFile (path_utf16, &find_data);
+       if (dir->handle == INVALID_HANDLE_VALUE) {
+               if (error) {
+                       gint err = errno;
+                       *error = g_error_new (G_LOG_DOMAIN, g_file_error_from_errno (err), strerror (err));
+               }
+               g_free (dir);
+               g_free (path_utf16);
+               return NULL;
+       }
+
+       /* now get files */
+       FindClose (dir->handle);
+       path_utf16_search = g_malloc ((wcslen(path_utf16) + 3)*sizeof(gunichar2));
+       wcscpy (path_utf16_search, path_utf16);
+       wcscat (path_utf16_search, L"\\*");
+
+       dir->handle = FindFirstFile (path_utf16_search, &find_data);
+       g_free (path_utf16_search);
+
+       while ((wcscmp (find_data.cFileName, L".") == 0) || (wcscmp (find_data.cFileName, L"..") == 0)) {
+               if (!FindNextFile (dir->handle, &find_data)) {
+                       if (error) {
+                               gint err = errno;
+                               *error = g_error_new (G_LOG_DOMAIN, g_file_error_from_errno (err), strerror (err));
+                       }
+                       g_free (dir);
+                       g_free (path_utf16);
+                       return NULL;
+               }
+       }
+
+       dir->current = NULL;
+       dir->next = u16to8 (find_data.cFileName);
+
+       g_free (path_utf16);
+       return dir;
+}
+
+const gchar *
+g_dir_read_name (GDir *dir)
+{
+       WIN32_FIND_DATA find_data;
+
+       g_return_val_if_fail (dir != NULL && dir->handle != 0, NULL);
+
+       if (dir->current)
+               g_free (dir->current);
+       dir->current = NULL;
+
+       dir->current = dir->next;
+
+       if (!dir->current)
+               return NULL;
+
+       dir->next = NULL;
+
+       do {
+               if (!FindNextFile (dir->handle, &find_data)) {
+                       dir->next = NULL;
+                       return dir->current;
+               }
+       } while ((wcscmp (find_data.cFileName, L".") == 0) || (wcscmp (find_data.cFileName, L"..") == 0));
+
+       dir->next = u16to8 (find_data.cFileName);
+       return dir->current;
+}
+
+void
+g_dir_rewind (GDir *dir)
+{
+}
+
+void
+g_dir_close (GDir *dir)
+{
+       g_return_if_fail (dir != NULL && dir->handle != 0);
+       
+       if (dir->current)
+               g_free (dir->current);
+       dir->current = NULL;
+       if (dir->next)
+               g_free (dir->next);
+       dir->next = NULL;
+       FindClose (dir->handle);
+       dir->handle = 0;
+       g_free (dir);
+}
+
+
diff --git a/eglib/src/gfile-posix.c b/eglib/src/gfile-posix.c
new file mode 100644 (file)
index 0000000..c1c481a
--- /dev/null
@@ -0,0 +1,142 @@
+/*
+ * File utility functions.
+ *
+ * Author:
+ *   Gonzalo Paniagua Javier (gonzalo@novell.com)
+ *
+ * (C) 2006 Novell, Inc.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+ * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+ * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+#include <config.h>
+#include <glib.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+
+#ifndef O_LARGEFILE
+#define OPEN_FLAGS (O_RDONLY)
+#else
+#define OPEN_FLAGS (O_RDONLY | O_LARGEFILE)
+#endif
+gboolean
+g_file_get_contents (const gchar *filename, gchar **contents, gsize *length, GError **error)
+{
+       gchar *str;
+       int fd;
+       struct stat st;
+       long offset;
+       int nread;
+
+       g_return_val_if_fail (filename != NULL, FALSE);
+       g_return_val_if_fail (contents != NULL, FALSE);
+       g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
+
+       *contents = NULL;
+       if (length)
+               *length = 0;
+
+       fd = open (filename, OPEN_FLAGS);
+       if (fd == -1) {
+               if (error != NULL) {
+                       int err = errno;
+                       *error = g_error_new (G_LOG_DOMAIN, g_file_error_from_errno (err), "Error opening file");
+               }
+               return FALSE;
+       }
+
+       if (fstat (fd, &st) != 0) {
+               if (error != NULL) {
+                       int err = errno;
+                       *error = g_error_new (G_LOG_DOMAIN, g_file_error_from_errno (err), "Error in fstat()");
+               }
+               close (fd);
+               return FALSE;
+       }
+
+       str = g_malloc (st.st_size + 1);
+       offset = 0;
+       do {
+               nread = read (fd, str + offset, st.st_size - offset);
+               if (nread > 0) {
+                       offset += nread;
+               }
+       } while ((nread > 0 && offset < st.st_size) || (nread == -1 && errno == EINTR));
+
+       close (fd);
+       str [st.st_size] = '\0';
+       if (length) {
+               *length = st.st_size;
+       }
+       *contents = str;
+       return TRUE;
+}
+
+gint
+g_file_open_tmp (const gchar *tmpl, gchar **name_used, GError **error)
+{
+       const static gchar *default_tmpl = ".XXXXXX";
+       gchar *t;
+       gint fd;
+       size_t len;
+
+       g_return_val_if_fail (error == NULL || *error == NULL, -1);
+
+       if (tmpl == NULL)
+               tmpl = default_tmpl;
+
+       if (strchr (tmpl, G_DIR_SEPARATOR) != NULL) {
+               if (error) {
+                       *error = g_error_new (G_LOG_DOMAIN, 24, "Template should not have any " G_DIR_SEPARATOR_S);
+               }
+               return -1;
+       }
+
+       len = strlen (tmpl);
+       if (len < 6 || strcmp (tmpl + len - 6, "XXXXXX")) {
+               if (error) {
+                       *error = g_error_new (G_LOG_DOMAIN, 24, "Template should end with XXXXXX");
+               }
+               return -1;
+       }
+
+       t = g_build_filename (g_get_tmp_dir (), tmpl, NULL);
+
+       fd = mkstemp (t);
+
+       if (fd == -1) {
+               if (error) {
+                       int err = errno;
+                       *error = g_error_new (G_LOG_DOMAIN, g_file_error_from_errno (err), "Error in mkstemp()");
+               }
+               g_free (t);
+               return -1;
+       }
+
+       if (name_used) {
+               *name_used = t;
+       } else {
+               g_free (t);
+       }
+       return fd;
+}
diff --git a/eglib/src/gfile-unix.c b/eglib/src/gfile-unix.c
new file mode 100644 (file)
index 0000000..52f1399
--- /dev/null
@@ -0,0 +1,84 @@
+/*
+ * File utility functions.
+ *
+ * Author:
+ *   Gonzalo Paniagua Javier (gonzalo@novell.com)
+ *
+ * (C) 2006 Novell, Inc.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+ * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+ * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+#include <config.h>
+#include <glib.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <errno.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+
+#ifdef HAVE_UNISTD_H
+#include <unistd.h>
+#endif
+
+gboolean
+g_file_test (const gchar *filename, GFileTest test)
+{
+       struct stat st;
+       gboolean have_stat;
+
+       if (filename == NULL || test == 0)
+               return FALSE;
+
+       have_stat = FALSE;
+
+       if ((test & G_FILE_TEST_EXISTS) != 0) {
+               if (access (filename, F_OK) == 0)
+                       return TRUE;
+       }
+
+       if ((test & G_FILE_TEST_IS_EXECUTABLE) != 0) {
+               if (access (filename, X_OK) == 0)
+                       return TRUE;
+       }
+       if ((test & G_FILE_TEST_IS_SYMLINK) != 0) {
+               have_stat = (lstat (filename, &st) == 0);
+               if (have_stat && S_ISLNK (st.st_mode))
+                       return TRUE;
+       }
+
+       if ((test & G_FILE_TEST_IS_REGULAR) != 0) {
+               if (!have_stat)
+                       have_stat = (stat (filename, &st) == 0);
+               if (have_stat && S_ISREG (st.st_mode))
+                       return TRUE;
+       }
+       if ((test & G_FILE_TEST_IS_DIR) != 0) {
+               if (!have_stat)
+                       have_stat = (stat (filename, &st) == 0);
+               if (have_stat && S_ISDIR (st.st_mode))
+                       return TRUE;
+       }
+       return FALSE;
+}
+
+
diff --git a/eglib/src/gfile-win32.c b/eglib/src/gfile-win32.c
new file mode 100644 (file)
index 0000000..944ebe5
--- /dev/null
@@ -0,0 +1,113 @@
+/*
+ * File utility functions.
+ *
+ * Author:
+ *   Gonzalo Paniagua Javier (gonzalo@novell.com)
+ *
+ * (C) 2006 Novell, Inc.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+ * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+ * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+#include <config.h>
+#include <glib.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <errno.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+
+#ifdef G_OS_WIN32
+#include <io.h>
+#define open _open
+#define S_ISREG(x) ((x &  _S_IFMT) == _S_IFREG)
+#define S_ISDIR(x) ((x &  _S_IFMT) == _S_IFDIR)
+#endif
+
+int mkstemp (char *tmp_template)
+{
+       int fd;
+       gunichar2* utf16_template;
+
+       utf16_template  = u8to16 (tmp_template);
+
+       fd = -1;
+       utf16_template = _wmktemp( utf16_template);
+       if (utf16_template && *utf16_template) {
+               /* FIXME: _O_TEMPORARY causes file to disappear on close causing a test to fail */
+               fd = _wopen( utf16_template, _O_BINARY | _O_CREAT /*| _O_TEMPORARY*/ | _O_EXCL, _S_IREAD | _S_IWRITE);
+       }
+
+       sprintf (tmp_template + strlen (tmp_template) - 6, "%S", utf16_template + wcslen (utf16_template) - 6);
+
+       g_free (utf16_template);
+       return fd;
+}
+
+#ifdef _MSC_VER
+#pragma warning(disable:4701)
+#endif
+
+gboolean
+g_file_test (const gchar *filename, GFileTest test)
+{
+       struct _stat64 stat;
+       int ret = 0;
+       gunichar2* utf16_filename = NULL;
+
+       if (filename == NULL || test == 0)
+               return FALSE;
+
+       utf16_filename = u8to16 (filename);
+       ret = _wstati64 (utf16_filename, &stat);
+       g_free (utf16_filename);
+
+       if ((test & G_FILE_TEST_EXISTS) != 0) {
+               if (ret == 0)
+                       return TRUE;
+       }
+
+       if (ret != 0)
+               return FALSE;
+
+       if ((test & G_FILE_TEST_IS_EXECUTABLE) != 0) {
+               if (stat.st_mode & _S_IEXEC)
+                       return TRUE;
+       }
+
+       if ((test & G_FILE_TEST_IS_REGULAR) != 0) {
+               if (stat.st_mode & _S_IFREG)
+                       return TRUE;
+       }
+
+       if ((test & G_FILE_TEST_IS_DIR) != 0) {
+               if (stat.st_mode & _S_IFDIR)
+                       return TRUE;
+       }
+
+       /* make this last in case it is OR'd with something else */
+       if ((test & G_FILE_TEST_IS_SYMLINK) != 0) {
+               return FALSE;
+       }
+
+       return FALSE;
+}
index 68201e753fc9bcc7a55347d2f072424fc8467cd3..c621a3168187f904a41b78c76d357d5ada0d19a4 100644 (file)
 #include <config.h>
 #include <glib.h>
 #include <stdio.h>
-#include <stdlib.h>
 #include <errno.h>
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <fcntl.h>
-
-#ifdef G_OS_WIN32
-#include <io.h>
-#define open _open
-#define S_ISREG(x) ((x &  _S_IFMT) == _S_IFREG)
-#define S_ISDIR(x) ((x &  _S_IFMT) == _S_IFDIR)
-#endif
-
-#ifdef HAVE_UNISTD_H
-#include <unistd.h>
-#endif
 
 GFileError
 g_file_error_from_errno (gint err_no)
@@ -106,221 +91,4 @@ g_file_error_from_errno (gint err_no)
        }
 }
 
-#ifndef O_LARGEFILE
-#define OPEN_FLAGS (O_RDONLY)
-#else
-#define OPEN_FLAGS (O_RDONLY | O_LARGEFILE)
-#endif
-gboolean
-g_file_get_contents (const gchar *filename, gchar **contents, gsize *length, GError **error)
-{
-       gchar *str;
-       int fd;
-       struct stat st;
-       long offset;
-       int nread;
-
-       g_return_val_if_fail (filename != NULL, FALSE);
-       g_return_val_if_fail (contents != NULL, FALSE);
-       g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
-
-       *contents = NULL;
-       if (length)
-               *length = 0;
-
-       fd = open (filename, OPEN_FLAGS);
-       if (fd == -1) {
-               if (error != NULL) {
-                       int err = errno;
-                       *error = g_error_new (G_LOG_DOMAIN, g_file_error_from_errno (err), "Error opening file");
-               }
-               return FALSE;
-       }
-
-       if (fstat (fd, &st) != 0) {
-               if (error != NULL) {
-                       int err = errno;
-                       *error = g_error_new (G_LOG_DOMAIN, g_file_error_from_errno (err), "Error in fstat()");
-               }
-               close (fd);
-               return FALSE;
-       }
-
-       str = g_malloc (st.st_size + 1);
-       offset = 0;
-       do {
-               nread = read (fd, str + offset, st.st_size - offset);
-               if (nread > 0) {
-                       offset += nread;
-               }
-       } while ((nread > 0 && offset < st.st_size) || (nread == -1 && errno == EINTR));
-
-       close (fd);
-       str [st.st_size] = '\0';
-       if (length) {
-               *length = st.st_size;
-       }
-       *contents = str;
-       return TRUE;
-}
-
-#ifdef _MSC_VER
-int mkstemp (char *tmp_template)
-{
-       int fd;
-       gunichar2* utf16_template;
-
-       utf16_template  = u8to16 (tmp_template);
-
-       fd = -1;
-       utf16_template = _wmktemp( utf16_template);
-       if (utf16_template && *utf16_template) {
-               /* FIXME: _O_TEMPORARY causes file to disappear on close causing a test to fail */
-               fd = _wopen( utf16_template, _O_BINARY | _O_CREAT /*| _O_TEMPORARY*/ | _O_EXCL, _S_IREAD | _S_IWRITE);
-       }
-
-       sprintf (tmp_template + strlen (tmp_template) - 6, "%S", utf16_template + wcslen (utf16_template) - 6);
-
-       g_free (utf16_template);
-       return fd;
-}
-#endif
-
-gint
-g_file_open_tmp (const gchar *tmpl, gchar **name_used, GError **error)
-{
-       const static gchar *default_tmpl = ".XXXXXX";
-       gchar *t;
-       gint fd;
-       size_t len;
-
-       g_return_val_if_fail (error == NULL || *error == NULL, -1);
-
-       if (tmpl == NULL)
-               tmpl = default_tmpl;
-
-       if (strchr (tmpl, G_DIR_SEPARATOR) != NULL) {
-               if (error) {
-                       *error = g_error_new (G_LOG_DOMAIN, 24, "Template should not have any " G_DIR_SEPARATOR_S);
-               }
-               return -1;
-       }
-
-       len = strlen (tmpl);
-       if (len < 6 || strcmp (tmpl + len - 6, "XXXXXX")) {
-               if (error) {
-                       *error = g_error_new (G_LOG_DOMAIN, 24, "Template should end with XXXXXX");
-               }
-               return -1;
-       }
-
-       t = g_build_filename (g_get_tmp_dir (), tmpl, NULL);
-
-       fd = mkstemp (t);
-
-       if (fd == -1) {
-               if (error) {
-                       int err = errno;
-                       *error = g_error_new (G_LOG_DOMAIN, g_file_error_from_errno (err), "Error in mkstemp()");
-               }
-               g_free (t);
-               return -1;
-       }
-
-       if (name_used) {
-               *name_used = t;
-       } else {
-               g_free (t);
-       }
-       return fd;
-}
-
-#ifdef _MSC_VER
-#pragma warning(disable:4701)
-#endif
-
-gboolean
-g_file_test (const gchar *filename, GFileTest test)
-{
-#ifdef G_OS_WIN32
-       struct _stat64 stat;
-       int ret = 0;
-       gunichar2* utf16_filename = NULL;
-
-       if (filename == NULL || test == 0)
-               return FALSE;
-
-       utf16_filename = u8to16 (filename);
-       ret = _wstati64 (utf16_filename, &stat);
-       g_free (utf16_filename);
-
-       if ((test & G_FILE_TEST_EXISTS) != 0) {
-               if (ret == 0)
-                       return TRUE;
-       }
-
-       if (ret != 0)
-               return FALSE;
-
-       if ((test & G_FILE_TEST_IS_EXECUTABLE) != 0) {
-               if (stat.st_mode & _S_IEXEC)
-                       return TRUE;
-       }
-
-       if ((test & G_FILE_TEST_IS_REGULAR) != 0) {
-               if (stat.st_mode & _S_IFREG)
-                       return TRUE;
-       }
-
-       if ((test & G_FILE_TEST_IS_DIR) != 0) {
-               if (stat.st_mode & _S_IFDIR)
-                       return TRUE;
-       }
-
-       /* make this last in case it is OR'd with something else */
-       if ((test & G_FILE_TEST_IS_SYMLINK) != 0) {
-               return FALSE;
-       }
-
-       return FALSE;
-#else
-       struct stat st;
-       gboolean have_stat;
-
-       if (filename == NULL || test == 0)
-               return FALSE;
-
-       have_stat = FALSE;
-
-       if ((test & G_FILE_TEST_EXISTS) != 0) {
-               if (access (filename, F_OK) == 0)
-                       return TRUE;
-       }
-
-       if ((test & G_FILE_TEST_IS_EXECUTABLE) != 0) {
-               if (access (filename, X_OK) == 0)
-                       return TRUE;
-       }
-       if ((test & G_FILE_TEST_IS_SYMLINK) != 0) {
-               have_stat = (lstat (filename, &st) == 0);
-               if (have_stat && S_ISLNK (st.st_mode))
-                       return TRUE;
-       }
-
-       if ((test & G_FILE_TEST_IS_REGULAR) != 0) {
-               if (!have_stat)
-                       have_stat = (stat (filename, &st) == 0);
-               if (have_stat && S_ISREG (st.st_mode))
-                       return TRUE;
-       }
-       if ((test & G_FILE_TEST_IS_DIR) != 0) {
-               if (!have_stat)
-                       have_stat = (stat (filename, &st) == 0);
-               if (have_stat && S_ISDIR (st.st_mode))
-                       return TRUE;
-       }
-       return FALSE;
-#endif
-}
-
 
index 5fcf0a786eb01db8e957048bbb597dd41796616e..c256f123a146a4694f5dfd229e2508bd72036f6b 100644 (file)
@@ -124,7 +124,7 @@ parse_value (const char *p, const char *end, char **value, GError **error)
                return end;
        l = (int)(p - start);
        p++;
-       *value = malloc (l + 1);
+       *value = g_malloc (l + 1);
        if (*value == NULL)
                return end;
        strncpy (*value, start, l);
@@ -144,7 +144,7 @@ parse_name (const char *p, const char *end, char **value)
                return end;
 
        l = (int)(p - start);
-       *value = malloc (l + 1);
+       *value = g_malloc (l + 1);
        if (*value == NULL)
                return end;
        strncpy (*value, start, l);
@@ -183,24 +183,24 @@ parse_attributes (const char *p, const char *end, char ***names, char ***values,
 
                        p = skip_space (p, end);
                        if (p == end){
-                               free (name);
+                               g_free (name);
                                return p;
                        }
                        if (*p != '='){
                                set_error ("Expected an = after the attribute name `%s'", name);
-                               free (name);
+                               g_free (name);
                                return end;
                        }
                        p++;
                        p = skip_space (p, end);
                        if (p == end){
-                               free (name);
+                               g_free (name);
                                return end;
                        }
 
                        p = parse_value (p, end, &value, error);
                        if (p == end){
-                               free (name);
+                               g_free (name);
                                return p;
                        }
 
@@ -312,7 +312,7 @@ g_markup_parse_context_parse (GMarkupParseContext *context,
                                goto fail;
                        }
                        l = (int)(element_end - element_start);
-                       ename = malloc (l + 1);
+                       ename = g_malloc (l + 1);
                        if (ename == NULL)
                                goto fail;
                        strncpy (ename, element_start, l);
@@ -331,7 +331,7 @@ g_markup_parse_context_parse (GMarkupParseContext *context,
                        }
 
                        if (error != NULL && *error != NULL){
-                               free (ename);
+                               g_free (ename);
                                goto fail;
                        }
                        
@@ -343,7 +343,7 @@ g_markup_parse_context_parse (GMarkupParseContext *context,
                                                goto fail;
                                        }
                                }
-                               free (ename);
+                               g_free (ename);
                        } else {
                                context->level = g_slist_prepend (context->level, ename);
                        }
@@ -404,11 +404,11 @@ g_markup_parse_context_parse (GMarkupParseContext *context,
                        if (context->parser.end_element != NULL){
                                context->parser.end_element (context, text, context->user_data, error);
                                if (error != NULL && *error != NULL){
-                                       free (text);
+                                       g_free (text);
                                        goto fail;
                                }
                        }
-                       free (text);
+                       g_free (text);
 
                        while (p < end && *p != '>')
                                p++;
index 5d864431308963cbcd0c9dd10296279160fa35de..3dc90a5b22045a9a967082dbecd179dd3b6a4106 100644 (file)
@@ -257,7 +257,7 @@ g_get_home_dir (void)
        gchar *home_dir = NULL;
        
        if (drive && path) {
-               home_dir = malloc(strlen(drive) + strlen(path) +1);
+               home_dir = g_malloc(strlen(drive) + strlen(path) +1);
                if (home_dir) {
                        sprintf(home_dir, "%s%s", drive, path);
                }
index f52979b25a22aeb2e521b011497c4fcfc2bdd01b..14ba231c4f36dacf0fcaaf24a31e289211fa60b7 100644 (file)
@@ -40,7 +40,7 @@ g_strndup (const gchar *str, gsize n)
        return strndup (str, n);
 #else
        if (str) {
-               char *retval = malloc(n+1);
+               char *retval = g_malloc(n+1);
                if (retval) {
                        strncpy(retval, str, n)[n] = 0;
                }
index 9b08c8c26aee30a8f76e3576b3fbaf8f53c5e4c2..ab3a04ea134e0b02562dc43535f483ab4c558193 100644 (file)
@@ -159,7 +159,7 @@ g_string_append_printf (GString *string, const gchar *format, ...)
        va_end (args);
        g_string_append (string, ret);
 
-       free (ret);
+       g_free (ret);
 }
 
 void
index 96921645b69172403b60fde9f9cb3c0d726fce40..8699df289bc98612a9fcc8602d747345cbbde459 100644 (file)
@@ -4,27 +4,27 @@
 
 int vasprintf(char **ret, const char *fmt, va_list ap)
 {
-char *buf;
-int len;
-size_t buflen;
-va_list ap2;
-
+       char *buf;
+       int len;
+       size_t buflen;
+       va_list ap2;
+       
 #ifdef _MSC_VER
-ap2 = ap;
-len = _vscprintf(fmt, ap2); // NOTE MS specific extension ( :-( )
+       ap2 = ap;
+       len = _vscprintf(fmt, ap2); // NOTE MS specific extension ( :-( )
 #else
-va_copy(ap2, ap);
-len = vsnprintf(NULL, 0, fmt, ap2);
+       va_copy(ap2, ap);
+       len = vsnprintf(NULL, 0, fmt, ap2);
 #endif
-
-if (len > 0 && (buf = malloc((buflen = (size_t) (len + 1)))) != NULL) {
-len = vsnprintf(buf, buflen, fmt, ap);
-*ret = buf;
-} else {
-*ret = NULL;
-len = -1;
-}
-
-va_end(ap2);
-return len;
+       
+       if (len > 0 && (buf = malloc((buflen = (size_t) (len + 1)))) != NULL) {
+               len = vsnprintf(buf, buflen, fmt, ap);
+               *ret = buf;
+       } else {
+               *ret = NULL;
+               len = -1;
+       }
+       
+       va_end(ap2);
+       return len;
 }