]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/flyweight/example/serialization.cpp
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / boost / libs / flyweight / example / serialization.cpp
1 /* Boost.Flyweight example of serialization.
2 *
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)
7 *
8 * See http://www.boost.org/libs/flyweight for library home page.
9 */
10
11 #include <boost/config.hpp> /* keep it first to prevent nasty warns in MSVC */
12 #include <algorithm>
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>
19 #include <cstdio>
20 #include <cstdlib>
21 #include <fstream>
22 #include <iostream>
23 #include <iterator>
24 #include <sstream>
25 #include <stdexcept>
26 #include <string>
27 #include <vector>
28
29 using namespace boost::flyweights;
30
31 typedef flyweight<std::string> fw_string;
32 typedef std::vector<fw_string> text_container;
33
34 /* Read a text file into a text_container and serialize to an archive. */
35
36 void save_serialization_file()
37 {
38 /* Define a tokenizer on std::istreambuf. */
39
40 typedef std::istreambuf_iterator<char> char_iterator;
41 typedef boost::tokenizer<
42 boost::char_separator<char>,
43 char_iterator
44 > tokenizer;
45
46 std::cout<<"enter input text file name: ";
47 std::string in;
48 std::getline(std::cin,in);
49 std::ifstream ifs(in.c_str());
50 if(!ifs){
51 std::cout<<"can't open "<<in<<std::endl;
52 std::exit(EXIT_FAILURE);
53 }
54
55 /* Tokenize using space and common punctuaction as separators, and
56 * keeping the separators.
57 */
58
59 tokenizer tok=tokenizer(
60 char_iterator(ifs),char_iterator(),
61 boost::char_separator<char>(
62 "",
63 "\t\n\r !\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~"));
64 text_container txt;
65 for(tokenizer::iterator it=tok.begin();it!=tok.end();++it){
66 txt.push_back(fw_string(*it));
67 }
68
69 std::cout<<"enter output serialization file name: ";
70 std::string out;
71 std::getline(std::cin,out);
72 std::ofstream ofs(out.c_str());
73 if(!ofs){
74 std::cout<<"can't open "<<out<<std::endl;
75 std::exit(EXIT_FAILURE);
76 }
77 boost::archive::text_oarchive oa(ofs);
78 oa<<const_cast<const text_container&>(txt);
79 }
80
81 /* Read a serialization archive and save the result to a text file. */
82
83 void load_serialization_file()
84 {
85 std::cout<<"enter input serialization file name: ";
86 std::string in;
87 std::getline(std::cin,in);
88 std::ifstream ifs(in.c_str());
89 if(!ifs){
90 std::cout<<"can't open "<<in<<std::endl;
91 std::exit(EXIT_FAILURE);
92 }
93 boost::archive::text_iarchive ia(ifs);
94 text_container txt;
95 ia>>txt;
96
97 std::cout<<"enter output text file name: ";
98 std::string out;
99 std::getline(std::cin,out);
100 std::ofstream ofs(out.c_str());
101 if(!ofs){
102 std::cout<<"can't open "<<out<<std::endl;
103 std::exit(EXIT_FAILURE);
104 }
105 std::copy(
106 txt.begin(),txt.end(),
107 std::ostream_iterator<std::string>(ofs));
108 }
109
110 int main()
111 {
112 try{
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";
115 for(;;){
116 std::cout<<"select option, enter to exit: ";
117 std::string str;
118 std::getline(std::cin,str);
119 if(str.empty())break;
120 std::istringstream istr(str);
121 int option=-1;
122 istr>>option;
123 if(option==1)save_serialization_file();
124 else if(option==2)load_serialization_file();
125 }
126 }
127 catch(const std::exception& e){
128 std::cout<<"error: "<<e.what()<<std::endl;
129 std::exit(EXIT_FAILURE);
130 }
131
132 return 0;
133 }