io.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*=============================================================================
  2. Copyright (C) 1999-2003 Jaakko Jarvi
  3. Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. ==============================================================================*/
  6. #include <boost/detail/lightweight_test.hpp>
  7. #include <boost/fusion/container/vector/vector.hpp>
  8. #include <boost/fusion/container/generation/make_vector.hpp>
  9. #include <boost/fusion/sequence/comparison/equal_to.hpp>
  10. #include <boost/fusion/sequence/io/out.hpp>
  11. #include <boost/fusion/sequence/io/in.hpp>
  12. #include <fstream>
  13. #include <iterator>
  14. #include <algorithm>
  15. #include <string>
  16. #if defined BOOST_NO_STRINGSTREAM
  17. # include <strstream>
  18. #else
  19. # include <sstream>
  20. #endif
  21. using boost::fusion::vector;
  22. using boost::fusion::make_vector;
  23. using boost::fusion::tuple_close;
  24. using boost::fusion::tuple_open;
  25. using boost::fusion::tuple_delimiter;
  26. #if defined BOOST_NO_STRINGSTREAM
  27. using std::ostrstream;
  28. using std::istrstream;
  29. typedef ostrstream useThisOStringStream;
  30. typedef istrstream useThisIStringStream;
  31. #else
  32. using std::ostringstream;
  33. using std::istringstream;
  34. typedef ostringstream useThisOStringStream;
  35. typedef istringstream useThisIStringStream;
  36. #endif
  37. using std::endl;
  38. using std::ofstream;
  39. using std::ifstream;
  40. using std::string;
  41. int
  42. main()
  43. {
  44. using boost::fusion::tuple_close;
  45. using boost::fusion::tuple_open;
  46. using boost::fusion::tuple_delimiter;
  47. useThisOStringStream os1;
  48. // Set format [a, b, c] for os1
  49. os1 << tuple_open('[');
  50. os1 << tuple_close(']');
  51. os1 << tuple_delimiter(',');
  52. os1 << make_vector(1, 2, 3);
  53. BOOST_TEST (os1.str() == std::string("[1,2,3]") );
  54. {
  55. useThisOStringStream os2;
  56. // Set format (a:b:c) for os2;
  57. os2 << tuple_open('(');
  58. os2 << tuple_close(')');
  59. os2 << tuple_delimiter(':');
  60. os2 << make_vector("TUPU", "HUPU", "LUPU", 4.5);
  61. BOOST_TEST (os2.str() == std::string("(TUPU:HUPU:LUPU:4.5)") );
  62. }
  63. {
  64. useThisOStringStream os2;
  65. // Set format (a:b:c) for os2;
  66. os2 << tuple_open('(');
  67. os2 << tuple_close(')');
  68. os2 << tuple_delimiter(':');
  69. // overwrite previous setting
  70. os2 << tuple_open("< ");
  71. os2 << tuple_close('>');
  72. os2 << tuple_delimiter(", ");
  73. os2 << make_vector("TUPU", "HUPU", "LUPU", 4.5);
  74. BOOST_TEST (os2.str() == std::string("< TUPU, HUPU, LUPU, 4.5>") );
  75. }
  76. // The format is still [a, b, c] for os1
  77. os1 << make_vector(1, 2, 3);
  78. BOOST_TEST (os1.str() == std::string("[1,2,3][1,2,3]") );
  79. std::ofstream tmp("temp.tmp");
  80. tmp << make_vector("One", "Two", 3);
  81. tmp << tuple_delimiter(':');
  82. tmp << make_vector(1000, 2000, 3000) << endl;
  83. tmp.close();
  84. // When reading tuples from a stream, manipulators must be set correctly:
  85. ifstream tmp3("temp.tmp");
  86. vector<string, string, int> j;
  87. tmp3 >> j;
  88. BOOST_TEST (tmp3.good() );
  89. tmp3 >> tuple_delimiter(':');
  90. vector<int, int, int> i;
  91. tmp3 >> i;
  92. BOOST_TEST (tmp3.good() );
  93. tmp3.close();
  94. // reading vector<int, int, int> in format (a b c);
  95. useThisIStringStream is("(100 200 300)");
  96. vector<int, int, int> ti;
  97. BOOST_TEST(!!(is >> ti));
  98. BOOST_TEST(ti == make_vector(100, 200, 300));
  99. // Note that strings are problematic:
  100. // writing a tuple on a stream and reading it back doesn't work in
  101. // general. If this is wanted, some kind of a parseable string class
  102. // should be used.
  103. return boost::report_errors();
  104. }