From 31ee901e01eccdc503d9bfa06260cb78871209a1 Mon Sep 17 00:00:00 2001 From: apio Date: Mon, 19 Dec 2022 12:21:17 +0100 Subject: [PATCH] TarStream: Add a variant of read_contents() returning an OwnedStringView --- luna/include/luna/TarStream.h | 3 +++ luna/src/TarStream.cpp | 12 ++++++++++++ 2 files changed, 15 insertions(+) diff --git a/luna/include/luna/TarStream.h b/luna/include/luna/TarStream.h index baf8322d..b4f62402 100644 --- a/luna/include/luna/TarStream.h +++ b/luna/include/luna/TarStream.h @@ -1,4 +1,5 @@ #pragma once +#include #include #include @@ -33,6 +34,8 @@ class TarStream usize read_contents(const Entry& entry, char* buf, usize offset, usize length); + Result read_contents_as_string(const Entry& entry, usize offset, usize max); + private: struct [[gnu::packed]] TarHeader { diff --git a/luna/src/TarStream.cpp b/luna/src/TarStream.cpp index ecc85e80..1dee4edd 100644 --- a/luna/src/TarStream.cpp +++ b/luna/src/TarStream.cpp @@ -1,5 +1,6 @@ #define _LUNA_SYSTEM_ERROR_EXTENSIONS #include +#include #include #include #include @@ -94,4 +95,15 @@ usize TarStream::read_contents(const Entry& entry, char* buf, usize offset, usiz memcpy(buf, offset_ptr(m_base, entry.pos + offset), length); return length; +} + +Result TarStream::read_contents_as_string(const Entry& entry, usize offset, usize max) +{ + char* buf = TRY(make_array(max + 1)); + + usize nread = read_contents(entry, buf, offset, max); + + buf[nread] = 0; + + return OwnedStringView{buf}; } \ No newline at end of file