[libgit2-glib] Add GgitDiffBinaryFile boxed type



commit 8263f83db97ad5dc826b4096a32163a3e462208b
Author: Ignacio Casal Quinteiro <icq gnome org>
Date:   Tue Jun 30 20:29:05 2015 +0200

    Add GgitDiffBinaryFile boxed type

 libgit2-glib/Makefile.am             |    2 +
 libgit2-glib/ggit-diff-binary-file.c |  149 ++++++++++++++++++++++++++++++++++
 libgit2-glib/ggit-diff-binary-file.h |   51 ++++++++++++
 libgit2-glib/ggit-types.h            |    7 ++
 4 files changed, 209 insertions(+), 0 deletions(-)
---
diff --git a/libgit2-glib/Makefile.am b/libgit2-glib/Makefile.am
index 093cfe8..1f7a9c1 100644
--- a/libgit2-glib/Makefile.am
+++ b/libgit2-glib/Makefile.am
@@ -34,6 +34,7 @@ H_FILES =                                     \
        ggit-cred.h                             \
        ggit-cred-plaintext.h                   \
        ggit-diff.h                             \
+       ggit-diff-binary-file.h                 \
        ggit-diff-delta.h                       \
        ggit-diff-file.h                        \
        ggit-diff-find-options.h                \
@@ -99,6 +100,7 @@ C_FILES =                                    \
        ggit-cred.c                             \
        ggit-cred-plaintext.c                   \
        ggit-diff.c                             \
+       ggit-diff-binary-file.c                 \
        ggit-diff-delta.c                       \
        ggit-diff-file.c                        \
        ggit-diff-find-options.c                \
