files module update
This commit is contained in:
@@ -224,12 +224,12 @@ void WorldFiles::writePlayer(Player* player){
|
|||||||
dst[offset++] = player->flight * PLAYER_FLAG_FLIGHT |
|
dst[offset++] = player->flight * PLAYER_FLAG_FLIGHT |
|
||||||
player->noclip * PLAYER_FLAG_NOCLIP;
|
player->noclip * PLAYER_FLAG_NOCLIP;
|
||||||
|
|
||||||
write_binary_file(getPlayerFile(), (const char*)dst, sizeof(dst));
|
files::write_binary_file(getPlayerFile(), (const char*)dst, sizeof(dst));
|
||||||
}
|
}
|
||||||
|
|
||||||
bool WorldFiles::readPlayer(Player* player) {
|
bool WorldFiles::readPlayer(Player* player) {
|
||||||
size_t length = 0;
|
size_t length = 0;
|
||||||
ubyte* data = (ubyte*)read_binary_file(getPlayerFile(), length);
|
ubyte* data = (ubyte*)files::read_binary_file(getPlayerFile(), length);
|
||||||
if (data == nullptr){
|
if (data == nullptr){
|
||||||
std::cerr << "could not to read player.bin (ignored)" << std::endl;
|
std::cerr << "could not to read player.bin (ignored)" << std::endl;
|
||||||
return false;
|
return false;
|
||||||
|
|||||||
+29
-17
@@ -5,17 +5,14 @@
|
|||||||
#include <memory>
|
#include <memory>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
bool write_binary_file_part(std::string filename, const char* data, size_t offset, size_t size){
|
using std::ios;
|
||||||
std::ofstream output(filename, std::ios::out | std::ios::binary | std::ios::in);
|
using std::string;
|
||||||
if (!output.is_open())
|
using std::unique_ptr;
|
||||||
return false;
|
using std::ifstream;
|
||||||
output.seekp(offset);
|
using std::ofstream;
|
||||||
output.write(data, size);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool write_binary_file(std::string filename, const char* data, size_t size) {
|
bool files::write_binary_file(string filename, const char* data, size_t size) {
|
||||||
std::ofstream output(filename, std::ios::binary);
|
ofstream output(filename, ios::binary);
|
||||||
if (!output.is_open())
|
if (!output.is_open())
|
||||||
return false;
|
return false;
|
||||||
output.write(data, size);
|
output.write(data, size);
|
||||||
@@ -23,8 +20,8 @@ bool write_binary_file(std::string filename, const char* data, size_t size) {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned int append_binary_file(std::string filename, const char* data, size_t size) {
|
unsigned int files::append_binary_file(string filename, const char* data, size_t size) {
|
||||||
std::ofstream output(filename, std::ios::binary | std::ios::app);
|
ofstream output(filename, ios::binary | ios::app);
|
||||||
if (!output.is_open())
|
if (!output.is_open())
|
||||||
return 0;
|
return 0;
|
||||||
unsigned int position = output.tellp();
|
unsigned int position = output.tellp();
|
||||||
@@ -33,8 +30,8 @@ unsigned int append_binary_file(std::string filename, const char* data, size_t s
|
|||||||
return position;
|
return position;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool read_binary_file(std::string filename, char* data, size_t size) {
|
bool files::read_binary_file(string filename, char* data, size_t size) {
|
||||||
std::ifstream output(filename, std::ios::binary);
|
ifstream output(filename, ios::binary);
|
||||||
if (!output.is_open())
|
if (!output.is_open())
|
||||||
return false;
|
return false;
|
||||||
output.read(data, size);
|
output.read(data, size);
|
||||||
@@ -42,16 +39,31 @@ bool read_binary_file(std::string filename, char* data, size_t size) {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
char* read_binary_file(std::string filename, size_t& length) {
|
char* files::read_binary_file(string filename, size_t& length) {
|
||||||
std::ifstream input(filename, std::ios::binary);
|
ifstream input(filename, ios::binary);
|
||||||
if (!input.is_open())
|
if (!input.is_open())
|
||||||
return nullptr;
|
return nullptr;
|
||||||
input.seekg(0, std::ios_base::end);
|
input.seekg(0, std::ios_base::end);
|
||||||
length = input.tellg();
|
length = input.tellg();
|
||||||
input.seekg(0, std::ios_base::beg);
|
input.seekg(0, std::ios_base::beg);
|
||||||
|
|
||||||
std::unique_ptr<char> data {new char[length]};
|
unique_ptr<char> data {new char[length]};
|
||||||
input.read(data.get(), length);
|
input.read(data.get(), length);
|
||||||
input.close();
|
input.close();
|
||||||
return data.release();
|
return data.release();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::string files::read_string(string filename) {
|
||||||
|
size_t size;
|
||||||
|
unique_ptr<char> chars (read_binary_file(filename, size));
|
||||||
|
return string(chars.get(), size);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool files::write_string(string filename, const string content) {
|
||||||
|
ofstream file(filename);
|
||||||
|
if (!file) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
file << content;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
+8
-4
@@ -4,9 +4,13 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
#include "../typedefs.h"
|
#include "../typedefs.h"
|
||||||
|
|
||||||
extern bool write_binary_file(std::string filename, const char* data, size_t size);
|
namespace files {
|
||||||
extern unsigned int append_binary_file(std::string filename, const char* data, size_t size);
|
extern bool write_binary_file(std::string filename, const char* data, size_t size);
|
||||||
extern bool read_binary_file(std::string filename, char* data, size_t size);
|
extern unsigned int append_binary_file(std::string filename, const char* data, size_t size);
|
||||||
extern char* read_binary_file(std::string filename, size_t& length);
|
extern bool read_binary_file(std::string filename, char* data, size_t size);
|
||||||
|
extern char* read_binary_file(std::string filename, size_t& length);
|
||||||
|
extern std::string read_string(std::string filename);
|
||||||
|
extern bool write_string(std::string filename, const std::string content);
|
||||||
|
}
|
||||||
|
|
||||||
#endif /* FILES_FILES_H_ */
|
#endif /* FILES_FILES_H_ */
|
||||||
|
|||||||
Reference in New Issue
Block a user