2 * Copyright (C) 2009 by Aiwota Programmer
3 * aiwotaprog@tetteke.tk
5 * This file is part of Dialektos.
7 * Dialektos is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
12 * Dialektos is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Dialektos. If not, see <http://www.gnu.org/licenses/>.
21 #include "bookmark_item.hxx"
23 #include <boost/archive/xml_iarchive.hpp>
24 #include <boost/archive/xml_oarchive.hpp>
25 #include <boost/serialization/nvp.hpp>
26 #include <boost/serialization/vector.hpp>
27 #include <boost/xpressive/xpressive.hpp>
28 #include <boost/algorithm/string.hpp>
29 #include <boost/filesystem/operations.hpp>
34 #include "convert/cp932.hxx"
43 std::vector<Item> from_html(const std::string& html) {
44 using namespace boost::xpressive;
46 const sregex regex = as_xpr("<A HREF=")
47 >> (s1=as_xpr("http://") >> +~set[as_xpr('>')|as_xpr(' ')])
48 >> *~as_xpr('>') >> '>'
49 >> (s2=+~as_xpr('<')) >> as_xpr("</A>") >> *_;
50 const sregex regex_category = "<BR><BR><B>"
51 >> (s1=+~as_xpr('<')) >> "</B><BR>" >> *_;
53 std::vector<Item> list;
56 std::string current_category;
58 while (std::getline(ss, line)) {
59 boost::algorithm::trim_right(line);
60 const std::string utf8 = convert::cp932(line);
63 current_category.clear();
69 if (regex_match(utf8, what, regex_category)) {
70 current_category = what[1];
74 if (regex_match(utf8, what, regex)) {
78 item.categories.push_back("bbsmenu");
79 if (!current_category.empty())
80 item.categories.push_back(current_category);
88 std::vector<Item> from_xml(const boost::filesystem::path& xml) {
89 std::vector<Item> items;
91 if (!boost::filesystem::exists(xml) || !boost::filesystem::is_regular(xml))
94 std::ifstream ifs(xml.file_string().c_str());
96 boost::archive::xml_iarchive ia(ifs);
97 ia >> boost::serialization::make_nvp("Bookmarks", items);
98 } catch (const boost::archive::archive_exception& e) {
99 std::cerr << "bookmark::from_xml(): " << e.what() << " "
105 void to_xml(const boost::filesystem::path& xml,
106 const std::vector<Item>& items) {
107 if (!misc::create_directories(xml.parent_path())) return;
108 std::ofstream ofs(xml.file_string().c_str());
110 boost::archive::xml_oarchive oa(ofs);
111 oa << boost::serialization::make_nvp("Bookmarks", items);
112 } catch (const boost::archive::archive_exception& e) {
113 std::cerr << "bookmark::to_xml(): " << e.what() << " " << xml << std::endl;
118 } // namespace bookmark
120 } // namespace dialektos