mirror of
https://github.com/alterware/iw4-validator.git
synced 2025-12-02 07:37:49 +00:00
init
This commit is contained in:
92
src/utils/io.cpp
Normal file
92
src/utils/io.cpp
Normal file
@@ -0,0 +1,92 @@
|
||||
#include <std_include.hpp>
|
||||
#include "io.hpp"
|
||||
|
||||
namespace utils
|
||||
{
|
||||
namespace io
|
||||
{
|
||||
bool remove_file(const std::string& file)
|
||||
{
|
||||
return std::remove(file.data()) == 0;
|
||||
}
|
||||
|
||||
bool move_file(const std::string& src, const std::string& target)
|
||||
{
|
||||
return std::rename(src.data(), target.data()) == 0;
|
||||
}
|
||||
|
||||
bool file_exists(const std::string& file)
|
||||
{
|
||||
return std::ifstream(file).good();
|
||||
}
|
||||
|
||||
bool write_file(const std::string& file, const std::string& data, const bool append)
|
||||
{
|
||||
auto mode = std::ios::binary | std::ofstream::out;
|
||||
if (append)
|
||||
{
|
||||
mode |= std::ofstream::app;
|
||||
}
|
||||
|
||||
std::ofstream stream(file, mode);
|
||||
|
||||
if (stream.is_open())
|
||||
{
|
||||
stream.write(data.data(), static_cast<std::streamsize>(data.size()));
|
||||
stream.close();
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
std::string read_file(const std::string& file)
|
||||
{
|
||||
std::string data;
|
||||
read_file(file, &data);
|
||||
return data;
|
||||
}
|
||||
|
||||
bool read_file(const std::string& file, std::string* data)
|
||||
{
|
||||
if (!data) return false;
|
||||
data->clear();
|
||||
|
||||
if (file_exists(file))
|
||||
{
|
||||
std::ifstream stream(file, std::ios::binary);
|
||||
if (!stream.is_open()) return false;
|
||||
|
||||
stream.seekg(0, std::ios::end);
|
||||
const std::streamsize size = stream.tellg();
|
||||
stream.seekg(0, std::ios::beg);
|
||||
|
||||
if (size > -1)
|
||||
{
|
||||
data->resize(static_cast<std::uint32_t>(size));
|
||||
stream.read(const_cast<char*>(data->data()), size);
|
||||
stream.close();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
std::size_t file_size(const std::string& file)
|
||||
{
|
||||
if (file_exists(file))
|
||||
{
|
||||
std::ifstream stream(file, std::ios::binary);
|
||||
|
||||
if (stream.good())
|
||||
{
|
||||
stream.seekg(0, std::ios::end);
|
||||
return static_cast<std::size_t>(stream.tellg());
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
17
src/utils/io.hpp
Normal file
17
src/utils/io.hpp
Normal file
@@ -0,0 +1,17 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace utils
|
||||
{
|
||||
namespace io
|
||||
{
|
||||
bool remove_file(const std::string& file);
|
||||
bool move_file(const std::string& src, const std::string& target);
|
||||
bool file_exists(const std::string& file);
|
||||
bool write_file(const std::string& file, const std::string& data, bool append = false);
|
||||
bool read_file(const std::string& file, std::string* data);
|
||||
std::string read_file(const std::string& file);
|
||||
std::size_t file_size(const std::string& file);
|
||||
}
|
||||
}
|
||||
41
src/utils/string.cpp
Normal file
41
src/utils/string.cpp
Normal file
@@ -0,0 +1,41 @@
|
||||
#include <std_include.hpp>
|
||||
#include "string.hpp"
|
||||
|
||||
namespace utils::string
|
||||
{
|
||||
std::vector<std::string> split(const std::string& s, const char delim)
|
||||
{
|
||||
std::stringstream ss(s);
|
||||
std::string item;
|
||||
std::vector<std::string> elems;
|
||||
|
||||
while (std::getline(ss, item, delim))
|
||||
{
|
||||
elems.push_back(item); // elems.push_back(std::move(item)); // if C++11
|
||||
}
|
||||
|
||||
return elems;
|
||||
}
|
||||
|
||||
std::string to_lower(const std::string& text)
|
||||
{
|
||||
std::string result;
|
||||
std::ranges::transform(text, std::back_inserter(result), [](const unsigned char input)
|
||||
{
|
||||
return static_cast<char>(std::tolower(input));
|
||||
});
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
std::string to_upper(const std::string& text)
|
||||
{
|
||||
std::string result;
|
||||
std::ranges::transform(text, std::back_inserter(result), [](const unsigned char input)
|
||||
{
|
||||
return static_cast<char>(std::toupper(input));
|
||||
});
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
9
src/utils/string.hpp
Normal file
9
src/utils/string.hpp
Normal file
@@ -0,0 +1,9 @@
|
||||
#pragma once
|
||||
|
||||
namespace utils::string
|
||||
{
|
||||
std::vector<std::string> split(const std::string& s, char delim);
|
||||
|
||||
std::string to_lower(const std::string& text);
|
||||
std::string to_upper(const std::string& text);
|
||||
}
|
||||
Reference in New Issue
Block a user