file.cpp (1588B)
1 #include "file.hpp" 2 #include "gbnl.hpp" 3 #include "header.hpp" 4 #include "../eof_item.hpp" 5 #include "../item.hpp" 6 #include "../../open.hpp" 7 8 namespace Neptools::Stcm 9 { 10 11 File::File(Source src) 12 { 13 ADD_SOURCE(Parse_(src), src); 14 } 15 16 void File::Parse_(Source& src) 17 { 18 auto root = Create<RawItem>(src); 19 SetupParseFrom(*root); 20 root->Split(root->GetSize(), Create<EofItem>()); 21 HeaderItem::CreateAndInsert({root.get(), 0}); 22 } 23 24 void File::Inspect_(std::ostream& os, unsigned indent) const 25 { 26 LIBSHIT_ASSERT(GetLabels().empty()); 27 os << "neptools.stcm.file()"; 28 InspectChildren(os, indent); 29 } 30 31 void File::SetGbnl(GbnlItem& gbnl) noexcept 32 { 33 LIBSHIT_ASSERT(&gbnl.GetUnsafeContext() == this); 34 if (!first_gbnl) first_gbnl = &gbnl; 35 } 36 37 void File::Gc() noexcept 38 { 39 for (auto it = GetChildren().begin(); it != GetChildren().end(); ) 40 if (dynamic_cast<RawItem*>(&*it) && it->GetLabels().empty()) 41 it = GetChildren().erase(it); 42 else 43 ++it; 44 } 45 46 void File::WriteTxt_(std::ostream& os) const 47 { if (first_gbnl) first_gbnl->WriteTxt(os); } 48 49 void File::ReadTxt_(std::istream& is) 50 { if (first_gbnl) first_gbnl->ReadTxt(is); } 51 52 static OpenFactory stcm_open{[](const Source& src) -> Libshit::SmartPtr<Dumpable> 53 { 54 if (src.GetSize() < sizeof(HeaderItem::Header)) return nullptr; 55 char buf[4]; 56 src.PreadGen(0, buf); 57 if (memcmp(buf, "STCM", 4) == 0) 58 return Libshit::MakeSmart<File>(src); 59 else 60 return nullptr; 61 }}; 62 63 } 64 65 #include <libshit/lua/table_ret_wrap.hpp> 66 #include "file.binding.hpp"