Read file to string/buffer

This commit is contained in:
Isuru Samarathunga
2025-07-18 22:46:35 +05:30
parent a324de4111
commit 83bb826f24
3 changed files with 34 additions and 2 deletions

View File

@ -300,6 +300,7 @@ namespace ia
define_member_function(BOOL, resize, IN size_type newCount)
{
newCount += 1;
const auto did_reallocate = reserve(newCount);
for(; Base::m_count < newCount; Base::m_count++) Base::m_data[Base::m_count] = 0;
return did_reallocate;

View File

@ -21,7 +21,7 @@
#include <filesystem>
#include <iacore/exception.hpp>
#include <iacore/string.hpp>
#include <iacore/vector.hpp>
namespace ia
{
@ -35,6 +35,19 @@ namespace ia
OPEN_FLAG_BINARY = 4,
};
public:
STATIC Vector<UINT8> ReadToVector(IN CONST String &path)
{
File f(path, OPEN_FLAG_READ | OPEN_FLAG_BINARY);
return f.ReadToVector();
}
STATIC String ReadToString(IN CONST String &path)
{
File f(path, OPEN_FLAG_READ);
return f.ReadToString();
}
public:
File(IN CONST String &path, IN UINT32 flags)
{
@ -78,6 +91,23 @@ namespace ia
return readSize;
}
Vector<UINT8> ReadToVector()
{
Vector<UINT8> result;
result.resize(Size());
Read(result.data(), result.size(), result.size());
return result;
}
String ReadToString()
{
String result;
result.resize(Size());
Read(result.data(), result.size(), result.size());
result.back() = '\0';
return result;
}
public:
template<BOOL includeExtension> STATIC INLINE String ExtractFilenameFromPath(IN CONST String &path)
{

View File

@ -5,7 +5,8 @@
using namespace ia;
VOID print(IN CONST Span<int>& s)
template<typename _value_type>
VOID print(IN CONST Span<_value_type>& s)
{
for(const auto& v: s)
Logger::Info(v);