source: Swollen/swollen/customargumentparser.cpp @ 132

Last change on this file since 132 was 132, checked in by darran, 19 years ago

Last release version changes

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