2 changed files with 105 additions and 0 deletions
			
			
		@ -0,0 +1,70 @@ | 
				
			|||||||
 | 
					/*
 | 
				
			||||||
 | 
					 * Copyright (C) 2021 Riyyi | 
				
			||||||
 | 
					 * | 
				
			||||||
 | 
					 * SPDX-License-Identifier: MIT | 
				
			||||||
 | 
					 */ | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#include <cassert> // assert | 
				
			||||||
 | 
					#include <cstdint> // int32_t | 
				
			||||||
 | 
					#include <fstream> // ifstream, ios, ofstream | 
				
			||||||
 | 
					#include <memory>  // make_unique | 
				
			||||||
 | 
					#include <string> | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#include "util/file.h" | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					namespace Util { | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					File::File(const std::string& path) | 
				
			||||||
 | 
						: m_path(path) | 
				
			||||||
 | 
					{ | 
				
			||||||
 | 
						// Create input stream object and open file
 | 
				
			||||||
 | 
						std::ifstream file(path, std::ios::in); | 
				
			||||||
 | 
						assert(file.is_open()); | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// Get length of the file
 | 
				
			||||||
 | 
						file.seekg(0, std::ios::end); | 
				
			||||||
 | 
						int32_t size = file.tellg(); | 
				
			||||||
 | 
						file.seekg(0, std::ios::beg); | 
				
			||||||
 | 
						assert(size != -1); | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// 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); | 
				
			||||||
 | 
					} | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					File::~File() | 
				
			||||||
 | 
					{ | 
				
			||||||
 | 
					} | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// -----------------------------------------
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					void File::clear() | 
				
			||||||
 | 
					{ | 
				
			||||||
 | 
						m_data.clear(); | 
				
			||||||
 | 
					} | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					File& File::append(std::string data) | 
				
			||||||
 | 
					{ | 
				
			||||||
 | 
						m_data.append(data); | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						return *this; | 
				
			||||||
 | 
					} | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					File& File::flush() | 
				
			||||||
 | 
					{ | 
				
			||||||
 | 
						// Create output stream object and open file
 | 
				
			||||||
 | 
						std::ofstream file(m_path, std::ios::out | std::ios::trunc); | 
				
			||||||
 | 
						assert(file.is_open()); | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// Write data to disk
 | 
				
			||||||
 | 
						file.write(m_data.c_str(), m_data.size()); | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						return *this; | 
				
			||||||
 | 
					} | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					} // namespace Util
 | 
				
			||||||
@ -0,0 +1,35 @@ | 
				
			|||||||
 | 
					/*
 | 
				
			||||||
 | 
					 * Copyright (C) 2021 Riyyi | 
				
			||||||
 | 
					 * | 
				
			||||||
 | 
					 * SPDX-License-Identifier: MIT | 
				
			||||||
 | 
					 */ | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#ifndef FILE_H | 
				
			||||||
 | 
					#define FILE_H | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#include <string> | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					namespace Util { | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					class File { | 
				
			||||||
 | 
					public: | 
				
			||||||
 | 
						File(const std::string& path); | 
				
			||||||
 | 
						virtual ~File(); | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						void clear(); | 
				
			||||||
 | 
						File& append(std::string data); | 
				
			||||||
 | 
						File& flush(); | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						const char* c_str() const { return m_data.c_str(); } | 
				
			||||||
 | 
						const std::string& data() const { return m_data; } | 
				
			||||||
 | 
						const std::string& path() const { return m_path; } | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					private: | 
				
			||||||
 | 
						std::string m_path; | 
				
			||||||
 | 
						std::string m_data; | 
				
			||||||
 | 
					}; | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					} // namespace Util
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#endif // FILE_H
 | 
				
			||||||
					Loading…
					
					
				
		Reference in new issue