Browse Source

Util: Add length() and raw() functions to File class

master
Riyyi 2 years ago
parent
commit
e592ee7fd4
  1. 55
      src/ruc/file.cpp
  2. 4
      src/ruc/file.h
  3. 2
      src/ruc/format/formatter.h

55
src/ruc/file.cpp

@ -6,8 +6,8 @@
#include <cstdint> // int32_t
#include <filesystem>
#include <fstream> // ifstream, ios, ofstream
#include <memory> // make_unique
#include <fstream> // std::ifstream, std::ofstream
#include <memory> // std::make_shared, std::shared_ptr
#include <string>
#include <string_view>
@ -19,24 +19,7 @@ namespace ruc {
File::File(std::string_view path)
: m_path(path)
{
// Create input stream object and open file
std::ifstream file(path.data(), std::ios::in);
VERIFY(file.is_open(), "failed to open file: '{}'", path);
// Get length of the file
file.seekg(0, std::ios::end);
int32_t size = file.tellg();
file.seekg(0, std::ios::beg);
VERIFY(size != -1, "failed to read file length: '{}'", path);
// Allocate memory filled with zeros
auto buffer = std::make_unique<char[]>(size);
// Fill buffer with file contents
file.read(buffer.get(), size);
file.close();
m_data = std::string(buffer.get(), size);
m_data = std::string(File::raw(path).get());
}
File::~File()
@ -54,6 +37,38 @@ File File::create(std::string_view path)
return File(path);
}
int32_t File::length(std::string_view path, std::ifstream& file)
{
file.seekg(0, std::ios::end);
int32_t length = file.tellg();
file.seekg(0, std::ios::beg);
VERIFY(length != -1, "failed to read file length: '{}'", path);
return length;
}
std::shared_ptr<char[]> File::raw(std::string_view path)
{
// Create input stream object and open file
std::ifstream file(path.data());
VERIFY(file.is_open(), "failed to open file: '{}'", path);
// Get length of the file
int32_t size = File::length(path, file);
// Allocate memory filled with zeros
auto buffer = std::make_shared<char[]>(size + 1);
// Fill buffer with file contents
file.read(buffer.get(), size);
file.close();
// Null termination
buffer[size] = '\0';
return buffer;
}
// -----------------------------------------
void File::clear()

4
src/ruc/file.h

@ -6,6 +6,8 @@
#pragma once
#include <cstdint> // int32_t
#include <memory> // std::shared_ptr
#include <string>
#include <string_view>
@ -17,6 +19,8 @@ public:
virtual ~File();
static File create(std::string_view path);
static int32_t length(std::string_view path, std::ifstream& file);
static std::shared_ptr<char[]> raw(std::string_view path);
void clear();
File& append(std::string_view data);

2
src/ruc/format/formatter.h

@ -331,8 +331,6 @@ struct Formatter<Specifier> : Formatter<std::nullptr_t> {
} // namespace ruc::format
using ruc::format::Formatter;
#if 0
TODO:

Loading…
Cancel
Save