]> git.street.me.uk Git - andy/viking.git/blobdiff - src/util.c
[DOC] Mention map tilesize configuration.
[andy/viking.git] / src / util.c
index 34f3abf1eeb32f684a75be2cd0586263d083a20c..2b129dc4199fb36a150befc97505a670ca15d63b 100644 (file)
@@ -2,12 +2,11 @@
  *    Viking - GPS data editor
  *    Copyright (C) 2007, Guilhem Bonnefille <guilhem.bonnefille@gmail.com>
  *    Copyright (C) 2014, Rob Norris <rw_norris@hotmail.com>
- *    Based on:
- *    Copyright (C) 2003-2007, Leandro A. F. Pereira <leandro@linuxmag.com.br>
  *
  *    This program is free software; you can redistribute it and/or modify
  *    it under the terms of the GNU General Public License as published by
- *    the Free Software Foundation, version 2.
+ *    the Free Software Foundation; either version 2 of the License, or
+ *    (at your option) any later version.
  *
  *    This program is distributed in the hope that it will be useful,
  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -30,6 +29,7 @@
 #include <glib/gstdio.h>
 #include <glib/gi18n.h>
 #include <glib/gprintf.h>
+#include <gio/gio.h>
 
 #include "util.h"
 #include "globals.h"
@@ -61,37 +61,6 @@ guint util_get_number_of_cpus ()
 #endif
 }
 
-gchar *uri_escape(gchar *str)
-{
-  gchar *esc_str = g_malloc(3*strlen(str));
-  gchar *dst = esc_str;
-  gchar *src;
-
-  for (src = str; *src; src++) {
-    if (*src == ' ')
-     *dst++ = '+';
-    else if (g_ascii_isalnum(*src))
-     *dst++ = *src;
-    else {
-      g_sprintf(dst, "%%%02hhX", *src);
-      dst += 3;
-    }
-  }
-  *dst = '\0';
-
-  return(esc_str);
-}
-
-
-GList * str_array_to_glist(gchar* data[])
-{
-  GList *gl = NULL;
-  gpointer * p;
-  for (p = (gpointer)data; *p; p++)
-    gl = g_list_prepend(gl, *p);
-  return g_list_reverse(gl);
-}
-
 /**
  * split_string_from_file_on_equals:
  *
@@ -161,7 +130,8 @@ void util_remove_all_in_deletion_list ( void )
 {
        while ( deletion_list )
        {
-               g_remove ( deletion_list->data );
+               if ( g_remove ( (const char*)deletion_list->data ) )
+                       g_warning ( "%s: Failed to remove %s", __FUNCTION__, (char*)deletion_list->data );
                g_free ( deletion_list->data );
                deletion_list = g_slist_delete_link ( deletion_list, deletion_list );
        }
@@ -212,3 +182,55 @@ int util_remove ( const gchar *filename )
        else
                return g_remove ( filename );
 }
+
+/**
+ * Stream write buffer to a temporary file (in one go)
+ *
+ * @param buffer The buffer to write
+ * @param count Size of the buffer to write
+ *
+ * @return the filename of the buffer that was written
+ */
+gchar* util_write_tmp_file_from_bytes ( const void *buffer, gsize count )
+{
+       GFileIOStream *gios;
+       GError *error = NULL;
+       gchar *tmpname = NULL;
+
+#if GLIB_CHECK_VERSION(2,32,0)
+       GFile *gf = g_file_new_tmp ( "vik-tmp.XXXXXX", &gios, &error );
+       tmpname = g_file_get_path (gf);
+#else
+       gint fd = g_file_open_tmp ( "vik-tmp.XXXXXX", &tmpname, &error );
+       if ( error ) {
+               g_warning ( "%s", error->message );
+               g_error_free ( error );
+               return NULL;
+       }
+       gios = g_file_open_readwrite ( g_file_new_for_path (tmpname), NULL, &error );
+       if ( error ) {
+               g_warning ( "%s", error->message );
+               g_error_free ( error );
+               return NULL;
+       }
+#endif
+
+       gios = g_file_open_readwrite ( g_file_new_for_path (tmpname), NULL, &error );
+       if ( error ) {
+               g_warning ( "%s", error->message );
+               g_error_free ( error );
+               return NULL;
+       }
+
+       GOutputStream *gos = g_io_stream_get_output_stream ( G_IO_STREAM(gios) );
+       if ( g_output_stream_write ( gos, buffer, count, NULL, &error ) < 0 ) {
+               g_critical ( "Couldn't write tmp %s file due to %s", tmpname, error->message );
+               g_free (tmpname);
+               tmpname = NULL;
+       }
+
+       g_output_stream_close ( gos, NULL, &error );
+       g_object_unref ( gios );
+
+       return tmpname;
+}