From 36e51ac77ffcdad877e4e3399ede4e482a33baf0 Mon Sep 17 00:00:00 2001 From: Aiwota Programmer Date: Sat, 27 Jun 2009 16:38:22 +0900 Subject: [PATCH] Add a write-out method to ThreadIdx. --- src/thread_idx.cxx | 24 ++++++++++++++++++++---- src/thread_idx.hxx | 2 ++ 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/src/thread_idx.cxx b/src/thread_idx.cxx index a5ae829..c35a22f 100644 --- a/src/thread_idx.cxx +++ b/src/thread_idx.cxx @@ -22,8 +22,8 @@ #include "thread_idx.hxx" #include +#include #include -#include #include #include #include @@ -33,17 +33,33 @@ namespace dialektos { +ThreadIdx::ThreadIdx() : title_(), line_count_(0), last_modified_(), etag_() +{} + +void ThreadIdx::to_xml(const boost::filesystem::path& xml) { + std::ofstream ofs(xml.file_string().c_str()); + try { + boost::archive::xml_oarchive oa(ofs); + oa << boost::serialization::make_nvp("ThreadIdx", *this); + } catch (const boost::archive::archive_exception& e) { + std::cerr << e.what() << " " << xml.file_string() << std::endl; + } + ofs.close(); +} ThreadIdx ThreadIdx::from_xml(const boost::filesystem::path& xml) { ThreadIdx idx; + if (!boost::filesystem::exists(xml) || + !boost::filesystem::is_regular_file(xml)) + return idx; + std::ifstream ifs(xml.file_string().c_str()); try { boost::archive::xml_iarchive ia(ifs); ia >> boost::serialization::make_nvp("ThreadIdx", idx); - } catch (const boost::exception& e) { - // TODO thread safety. - std::cerr << boost::format("cannot read %1%") % xml.file_string() << std::endl; + } catch (const boost::archive::archive_exception& e) { + std::cerr << e.what() << " " << xml.file_string() << std::endl; return ThreadIdx(); } ifs.close(); diff --git a/src/thread_idx.hxx b/src/thread_idx.hxx index 9995161..4c2cc72 100644 --- a/src/thread_idx.hxx +++ b/src/thread_idx.hxx @@ -32,12 +32,14 @@ namespace dialektos { struct ThreadIdx { + ThreadIdx(); std::string title_; int line_count_; std::string last_modified_; std::string etag_; + void to_xml(const boost::filesystem::path&); static ThreadIdx from_xml(const boost::filesystem::path&); private: -- 2.11.0