/* Boost.Flyweight example of serialization. * * Copyright 2006-2014 Joaquin M Lopez Munoz. * Distributed under the Boost Software License, Version 1.0. * (See accompanying file LICENSE_1_0.txt or copy at * http://www.boost.org/LICENSE_1_0.txt) * * See http://www.boost.org/libs/flyweight for library home page. */ #include /* keep it first to prevent nasty warns in MSVC */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace boost::flyweights; typedef flyweight fw_string; typedef std::vector text_container; /* Read a text file into a text_container and serialize to an archive. */ void save_serialization_file() { /* Define a tokenizer on std::istreambuf. */ typedef std::istreambuf_iterator char_iterator; typedef boost::tokenizer< boost::char_separator, char_iterator > tokenizer; std::cout<<"enter input text file name: "; std::string in; std::getline(std::cin,in); std::ifstream ifs(in.c_str()); if(!ifs){ std::cout<<"can't open "<( "", "\t\n\r !\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~")); text_container txt; for(tokenizer::iterator it=tok.begin();it!=tok.end();++it){ txt.push_back(fw_string(*it)); } std::cout<<"enter output serialization file name: "; std::string out; std::getline(std::cin,out); std::ofstream ofs(out.c_str()); if(!ofs){ std::cout<<"can't open "<(txt); } /* Read a serialization archive and save the result to a text file. */ void load_serialization_file() { std::cout<<"enter input serialization file name: "; std::string in; std::getline(std::cin,in); std::ifstream ifs(in.c_str()); if(!ifs){ std::cout<<"can't open "<>txt; std::cout<<"enter output text file name: "; std::string out; std::getline(std::cin,out); std::ofstream ofs(out.c_str()); if(!ofs){ std::cout<<"can't open "<(ofs)); } int main() { try{ std::cout<<"1 load a text file and save it as a serialization file\n" "2 load a serialization file and save it as a text file\n"; for(;;){ std::cout<<"select option, enter to exit: "; std::string str; std::getline(std::cin,str); if(str.empty())break; std::istringstream istr(str); int option=-1; istr>>option; if(option==1)save_serialization_file(); else if(option==2)load_serialization_file(); } } catch(const std::exception& e){ std::cout<<"error: "<