libultraship
Reimplementations of libultra (N64 SDK) functions for modern hardware
Loading...
Searching...
No Matches
FileHelper.h
Go to the documentation of this file.
1#pragma once
2
3#include <cstdio>
4#include <fstream>
5#include <iostream>
6#include <string>
7#include <vector>
8#include "PathHelper.h"
9#include "Directory.h"
10
11namespace Ship {
19 public:
25 static bool Exists(const fs::path& filePath) {
26 std::ifstream file(filePath, std::ios::in | std::ios::binary | std::ios::ate);
27 return file.good();
28 }
29
35 static std::vector<uint8_t> ReadAllBytes(const fs::path& filePath) {
36 std::ifstream file(filePath, std::ios::in | std::ios::binary | std::ios::ate);
37
38 if (!file) {
39 return std::vector<uint8_t>();
40 }
41
42 int32_t fileSize = (int32_t)file.tellg();
43 file.seekg(0);
44 char* data = nullptr;
45 std::vector<uint8_t> result;
46
47 try {
48 data = new char[fileSize];
49 file.read(data, fileSize);
50 result = std::vector<uint8_t>(data, data + fileSize);
51 } catch (const std::exception& e) {
52 delete[] data;
53 throw e;
54 }
55
56 delete[] data;
57
58 return result;
59 };
60
66 static std::string ReadAllText(const fs::path& filePath) {
67 std::ifstream file(filePath, std::ios::in | std::ios::binary | std::ios::ate);
68 int32_t fileSize = (int32_t)file.tellg();
69 file.seekg(0);
70 char* data = nullptr;
71 std::string str;
72
73 try {
74 data = new char[fileSize + 1];
75 memset(data, 0, fileSize + 1);
76 file.read(data, fileSize);
77 str = std::string((const char*)data);
78 } catch (const std::exception& e) {
79 delete[] data;
80 throw e;
81 }
82
83 delete[] data;
84
85 return str;
86 };
87
93 static std::vector<std::string> ReadAllLines(const fs::path& filePath) {
94 std::string text = ReadAllText(filePath);
95 std::vector<std::string> lines = StringHelper::Split(text, "\n");
96
97 return lines;
98 };
99
106 static void WriteAllBytes(const fs::path& filePath, const std::vector<uint8_t>& data) {
107 if (!Directory::Exists(PathHelper::GetDirectoryName(filePath))) {
108 Directory::MakeDirectory(PathHelper::GetDirectoryName(filePath).string());
109 }
110 std::ofstream file(filePath, std::ios::binary);
111 file.write((char*)data.data(), data.size());
112 };
113
120 static void WriteAllBytes(const std::string& filePath, const std::vector<char>& data) {
121 if (!Directory::Exists(PathHelper::GetDirectoryName(filePath))) {
122 Directory::MakeDirectory(PathHelper::GetDirectoryName(filePath).string());
123 }
124
125 std::ofstream file(filePath, std::ios::binary);
126 file.write((char*)data.data(), data.size());
127 };
128
136 static void WriteAllBytes(const std::string& filePath, const char* data, int dataSize) {
137 if (!Directory::Exists(PathHelper::GetDirectoryName(filePath))) {
138 Directory::MakeDirectory(PathHelper::GetDirectoryName(filePath).string());
139 }
140 std::ofstream file(filePath, std::ios::binary);
141 file.write((char*)data, dataSize);
142 };
143
149 static void WriteAllText(const fs::path& filePath, const std::string& text) {
150 std::ofstream file(filePath, std::ios::out | std::ios::binary);
151 file.write(text.c_str(), text.size());
152 }
153};
154} // namespace Ship
Utility class providing static helpers for reading and writing files on disk.
Definition FileHelper.h:18
static void WriteAllBytes(const std::string &filePath, const std::vector< char > &data)
Writes a char vector to a file, overwriting any existing content.
Definition FileHelper.h:120
static void WriteAllBytes(const std::string &filePath, const char *data, int dataSize)
Writes raw data to a file from a pointer and size, overwriting any existing content.
Definition FileHelper.h:136
static bool Exists(const fs::path &filePath)
Checks whether a file exists and is readable.
Definition FileHelper.h:25
static std::vector< uint8_t > ReadAllBytes(const fs::path &filePath)
Reads the entire contents of a file into a byte vector.
Definition FileHelper.h:35
static void WriteAllText(const fs::path &filePath, const std::string &text)
Writes a string to a file as text, overwriting any existing content.
Definition FileHelper.h:149
static void WriteAllBytes(const fs::path &filePath, const std::vector< uint8_t > &data)
Writes a byte vector to a file, overwriting any existing content.
Definition FileHelper.h:106
static std::vector< std::string > ReadAllLines(const fs::path &filePath)
Reads a file and splits it into lines.
Definition FileHelper.h:93
static std::string ReadAllText(const fs::path &filePath)
Reads the entire contents of a file as a text string.
Definition FileHelper.h:66
static fs::path GetDirectoryName(const fs::path &path)
Returns the parent directory of the given path.
Definition PathHelper.h:81
Core namespace for the libultraship engine framework.
Definition gfx_direct3d_common.h:14