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) define_member_function(BOOL, resize, IN size_type newCount)
{ {
newCount += 1;
const auto did_reallocate = reserve(newCount); const auto did_reallocate = reserve(newCount);
for(; Base::m_count < newCount; Base::m_count++) Base::m_data[Base::m_count] = 0; for(; Base::m_count < newCount; Base::m_count++) Base::m_data[Base::m_count] = 0;
return did_reallocate; return did_reallocate;

View File

@ -21,7 +21,7 @@
#include <filesystem> #include <filesystem>
#include <iacore/exception.hpp> #include <iacore/exception.hpp>
#include <iacore/string.hpp> #include <iacore/string.hpp>
#include <iacore/vector.hpp>
namespace ia namespace ia
{ {
@ -35,6 +35,19 @@ namespace ia
OPEN_FLAG_BINARY = 4, 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: public:
File(IN CONST String &path, IN UINT32 flags) File(IN CONST String &path, IN UINT32 flags)
{ {
@ -78,6 +91,23 @@ namespace ia
return readSize; 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: public:
template<BOOL includeExtension> STATIC INLINE String ExtractFilenameFromPath(IN CONST String &path) template<BOOL includeExtension> STATIC INLINE String ExtractFilenameFromPath(IN CONST String &path)
{ {

View File

@ -5,7 +5,8 @@
using namespace ia; 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) for(const auto& v: s)
Logger::Info(v); Logger::Info(v);