appkit  1.5.1
RoadNarrows Robotics Application Kit
Token.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: Token.cxx
10 //
11 /*! \file
12  *
13  * \brief Simple, token container class 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 
60 #include <iostream>
61 #include <sstream>
62 #include <string>
63 
64 #include "rnr/rnrconfig.h"
65 #include "rnr/log.h"
66 #include "rnr/color.h"
67 
69 #include "rnr/appkit/LogBook.h"
70 #include "rnr/appkit/Token.h"
71 
72 using namespace std;
73 using namespace rnr;
74 using namespace rnr::str;
75 using namespace rnr::cmd;
76 
77 // -----------------------------------------------------------------------------
78 // Token Class
79 // -----------------------------------------------------------------------------
80 
81 Token::Token()
82 {
83  m_lineNum = 0;
84  m_posStart = 0;
85  m_posEnd = 0;
86 }
87 
88 Token::Token(const string &strValue)
89  : m_strValue(strValue), m_lineNum(0), m_posStart(0), m_posEnd(0)
90 {
91 }
92 
93 Token::Token(const string &strValue,
94  const size_t lineNum,
95  const size_t posStart,
96  const size_t posEnd)
97  : m_strValue(strValue),
98  m_lineNum(lineNum),
99  m_posStart(posStart),
100  m_posEnd(posEnd)
101 {
102 }
103 
104 Token::Token(const Token &src)
105 {
106  m_strValue = src.m_strValue;
107  m_lineNum = src.m_lineNum;
108  m_posStart = src.m_posStart;
109  m_posEnd = src.m_posEnd;
110 }
111 
112 Token::~Token()
113 {
114 }
115 
116 Token &Token::operator=(const Token &rhs)
117 {
118  m_strValue = rhs.m_strValue;
119  m_lineNum = rhs.m_lineNum;
120  m_posStart = rhs.m_posStart;
121  m_posEnd = rhs.m_posEnd;
122 
123  return *this;
124 }
125 
126 ostream &Token::printAnnotated(ostream &os,
127  const string &strLine,
128  const bool bLoc)
129 {
130  string color(ANSI_FG_BRIGHT_RED);
131  string reset(ANSI_COLOR_RESET);
132 
133  size_t lw = strLine.length(); // line width
134  size_t pos; // working substring starting position
135  size_t len; // working substring length
136 
137  //
138  // Print line number
139  //
140  if( bLoc )
141  {
142  os << "[";
143  if( m_lineNum > 0 )
144  {
145  os << m_lineNum << ":";
146  }
147  os << m_posStart << "," << m_posEnd;
148  os << "] ";
149  }
150 
151  //
152  // Print token embedded in line.
153  //
154  if( m_posStart < lw )
155  {
156  //
157  // Print substring before token in plaintext
158  //
159  if( m_posStart > 0 )
160  {
161  os << strLine.substr(0, m_posStart);
162  }
163 
164  //
165  // Print token substring in color.
166  //
167  len = m_posEnd - m_posStart + 1;
168  if( len > lw )
169  {
170  len = lw - m_posStart;
171  }
172  os << color << strLine.substr(m_posStart, len) << reset;
173 
174  //
175  // Print substring after token in plaintext
176  //
177  pos = m_posEnd + 1;
178  if( pos < lw )
179  {
180  os << strLine.substr(pos);
181  }
182  }
183 
184  //
185  // Print line.
186  //
187  else
188  {
189  os << strLine << endl;
190  }
191 
192  return os;
193 }
194 
195 ostream &rnr::cmd::operator<<(ostream &os, const Token &tok)
196 {
197  os << "'" << prettify(tok.value()) << "'";
198  //os << " @" << tok.m_posStart << ":" << tok.m_posEnd;
199 
200  return os;
201 }
202 
203 LogBook &rnr::cmd::operator<<(LogBook &log, const Token &tok)
204 {
205  stringstream ss;
206 
207  ss << tok;
208  log << ss.str();
209 
210  return log;
211 }
String.
Definition: StringTheory.h:78
size_t m_lineNum
line number
Definition: Token.h:88
size_t m_posEnd
line end position of token
Definition: Token.h:90
Of string spaces and their strangian operators.
size_t m_posStart
line start position of token
Definition: Token.h:89
std::string prettify(const std::string &str)
Prettify string.
std::string m_strValue
token string value
Definition: Token.h:87
const std::string & value() const
Return token string.
Definition: Token.h:149
LogBook class interface.
Commands.
Definition: CmdAddOns.h:81
Parsed token container class.
Definition: Token.h:84
RoadNarrows Robotics.
Definition: Camera.h:74
std::ostream & operator<<(std::ostream &os, const timespec &obj)
A timespec insertion operator.
Simple, token container class interface.