1 // Copyright Frank Mori Hess 2009.
3 // Quick hack to extract code snippets from example programs, so
4 // they can be included into boostbook.
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)
16 int main(int argc
, const char *argv
[])
20 std::cerr
<< "Too few arguments: need output directory and input file name(s).\n";
23 static const std::string output_directory
= argv
[1];
24 static const int num_files
= argc
- 2;
26 for(i
= 0; i
< num_files
; ++i
)
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
;
36 getline(infile
, line
);
37 if(infile
.bad()) break;
40 size_t snippet_end_pos
= line
.find("//]");
41 if(snippet_end_pos
== std::string::npos
)
43 snippet_out_file
<< line
<< "\n";
46 snippet_out_file
<< "]]></code>";
47 inside_snippet
= false;
48 std::cout
<< "done.\n";
53 size_t snippet_start_pos
= line
.find("//[");
54 if(snippet_start_pos
== std::string::npos
)
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
== "")
65 throw std::runtime_error("failed to obtain snippet name");
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" <<
73 std::cout
<< "processing snippet \"" << snippet_name
<< "\"... ";