TarStream: Add a variant of read_contents() returning an OwnedStringView

This commit is contained in:
apio 2022-12-19 12:21:17 +01:00
parent 9eb829f3a2
commit 31ee901e01
Signed by: apio
GPG Key ID: B8A7D06E42258954
2 changed files with 15 additions and 0 deletions

View File

@ -1,4 +1,5 @@
#pragma once #pragma once
#include <luna/OwnedStringView.h>
#include <luna/Result.h> #include <luna/Result.h>
#include <luna/Types.h> #include <luna/Types.h>
@ -33,6 +34,8 @@ class TarStream
usize read_contents(const Entry& entry, char* buf, usize offset, usize length); usize read_contents(const Entry& entry, char* buf, usize offset, usize length);
Result<OwnedStringView> read_contents_as_string(const Entry& entry, usize offset, usize max);
private: private:
struct [[gnu::packed]] TarHeader struct [[gnu::packed]] TarHeader
{ {

View File

@ -1,5 +1,6 @@
#define _LUNA_SYSTEM_ERROR_EXTENSIONS #define _LUNA_SYSTEM_ERROR_EXTENSIONS
#include <luna/Alignment.h> #include <luna/Alignment.h>
#include <luna/Alloc.h>
#include <luna/CString.h> #include <luna/CString.h>
#include <luna/NumberParsing.h> #include <luna/NumberParsing.h>
#include <luna/TarStream.h> #include <luna/TarStream.h>
@ -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); memcpy(buf, offset_ptr(m_base, entry.pos + offset), length);
return length; return length;
}
Result<OwnedStringView> TarStream::read_contents_as_string(const Entry& entry, usize offset, usize max)
{
char* buf = TRY(make_array<char>(max + 1));
usize nread = read_contents(entry, buf, offset, max);
buf[nread] = 0;
return OwnedStringView{buf};
} }