1 | |
---|
2 | #include "customargumentparser.h" |
---|
3 | #include <fstream> |
---|
4 | #include <iostream> |
---|
5 | |
---|
6 | |
---|
7 | #define MAX_LINE_LENGTH 200 |
---|
8 | |
---|
9 | using namespace osg; |
---|
10 | |
---|
11 | |
---|
12 | CustomArgumentParser::CustomArgumentParser(int* argc, char **argv) : ArgumentParser(argc, argv) |
---|
13 | { |
---|
14 | |
---|
15 | // last argument is either .sww or .swm filename |
---|
16 | _filename = new std::string(argv[*argc-1]); |
---|
17 | |
---|
18 | // ends in .swm? |
---|
19 | if( _filename->substr(_filename->size()-4,4).find(".swm",0) != -1 ) |
---|
20 | { |
---|
21 | _isswm = true; |
---|
22 | |
---|
23 | // attempt to open the macro file ... |
---|
24 | std::fstream f; |
---|
25 | f.open( _filename->c_str(), std::fstream::in ); |
---|
26 | if( f.is_open() ) |
---|
27 | { |
---|
28 | char str[MAX_LINE_LENGTH]; |
---|
29 | |
---|
30 | // FIXME: verify this really is the SWM header ... |
---|
31 | f.getline(str,MAX_LINE_LENGTH); |
---|
32 | |
---|
33 | // read the stored argc count |
---|
34 | f.getline(str,MAX_LINE_LENGTH); |
---|
35 | sscanf( str, "# SWM CLP %d", &_nargs ); |
---|
36 | |
---|
37 | // we will modify the argv array to include the original |
---|
38 | // argv[1] ... argv[argc-1] (ie., ignore the final argument which is the swm |
---|
39 | // filename) extended by the stored arguments contained in the macro file. |
---|
40 | char** newargv = new char*[*argc-1+_nargs]; |
---|
41 | |
---|
42 | // current command line params (sans last entry, the .swm file) |
---|
43 | for( int i=0; i<*argc-1; i++ ) |
---|
44 | newargv[i] = argv[i]; |
---|
45 | |
---|
46 | // now fill in the stored argv[] from the macro file |
---|
47 | for( int i=0; i<_nargs; i++ ) |
---|
48 | { |
---|
49 | f.getline(str,MAX_LINE_LENGTH); |
---|
50 | char *s = new char[strlen(str)+1]; |
---|
51 | strcpy(s,str); |
---|
52 | newargv[*argc-1+i] = s; |
---|
53 | } |
---|
54 | |
---|
55 | // change baseclass private members to the extended argc/argv |
---|
56 | *_argc = *argc - 1 + _nargs; |
---|
57 | _argv = newargv; |
---|
58 | |
---|
59 | |
---|
60 | std::cout << "Modified argument list based on SWM contents" << std::endl; |
---|
61 | for( int i=0; i<*_argc; i++ ) |
---|
62 | std::cout << " " << i << ": " << _argv[i] << std::endl; |
---|
63 | |
---|
64 | } |
---|
65 | f.close(); |
---|
66 | |
---|
67 | } |
---|
68 | |
---|
69 | else |
---|
70 | |
---|
71 | { |
---|
72 | _isswm = false; |
---|
73 | |
---|
74 | // local copies of current argc, argv[] as we may have to save them on '3' save. |
---|
75 | // note that we don't save argv[0] which is the program name. |
---|
76 | _nargs = *argc - 1; |
---|
77 | for( int i=1; i<*argc; i++ ) |
---|
78 | _vargs.push_back( std::string(argv[i]) ); |
---|
79 | } |
---|
80 | |
---|
81 | } |
---|
82 | |
---|
83 | |
---|
84 | void CustomArgumentParser::write(std::ostream& s) |
---|
85 | { |
---|
86 | s << "# SWM CLP " << _nargs << std::endl; |
---|
87 | |
---|
88 | for( int i=0; i < _nargs; i++ ) |
---|
89 | s << _vargs[i] << std::endl; |
---|
90 | |
---|
91 | } |
---|