OSSIM - Open Source Software Image Map  Version 1.9.0 (20180803)
ossimTool.cpp
Go to the documentation of this file.
1 //**************************************************************************************************
2 //
3 // OSSIM Open Source Geospatial Data Processing Library
4 // See top level LICENSE.txt file for license information
5 //
6 //**************************************************************************************************
7 
12 #include <ossim/init/ossimInit.h>
13 #include <ossim/util/ossimTool.h>
14 
15 using namespace std;
16 
18  : m_kwl(),
19  m_consoleStream (&cout),
20  m_helpRequested (false)
21 {
22 }
23 
25 {
26 }
27 
29 {
30 }
31 
33 {
34 }
35 
37 {
38  return "ossimTool";
39 }
40 
42 {
43  // Add global usage options.
45 
46  std::string appName = ap.getApplicationName();
48  au->setApplicationName( ossimString( appName ) );
49 
51  "--write-api <filename>",
52  "Writes a JSON API specification to the specified filename.");
54  "--write-template <filename>",
55  "Writes a template keyword-list to the specified filename.");
56 }
57 
59 {
60  m_helpRequested = false;
61  if (ap.read("-h") || ap.read("--help") )
62  {
63  // Write usage.
64  setUsage(ap);
66  m_helpRequested = true;
67  return true;
68  }
69 
70  std::string ts1;
72 
73  if ( ap.read("--write-api", sp1))
74  {
75  ofstream ofs ( ts1.c_str() );
76  ossimString json_str;
77  getAPI(json_str);
78  ofs << json_str <<endl;
79  return true;
80  }
81 
82  if ( ap.read("--write-template", sp1))
83  {
84  ofstream ofs ( ts1.c_str() );
85  ossimKeywordlist kwl;
86  getKwlTemplate(kwl);
87  ofs << kwl <<endl;
88  return true;
89  }
90 
91  return true;
92 }
93 
95 {
96  m_helpRequested = false;
97  m_kwl = kwl;
98 }
99 
101 {
103  preferencesKWL().findKey( std::string( "ossim_share_directory" ) );
104  ossimFilename classTemplate = "util/" + getClassName() + ".kwl";
105  ossimFilename kwl_path = share_dir.dirCat( classTemplate );
106 
107  if (!kwl.addFile(kwl_path))
108  {
109  ossimNotify(ossimNotifyLevel_WARN)<<"ossimTool::getKwlTemplate() -- Could not find <"
110  <<kwl_path<<">. Ignoring"<<endl;
111  }
112 }
113 
114 void ossimTool::getAPI(string& json) const
115 {
117  preferencesKWL().findKey( std::string( "ossim_share_directory" ) );
118  ossimFilename classJson = "util/" + getClassName() + ".json";
119  ossimFilename json_path = share_dir.dirCat( classJson );
120 
121  readTextFile(json_path, json);
122 }
123 
124 string ossimTool::getAPI() const
125 {
126  string result;
127  getAPI(result);
128  return result;
129 }
130 
131 bool ossimTool::readTextFile(const ossimFilename& filename, string& contents) const
132 {
133  contents.clear();
134 
135  std::ifstream is(filename.chars());
136  if (!is)
137  {
138  ossimNotify(ossimNotifyLevel_WARN)<<"ossimTool::readTextFile() -- Could not find <"
139  <<filename<<">. Ignoring."<<endl;
140  return false;
141  }
142 
143  // get length of file:
144  is.seekg (0, is.end);
145  int length = is.tellg();
146  is.seekg (0, is.beg);
147 
148  // allocate memory:
149  char *buffer = new char [length];
150 
151  // read data as a block:
152  is.read (buffer,length);
153  is.close();
154 
155  contents.append(buffer);
156  delete [] buffer;
157 
158  return true;
159 }
160 
161 void ossimTool::getBuildDate(std::string& s) const
162 {
163 #ifdef OSSIM_BUILD_DATE
164  s = OSSIM_BUILD_DATE;
165 #else
166  s = "unknown";
167 #endif
168 }
169 
170 void ossimTool::getRevision(std::string& s) const
171 {
172 #ifdef OSSIM_REVISION
173  s = OSSIM_REVISION;
174 #else
175  s = "unknown";
176 #endif
177 }
178 
179 void ossimTool::getVersion(std::string& s) const
180 {
181 #ifdef OSSIM_VERSION
182  s = OSSIM_VERSION;
183 #else
184  s = "unknown";
185 #endif
186 }
187 
void write(std::ostream &output, const UsageMap &um, unsigned int widthOfOutput=80)
std::string getApplicationName() const
return the application name, as specified by argv[0]
#define OSSIM_REVISION
Definition: ossimVersion.h:9
void addCommandLineOption(const ossimString &option, const ossimString &explanation)
void getRevision(std::string &s) const
Gets revision.
Definition: ossimTool.cpp:170
bool readTextFile(const ossimFilename &filename, std::string &contents) const
Used for reading text files of template and JSON API from disk ONLY.
Definition: ossimTool.cpp:131
Represents serializable keyword/value map.
bool addFile(const char *file)
bool m_helpRequested
Definition: ossimTool.h:150
std::basic_ifstream< char > ifstream
Class for char input file streams.
Definition: ossimIosFwd.h:44
void addOptions(ossimArgumentParser &parser)
Definition: ossimInit.cpp:100
bool read(const std::string &str)
search for an occurance of a string in the argument list, on sucess remove that occurance from the li...
virtual ossimString getClassName() const
Definition: ossimTool.cpp:36
ossimKeywordlist m_kwl
Definition: ossimTool.h:148
virtual void getKwlTemplate(ossimKeywordlist &kwl)
Assigns a template keywordlist to string for initializing derived classes.
Definition: ossimTool.cpp:100
ossimApplicationUsage * getApplicationUsage()
std::string getAPI() const
Definition: ossimTool.cpp:124
virtual ~ossimTool()
Definition: ossimTool.cpp:24
virtual void clear()
Disconnects and clears the DEM and image layers.
Definition: ossimTool.cpp:28
void getBuildDate(std::string &s) const
Gets build date.
Definition: ossimTool.cpp:161
void getVersion(std::string &s) const
Gets version.
Definition: ossimTool.cpp:179
virtual void setUsage(ossimArgumentParser &ap)
Initializes the aurgument parser with expected parameters and options.
Definition: ossimTool.cpp:41
const char * chars() const
For backward compatibility.
Definition: ossimString.h:77
void setApplicationName(const ossimString &name)
virtual bool initialize(ossimArgumentParser &ap)
Initializes from command line arguments.
Definition: ossimTool.cpp:58
static ossimPreferences * instance()
ossimFilename & append(const ossimString &append_this_to_filename)
Convenience method to append a string to the base-name portion of the filename.
ossimFilename dirCat(const ossimFilename &file) const
static ossimInit * instance()
Definition: ossimInit.cpp:89
std::basic_ofstream< char > ofstream
Class for char output file streams.
Definition: ossimIosFwd.h:47
#define OSSIM_VERSION
Definition: ossimVersion.h:4
virtual void abort()
Kills current (asynchronous) process.
Definition: ossimTool.cpp:32
#define OSSIM_BUILD_DATE
Definition: ossimVersion.h:12
OSSIMDLLEXPORT std::ostream & ossimNotify(ossimNotifyLevel level=ossimNotifyLevel_WARN)