SpiecsEngine
 
Loading...
Searching...
No Matches
FileLibrary.cpp
Go to the documentation of this file.
1/**
2* @file FileLibrary.cpp
3* @brief The FileLibrary Class Implementation.
4* @author Travis Vroman.
5*/
6
7#include "Pchheader.h"
8#include "FileLibrary.h"
9
10#include <commdlg.h>
11
12namespace Spices {
13
14 bool FileLibrary::FileLibrary_Exists(const char* path)
15 {
17
18 struct _stat buffer {};
19 return _stat (path, &buffer) == 0;
20 }
21
22 bool FileLibrary::FileLibrary_Open(const char* path, FileModes mode, bool binary, FileHandle* out_handle)
23 {
25
26 out_handle->is_valid = false;
27 out_handle->handle = nullptr;
28 const char* mode_str;
29
30 if ((mode & FILE_MODE_READ) != 0 && (mode & FILE_MODE_WRITE) != 0)
31 {
32 mode_str = binary ? "w+b" : "w+";
33 }
34 else if ((mode & FILE_MODE_READ) != 0 && (mode & FILE_MODE_WRITE) == 0)
35 {
36 mode_str = binary ? "rb" : "r";
37 }
38 else if ((mode & FILE_MODE_READ) == 0 && (mode & FILE_MODE_WRITE) != 0)
39 {
40 mode_str = binary ? "wb" : "w";
41 }
42 else
43 {
44 SPICES_CORE_INFO("Invalid mode passed while trying to open file")
45 return false;
46 }
47
48 FILE* file;
49 auto state = fopen_s(&file, path, mode_str);
50
51 if (!file)
52 {
53 std::stringstream ss;
54 ss << "Error opening file: " << path;
55
56 SPICES_CORE_WARN(ss.str().c_str())
57 return false;
58 }
59
60 out_handle->handle = file;
61 out_handle->is_valid = true;
62
63 return true;
64 }
65
67 {
69
70 if (handle->handle)
71 {
72 auto state = fclose(static_cast<FILE*>(handle->handle));
73 handle->handle = nullptr;
74 handle->is_valid = false;
75 }
76 }
77
78 bool FileLibrary::FileLibrary_Size(const FileHandle* handle, uint64_t* out_size)
79 {
81
82 if (handle->handle)
83 {
84 auto state = fseek(static_cast<FILE*>(handle->handle), 0, SEEK_END);
85 *out_size = ftell(static_cast<FILE*>(handle->handle));
86 rewind(static_cast<FILE*>(handle->handle));
87 return true;
88 }
89 return false;
90 }
91
92 bool FileLibrary::FileLibrary_Read(const FileHandle* handle, uint64_t data_size, void* out_data, uint64_t* out_bytes_read)
93 {
95
96 if (handle->handle && out_data)
97 {
98 *out_bytes_read = fread(out_data, 1, data_size, static_cast<FILE*>(handle->handle));
99 if (*out_bytes_read != data_size)
100 {
101 return false;
102 }
103 return true;
104 }
105 return false;
106 }
107
108 bool FileLibrary::FileLibrary_Read_Line(const FileHandle* handle, uint64_t max_length, char* line_buf, uint64_t* out_line_length)
109 {
111
112 if (handle->handle && line_buf && out_line_length && max_length > 0)
113 {
114 if (fgets(line_buf, static_cast<int>(max_length), static_cast<FILE*>(handle->handle)) != nullptr)
115 {
116 *out_line_length = strlen(line_buf);
117 return true;
118 }
119 }
120 return false;
121 }
122
123 bool FileLibrary::FileLibrary_Read_all_bytes(const FileHandle* handle, char* out_bytes, uint64_t* out_bytes_read)
124 {
126
127 if (handle->handle && out_bytes && out_bytes_read)
128 {
129 uint64_t size = 0;
130 if (!FileLibrary_Size(handle, &size))
131 {
132 return false;
133 }
134
135 *out_bytes_read = fread(out_bytes, 1, size, static_cast<FILE*>(handle->handle));
136 return *out_bytes_read == size;
137 }
138 return false;
139 }
140
141 bool FileLibrary::FileLibrary_Write(const FileHandle* handle, uint64_t data_size, const void* data, uint64_t* out_bytes_written)
142 {
144
145 if (handle->handle)
146 {
147 *out_bytes_written = fwrite(data, 1, data_size, static_cast<FILE*>(handle->handle));
148 if (*out_bytes_written != data_size)
149 {
150 return false;
151 }
152 auto state = fflush(static_cast<FILE*>(handle->handle));
153 return true;
154 }
155 return false;
156 }
157
158 bool FileLibrary::FileLibrary_Write_Line(const FileHandle* handle, const char* text)
159 {
161
162 if (handle->handle)
163 {
164 int result = fputs(text, static_cast<FILE*>(handle->handle));
165 if (result != EOF)
166 {
167 result = fputc('\n', static_cast<FILE*>(handle->handle));
168 }
169
170 // Make sure to flush the stream so it is written to the file immediately.
171 // This prevents data loss in the event of a crash.
172 auto state = fflush(static_cast<FILE*>(handle->handle));
173 return result != EOF;
174 }
175 return false;
176 }
177
178 std::string FileLibrary::FileLibrary_OpenInExplore(const char* filter, HWND hwnd)
179 {
181
182 OPENFILENAMEA ofn; // common dialog box structure
183 CHAR szFile[260] = { 0 }; // if using TCHAR macros
184 // Initialize OPEN FILENAME
185 ZeroMemory(&ofn, sizeof(OPENFILENAMEA));
186 ofn.lStructSize = sizeof(OPENFILENAMEA);
187 ofn.hwndOwner = hwnd;
188 ofn.lpstrFile = szFile;
189 ofn.nMaxFile = sizeof(szFile);
190 ofn.lpstrFilter = filter;
191 ofn.nFilterIndex = 1;
192 ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST | OFN_NOCHANGEDIR;
193 if (GetOpenFileNameA(&ofn) == TRUE)
194 {
195 return ofn.lpstrFile;
196 }
197 return "";
198 }
199
200 std::string FileLibrary::FileLibrary_SaveInExplore(const char* filter, HWND hwnd)
201 {
203
204 OPENFILENAMEA ofn; // common dialog box structure
205 CHAR szFile[260] = { 0 }; // if using TCHAR macros
206 // Initialize OPEN FILENAME
207 ZeroMemory(&ofn, sizeof(OPENFILENAMEA));
208 ofn.lStructSize = sizeof(OPENFILENAMEA);
209 ofn.hwndOwner = hwnd;
210 ofn.lpstrFile = szFile;
211 ofn.nMaxFile = sizeof(szFile);
212 ofn.lpstrFilter = filter;
213 ofn.nFilterIndex = 1;
214 ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST | OFN_NOCHANGEDIR;
215 if (GetSaveFileNameA(&ofn) == TRUE)
216 {
217 return ofn.lpstrFile;
218 }
219 return "";
220 }
221
222 bool FileLibrary::FileLibrary_CopyFile(std::string srcFilePath, std::string dstFilePath)
223 {
225
226 if (!FileLibrary_Exists(srcFilePath.c_str()))
227 {
228 std::stringstream ss;
229 ss << "File path: " << srcFilePath << " was not found.";
230
231 SPICES_CORE_WARN(ss.str())
232 return false;
233 }
234
235 std::ifstream src(srcFilePath, std::ios::binary);
236 std::ofstream dst(dstFilePath, std::ios::binary);
237
238 dst << src.rdbuf();
239
240 return true;
241 }
242
243 bool FileLibrary::FileLibrary_Delete(const char* filePath)
244 {
246
247 if (FileLibrary_Exists(filePath))
248 {
249 if (remove(filePath) != 0)
250 {
251 std::stringstream ss;
252 ss << "File path: " << filePath << " delete failed.";
253
254 SPICES_CORE_WARN(ss.str())
255 return false;
256 }
257 }
258
259 return true;
260 }
261}
#define SPICES_PROFILE_ZONE
static bool FileLibrary_Size(const FileHandle *handle, uint64_t *out_size)
Calculate The file size.
static bool FileLibrary_Read(const FileHandle *handle, uint64_t data_size, void *out_data, uint64_t *out_bytes_read)
Read Specific size of data form the current file handle pointer, and move pointer the same size.
static bool FileLibrary_Write_Line(const FileHandle *handle, const char *text)
Write one line data to the file handle pointer.
static bool FileLibrary_Write(const FileHandle *handle, uint64_t data_size, const void *data, uint64_t *out_bytes_written)
Write given data to the file handle pointer.
static bool FileLibrary_Open(const char *path, FileModes mode, bool binary, FileHandle *out_handle)
Open the file using given string.
static void FileLibrary_Close(FileHandle *handle)
Close the file by the file handle.
static bool FileLibrary_Delete(const char *filePath)
Delete a file from disk.
static bool FileLibrary_CopyFile(std::string srcFilePath, std::string dstFilePath)
Copy a file to dst path.
static bool FileLibrary_Read_Line(const FileHandle *handle, uint64_t max_length, char *line_buf, uint64_t *out_line_length)
Read one line from the current file handle pointer, and move pointer the same size.
static bool FileLibrary_Exists(const char *path)
Determine whether the given string is existing a file.
static bool FileLibrary_Read_all_bytes(const FileHandle *handle, char *out_bytes, uint64_t *out_bytes_read)
Read all data form the current file handle pointer.
File Static Function Library.
Definition FileLibrary.h:49
FileModes
file mode
Definition FileLibrary.h:32
@ FILE_MODE_READ
model : read
Definition FileLibrary.h:37
@ FILE_MODE_WRITE
model : write
Definition FileLibrary.h:42
void * handle
FILE* handle. Need cast while use.
Definition FileLibrary.h:21
bool is_valid
Is this handle Valid.
Definition FileLibrary.h:26
This Struct is FILE* handle pointer Wrapper.
Definition FileLibrary.h:15