Mercurial > geeqie
view src/misc.c @ 1234:31f50c1b6a9a
write keywords and comments with separate functions
author | nadvornik |
---|---|
date | Sat, 03 Jan 2009 15:29:08 +0000 |
parents | 5fe3b8b3a612 |
children | 3a0bb56adf8e |
line wrap: on
line source
/* * Geeqie * Copyright (C) 2008 The Geeqie Team * * Authors: Vladimir Nadvornik / Laurent Monin * * This software is released under the GNU General Public License (GNU GPL). * Please read the included file COPYING for more information. * This software comes with no warranty of any kind, use at your own risk! */ #include "main.h" #include "misc.h" gdouble get_zoom_increment(void) { return ((options->image.zoom_increment != 0) ? (gdouble)options->image.zoom_increment / 10.0 : 1.0); } 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); return g_strdup(text); } gint utf8_compare(const gchar *s1, const gchar *s2, gboolean case_sensitive) { gchar *s1_key, *s2_key; gchar *s1_t, *s2_t; gint ret; g_assert(g_utf8_validate(s1, -1, NULL)); g_assert(g_utf8_validate(s2, -1, NULL)); if (!case_sensitive) { s1_t = g_utf8_casefold(s1, -1); s2_t = g_utf8_casefold(s2, -1); } s1_key = g_utf8_collate_key(s1_t, -1); s2_key = g_utf8_collate_key(s2_t, -1); ret = strcmp(s1_key, s2_key); g_free(s1_key); g_free(s2_key); if (!case_sensitive) { g_free(s1_t); g_free(s2_t); } return ret; } /* Borrowed from gtkfilesystemunix.c */ gchar *expand_tilde(const gchar *filename) { #ifndef G_OS_UNIX return g_strdup(filename); #else const gchar *notilde; const gchar *slash; const gchar *home; if (filename[0] != '~') return g_strdup(filename); notilde = filename + 1; slash = strchr(notilde, G_DIR_SEPARATOR); if (slash == notilde || !*notilde) { home = g_get_home_dir(); if (!home) return g_strdup(filename); } else { gchar *username; struct passwd *passwd; if (slash) username = g_strndup(notilde, slash - notilde); else username = g_strdup(notilde); passwd = getpwnam(username); g_free(username); if (!passwd) return g_strdup(filename); home = passwd->pw_dir; } if (slash) return g_build_filename(home, G_DIR_SEPARATOR_S, slash + 1, NULL); else return g_build_filename(home, G_DIR_SEPARATOR_S, NULL); #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) { const gchar *ptr; gint c = 0; gint l = strlen(text); gchar *retval = NULL; if (tail) *tail = text; if (l == 0) return retval; while (c < l && text[c] != '"') c++; if (text[c] == '"') { gint e; c++; ptr = text + c; e = c; while (e < l) { if (text[e-1] != '\\' && text[e] == '"') break; e++; } if (text[e] == '"') { if (e - c > 0) { gchar *substring = g_strndup(ptr, e - c); if (substring) { retval = g_strcompress(substring); g_free(substring); } } } 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) { retval = g_strndup(text, c); } if (tail) *tail = text + c; } return retval; } 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("\"\""); } /* Run a command like system() but may output debug messages. */ int runcmd(gchar *cmd) { #if 1 return system(cmd); return 0; #else /* For debugging purposes */ int retval = -1; FILE *in; DEBUG_1("Running command: %s", cmd); in = popen(cmd, "r"); if (in) { int status; const gchar *msg; gchar buf[2048]; while (fgets(buf, sizeof(buf), in) != NULL ) { DEBUG_1("Output: %s", buf); } status = pclose(in); if (WIFEXITED(status)) { msg = "Command terminated with exit code"; retval = WEXITSTATUS(status); } else if (WIFSIGNALED(status)) { msg = "Command was killed by signal"; retval = WTERMSIG(status); } else { msg = "pclose() returned"; retval = status; } DEBUG_1("%s : %d\n", msg, retval); } return retval; #endif } /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */