appkit  1.5.1
RoadNarrows Robotics Application Kit
CmdExtArg.cxx
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // Package: RoadNarrows Robotics Application Tool Kit
4 //
5 // Link: https://github.com/roadnarrows-robotics/rnr-sdk
6 //
7 // Library: librnr_appkit
8 //
9 // File: CmdExtArg.cxx
10 //
11 /*! \file
12  *
13  * \brief Command line extended argument implementation.
14  *
15  * \author Robin Knight (robin.knight@roadnarrows.com)
16  *
17  * \par Copyright
18  * \h_copy 2016-2017. RoadNarrows LLC.\n
19  * http://www.roadnarrows.com\n
20  * All Rights Reserved
21  *
22  * \par License:
23  * MIT
24  */
25 /*
26  * @EulaBegin@
27  *
28  * Permission is hereby granted, without written agreement and without
29  * license or royalty fees, to use, copy, modify, and distribute this
30  * software and its documentation for any purpose, provided that
31  * (1) The above copyright notice and the following two paragraphs
32  * appear in all copies of the source code and (2) redistributions
33  * including binaries reproduces these notices in the supporting
34  * documentation. Substantial modifications to this software may be
35  * copyrighted by their authors and need not follow the licensing terms
36  * described here, provided that the new terms are clearly indicated in
37  * all files where they apply.
38  *
39  * IN NO EVENT SHALL THE AUTHOR, ROADNARROWS LLC, OR ANY MEMBERS/EMPLOYEES
40  * OF ROADNARROW LLC OR DISTRIBUTORS OF THIS SOFTWARE BE LIABLE TO ANY
41  * PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
42  * DAMAGES ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION,
43  * EVEN IF THE AUTHORS OR ANY OF THE ABOVE PARTIES HAVE BEEN ADVISED OF
44  * THE POSSIBILITY OF SUCH DAMAGE.
45  *
46  * THE AUTHOR AND ROADNARROWS LLC SPECIFICALLY DISCLAIM ANY WARRANTIES,
47  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
48  * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN
49  * "AS IS" BASIS, AND THE AUTHORS AND DISTRIBUTORS HAVE NO OBLIGATION TO
50  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
51  *
52  * @EulaEnd@
53  */
54 ////////////////////////////////////////////////////////////////////////////////
55 
56 #include <stdio.h>
57 #include <unistd.h>
58 #include <stdlib.h>
59 #include <ctype.h>
60 
61 #include <iostream>
62 #include <sstream>
63 #include <string>
64 
65 #include "rnr/rnrconfig.h"
66 #include "rnr/log.h"
67 
68 #include "rnr/appkit/LogBook.h"
69 #include "rnr/appkit/CmdCore.h"
70 #include "rnr/appkit/CmdExtArg.h"
71 
72 using namespace std;
73 using namespace rnr;
74 using namespace rnr::cmd;
75 
76 // -----------------------------------------------------------------------------
77 // CmdExtArg Class
78 // -----------------------------------------------------------------------------
79 
80 CmdExtArg::CmdExtArg()
81 {
82  m_nCmdUid = NoUid;
83  m_nFormIndex = NoIndex;
84  m_nArgIndex = NoIndex;
85  m_nArgInstance = 0;
86  m_eCvtType = CvtTypeUndef;
87  m_bCvtVal = false;
88  m_lCvtVal = 0;
89  m_fCvtVal = 0.0;
90 }
91 
92 CmdExtArg::CmdExtArg(const int &nCmdUid,
93  const int &nFormIndex,
94  const int &nArgIndex,
95  const int &nArgInstance,
96  const string &strArg) :
97  m_nCmdUid(nCmdUid),
98  m_nFormIndex(nFormIndex),
99  m_nArgIndex(nArgIndex),
100  m_nArgInstance(nArgInstance),
101  m_strArg(strArg)
102 {
103  m_eCvtType = CvtTypeUndef;
104  m_bCvtVal = false;
105  m_lCvtVal = 0;
106  m_fCvtVal = 0.0;
107 }
108 
109 CmdExtArg::CmdExtArg(const CmdExtArg &src)
110 {
111  m_nCmdUid = src.m_nCmdUid;
112  m_nFormIndex = src.m_nFormIndex;
113  m_nArgIndex = src.m_nArgIndex;
114  m_nArgInstance = src.m_nArgInstance;
115  m_strArg = src.m_strArg;
116  m_eCvtType = src.m_eCvtType;
117  m_strCvtVal = src.m_strCvtVal;
118  m_bCvtVal = src.m_bCvtVal;
119  m_lCvtVal = src.m_lCvtVal;
120  m_fCvtVal = src.m_fCvtVal;
121 }
122 
123 CmdExtArg::~CmdExtArg()
124 {
125 }
126 
127 CmdExtArg &CmdExtArg::operator=(const CmdExtArg &rhs)
128 {
129  m_nCmdUid = rhs.m_nCmdUid;
130  m_nFormIndex = rhs.m_nFormIndex;
131  m_nArgIndex = rhs.m_nArgIndex;
132  m_nArgInstance = rhs.m_nArgInstance;
133  m_strArg = rhs.m_strArg;
134  m_eCvtType = rhs.m_eCvtType;
135  m_strCvtVal = rhs.m_strCvtVal;
136  m_bCvtVal = rhs.m_bCvtVal;
137  m_lCvtVal = rhs.m_lCvtVal;
138  m_fCvtVal = rhs.m_fCvtVal;
139 }
140 
141 void CmdExtArg::s(const string &strVal)
142 {
143  m_strCvtVal = strVal;
144  m_eCvtType = CvtTypeString;
145 }
146 
147 void CmdExtArg::e(const long eVal)
148 {
149  m_strCvtVal = m_strArg;
150  m_lCvtVal = eVal;
151  m_eCvtType = CvtTypeEnum;
152 }
153 
154 void CmdExtArg::b(const bool bVal)
155 {
156  m_bCvtVal = bVal;
157  m_eCvtType = CvtTypeBoolean;
158 }
159 
160 void CmdExtArg::i(const long lVal)
161 {
162  m_lCvtVal = lVal;
163  m_eCvtType = CvtTypeInteger;
164 }
165 
166 void CmdExtArg::f(const double fVal)
167 {
168  m_fCvtVal = fVal;
169  m_eCvtType = CvtTypeFpn;
170 }
171 
172 bool CmdExtArg::operator==(const CmdExtArg &rval) const
173 {
174  return (type() == rval.type()) && (arg() == rval.arg());
175 }
176 
177 bool CmdExtArg::operator==(const std::string &rval) const
178 {
179  return ((type() == CvtTypeString) || (type() == CvtTypeEnum)) &&
180  (s() == rval);
181 }
182 
183 bool CmdExtArg::operator==(const char* const &rval) const
184 {
185  if( rval == NULL )
186  {
187  return false;
188  }
189 
190  return ((type() == CvtTypeString) || (type() == CvtTypeEnum)) &&
191  (strcmp(s().c_str(), rval) == 0);
192 }
193 
194 bool CmdExtArg::operator==(const bool &rval) const
195 {
196  return (type() == CvtTypeBoolean) && (b() == rval);
197 }
198 
199 bool CmdExtArg::operator==(const long &rval) const
200 {
201  return ((type() == CvtTypeEnum) || (type() == CvtTypeInteger)) &&
202  (i() == rval);
203 }
204 
205 bool CmdExtArg::operator==(const double &rval) const
206 {
207  return (type() == CvtTypeFpn) && (f() == rval);
208 }
209 
210 ostream &rnr::cmd::operator<<(ostream &os, const CmdExtArg &arg)
211 {
212  switch( arg.type() )
213  {
214  case CmdExtArg::CvtTypeString:
215  os << arg.s();
216  break;
217  case CmdExtArg::CvtTypeEnum:
218  os << arg.s(); // string has priority over enum
219  break;
220  case CmdExtArg::CvtTypeBoolean:
221  os << arg.b();
222  break;
223  case CmdExtArg::CvtTypeInteger:
224  os << arg.i();
225  break;
226  case CmdExtArg::CvtTypeFpn:
227  os << arg.f();
228  break;
229  case CmdExtArg::CvtTypeUndef:
230  default:
231  os << undefstring;
232  break;
233  }
234 
235  return os;
236 }
237 
239 {
240  stringstream ss;
241 
242  ss << arg;
243  log << ss.str();
244 
245  return log;
246 }
CvtType m_eCvtType
converted type
Definition: CmdExtArg.h:339
Command line extended argument interface.
std::string m_strArg
argument unconverted raw value string
Definition: CmdExtArg.h:336
Command EXTended ARGument class holding parsed command context and the raw and converted argmument va...
Definition: CmdExtArg.h:91
const std::string & s() const
Get the converted string value.
Definition: CmdExtArg.h:215
int m_nCmdUid
command definition unique id
Definition: CmdExtArg.h:330
int m_nArgIndex
argument definition index
Definition: CmdExtArg.h:332
CvtType type() const
Get the converted argument type.
Definition: CmdExtArg.h:208
long i() const
Get the converted integer value.
Definition: CmdExtArg.h:236
const int NoUid
Special values.
Definition: CmdCore.h:116
std::string m_strCvtVal
converted string value
Definition: CmdExtArg.h:340
double m_fCvtVal
converted float-point number value
Definition: CmdExtArg.h:343
int m_nArgInstance
argument instance (FUTURE)
Definition: CmdExtArg.h:333
const std::string undefstring
"undef" string
LogBook class interface.
bool m_bCvtVal
converted boolean value
Definition: CmdExtArg.h:341
Commands.
Definition: CmdAddOns.h:81
bool b() const
Get the converted boolean value.
Definition: CmdExtArg.h:229
int m_nFormIndex
form definition index
Definition: CmdExtArg.h:331
const int NoIndex
no index
Definition: CmdCore.h:117
const std::string & arg() const
Get raw source argument string (or a pirate grunting).
Definition: CmdExtArg.h:194
double f() const
Get the converted floating-point number value.
Definition: CmdExtArg.h:243
Command line core data types.
RoadNarrows Robotics.
Definition: Camera.h:74
std::ostream & operator<<(std::ostream &os, const timespec &obj)
A timespec insertion operator.
long m_lCvtVal
converted integer/index value
Definition: CmdExtArg.h:342