coan  6.0.1
A C/C++ Configuration Analyzer
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
citable.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2013 Mike Kinghan, imk@burroingroingjoing.com *
3  * All rights reserved. *
4  * *
5  * Contributed originally by Mike Kinghan, imk@burroingroingjoing.com *
6  * *
7  * Redistribution and use in source and binary forms, with or without *
8  * modification, are permitted provided that the following conditions *
9  * are met: *
10  * *
11  * Redistributions of source code must retain the above copyright *
12  * notice, this list of conditions and the following disclaimer. *
13  * *
14  * Redistributions in binary form must reproduce the above copyright *
15  * notice, this list of conditions and the following disclaimer in the *
16  * documentation and/or other materials provided with the distribution. *
17  * *
18  * Neither the name of Mike Kinghan nor the names of its *
19  * contributors may be used to endorse or promote products derived from *
20  * this software without specific prior written permission. *
21  * *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
23  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT *
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS *
25  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE *
26  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, *
27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, *
28  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS *
29  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED *
30  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,*
31  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF *
32  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH *
33  * DAMAGE. *
34  * *
35  **************************************************************************/
36 
37 #include "citable.h"
38 #include <cassert>
39 
44 
46 using namespace std;
47 
48 namespace innards {
49 
50 template<class CharSeq>
51 std::string citable(chewer<CharSeq> & chew, size_t len)
52 {
54  std::string citable;
55  bool escaped = false;
56  size_t ndx = 0;
57  char const space = ' ';
58  char last_ch = 0;
59  if (len == std::string::npos) {
60  len = chew.remaining();
61  }
62  chew(greyspace);
63  for ( ; ndx < len; ++ndx, ++chew) {
64  if (!last_ch && isspace(*chew)) {
65  continue;
66  }
67  switch(*chew) {
68  case '\\':
69  if (!escaped) {
70  if (ndx + 2 < len &&
71  chew.atoff(1) == '\r' &&
72  chew.atoff(2) == '\n') {
73  last_ch = *chew;
74  ndx += 2;
75  chew += 2;
76  } else if (ndx + 1 < len && chew.atoff(1) == '\n') {
77  last_ch = *chew;
78  ++ndx;
79  ++chew;
80 
81  } else {
82  citable.append(1,*chew);
83  last_ch = *chew;
84  escaped = true;
85  }
86  continue;
87  }
88  break;
89  case '\r':
90  if (ndx + 1 < len &&
91  chew.atoff(1) == '\n') {
92  ++ndx;
93  ++chew;
94  }
95  if (last_ch != space) {
96  citable.append(1,space);
97  last_ch = space;
98  }
99  break;
100  case '\n':
101  case '\t':
102  case space:
103  if (last_ch != space) {
104  citable.append(1,space);
105  last_ch = space;
106  }
107  break;
108  default:
109  if (isprint(*chew)) {
110  citable.append(1,*chew);
111  last_ch = *chew;
112  }
113  }
114  escaped = false;
115  }
116  ndx = citable.length();
117  if (ndx) {
118  while (ndx-- > 0 && isspace(citable[ndx])) {}
119  citable.resize(ndx + 1);
120  }
121  return citable;
122 }
123 
124 } // namespace innards
125 
126 string citable(chewer<std::string> & chew, size_t len)
127 {
128  return innards::citable(chew,len);
129 }
130 std::string citable(chewer<parse_buffer> & chew, size_t len)
131 {
132  return innards::citable(chew,len);
133 }
134 
135 string citable(integer const & integ)
136 {
137  switch(integ.type()) {
138  case INT_INT:
139  return to_string((int)integ.raw());
140  case INT_UINT:
141  return to_string((unsigned)integ.raw());
142  case INT_LONG:
143  return to_string((long)integ.raw());
144  case INT_ULONG:
145  return to_string((unsigned long)integ.raw());
146  case INT_LLONG:
147  return to_string((long long)integ.raw());
148  case INT_ULLONG:
149  return to_string((unsigned long long)integ.raw());
150  default:
151  assert(false);
152  }
153  return string();
154 }
155 
157 
158 // EOF
159 
160 
template struct traits::is_random_access_char_sequence<T> exports a static const boolean member value...
Definition: traits.h:166
unsigned long long raw() const
Get the bits comprising the integer as an unsigned long long
Definition: integer.h:93
Type long long int
Definition: integer.h:59
Type unsigned long
Definition: integer.h:57
Class integer encapsulates an integer of some type.
Definition: integer.h:65
std::string citable(chewer< std::string > &chew, size_t len=std::string::npos)
Make a citable version of length-delimited text.
Type int
Definition: integer.h:51
Type long
Definition: integer.h:55
size_t remaining() const
Get the remaining length of the associated sequence_type from the scanning position.
Definition: chew.h:376
chew_mode::greyspace const greyspace
An exemplar chew_mode::greyspace
Definition: chew.h:211
Type unsigned long long
Definition: integer.h:61
integer_type type() const
Get the type of the integer
Definition: integer.h:77
`template struct chewer<CharSeq> is a cursor-like type that is associated with a character-sequence t...
Definition: chew.h:248
Type unsigned
Definition: integer.h:53
char_type atoff(ptrdiff_t off) const
Get the character at an offset from the scanning position.
Definition: chew.h:349