diff options
author | Chris Wilson <chris@chris-wilson.co.uk> | 2008-11-06 12:11:54 +0000 |
---|---|---|
committer | Chris Wilson <chris@chris-wilson.co.uk> | 2008-11-06 12:18:51 +0000 |
commit | 1f5d97ec6a8fcc770ee8b80b21b7a852b55567cd (patch) | |
tree | ec5d71b8bd5aea79bb8e4d178a3076348fc3bc7b | |
parent | 78bc3c8a3c48f09aa753eaee17cdd99bde9e0fa0 (diff) |
Read zlib compressed traces.
-rw-r--r-- | src/main.c | 94 |
1 files changed, 93 insertions, 1 deletions
@@ -32,11 +32,14 @@ #include <string.h> #include <unistd.h> #include <errno.h> +#include <zlib.h> #include <fcntl.h> #define APPNAME "Sphinx" #define GCONF_DIR "/apps/sphinx" +#define _(x) x + static gboolean sphinx_trace_command (sphinx_t *sphinx, const char *command, GError **error); @@ -1174,6 +1177,95 @@ sphinx_trace_command (sphinx_t *sphinx, const char *command, GError **error) } static gboolean +gz_file_get_contents (const char *filename, + gchar **buf_out, + gsize *len_out, + GError **error) +{ + gzFile *file; + gchar *buf = NULL; + gsize allocated; + gsize len; + int ret; + gchar *display_filename; + const gchar *errmsg; + int errnum; + + file = gzopen (filename, "r"); + if (file == NULL) + goto ERRNO; + + len = 0; + allocated = 1024 * 1024; + buf = g_try_malloc (1024*1024); + if (buf == NULL) + goto ERRNO; + + do { + ret = gzread (file, buf + len, allocated - len); + if (ret < 0) { + errmsg = gzerror (file, &errnum); + if (errnum == Z_ERRNO) + goto ERRNO; + else + goto GZERR; + } + if (ret == 0) + break; + + len += ret; + if (allocated - len < 16384) { + gchar *newbuf; + + allocated *= 2; + newbuf = g_try_realloc (buf, allocated); + if (newbuf == NULL) + goto ERRNO; + + buf = newbuf; + } + } while (TRUE); + + *len_out = len; + *buf_out = g_try_realloc (buf, len); + if (*buf_out == NULL) + *buf_out = buf; + + return TRUE; + + + GZERR: + { + display_filename = g_filename_display_name (filename); + g_set_error (error, + G_FILE_ERROR, + errnum, + _("Failed to read file '%s': %s"), + display_filename, + errmsg); + goto FAIL; + } + ERRNO: + { + int save_errno = errno; + + display_filename = g_filename_display_name (filename); + g_set_error (error, + G_FILE_ERROR, + g_file_error_from_errno (save_errno), + _("Failed to read file '%s': %s"), + display_filename, + g_strerror (save_errno)); + goto FAIL; + } + FAIL: + g_free (display_filename); + g_free (buf); + gzclose (file); + return FALSE; +} + +static gboolean sphinx_open_trace (sphinx_t *sphinx, const char *filename, GError **error) { GtkTextIter iter; @@ -1182,7 +1274,7 @@ sphinx_open_trace (sphinx_t *sphinx, const char *filename, GError **error) gsize len; gchar *str; - if (! g_file_get_contents (filename, &buf, &len, error)) + if (! gz_file_get_contents (filename, &buf, &len, error)) return FALSE; str = g_strdup_printf ("%s - " APPNAME, filename); |