]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/flyweight/example/serialization.cpp
1 /* Boost.Flyweight example of serialization.
3 * Copyright 2006-2014 Joaquin M Lopez Munoz.
4 * Distributed under the Boost Software License, Version 1.0.
5 * (See accompanying file LICENSE_1_0.txt or copy at
6 * http://www.boost.org/LICENSE_1_0.txt)
8 * See http://www.boost.org/libs/flyweight for library home page.
11 #include <boost/config.hpp> /* keep it first to prevent nasty warns in MSVC */
13 #include <boost/archive/text_iarchive.hpp>
14 #include <boost/archive/text_oarchive.hpp>
15 #include <boost/flyweight.hpp>
16 #include <boost/flyweight/serialize.hpp>
17 #include <boost/serialization/vector.hpp>
18 #include <boost/tokenizer.hpp>
29 using namespace boost::flyweights
;
31 typedef flyweight
<std::string
> fw_string
;
32 typedef std::vector
<fw_string
> text_container
;
34 /* Read a text file into a text_container and serialize to an archive. */
36 void save_serialization_file()
38 /* Define a tokenizer on std::istreambuf. */
40 typedef std::istreambuf_iterator
<char> char_iterator
;
41 typedef boost::tokenizer
<
42 boost::char_separator
<char>,
46 std::cout
<<"enter input text file name: ";
48 std::getline(std::cin
,in
);
49 std::ifstream
ifs(in
.c_str());
51 std::cout
<<"can't open "<<in
<<std::endl
;
52 std::exit(EXIT_FAILURE
);
55 /* Tokenize using space and common punctuaction as separators, and
56 * keeping the separators.
59 tokenizer tok
=tokenizer(
60 char_iterator(ifs
),char_iterator(),
61 boost::char_separator
<char>(
63 "\t\n\r !\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~"));
65 for(tokenizer::iterator it
=tok
.begin();it
!=tok
.end();++it
){
66 txt
.push_back(fw_string(*it
));
69 std::cout
<<"enter output serialization file name: ";
71 std::getline(std::cin
,out
);
72 std::ofstream
ofs(out
.c_str());
74 std::cout
<<"can't open "<<out
<<std::endl
;
75 std::exit(EXIT_FAILURE
);
77 boost::archive::text_oarchive
oa(ofs
);
78 oa
<<const_cast<const text_container
&>(txt
);
81 /* Read a serialization archive and save the result to a text file. */
83 void load_serialization_file()
85 std::cout
<<"enter input serialization file name: ";
87 std::getline(std::cin
,in
);
88 std::ifstream
ifs(in
.c_str());
90 std::cout
<<"can't open "<<in
<<std::endl
;
91 std::exit(EXIT_FAILURE
);
93 boost::archive::text_iarchive
ia(ifs
);
97 std::cout
<<"enter output text file name: ";
99 std::getline(std::cin
,out
);
100 std::ofstream
ofs(out
.c_str());
102 std::cout
<<"can't open "<<out
<<std::endl
;
103 std::exit(EXIT_FAILURE
);
106 txt
.begin(),txt
.end(),
107 std::ostream_iterator
<std::string
>(ofs
));
113 std::cout
<<"1 load a text file and save it as a serialization file\n"
114 "2 load a serialization file and save it as a text file\n";
116 std::cout
<<"select option, enter to exit: ";
118 std::getline(std::cin
,str
);
119 if(str
.empty())break;
120 std::istringstream
istr(str
);
123 if(option
==1)save_serialization_file();
124 else if(option
==2)load_serialization_file();
127 catch(const std::exception
& e
){
128 std::cout
<<"error: "<<e
.what()<<std::endl
;
129 std::exit(EXIT_FAILURE
);