OSSIM - Open Source Software Image Map  Version 1.9.0 (20180803)
ossimApplicationUsage.cpp
Go to the documentation of this file.
1 //-------------------------------------------------------------------------
2 //
3 // This code was taken from Open Scene Graph and incorporated from into
4 // OSSIM.
5 //
6 //-------------------------------------------------------------------------
7 // $Id: ossimApplicationUsage.cpp 23244 2015-04-08 20:48:34Z rashadkm $
8 
9 #include <iostream>
11 #include <ossim/base/ossimCommon.h>
12 
14  theCommandLineUsage(commandLineUsage)
15 {
16 }
17 
19 {
20  static ossimApplicationUsage theApplicationUsage;
21  return &theApplicationUsage;
22 }
24 {
25  theApplicationName = name;
26 }
27 
29 {
30  return theApplicationName;
31 }
33 {
34  theDescription = desc;
35 }
36 
38 {
39  return theDescription;
40 }
41 
42 void ossimApplicationUsage::addUsageExplanation(Type type,const ossimString& option,const ossimString& explanation)
43 {
44  switch(type)
45  {
47  addCommandLineOption(option,explanation);
48  break;
50  addEnvironmentalVariable(option,explanation);
51  break;
52  }
53 }
55 {
56  theCommandLineUsage=explanation;
57 }
58 
60 {
61  return theCommandLineUsage;
62 }
63 
65 {
66  theCommandLineOptions[option]=explanation;
67 }
68 
70 {
71  return theCommandLineOptions;
72 }
73 
75 {
76  theEnvironmentalVariables[option]=explanation;
77 }
79 {
81 }
82 
83 void ossimApplicationUsage::getFormatedString(ossimString& str, const UsageMap& um,unsigned int widthOfOutput)
84 {
85 
86  unsigned int maxNumCharsInOptions = 0;
87  ossimApplicationUsage::UsageMap::const_iterator citr;
88  for(citr=um.begin();
89  citr!=um.end();
90  ++citr)
91  {
92  maxNumCharsInOptions = ossim::max(maxNumCharsInOptions,(unsigned int)citr->first.length());
93  }
94 
95  unsigned int fullWidth = widthOfOutput;
96  unsigned int optionPos = 2;
97  unsigned int explanationPos = 2+maxNumCharsInOptions+2;
98  unsigned int explanationWidth = fullWidth-explanationPos;
99 
100  ossimString line;
101 
102  for(citr=um.begin();
103  citr!=um.end();
104  ++citr)
105  {
106  line.assign(fullWidth,' ');
107  line.replace(optionPos,citr->first.length(),citr->first);
108 
109  const ossimString& explanation = citr->second;
110  std::string::size_type pos = 0;
111  std::string::size_type offset = 0;
112  bool firstInLine = true;
113  while (pos<explanation.length())
114  {
115  if (firstInLine) offset = 0;
116 
117  // skip any leading white space.
118  while (pos<explanation.length() && *(explanation.begin()+pos)==' ')
119  {
120  if (firstInLine) ++offset;
121  ++pos;
122  }
123 
124  firstInLine = false;
125 
126  std::string::size_type width = ossim::min((ossim_int64)(explanation.length()-pos),
127  (ossim_int64)(explanationWidth-offset));
128  std::string::size_type slashn_pos = explanation.find('\n',pos);
129 
130  unsigned int extraSkip = 0;
131  bool concatinated = false;
132  if (slashn_pos!=std::string::npos)
133  {
134  if (slashn_pos<pos+width)
135  {
136  width = slashn_pos-pos;
137  ++extraSkip;
138  firstInLine = true;
139  }
140  else if (slashn_pos==pos+width)
141  {
142  ++extraSkip;
143  firstInLine = true;
144  }
145  }
146 
147  if (pos+width<explanation.length())
148  {
149  // now reduce width until we get a space or a return
150  // so that we ensure that whole words are printed.
151  while (width>0 &&
152  *(explanation.begin()+(pos+width))!=' ' &&
153  *(explanation.begin()+(pos+width))!='\n') --width;
154 
155  if (width==0)
156  {
157  // word must be longer than a whole line so will need
158  // to concatinate it.
159  width = explanationWidth-1;
160  concatinated = true;
161  }
162  }
163 
164  line.replace(explanationPos+offset,explanationWidth, explanation, pos, width);
165 
166  if (concatinated) { str += line; str += "-\n"; }
167  else { str += line; str += "\n"; }
168 
169  // move to the next line of output.
170  line.assign(fullWidth,' ');
171 
172  pos += width+extraSkip;
173 
174 
175  }
176 
177  }
178 }
179 
180 void ossimApplicationUsage::write(std::ostream& output, const ossimApplicationUsage::UsageMap& um,unsigned int widthOfOutput)
181 {
182  ossimString str;
183  getFormatedString(str, um, widthOfOutput);
184  output << str << std::endl;
185 }
186 
187 void ossimApplicationUsage::write(std::ostream& output, unsigned int type, unsigned int widthOfOutput)
188 {
189 
190  output << "Usage: "<<getCommandLineUsage()<<std::endl;
191  if(theDescription.size()>0)
192  {
193  // TODO: take into account "widthOfOutput"
194  output << "\nDescription:\n" << theDescription.c_str() << "\n"
195  << std::endl;
196  }
197 
198  bool needspace = false;
199  if ((type&OSSIM_COMMAND_LINE_OPTION) && !getCommandLineOptions().empty())
200  {
201  // if (needspace) output << std::endl;
202  output << "Options:"<<std::endl;
203  write(output,getCommandLineOptions(),widthOfOutput);
204  needspace = true;
205  }
206 
208  {
209  if (needspace) output << std::endl;
210  output << "Environmental Variables:"<<std::endl;
211  write(output,getEnvironmentalVariables(),widthOfOutput);
212  needspace = true;
213  }
214 
215 }
216 
void write(std::ostream &output, const UsageMap &um, unsigned int widthOfOutput=80)
void addCommandLineOption(const ossimString &option, const ossimString &explanation)
const UsageMap & getEnvironmentalVariables() const
const ossimString & getApplicationName() const
T max(T a, T b)
Definition: ossimCommon.h:236
const UsageMap & getCommandLineOptions() const
static ossimApplicationUsage * instance()
void setCommandLineUsage(const ossimString &explanation)
void getFormatedString(ossimString &str, const UsageMap &um, unsigned int widthOfOutput=80)
const ossimString & getDescription() const
std::string::size_type length() const
Definition: ossimString.h:408
std::string::size_type size() const
Definition: ossimString.h:405
std::string::iterator begin()
Definition: ossimString.h:420
void setApplicationName(const ossimString &name)
T min(T a, T b)
Definition: ossimCommon.h:203
const ossimString & getCommandLineUsage() const
void addUsageExplanation(Type type, const ossimString &option, const ossimString &explanation)
void setDescription(const ossimString &desc)
std::string & replace(std::string::size_type pos, std::string::size_type n, const std::string &s)
Replaces a substring of *this with the string s.
Definition: ossimString.h:870
void addEnvironmentalVariable(const ossimString &option, const ossimString &explanation)
long long ossim_int64
const char * c_str() const
Returns a pointer to a null-terminated array of characters representing the string&#39;s contents...
Definition: ossimString.h:396
std::map< ossimString, ossimString, ossimStringLtstr > UsageMap
std::string & assign(std::string::size_type n, char c)
Erases the existing characters and replaces them by n copies of c.
Definition: ossimString.h:865
std::string::size_type find(const std::string &s, std::string::size_type pos=0) const
Searches for s as a substring of *this, beginning at character pos of *this.
Definition: ossimString.h:753
std::basic_ostream< char > ostream
Base class for char output streams.
Definition: ossimIosFwd.h:23