Browse Source

Util: Prefer string_view as function parameter in File class

master
Riyyi 2 years ago
parent
commit
f7157feddf
  1. 17
      src/ruc/file.cpp
  2. 13
      src/ruc/file.h

17
src/ruc/file.cpp

@ -9,24 +9,25 @@
#include <fstream> // ifstream, ios, ofstream #include <fstream> // ifstream, ios, ofstream
#include <memory> // make_unique #include <memory> // make_unique
#include <string> #include <string>
#include <string_view>
#include "ruc/file.h" #include "ruc/file.h"
#include "ruc/meta/assert.h" #include "ruc/meta/assert.h"
namespace ruc { namespace ruc {
File::File(const std::string& path) File::File(std::string_view path)
: m_path(path) : m_path(path)
{ {
// Create input stream object and open file // Create input stream object and open file
std::ifstream file(path, std::ios::in); std::ifstream file(path.data(), std::ios::in);
VERIFY(file.is_open(), "failed to open file: '{}'", path); VERIFY(file.is_open(), "failed to open file: '{}'", path);
// Get length of the file // Get length of the file
file.seekg(0, std::ios::end); file.seekg(0, std::ios::end);
int32_t size = file.tellg(); int32_t size = file.tellg();
file.seekg(0, std::ios::beg); file.seekg(0, std::ios::beg);
VERIFY(size != -1, "failed to read file length: '{}', path"); VERIFY(size != -1, "failed to read file length: '{}'", path);
// Allocate memory filled with zeros // Allocate memory filled with zeros
auto buffer = std::make_unique<char[]>(size); auto buffer = std::make_unique<char[]>(size);
@ -44,10 +45,10 @@ File::~File()
// ----------------------------------------- // -----------------------------------------
File File::create(const std::string& path) File File::create(std::string_view path)
{ {
if (!std::filesystem::exists(path)) { if (!std::filesystem::exists(path)) {
std::ofstream { path }; std::ofstream { path.data() };
} }
return File(path); return File(path);
@ -60,14 +61,14 @@ void File::clear()
m_data.clear(); m_data.clear();
} }
File& File::append(const std::string& data) File& File::append(std::string_view data)
{ {
m_data.append(data); m_data.append(data);
return *this; return *this;
} }
File& File::replace(size_t index, size_t length, const std::string& data) File& File::replace(size_t index, size_t length, std::string_view data)
{ {
m_data.replace(index, length, data); m_data.replace(index, length, data);
@ -77,7 +78,7 @@ File& File::replace(size_t index, size_t length, const std::string& data)
File& File::flush() File& File::flush()
{ {
// Create output stream object and open file // Create output stream object and open file
std::ofstream file(m_path, std::ios::out | std::ios::trunc); std::ofstream file(m_path.data(), std::ios::out | std::ios::trunc);
VERIFY(file.is_open(), "failed to open file: '{}'", m_path); VERIFY(file.is_open(), "failed to open file: '{}'", m_path);
// Write data to disk // Write data to disk

13
src/ruc/file.h

@ -7,27 +7,28 @@
#pragma once #pragma once
#include <string> #include <string>
#include <string_view>
namespace ruc { namespace ruc {
class File { class File {
public: public:
File(const std::string& path); File(std::string_view path);
virtual ~File(); virtual ~File();
static File create(const std::string& path); static File create(std::string_view path);
void clear(); void clear();
File& append(const std::string& data); File& append(std::string_view data);
File& replace(size_t index, size_t length, const std::string& data); File& replace(size_t index, size_t length, std::string_view data);
File& flush(); File& flush();
const char* c_str() const { return m_data.c_str(); } const char* c_str() const { return m_data.c_str(); }
const std::string& data() const { return m_data; } const std::string& data() const { return m_data; }
const std::string& path() const { return m_path; } std::string_view path() const { return m_path; }
private: private:
std::string m_path; std::string_view m_path;
std::string m_data; std::string m_data;
}; };

Loading…
Cancel
Save