appkit  1.5.1
RoadNarrows Robotics Application Kit
IOManip.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: IOManip.cxx
10 //
11 /*! \file
12  *
13  * \brief Stream I/O manipulators and helpers.
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 <iomanip>
63 #include <sstream>
64 #include <string>
65 
66 #include "rnr/appkit/IOManip.h"
67 
68 using namespace std;
69 
70 namespace rnr
71 {
72  namespace io
73  {
74  /*!
75  * \brief Set absolute indentation level.
76  *
77  * \param nIndent Number of spaces to left indent.
78  *
79  * \return Indent manipulation object.
80  */
81  osManipIndent setindent(const long nIndent)
82  {
83  osManipIndent obj;
84 
85  obj.m_nIndent = nIndent;
86  obj.m_eChange = osManipIndent::Absolute;
87  obj.m_bOut = false;
88 
89  return obj;
90  }
91 
92  /*!
93  * \brief Set relative delta indentation level.
94  *
95  * \param nDelta Plus or minus delta from current indentation level.
96  *
97  * \return Indent manipulation object.
98  */
99  osManipIndent deltaindent(const long nDelta)
100  {
101  osManipIndent obj;
102 
103  obj.m_nIndent = nDelta;
104  obj.m_eChange = osManipIndent::Relative;
105  obj.m_bOut = false;
106 
107  return obj;
108  }
109 
110  /*!
111  * \brief Left indent at current stream indentation level.
112  *
113  * \return Indent manipulation object.
114  */
116  {
117  osManipIndent obj;
118 
119  obj.m_nIndent = 0;
120  obj.m_eChange = osManipIndent::NoChange;
121  obj.m_bOut = true;
122 
123  return obj;
124  }
125 
126  /*!
127  * \brief Insert indentation object into output stream.
128  *
129  * \param os Output stream.
130  * \param f Object to insert.
131  *
132  * \return Reference to output stream.
133  */
134  ostream &operator<<(ostream &os, const osManipIndent &f)
135  {
136  const static int index = os.xalloc();
137 
138  long level;
139 
140  switch( f.m_eChange )
141  {
142  case osManipIndent::Relative:
143  level = os.iword(index) + f.m_nIndent;
144  if( level < 0 )
145  {
146  level = 0;
147  }
148  os.iword(index) = level;
149  break;
150  case osManipIndent::Absolute:
151  level = f.m_nIndent >= 0? f.m_nIndent: 0;
152  os.iword(index) = level;
153  break;
154  case osManipIndent::NoChange:
155  default:
156  break;
157  }
158 
159  if( f.m_bOut )
160  {
161  for(level = 0; level < os.iword(index); ++level)
162  {
163  os << ' ';
164  }
165  }
166 
167  return os;
168  }
169 
170  } // namespace io
171 } // namespace rnr
bool m_bOut
do [not] output indentaion
Definition: IOManip.h:96
osManipIndent indent()
Left indent at current stream indentation level.
Definition: IOManip.cxx:115
change m_eChange
how the new indentationn is to be applied
Definition: IOManip.h:95
osManipIndent setindent(const long nIndent)
Set absolute indentation level.
Definition: IOManip.cxx:81
long m_nIndent
new indentation value
Definition: IOManip.h:94
Output stream indentation manipulator structure.
Definition: IOManip.h:82
Stream I/O manipulators and helpers.
osManipIndent deltaindent(const long nDelta)
Set relative delta indentation level.
Definition: IOManip.cxx:99
RoadNarrows Robotics.
Definition: Camera.h:74
std::ostream & operator<<(std::ostream &os, const timespec &obj)
A timespec insertion operator.