serialization.cpp 3.5 KB

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