[gcab] Add gcab_folder_get_file_by_name()
- From: Richard Hughes <rhughes src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gcab] Add gcab_folder_get_file_by_name()
- Date: Fri, 15 Dec 2017 15:43:12 +0000 (UTC)
commit bcecbd6f8c256ac19a32bca0aaa25355b5b35a3c
Author: Richard Hughes <richard hughsie com>
Date: Fri Dec 15 11:21:21 2017 +0000
Add gcab_folder_get_file_by_name()
If we know the name of the file we want to process, using the pre-existing hash
table is a lot quicker and more efficient than getting the file list and
strcmp()ing the gcab_file_get_name() on each of the GCabFile's.
libgcab/gcab-folder.c | 17 +++++++++++++++++
libgcab/gcab-folder.h | 2 ++
libgcab/libgcab.syms | 1 +
tests/gcab-self-test.c | 6 ++++++
4 files changed, 26 insertions(+), 0 deletions(-)
---
diff --git a/libgcab/gcab-folder.c b/libgcab/gcab-folder.c
index c46f383..1a12371 100644
--- a/libgcab/gcab-folder.c
+++ b/libgcab/gcab-folder.c
@@ -366,6 +366,23 @@ gcab_folder_get_files (GCabFolder *self)
return g_slist_reverse (g_slist_copy (self->files));
}
+/**
+ * gcab_folder_get_file_by_name:
+ * @cabfolder: a #GCabFolder
+ * @name: a file name
+ *
+ * Gets a specific #GCabFile files contained in the @cabfolder.
+ *
+ * Returns: (transfer none): A #GCabFile, or %NULL if not found
+ **/
+GCabFile *
+gcab_folder_get_file_by_name (GCabFolder *self, const gchar *name)
+{
+ g_return_val_if_fail (GCAB_IS_FOLDER (self), NULL);
+ g_return_val_if_fail (name != NULL, NULL);
+ return g_hash_table_lookup (self->hash, name);
+}
+
static gint
sort_by_offset (GCabFile *a, GCabFile *b)
{
diff --git a/libgcab/gcab-folder.h b/libgcab/gcab-folder.h
index e54a468..e4f7168 100644
--- a/libgcab/gcab-folder.h
+++ b/libgcab/gcab-folder.h
@@ -63,6 +63,8 @@ gboolean gcab_folder_add_file (GCabFolder *cabfolder,
GError **error);
guint gcab_folder_get_nfiles (GCabFolder *cabfolder);
GSList * gcab_folder_get_files (GCabFolder *cabfolder);
+GCabFile * gcab_folder_get_file_by_name (GCabFolder *cabfolder,
+ const gchar *name);
gint gcab_folder_get_comptype (GCabFolder *cabfolder);
G_END_DECLS
diff --git a/libgcab/libgcab.syms b/libgcab/libgcab.syms
index 76bda2b..d14729b 100644
--- a/libgcab/libgcab.syms
+++ b/libgcab/libgcab.syms
@@ -45,4 +45,5 @@ LIBGCAB1_0.6 {
LIBGCAB1_1.0 {
gcab_file_set_date;
gcab_folder_get_comptype;
+ gcab_folder_get_file_by_name;
} LIBGCAB1_0.6;
diff --git a/tests/gcab-self-test.c b/tests/gcab-self-test.c
index b44a800..defd278 100644
--- a/tests/gcab-self-test.c
+++ b/tests/gcab-self-test.c
@@ -119,6 +119,12 @@ gcab_test_folder_func (void)
ret = gcab_folder_add_file (cabfolder, cabfile2, FALSE, NULL, &error);
g_assert_error (error, GCAB_ERROR, GCAB_ERROR_FORMAT);
g_assert (!ret);
+
+ /* find using the hash table */
+ cabfile_tmp = gcab_folder_get_file_by_name (cabfolder, "test.bin");
+ g_assert_nonnull (cabfile_tmp);
+ cabfile_tmp = gcab_folder_get_file_by_name (cabfolder, "notgoingtoexist");
+ g_assert_null (cabfile_tmp);
}
static void
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]