1 // Copyright Frank Mori Hess 2009. 2 // 3 // Quick hack to extract code snippets from example programs, so 4 // they can be included into boostbook. 5 // 6 // Use, modification and 7 // distribution is subject to the Boost Software License, Version 8 // 1.0. (See accompanying file LICENSE_1_0.txt or copy at 9 // http://www.boost.org/LICENSE_1_0.txt) 10 11 #include <fstream> 12 #include <iostream> 13 #include <sstream> 14 #include <stdexcept> 15 main(int argc,const char * argv[])16int main(int argc, const char *argv[]) 17 { 18 if(argc < 3) 19 { 20 std::cerr << "Too few arguments: need output directory and input file name(s).\n"; 21 return -1; 22 } 23 static const std::string output_directory = argv[1]; 24 static const int num_files = argc - 2; 25 int i; 26 for(i = 0; i < num_files; ++i) 27 { 28 const std::string file_name = argv[2 + i]; 29 std::cout << "opening file: " << file_name << std::endl; 30 std::ifstream infile(file_name.c_str()); 31 bool inside_snippet = false; 32 std::ofstream snippet_out_file; 33 while(infile.good()) 34 { 35 std::string line; 36 getline(infile, line); 37 if(infile.bad()) break; 38 if(inside_snippet) 39 { 40 size_t snippet_end_pos = line.find("//]"); 41 if(snippet_end_pos == std::string::npos) 42 { 43 snippet_out_file << line << "\n"; 44 }else 45 { 46 snippet_out_file << "]]></code>"; 47 inside_snippet = false; 48 std::cout << "done.\n"; 49 continue; 50 } 51 }else 52 { 53 size_t snippet_start_pos = line.find("//["); 54 if(snippet_start_pos == std::string::npos) 55 { 56 continue; 57 }else 58 { 59 inside_snippet = true; 60 std::string snippet_name = line.substr(snippet_start_pos + 3); 61 std::istringstream snippet_stream(snippet_name); 62 snippet_stream >> snippet_name; 63 if(snippet_name == "") 64 { 65 throw std::runtime_error("failed to obtain snippet name"); 66 } 67 snippet_out_file.close(); 68 snippet_out_file.clear(); 69 snippet_out_file.open(std::string(output_directory + "/" + snippet_name + ".xml").c_str()); 70 snippet_out_file << "<!-- Code snippet \"" << snippet_name << 71 "\" extracted from \"" << file_name << "\" by snippet_extractor.\n" << 72 "--><code><![CDATA["; 73 std::cout << "processing snippet \"" << snippet_name << "\"... "; 74 continue; 75 } 76 } 77 } 78 } 79 return 0; 80 } 81