sample_formats.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // ----------------------------------------------------------------------------
  2. // sample_formats.cpp : example of basic 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 <cassert>
  12. #include "boost/format.hpp"
  13. // 2 custom namespaces, to bring in a few useful names :
  14. namespace MyNS_ForOutput {
  15. using std::cout; using std::cerr;
  16. using std::string;
  17. using std::endl; using std::flush;
  18. using boost::format;
  19. using boost::io::group;
  20. }
  21. namespace MyNS_Manips {
  22. using std::setfill;
  23. using std::setw;
  24. using std::hex ;
  25. using std::dec ;
  26. // gcc-2.95 doesnt define the next ones
  27. // using std::showbase ;
  28. // using std::left ;
  29. // using std::right ;
  30. // using std::internal ;
  31. }
  32. int main(){
  33. using namespace MyNS_ForOutput;
  34. using namespace MyNS_Manips;
  35. std::cout << format("%|1$1| %|2$3|") % "Hello" % 3 << std::endl;
  36. // Reordering :
  37. cout << format("%1% %2% %3% %2% %1% \n") % "o" % "oo" % "O"; // 'simple' style.
  38. // prints "o oo O oo o \n"
  39. cout << format("(x,y) = (%1$+5d,%2$+5d) \n") % -23 % 35; // Posix-Printf style
  40. // No reordering :
  41. cout << format("writing %s, x=%s : %d-th step \n") % "toto" % 40.23 % 50;
  42. // prints "writing toto, x=40.23 : 50-th step \n"
  43. cout << format("(x,y) = (%+5d,%+5d) \n") % -23 % 35;
  44. cout << format("(x,y) = (%|+5|,%|+5|) \n") % -23 % 35;
  45. cout << format("(x,y) = (%|1$+5|,%|2$+5|) \n") % -23 % 35;
  46. // all those are the same, it prints "(x,y) = ( -23, +35) \n"
  47. // Using manipulators, via 'group' :
  48. cout << format("%2% %1% %2%\n") % 1 % group(setfill('X'), hex, setw(4), 16+3) ;
  49. // prints "XX13 1 XX13\n"
  50. // printf directives's type-flag can be used to pass formatting options :
  51. cout << format("_%1$4d_ is : _%1$#4x_, _%1$#4o_, and _%1$s_ by default\n") % 18;
  52. // prints "_ 18_ is : _0x12_, _ 022_, and _18_ by default\n"
  53. // Taking the string value :
  54. std::string s;
  55. s= str( format(" %d %d ") % 11 % 22 );
  56. assert( s == " 11 22 ");
  57. // -----------------------------------------------
  58. // %% prints '%'
  59. cout << format("%%##%#x ") % 20 << endl;
  60. // prints "%##0x14 "
  61. // -----------------------------------------------
  62. // Enforcing the right number of arguments
  63. // Too much arguments will throw an exception when feeding the unwanted argument :
  64. try {
  65. format(" %1% %1% ") % 101 % 102;
  66. // the format-string refers to ONE argument, twice. not 2 arguments.
  67. // thus giving 2 arguments is an error
  68. }
  69. catch (boost::io::too_many_args& exc) {
  70. cerr << exc.what() << "\n\t\t***Dont worry, that was planned\n";
  71. }
  72. // Too few arguments when requesting the result will also throw an exception :
  73. try {
  74. cerr << format(" %|3$| ") % 101;
  75. // even if %1$ and %2$ are not used, you should have given 3 arguments
  76. }
  77. catch (boost::io::too_few_args& exc) {
  78. cerr << exc.what() << "\n\t\t***Dont worry, that was planned\n";
  79. }
  80. cerr << "\n\nEverything went OK, exiting. \n";
  81. return 0;
  82. }