sample_advanced.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. // ----------------------------------------------------------------------------
  2. // sample_advanced.cc : examples of adanced usage of format
  3. // ----------------------------------------------------------------------------
  4. // Copyright Samuel Krempp 2003. Use, modification, and distribution are
  5. // subject to the Boost Software License, Version 1.0. (See accompanying
  6. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  7. // See http://www.boost.org/libs/format for library home page
  8. // ----------------------------------------------------------------------------
  9. #include <iostream>
  10. #include <iomanip>
  11. #include "boost/format.hpp"
  12. namespace MyNS_ForOutput {
  13. using std::cout; using std::cerr;
  14. using std::string;
  15. using std::endl; using std::flush;
  16. using boost::format;
  17. using boost::io::group;
  18. }
  19. namespace MyNS_Manips {
  20. using std::setfill;
  21. using std::setw;
  22. using std::hex ;
  23. using std::dec ;
  24. using std::showbase ;
  25. using std::left ;
  26. using std::right ;
  27. using std::internal ;
  28. }
  29. int main(){
  30. using namespace MyNS_ForOutput;
  31. using namespace MyNS_Manips;
  32. std::string s;
  33. //------------------------------------------------------------------------
  34. // storing the parsed format-string in a 'formatter' :
  35. // format objects are regular objects that can be copied, assigned,
  36. // fed arguments, dumped to a stream, re-fed arguments, etc...
  37. // So users can use them the way they like.
  38. format fmter("%1% %2% %3% %1% \n");
  39. fmter % 10 % 20 % 30;
  40. cout << fmter;
  41. // prints "10 20 30 10 \n"
  42. // note that once the fmter got all its arguments,
  43. // the formatted string stays available (until next call to '%')
  44. // The result is available via function str() or stream's << :
  45. cout << fmter;
  46. // prints the same string again.
  47. // once you call operator% again, arguments are cleared inside the object
  48. // and it is an error to ask for the conversion string before feeding all arguments :
  49. fmter % 1001;
  50. try { cout << fmter; }
  51. catch (boost::io::too_few_args& exc) {
  52. cout << exc.what() << "***Dont worry, that was planned\n";
  53. }
  54. // we just need to feed the last two arguments, and it will be ready for output again :
  55. cout << fmter % 1002 % 1003;
  56. // prints "1001 1002 1003 1001 \n"
  57. cout << fmter % 10 % 1 % 2;
  58. // prints "10 1 2 10 \n"
  59. //---------------------------------------------------------------
  60. // using format objects
  61. // modify the formatting options for a given directive :
  62. fmter = format("%1% %2% %3% %2% %1% \n");
  63. fmter.modify_item(4, group(setfill('_'), hex, showbase, setw(5)) );
  64. cout << fmter % 1 % 2 % 3;
  65. // prints "1 2 3 __0x2 1 \n"
  66. // bind one of the argumets :
  67. fmter.bind_arg(1, 18);
  68. cout << fmter % group(hex, showbase, 20) % 30; // %2 is 20, and 20 == 0x14
  69. // prints "18 0x14 30 _0x14 18 \n"
  70. fmter.modify_item(4, setw(0)); // cancels previous width-5
  71. fmter.bind_arg(1, 77); // replace 18 with 77 for first argument.
  72. cout << fmter % 10 % 20;
  73. // prints "77 10 20 0xa 77 \n"
  74. try
  75. {
  76. cout << fmter % 6 % 7 % 8; // Aye ! too many args, because arg1 is bound already
  77. }
  78. catch (boost::io::too_many_args& exc)
  79. {
  80. cout << exc.what() << "***Dont worry, that was planned\n";
  81. }
  82. // clear regular arguments, but not bound arguments :
  83. fmter.clear();
  84. cout << fmter % 2 % 3;
  85. // prints "77 2 3 0x2 77 \n"
  86. // clear_binds() clears both regular AND bound arguments :
  87. fmter.clear_binds();
  88. cout << fmter % 1 % 2 % 3;
  89. // prints "1 2 3 0x2 1 \n"
  90. // setting desired exceptions :
  91. fmter.exceptions( boost::io::all_error_bits ^( boost::io::too_many_args_bit ) );
  92. cout << fmter % 1 % 2 % 3 % 4 % 5 % 6 ;
  93. // -----------------------------------------------------------
  94. // misc:
  95. // unsupported printf directives %n and asterisk-fields are purely ignored.
  96. // do *NOT* provide an argument for them, it is an error.
  97. cout << format("|%5d| %n") % 7 << endl;
  98. // prints "| 7| "
  99. cout << format("|%*.*d|") % 7 << endl;
  100. // prints "|7|"
  101. // truncations of strings :
  102. cout << format("%|.2s| %|8c|.\n") % "root" % "user";
  103. // prints "ro u.\n"
  104. // manipulators conflicting with format-string : manipulators win.
  105. cout << format("%2s") % group(setfill('0'), setw(6), 1) << endl;
  106. // prints "000001"
  107. cout << format("%2$5s %1% %2$3s\n") % 1 % group(setfill('X'), setw(4), 2) ;
  108. // prints "XXX2 1 XXX2\n"
  109. // width is 4, as set by manip, not the format-string.
  110. // nesting :
  111. cout << format("%2$014x [%1%] %2$05s\n") % (format("%05s / %s") % -18 % 7)
  112. % group(showbase, -100);
  113. // prints "0x0000ffffff9c [-0018 / 7] -0100\n"
  114. cout << "\n\nEverything went OK, exiting. \n";
  115. return 0;
  116. }