VR-Link API Documentation for HLA 1516
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
cmdStdOutput.h
Go to the documentation of this file.
1 
2 /******************************************************************************
3  *
4  * file: StdOutput.h
5  *
6  * Copyright (c) 2004, Michael E. Smoot
7  * All rights reverved.
8  *
9  * See the file COPYING in the top directory of this distribution for
10  * more information.
11  *
12  * THE SOFTWARE IS PROVIDED _AS IS_, WITHOUT WARRANTY OF ANY KIND, EXPRESS
13  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
15  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
16  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
17  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
18  * DEALINGS IN THE SOFTWARE.
19  *
20  *****************************************************************************/
21 
22 #ifndef DtCmdLine_STDCMDLINEOUTPUT_H
23 #define DtCmdLine_STDCMDLINEOUTPUT_H
24 
28 
29 #include <string>
30 #include <vector>
31 #include <list>
32 #include <iostream>
33 #include <algorithm>
34 #include <vlutil/vlPrint.h>
36 
37 #include "cmdLineInterface.h"
38 #include "cmdLineOutput.h"
39 #include "cmdXorHandler.h"
40 #include "cmdArg.h"
41 
42 #ifdef DtUSE_UTILITIES_NAMESPACE
43 namespace DtUSE_UTILITIES_NAMESPACE
44 {
45 #endif
46 
47  namespace DtCmdLine {
48 
53  class StdOutput : public CmdLineOutput
55  {
56 
57  public:
58 
59  virtual ~StdOutput() { }
60 
66  virtual void usage(CmdLineInterface& c);
67 
73  virtual void version(CmdLineInterface& c);
74 
81  virtual void failure(CmdLineInterface& c,
82  ArgException& e );
83 
84  protected:
85 
91  void _shortUsage( CmdLineInterface& c, std::ostream& os ) const;
92 
99  void _longUsage( CmdLineInterface& c, std::ostream& os ) const;
100 
112  void spacePrint( std::ostream& os,
113  const std::string& s,
114  int maxWidth,
115  int indentSpaces,
116  int secondLineOffset ) const;
117 
118  };
119 
120 
121  inline void StdOutput::version(CmdLineInterface& _cmd)
122  {
123  std::string progName = _cmd.getProgramName();
124  std::string version = _cmd.getVersion();
125 
126  int oldNotifyLevel = DtNotifyLevel;
127  DtNotifyLevel = 3;
128  DtInfo
129  << '\n' << progName << " version: "
130  << version << "\n\n";
131  DtNotifyLevel = oldNotifyLevel;
132  }
133 
134  inline void StdOutput::usage(CmdLineInterface& _cmd )
135  {
136  int oldNotifyLevel = DtNotifyLevel;
137  DtNotifyLevel = 3;
138  DtInfo << '\n' << "USAGE: " << '\n' << '\n';
139 
140  _shortUsage( _cmd, DtInfo );
141 
142  DtInfo << '\n' << "Where: " << '\n';
143 
144  _longUsage( _cmd, DtInfo );
145 
146  DtInfo << std::endl;
147  DtNotifyLevel = oldNotifyLevel;
148 
149  }
150 
151  inline void StdOutput::failure( CmdLineInterface& _cmd, ArgException& e )
152  {
154  int oldNotifyLevel = DtNotifyLevel;
155  DtNotifyLevel = 4;
156  std::string progName = _cmd.getProgramName();
157 
158  DtWarn
159  << "PARSE ERROR: " << e.argId() << '\n'
160  << " " << e.error() << "\n\n"
161  << "Brief USAGE: " << '\n';
162 
163  _shortUsage( _cmd, DtWarn );
164 
165  DtWarn
166  << '\n' << "For complete USAGE and HELP type: "
167  << '\n' << " " << progName << " --help"
168  << '\n' << std::endl;
169  DtNotifyLevel = oldNotifyLevel;
170 
171  }
172 
173  inline void StdOutput::_shortUsage( CmdLineInterface& _cmd,
174  std::ostream& os ) const
175  {
176  std::list<Arg*> argList = _cmd.getArgList();
177  std::string progName = _cmd.getProgramName();
178  XorHandler xorHandler = _cmd.getXorHandler();
179  std::vector< std::vector<Arg*> > xorList = xorHandler.getXorList();
180 
181  std::string s = progName + " ";
182 
184  for ( int i = 0; (unsigned int)i < xorList.size(); i++ )
185  {
186  s += " {";
187  for ( ArgVectorIterator it = xorList[i].begin();
188  it != xorList[i].end(); it++ )
189  s += (*it)->shortID() + "|";
190 
191  s[s.length()-1] = '}';
192  }
193 
195  for (ArgListIterator it = argList.begin(); it != argList.end(); it++)
196  if ( !xorHandler.contains( (*it) ) )
197  s += " " + (*it)->shortID();
198 
200  int secondLineOffset = (int)(progName.length()) + 2;
201  if ( secondLineOffset > 80/2 )
202  secondLineOffset = (int)(80/2);
203 
204  spacePrint( os, s, 80, 3, secondLineOffset );
205  }
206 
207  inline void StdOutput::_longUsage( CmdLineInterface& _cmd,
208  std::ostream& os ) const
209  {
210  std::list<Arg*> argList = _cmd.getArgList();
211  std::string message = _cmd.getMessage();
212  XorHandler xorHandler = _cmd.getXorHandler();
213  std::vector< std::vector<Arg*> > xorList = xorHandler.getXorList();
214 
216  for ( int i = 0; (unsigned int)i < xorList.size(); i++ )
217  {
218  for ( ArgVectorIterator it = xorList[i].begin();
219  it != xorList[i].end();
220  it++ )
221  {
222  spacePrint( os, (*it)->longID(), 80, 3, 3 );
223  spacePrint( os, (*it)->getDescription(), 80, 12, 0 );
224 
225  if ( it+1 != xorList[i].end() )
226  spacePrint(os, "-- OR --", 80, 25, 0);
227  }
228  }
229 
231  for (ArgListIterator it = argList.begin(); it != argList.end(); it++)
232  {
233  if ( !xorHandler.contains( (*it) ) )
234  {
235  spacePrint( os, (*it)->longID(), 80, 3, 3 );
236  spacePrint( os, (*it)->getDescription(), 80, 12, 0 );
237  }
238  }
239  os << '\n';
240  spacePrint( os, message, 80, 3, 0 );
241  }
242 
243  inline void StdOutput::spacePrint( std::ostream& os,
244  const std::string& s,
245  int maxWidth,
246  int indentSpaces,
247  int secondLineOffset ) const
248  {
249  int len = (int)(s.length());
250 
251  if ( (len + indentSpaces > maxWidth) && maxWidth > 0 )
252  {
253  int allowedLen = maxWidth - indentSpaces;
254  int start = 0;
255  while ( start < len )
256  {
258 #ifdef _WIN32
259  int stringLen = ( len - start < allowedLen ? len - start : allowedLen);
260 #else
261  int stringLen = std::min( len - start, allowedLen );
262 #endif
263 
265  if ( stringLen == allowedLen )
266  while ( stringLen >= 0 &&
267  s[stringLen+start] != ' ' &&
268  s[stringLen+start] != ',' &&
269  s[stringLen+start] != '|')
270  stringLen--;
271 
274  if ( stringLen <= 0 )
275  stringLen = allowedLen;
276 
278  int i = 0;
279  for (i = 0; i < stringLen; i++ )
280  if ( s[start+i] == '\n' )
281  stringLen = i+1;
282 
284  for (i = 0; i < indentSpaces; i++ )
285  os << " ";
286 
287  if ( start == 0 )
288  {
290  indentSpaces += secondLineOffset;
291 
293  allowedLen -= secondLineOffset;
294  }
295 
296  os << s.substr(start,stringLen) << std::endl;
297 
299  while ( s[stringLen+start] == ' ' && start < len )
300  start++;
301 
302  start += stringLen;
303  }
304  }
305  else
306  {
307  for ( int i = 0; i < indentSpaces; i++ )
308  os << " ";
309  os << s << std::endl;
310  }
311  }
312 
313  } //namespace DtCmdLine
314 
315 #ifdef DtUSE_UTILITIES_NAMESPACE
316 }
317 #endif
318 
319 #endif
This file declares the class DtCmdLine::CmdLineOutput.
std::vector< std::vector< Arg * > > & getXorList()
Definition: cmdXorHandler.h:185
This file declares classes for VR-Link command line parsing utility.
This file declares the class DtCmdLine::CmdLineInterface.
Functions in vlPrint.h provide an error and print facility for vrlink.
This class handles lists of Arg&#39;s that are to be XOR&#39;d on the command line.
Definition: cmdXorHandler.h:48
std::string argId() const
Returns the argument id.
Definition: cmdArgException.h:76
virtual std::list< Arg * > & getArgList()=0
Returns the argList.
A simple class that defines and argument exception.
Definition: cmdArgException.h:43
virtual XorHandler & getXorHandler()=0
Returns the XorHandler.
int DtNotifyLevel
DtNotifyLevel is used to control the level of diagnostics output by VR-Link.
virtual std::string & getProgramName()=0
Returns the program name string.
This file declares the class DtCmdLine::XorHandler.
virtual std::string & getMessage()=0
Returns the message string.
DtOutputStream DtWarn
These DtOutputStream instances are used for all print messaging in VR-Link.
std::string error() const
Returns the error text.
Definition: cmdArgException.h:70
bool contains(const Arg *a)
Simply checks whether the Arg is contained in one of the arg lists.
Definition: cmdXorHandler.h:173
DtOutputStream DtInfo
DtInfo is an global instance of a DtOutputStream.
virtual std::string & getVersion()=0
Returns the version string.
std::list< Arg * >::iterator ArgListIterator
Typedef of an Arg list iterator.
Definition: cmdArg.h:342
The base class that manages the command line definition and passes along the parsing to the appropria...
Definition: cmdLineInterface.h:52
std::vector< Arg * >::iterator ArgVectorIterator
Typedef of an Arg vector iterator.
Definition: cmdArg.h:347
virtual ~StdOutput()
Definition: cmdStdOutput.h:59
ProcControl.h provides operating system independent methods to control processes. ...

Document ID: Generated on Tue Feb 2 21:02:17 EST 2021 from SVN revision 223668
Copyright © 2005-2021 MAK Technologies. All Rights Reserved (www.mak.com)