OSDN Git Service

Change idx sub dir's time stamp when saving idx.
[fukui-no-namari/dialektos.git] / src / bookmark_item.cxx
1 /*
2  * Copyright (C) 2009 by Aiwota Programmer
3  * aiwotaprog@tetteke.tk
4  *
5  * This file is part of Dialektos.
6  *
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.
11  *
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.
16  *
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/>.
19  */
20
21 #include "bookmark_item.hxx"
22
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>
30 #include <sstream>
31 #include <fstream>
32 #include <vector>
33 #include <string>
34 #include "convert/cp932.hxx"
35 #include "misc.hxx"
36
37
38 namespace dialektos {
39
40 namespace bookmark {
41
42
43 std::vector<Item> from_html(const std::string& html) {
44   using namespace boost::xpressive;
45
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>" >> *_;
52
53   std::vector<Item> list;
54   std::stringstream ss;
55   ss << html;
56   std::string current_category;
57   std::string line;
58   while (std::getline(ss, line)) {
59     boost::algorithm::trim_right(line);
60     const std::string utf8 = convert::cp932(line);
61
62     if (utf8.empty()) {
63       current_category.clear();
64       continue;
65     }
66
67     smatch what;
68
69     if (regex_match(utf8, what, regex_category)) {
70       current_category = what[1];
71       continue;
72     }
73
74     if (regex_match(utf8, what, regex)) {
75       Item item;
76       item.uri = what[1];
77       item.name = what[2];
78       item.categories.push_back("bbsmenu");
79       if (!current_category.empty())
80         item.categories.push_back(current_category);
81       list.push_back(item);
82     }
83   }
84
85   return list;
86 }
87
88 std::vector<Item> from_xml(const boost::filesystem::path& xml) {
89   std::vector<Item> items;
90
91   if (!boost::filesystem::exists(xml) || !boost::filesystem::is_regular(xml))
92     return items;
93
94   std::ifstream ifs(xml.file_string().c_str());
95   try {
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() << " "
100     << xml << std::endl;
101   }
102   return items;
103 }
104
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());
109   try {
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;
114   }
115 }
116
117
118 } // namespace bookmark
119
120 } // namespace dialektos