Use GMappedFile if possible

This commit is contained in:
Přemysl Eric Janouch 2013-05-06 03:51:06 +02:00
parent 0e6c42a649
commit 818ee593aa
2 changed files with 34 additions and 25 deletions

View File

@ -456,6 +456,10 @@ stardict_dict_finalize (GObject *self)
stardict_info_free (sd->info);
g_array_free (sd->index, TRUE);
g_array_free (sd->synonyms, TRUE);
if (sd->mapped_dict)
g_mapped_file_unref (sd->mapped_dict);
else
g_free (sd->dict);
G_OBJECT_CLASS (stardict_dict_parent_class)->finalize (self);
@ -634,6 +638,8 @@ syn_destroy_cb (gpointer sde)
static gboolean
load_dict (StardictDict *sd, const gchar *filename, gboolean gzipped,
GError **error)
{
if (gzipped)
{
gboolean ret_val = FALSE;
GFile *file = g_file_new_for_path (filename);
@ -644,8 +650,6 @@ load_dict (StardictDict *sd, const gchar *filename, gboolean gzipped,
// Just read it all, as it is, into memory
GByteArray *ba = g_byte_array_new ();
if (gzipped)
{
GZlibDecompressor *zd
= g_zlib_decompressor_new (G_ZLIB_COMPRESSOR_FORMAT_GZIP);
GInputStream *cis = g_converter_input_stream_new
@ -655,26 +659,30 @@ load_dict (StardictDict *sd, const gchar *filename, gboolean gzipped,
g_object_unref (cis);
g_object_unref (zd);
}
else
ret_val = stream_read_all (ba, G_INPUT_STREAM (fis), error);
if (!ret_val)
if (ret_val)
{
g_byte_array_free (ba, TRUE);
goto reading_failed;
}
sd->dict_length = ba->len;
sd->dict = g_byte_array_free (ba, FALSE);
}
else
g_byte_array_free (ba, TRUE);
reading_failed:
g_object_unref (fis);
cannot_open:
g_object_unref (file);
return ret_val;
}
sd->mapped_dict = g_mapped_file_new (filename, FALSE, error);
if (!sd->mapped_dict)
return FALSE;
sd->dict_length = g_mapped_file_get_length (sd->mapped_dict);
sd->dict = g_mapped_file_get_contents (sd->mapped_dict);
return TRUE;
}
/** Load a StarDict dictionary.
* @param[in] sdi Parsed .ifo data.
*/

View File

@ -126,6 +126,7 @@ struct stardict_dict
GArray * synonyms; //!< Synonyms
gpointer dict; //!< Dictionary data
gsize dict_length; //!< Length of the dict data in bytes
GMappedFile * mapped_dict; //!< Memory map handle
};
struct stardict_dict_class