dumpable.cpp (2097B)
1 #include "dumpable.hpp" 2 3 #include "sink.hpp" 4 5 #include <libshit/platform.hpp> 6 7 #include <boost/filesystem/operations.hpp> 8 #include <fstream> 9 10 #if LIBSHIT_OS_IS_WINDOWS 11 # include <vector> 12 # define WIN32_LEAN_AND_MEAN 13 # define NOMINMAX 14 #include <windows.h> 15 16 namespace 17 { 18 struct DeleteOnExitHelper 19 { 20 ~DeleteOnExitHelper() 21 { 22 for (auto& p : pths) 23 DeleteFileW(p.c_str()); 24 } 25 26 std::vector<boost::filesystem::path> pths; 27 }; 28 void DeleteOnExit(boost::filesystem::path pth) 29 { 30 static DeleteOnExitHelper hlp; 31 hlp.pths.push_back(std::move(pth)); 32 } 33 } 34 #endif 35 36 namespace Neptools 37 { 38 39 Libshit::NotNullSharedPtr<TxtSerializable> 40 Dumpable::GetDefaultTxtSerializable( 41 const Libshit::NotNullSharedPtr<Dumpable>& thiz) 42 { LIBSHIT_THROW(Libshit::DecodeError, "Not txt-serializable file"); } 43 44 void Dumpable::Dump(const boost::filesystem::path& path) const 45 { 46 #if LIBSHIT_OS_IS_VITA 47 // no unique_path on vita 48 Dump(*Sink::ToFile(path, GetSize())); 49 #else 50 auto path2 = path; 51 { 52 auto sink = Sink::ToFile(path2+=boost::filesystem::unique_path(), GetSize()); 53 Dump(*sink); 54 } 55 56 #if LIBSHIT_OS_IS_WINDOWS 57 if (LIBSHIT_OS_IS_WINDOWS && boost::filesystem::is_regular_file(path)) 58 { 59 auto path3 = path; 60 boost::filesystem::rename(path, path3+=boost::filesystem::unique_path()); 61 if (!DeleteFileW(path3.c_str()) && GetLastError() == ERROR_ACCESS_DENIED) 62 DeleteOnExit(std::move(path3)); 63 } 64 #endif 65 boost::filesystem::rename(path2, path); 66 #endif 67 } 68 69 void Dumpable::Inspect(const boost::filesystem::path& path) const 70 { 71 return Inspect(OpenOut(path)); 72 } 73 74 std::ostream& Dumpable::Indent(std::ostream& os, unsigned indent) 75 { 76 std::ostreambuf_iterator<char> it{os}; 77 for (size_t i = 0; i < 2*indent; ++i) *it = ' '; 78 return os; 79 } 80 81 82 std::ostream& operator<<(std::ostream& os, const Dumpable& dmp) 83 { 84 dmp.Inspect(os); 85 return os; 86 } 87 88 std::string Dumpable::Inspect() const 89 { 90 std::stringstream ss; 91 Inspect(ss); 92 return ss.str(); 93 } 94 95 } 96 97 #include "dumpable.binding.hpp"