From 483eb2f56657e8e7f419ab1a4fab8dce9ade8609 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sat, 27 Apr 2024 20:24:20 +0200 Subject: Adding upstream version 14.2.21. Signed-off-by: Daniel Baumann --- src/boost/libs/flyweight/example/serialization.cpp | 133 +++++++++++++++++++++ 1 file changed, 133 insertions(+) create mode 100644 src/boost/libs/flyweight/example/serialization.cpp (limited to 'src/boost/libs/flyweight/example/serialization.cpp') diff --git a/src/boost/libs/flyweight/example/serialization.cpp b/src/boost/libs/flyweight/example/serialization.cpp new file mode 100644 index 00000000..80227a53 --- /dev/null +++ b/src/boost/libs/flyweight/example/serialization.cpp @@ -0,0 +1,133 @@ +/* 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: "<