Trim trailing white spaces on empty lines.
[geeqie.git] / src / misc.c
index d52e1e6..6f244ec 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Geeqie
- * Copyright (C) 2008 The Geeqie Team
+ * Copyright (C) 2008 - 2012 The Geeqie Team
  *
  * Authors: Vladimir Nadvornik / Laurent Monin
  *
@@ -22,7 +22,7 @@ gchar *utf8_validate_or_convert(const gchar *text)
        gint len;
 
        if (!text) return NULL;
-       
+
        len = strlen(text);
        if (!g_utf8_validate(text, len, NULL))
                return g_convert(text, len, "UTF-8", "ISO-8859-1", NULL, NULL, NULL);
@@ -44,6 +44,11 @@ gint utf8_compare(const gchar *s1, const gchar *s2, gboolean case_sensitive)
                s1_t = g_utf8_casefold(s1, -1);
                s2_t = g_utf8_casefold(s2, -1);
                }
+       else
+               {
+               s1_t = (gchar *) s1;
+               s2_t = (gchar *) s2;
+               }
 
        s1_key = g_utf8_collate_key(s1_t, -1);
        s2_key = g_utf8_collate_key(s2_t, -1);
@@ -109,80 +114,56 @@ gchar *expand_tilde(const gchar *filename)
 #endif
 }
 
-/*
-   returns text without quotes or NULL for empty or broken string
-   any text up to first '"' is skipped
-   tail is set to point at the char after the second '"'
-   or at the ending \0
-
-*/
 
-gchar *quoted_value(const gchar *text, const gchar **tail)
+/* Run a command like system() but may output debug messages. */
+int runcmd(gchar *cmd)
 {
-       const gchar *ptr;
-       gint c = 0;
-       gint l = strlen(text);
-       gchar *retval = NULL;
-
-       if (tail) *tail = text;
+#if 1
+       return system(cmd);
+       return 0;
+#else
+       /* For debugging purposes */
+       int retval = -1;
+       FILE *in;
 
-       if (l == 0) return retval;
+       DEBUG_1("Running command: %s", cmd);
 
-       while (c < l && text[c] != '"') c++;
-       if (text[c] == '"')
+       in = popen(cmd, "r");
+       if (in)
                {
-               gint e;
-               c++;
-               ptr = text + c;
-               e = c;
-               while (e < l)
+               int status;
+               const gchar *msg;
+               gchar buf[2048];
+
+               while (fgets(buf, sizeof(buf), in) != NULL )
                        {
-                       if (text[e-1] != '\\' && text[e] == '"') break;
-                       e++;
+                       DEBUG_1("Output: %s", buf);
                        }
-               if (text[e] == '"')
+
+               status = pclose(in);
+
+               if (WIFEXITED(status))
                        {
-                       if (e - c > 0)
-                               {
-                               gchar *substring = g_strndup(ptr, e - c);
-
-                               if (substring)
-                                       {
-                                       retval = g_strcompress(substring);
-                                       g_free(substring);
-                                       }
-                               }
+                       msg = "Command terminated with exit code";
+                       retval = WEXITSTATUS(status);
                        }
-               if (tail) *tail = text + e + 1;
-               }
-       else
-               /* for compatibility with older formats (<0.3.7)
-                * read a line without quotes too */
-               {
-               c = 0;
-               while (c < l && text[c] != '\n' && !g_ascii_isspace(text[c])) c++;
-               if (c != 0)
+               else if (WIFSIGNALED(status))
                        {
-                       retval = g_strndup(text, c);
+                       msg = "Command was killed by signal";
+                       retval = WTERMSIG(status);
                        }
-               if (tail) *tail = text + c;
-               }
+               else
+                       {
+                       msg = "pclose() returned";
+                       retval = status;
+                       }
+
+               DEBUG_1("%s : %d\n", msg, retval);
+       }
 
        return retval;
+#endif
 }
 
-gchar *escquote_value(const gchar *text)
-{
-       gchar *e;
-
-       if (!text) return g_strdup("\"\"");
 
-       e = g_strescape(text, "");
-       if (e)
-               {
-               gchar *retval = g_strdup_printf("\"%s\"", e);
-               g_free(e);
-               return retval;
-               }
-       return g_strdup("\"\"");
-}
+/* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */