| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
#pragma once |
| 32 |
|
| 33 |
#include <boost/archive/binary_iarchive.hpp> |
| 34 |
#include <boost/archive/portable_binary_oarchive.hpp> |
| 35 |
#include <boost/archive/portable_binary_iarchive.hpp> |
| 36 |
#include <boost/filesystem/operations.hpp> |
| 37 |
|
| 38 |
#include "common/util.h" |
| 39 |
|
| 40 |
namespace tools |
| 41 |
{ |
| 42 |
template<class t_object> |
| 43 |
bool serialize_obj_to_file(t_object& obj, const std::string& file_path) |
| 44 |
{ |
| 45 |
TRY_ENTRY(); |
| 46 |
#if defined(_MSC_VER) |
| 47 |
|
| 48 |
HANDLE data_file_handle = ::CreateFile(file_path.c_str(), GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); |
| 49 |
if (INVALID_HANDLE_VALUE == data_file_handle) |
| 50 |
return false; |
| 51 |
|
| 52 |
int data_file_descriptor = _open_osfhandle((intptr_t)data_file_handle, 0); |
| 53 |
if (-1 == data_file_descriptor) |
| 54 |
{ |
| 55 |
::CloseHandle(data_file_handle); |
| 56 |
return false; |
| 57 |
} |
| 58 |
|
| 59 |
const std::unique_ptr<FILE, tools::close_file> data_file_file{_fdopen(data_file_descriptor, "wb")}; |
| 60 |
if (nullptr == data_file_file) |
| 61 |
{ |
| 62 |
|
| 63 |
_close(data_file_descriptor); |
| 64 |
return false; |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
std::ofstream data_file(data_file_file.get()); |
| 69 |
if (data_file.fail()) |
| 70 |
{ |
| 71 |
|
| 72 |
return false; |
| 73 |
} |
| 74 |
#else |
| 75 |
std::ofstream data_file; |
| 76 |
data_file.open(file_path , std::ios_base::binary | std::ios_base::out| std::ios::trunc); |
| 77 |
if (data_file.fail()) |
| 78 |
return false; |
| 79 |
#endif |
| 80 |
|
| 81 |
boost::archive::portable_binary_oarchive a(data_file); |
| 82 |
a << obj; |
| 83 |
if (data_file.fail()) |
| 84 |
return false; |
| 85 |
|
| 86 |
data_file.flush(); |
| 87 |
#if defined(_MSC_VER) |
| 88 |
|
| 89 |
::FlushFileBuffers(data_file_handle); |
| 90 |
#endif |
| 91 |
|
| 92 |
return true; |
| 93 |
CATCH_ENTRY_L0("serialize_obj_to_file", false); |
| 94 |
} |
| 95 |
|
| 96 |
template<class t_object> |
| 97 |
bool unserialize_obj_from_file(t_object& obj, const std::string& file_path) |
| 98 |
{ |
| 99 |
TRY_ENTRY(); |
| 100 |
|
| 101 |
std::ifstream data_file; |
| 102 |
data_file.open( file_path, std::ios_base::binary | std::ios_base::in); |
| 103 |
if(data_file.fail()) |
| 104 |
return false; |
| 105 |
try |
| 106 |
{ |
| 107 |
|
| 108 |
boost::archive::portable_binary_iarchive a(data_file); |
| 109 |
a >> obj; |
| 110 |
} |
| 111 |
catch(...) |
| 112 |
{ |
| 113 |
|
| 114 |
tools::copy_file(file_path, file_path + ".unportable"); |
| 115 |
data_file.close(); |
| 116 |
data_file.open( file_path, std::ios_base::binary | std::ios_base::in); |
| 117 |
if(data_file.fail()) |
| 118 |
return false; |
| 119 |
boost::archive::binary_iarchive a(data_file); |
| 120 |
a >> obj; |
| 121 |
} |
| 122 |
return !data_file.fail(); |
| 123 |
CATCH_ENTRY_L0("unserialize_obj_from_file", false); |
| 124 |
} |
| 125 |
} |