/Src/Dependencies/Boost/libs/filesystem/v2/example/path_table.cpp

http://hadesmem.googlecode.com/ · C++ · 264 lines · 202 code · 50 blank · 12 comment · 26 complexity · 7b97bc0c5222d8992a60992736a888dc MD5 · raw file

  1. // Generate an HTML table showing path decomposition -----------------------//
  2. // Copyright Beman Dawes 2005. Distributed under the Boost
  3. // Software License, Version 1.0. (See accompanying file
  4. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. // See http://www.boost.org/libs/filesystem for documentation.
  6. // For purposes of generating the table, support both POSIX and Windows paths
  7. #define BOOST_FILESYSTEM_NAMESPACE posix
  8. #define BOOST_POSIX_PATH
  9. #include "boost/filesystem/path.hpp"
  10. #undef BOOST_FILESYSTEM_PATH_HPP
  11. #undef BOOST_FILESYSTEM_NAMESPACE
  12. #define BOOST_FILESYSTEM_NAMESPACE windows
  13. #define BOOST_WINDOWS_PATH
  14. #include "boost/filesystem/path.hpp"
  15. namespace pos = boost::posix;
  16. namespace win = boost::windows;
  17. #include <iostream>
  18. #include <fstream>
  19. using std::string;
  20. using std::cout;
  21. namespace
  22. {
  23. std::ifstream infile;
  24. std::ofstream outfile;
  25. const string empty_string;
  26. struct column_abc
  27. {
  28. virtual string heading() const = 0;
  29. virtual string cell_value( const pos::path & p ) const = 0;
  30. virtual string cell_value( const win::path & p ) const = 0;
  31. };
  32. struct c0 : public column_abc
  33. {
  34. string heading() const { return string("<code>string()</code>"); }
  35. string cell_value( const pos::path & p ) const
  36. {
  37. return p.string();
  38. }
  39. string cell_value( const win::path & p ) const
  40. {
  41. return p.string();
  42. }
  43. } o0;
  44. struct c1 : public column_abc
  45. {
  46. string heading() const { return string("<code>file_<br>string()"); }
  47. string cell_value( const pos::path & p ) const { return p.file_string(); }
  48. string cell_value( const win::path & p ) const { return p.file_string(); }
  49. } o1;
  50. struct c2 : public column_abc
  51. {
  52. string heading() const { return string("Elements found<br>by iteration"); }
  53. string cell_value( const pos::path & p ) const
  54. {
  55. string s;
  56. for( pos::path::iterator i(p.begin()); i != p.end(); ++i )
  57. {
  58. if ( i != p.begin() ) s += ',';
  59. s += '\"';
  60. s += *i;
  61. s += '\"';
  62. }
  63. if ( s.empty() ) s += "\"\"";
  64. return s;
  65. }
  66. string cell_value( const win::path & p ) const
  67. {
  68. string s;
  69. for( win::path::iterator i(p.begin()); i != p.end(); ++i )
  70. {
  71. if ( i != p.begin() ) s += ',';
  72. s += '\"';
  73. s += *i;
  74. s += '\"';
  75. }
  76. if ( s.empty() ) s += "\"\"";
  77. return s;
  78. }
  79. } o2;
  80. struct c3 : public column_abc
  81. {
  82. string heading() const { return string("<code>root_<br>path()<br>.string()</code>"); }
  83. string cell_value( const pos::path & p ) const { return p.root_path().string(); }
  84. string cell_value( const win::path & p ) const { return p.root_path().string(); }
  85. } o3;
  86. struct c4 : public column_abc
  87. {
  88. string heading() const { return string("<code>root_<br>name()</code>"); }
  89. string cell_value( const pos::path & p ) const { return p.root_name(); }
  90. string cell_value( const win::path & p ) const { return p.root_name(); }
  91. } o4;
  92. struct c5 : public column_abc
  93. {
  94. string heading() const { return string("<code>root_<br>directory()</code>"); }
  95. string cell_value( const pos::path & p ) const { return p.root_directory(); }
  96. string cell_value( const win::path & p ) const { return p.root_directory(); }
  97. } o5;
  98. struct c6 : public column_abc
  99. {
  100. string heading() const { return string("<code>relative_<br>path()<br>.string()</code>"); }
  101. string cell_value( const pos::path & p ) const { return p.relative_path().string(); }
  102. string cell_value( const win::path & p ) const { return p.relative_path().string(); }
  103. } o6;
  104. struct c7 : public column_abc
  105. {
  106. string heading() const { return string("<code>branch_<br>path()<br>.string()</code>"); }
  107. string cell_value( const pos::path & p ) const { return p.branch_path().string(); }
  108. string cell_value( const win::path & p ) const { return p.branch_path().string(); }
  109. } o7;
  110. struct c8 : public column_abc
  111. {
  112. string heading() const { return string("<code>leaf()</code>"); }
  113. string cell_value( const pos::path & p ) const { return p.leaf(); }
  114. string cell_value( const win::path & p ) const { return p.leaf(); }
  115. } o8;
  116. const column_abc * column[] = { &o2, &o0, &o1, &o3, &o4, &o5, &o6, &o7, &o8 };
  117. // do_cell ---------------------------------------------------------------//
  118. void do_cell( const string & test_case, int i )
  119. {
  120. string posix_result( column[i]->cell_value( pos::path(test_case) ) );
  121. string windows_result( column[i]->cell_value( win::path(test_case) ) );
  122. outfile <<
  123. (posix_result != windows_result
  124. ? "<td bgcolor=\"#CCFF99\"><code>"
  125. : "<td><code>");
  126. if ( posix_result.empty() || posix_result[0] != '\"' )
  127. outfile << '\"' << posix_result << '\"';
  128. else
  129. outfile << posix_result;
  130. if ( posix_result != windows_result )
  131. {
  132. outfile << "<br>";
  133. if ( windows_result.empty() || windows_result[0] != '\"' )
  134. outfile << '\"' << windows_result << '\"';
  135. else
  136. outfile << windows_result;
  137. }
  138. outfile << "</code></td>\n";
  139. }
  140. // do_row ------------------------------------------------------------------//
  141. void do_row( const string & test_case )
  142. {
  143. outfile << "<tr>\n<td><code>\"" << test_case << "\"</code></td>\n";
  144. for ( int i = 0; i < sizeof(column)/sizeof(column_abc&); ++i )
  145. {
  146. do_cell( test_case, i );
  147. }
  148. outfile << "</tr>\n";
  149. }
  150. // do_table ----------------------------------------------------------------//
  151. void do_table()
  152. {
  153. outfile <<
  154. "<h1>Path Decomposition Table for <i>POSIX</i> and <i>Windows</i></h1>\n"
  155. "<p>Shaded entries indicate cases where <i>POSIX</i> and <i>Windows</i>\n"
  156. "implementations yield different results. The top value is the\n"
  157. "<i>POSIX</i> result and the bottom value is the <i>Windows</i> result.\n"
  158. "<table border=\"1\" cellspacing=\"0\" cellpadding=\"5\">\n"
  159. "<p>\n"
  160. ;
  161. // generate the column headings
  162. outfile << "<tr><td><b>Constructor<br>argument</b></td>\n";
  163. for ( int i = 0; i < sizeof(column)/sizeof(column_abc&); ++i )
  164. {
  165. outfile << "<td><b>" << column[i]->heading() << "</b></td>\n";
  166. }
  167. outfile << "</tr>\n";
  168. // generate a row for each input line
  169. string test_case;
  170. while ( std::getline( infile, test_case ) )
  171. {
  172. do_row( test_case );
  173. }
  174. outfile << "</table>\n";
  175. }
  176. } // unnamed namespace
  177. // main --------------------------------------------------------------------//
  178. #define BOOST_NO_CPP_MAIN_SUCCESS_MESSAGE
  179. #include <boost/test/included/prg_exec_monitor.hpp>
  180. int cpp_main( int argc, char * argv[] ) // note name!
  181. {
  182. if ( argc != 3 )
  183. {
  184. std::cerr <<
  185. "Usage: path_table input-file output-file\n"
  186. " input-file contains the paths to appear in the table.\n"
  187. " output-file will contain the generated HTML.\n"
  188. ;
  189. return 1;
  190. }
  191. infile.open( argv[1] );
  192. if ( !infile )
  193. {
  194. std::cerr << "Could not open input file: " << argv[1] << std::endl;
  195. return 1;
  196. }
  197. outfile.open( argv[2] );
  198. if ( !outfile )
  199. {
  200. std::cerr << "Could not open output file: " << argv[2] << std::endl;
  201. return 1;
  202. }
  203. outfile << "<html>\n"
  204. "<head>\n"
  205. "<title>Path Decomposition Table</title>\n"
  206. "</head>\n"
  207. "<body bgcolor=\"#ffffff\" text=\"#000000\">\n"
  208. ;
  209. do_table();
  210. outfile << "</body>\n"
  211. "</html>\n"
  212. ;
  213. return 0;
  214. }