From 6429768373195ffc99c1a2b287f13171948c80a6 Mon Sep 17 00:00:00 2001 From: Christian Hergert Date: Sun, 14 May 2023 18:19:30 -0700 Subject: [PATCH] libsysprof-analyze: add API to open input stream --- .../sysprof-document-file.c | 40 +++++++++++++++++++ .../sysprof-document-file.h | 8 ++-- 2 files changed, 45 insertions(+), 3 deletions(-) diff --git a/src/libsysprof-analyze/sysprof-document-file.c b/src/libsysprof-analyze/sysprof-document-file.c index 56e7a00b..a3f33e8e 100644 --- a/src/libsysprof-analyze/sysprof-document-file.c +++ b/src/libsysprof-analyze/sysprof-document-file.c @@ -22,6 +22,7 @@ #include "sysprof-document-file-chunk.h" #include "sysprof-document-file-private.h" +#include "sysprof-document-frame-private.h" struct _SysprofDocumentFile { @@ -155,3 +156,42 @@ sysprof_document_file_dup_bytes (SysprofDocumentFile *self) return g_bytes_new_take (g_array_free (ar, FALSE), len); } + +/** + * sysprof_document_file_read: + * @self: a #SysprofDocumentFile + * + * Creates a new input stream containing the contents of the file + * within the document. + * + * Returns: (transfer full): a #GInputstream + */ +GInputStream * +sysprof_document_file_read (SysprofDocumentFile *self) +{ + GInputStream *input; + + g_return_val_if_fail (SYSPROF_IS_DOCUMENT_FILE (self), NULL); + + input = g_memory_input_stream_new (); + + for (guint i = 0; i < self->file_chunks->len; i++) + { + g_autoptr(GBytes) bytes = NULL; + SysprofDocumentFileChunk *file_chunk; + const guint8 *data; + guint len; + + file_chunk = g_ptr_array_index (self->file_chunks, i); + data = sysprof_document_file_chunk_get_data (file_chunk, &len); + + bytes = g_bytes_new_with_free_func (data, + len, + (GDestroyNotify)g_mapped_file_unref, + g_mapped_file_ref (SYSPROF_DOCUMENT_FRAME (self)->mapped_file)); + + g_memory_input_stream_add_bytes (G_MEMORY_INPUT_STREAM (input), bytes); + } + + return g_steal_pointer (&input); +} diff --git a/src/libsysprof-analyze/sysprof-document-file.h b/src/libsysprof-analyze/sysprof-document-file.h index 0b66501d..3ca728dc 100644 --- a/src/libsysprof-analyze/sysprof-document-file.h +++ b/src/libsysprof-analyze/sysprof-document-file.h @@ -20,7 +20,7 @@ #pragma once -#include +#include #include @@ -32,8 +32,10 @@ SYSPROF_AVAILABLE_IN_ALL G_DECLARE_FINAL_TYPE (SysprofDocumentFile, sysprof_document_file, SYSPROF, DOCUMENT_FILE, GObject) SYSPROF_AVAILABLE_IN_ALL -const char *sysprof_document_file_get_path (SysprofDocumentFile *self); +const char *sysprof_document_file_get_path (SysprofDocumentFile *self); SYSPROF_AVAILABLE_IN_ALL -GBytes *sysprof_document_file_dup_bytes (SysprofDocumentFile *self); +GBytes *sysprof_document_file_dup_bytes (SysprofDocumentFile *self); +SYSPROF_AVAILABLE_IN_ALL +GInputStream *sysprof_document_file_read (SysprofDocumentFile *self); G_END_DECLS