diff --git a/libgit2-glib/ggit-diff-binary-file.c b/libgit2-glib/ggit-diff-binary-file.c
new file mode 100644
index 0000000..f89c76d
--- /dev/null
+++ b/libgit2-glib/ggit-diff-binary-file.c
@@ -0,0 +1,149 @@
+/*
+ * ggit-diff-binary-file.c
+ * This file is part of libgit2-glib
+ *
+ * Copyright (C) 2015 - Ignacio Casal Quinteiro
+ *
+ * libgit2-glib is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * libgit2-glib is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with libgit2-glib. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <glib-object.h>
+#include <string.h>
+
+#include "ggit-diff-binary-file.h"
+
+struct _GgitDiffBinaryFile {
+       gint ref_count;
+
+       GgitDiffBinaryType type;
+       guint8 *data;
+       gsize data_len;
+       gsize inflated_len;
+};
+
+G_DEFINE_BOXED_TYPE (GgitDiffBinaryFile, ggit_diff_binary_file,
+                     ggit_diff_binary_file_ref, ggit_diff_binary_file_unref)
+
+GgitDiffBinaryFile *
+_ggit_diff_binary_file_wrap (const git_diff_binary_file *file)
+{
+       GgitDiffBinaryFile *gfile;
+
+       g_return_val_if_fail (file != NULL, NULL);
+
+       gfile = g_slice_new (GgitDiffBinaryFile);
+       gfile->ref_count = 1;
+       gfile->type = (GgitDiffBinaryType)file->type;
+       gfile->data = g_malloc (file->datalen);
+       memcpy (gfile->data, file->data, file->datalen);
+       gfile->data_len = file->datalen;
+       gfile->inflated_len = file->inflatedlen;
+
+       return gfile;
+}
+
+/**
+ * ggit_diff_binary_file_ref:
+ * @file: a #GgitDiff.
+ *
+ * Atomically increments the reference count of @file by one.
+ * This function is MT-safe and may be called from any thread.
+ *
+ * Returns: a #GgitDiffBinaryFile.
+ **/
+GgitDiffBinaryFile *
+ggit_diff_binary_file_ref (GgitDiffBinaryFile *file)
+{
+       g_return_val_if_fail (file != NULL, NULL);
+
+       g_atomic_int_inc (&file->ref_count);
+
+       return file;
+}
+
+/**
+ * ggit_diff_binary_file_unref:
+ * @file: a #GgitDiffBinaryFile.
+ *
+ * Atomically decrements the reference count of @file by one.
+ * If the reference count drops to 0, @file is freed.
+ **/
+void
+ggit_diff_binary_file_unref (GgitDiffBinaryFile *file)
+{
+       g_return_if_fail (file != NULL);
+
+       if (g_atomic_int_dec_and_test (&file->ref_count))
+       {
+               g_free (file->data);
+               g_slice_free (GgitDiffBinaryFile, file);
+       }
+}
+
+/**
+ * ggit_diff_binary_file_get_binary_type:
+ * @file: a #GgitDiffBinaryFile.
+ *
+ * Gets the #GgitDiffBinaryType for @file.
+ *
+ * Returns: the file's binary type.
+ */
+GgitDiffBinaryType
+ggit_diff_binary_file_get_binary_type (GgitDiffBinaryFile *file)
+{
+       g_return_val_if_fail (file != NULL, GIT_DIFF_BINARY_NONE);
+
+       return file->type;
+}
+
+/**
+ * ggit_diff_binary_file_get_data:
+ * @file: a #GgitDiffBinaryFile.
+ * @size: (allow-none): location to return size of byte data.
+ *
+ * Get the binary data. This data should not be modified.
+ *
+ * Returns: a pointer to the binary data, or %NULL.
+ */
+const guint8 *
+ggit_diff_binary_file_get_data (GgitDiffBinaryFile *file,
+                                gsize              *size)
+{
+       g_return_val_if_fail (file != NULL, NULL);
+
+       if (size != NULL)
+       {
+               *size = file->data_len;
+       }
+
+       return file->data;
+}
+
+/**
+ * ggit_diff_binary_file_get_inflated_size:
+ * @file: a #GgitDiffBinaryFile.
+ *
+ * Gets the length of the binary data after inflation.
+ *
+ * Returns: the length of the binary data after inflation.
+ */
+gsize
+ggit_diff_binary_file_get_inflated_size (GgitDiffBinaryFile *file)
+{
+       g_return_val_if_fail (file != NULL, 0);
+
+       return file->inflated_len;
+}
+
+/* ex:set ts=8 noet: */
diff --git a/libgit2-glib/ggit-diff-binary-file.h b/libgit2-glib/ggit-diff-binary-file.h
new file mode 100644
index 0000000..f83f96c
--- /dev/null
+++ b/libgit2-glib/ggit-diff-binary-file.h
@@ -0,0 +1,51 @@
+/*
+ * ggit-diff-binary-file.h
+ * This file is part of libgit2-glib
+ *
+ * Copyright (C) 2015 - Ignacio Casal Quinteiro
+ *
+ * libgit2-glib is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * libgit2-glib is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with libgit2-glib. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef __GGIT_DIFF_BINARY_FILE_H__
+#define __GGIT_DIFF_BINARY_FILE_H__
+
+#include <git2.h>
+
+#include "ggit-types.h"
+
+G_BEGIN_DECLS
+
+#define GGIT_TYPE_DIFF_BINARY_FILE       (ggit_diff_binary_file_get_type ())
+#define GGIT_DIFF_BINARY_FILE(obj)       ((GgitDiffBinaryFile *)obj)
+
+GType                    ggit_diff_binary_file_get_type          (void) G_GNUC_CONST;
+
+GgitDiffBinaryFile      *_ggit_diff_binary_file_wrap             (const git_diff_binary_file *file);
+
+GgitDiffBinaryFile      *ggit_diff_binary_file_ref               (GgitDiffBinaryFile         *file);
+void                     ggit_diff_binary_file_unref             (GgitDiffBinaryFile         *file);
+
+GgitDiffBinaryType       ggit_diff_binary_file_get_binary_type   (GgitDiffBinaryFile         *file);
+
+const guint8            *ggit_diff_binary_file_get_data          (GgitDiffBinaryFile         *file,
+                                                                  gsize                      *size);
+
+gsize                    ggit_diff_binary_file_get_inflated_size (GgitDiffBinaryFile         *file);
+
+G_END_DECLS
+
+#endif /* __GGIT_DIFF_BINARY_FILE_H__ */
+
+/* ex:set ts=8 noet: */
diff --git a/libgit2-glib/ggit-types.h b/libgit2-glib/ggit-types.h
index 74362d3..8e82d55 100644
--- a/libgit2-glib/ggit-types.h
+++ b/libgit2-glib/ggit-types.h
@@ -131,6 +131,13 @@ typedef struct _GgitCredPlaintext GgitCredPlaintext;
 typedef struct _GgitDiff GgitDiff;
 
 /**
+ * GgitDiffBinaryFile:
+ *
+ * Represents a diff binary file.
+ */
+typedef struct _GgitDiffBinaryFile GgitDiffBinaryFile;
+
+/**
  * GgitDiffDelta:
  *
  * Represents the changes done to one file.


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]