From 1f5d97ec6a8fcc770ee8b80b21b7a852b55567cd Mon Sep 17 00:00:00 2001 From: Chris Wilson Date: Thu, 6 Nov 2008 12:11:54 +0000 Subject: Read zlib compressed traces. --- src/main.c | 94 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 93 insertions(+), 1 deletion(-) diff --git a/src/main.c b/src/main.c index 8ff0ede..25e6dc3 100644 --- a/src/main.c +++ b/src/main.c @@ -32,11 +32,14 @@ #include #include #include +#include #include #define APPNAME "Sphinx" #define GCONF_DIR "/apps/sphinx" +#define _(x) x + static gboolean sphinx_trace_command (sphinx_t *sphinx, const char *command, GError **error); @@ -1173,6 +1176,95 @@ sphinx_trace_command (sphinx_t *sphinx, const char *command, GError **error) return TRUE; } +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) { @@ -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); -- cgit v1.2.3