Stun Server  Compliant with the latest RFCs including 5389, 5769, and 5780
discover the local host's own external IP address
prettyprint.cpp
Go to the documentation of this file.
1 /*
2  Copyright 2011 John Selbie
3 
4  Licensed under the Apache License, Version 2.0 (the "License");
5  you may not use this file except in compliance with the License.
6  You may obtain a copy of the License at
7 
8  http://www.apache.org/licenses/LICENSE-2.0
9 
10  Unless required by applicable law or agreed to in writing, software
11  distributed under the License is distributed on an "AS IS" BASIS,
12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  See the License for the specific language governing permissions and
14  limitations under the License.
15 */
16 
17 
18 #include "commonincludes.hpp"
19 
20 
21 static bool IsWhitespace(char ch)
22 {
23  return ((ch == ' ') || (ch == '\t') || (ch == '\v') || (ch == '\r') || (ch == '\n'));
24 }
25 
26 static void SplitParagraphIntoWords(const char* pszLine, std::vector<std::string>& listWords)
27 {
28  std::string strWord;
29 
30  if (pszLine == NULL)
31  {
32  return;
33  }
34 
35  while (*pszLine)
36  {
37  while ((*pszLine) && IsWhitespace(*pszLine))
38  {
39  pszLine++;
40  }
41 
42  if (*pszLine == 0)
43  {
44  return;
45  }
46 
47  while ((*pszLine) && IsWhitespace(*pszLine)==false)
48  {
49  strWord += *pszLine;
50  pszLine++;
51  }
52 
53  listWords.push_back(strWord);
54  strWord.clear();
55  }
56 }
57 
58 static void SplitInputIntoParagraphs(const char* pszInput, std::vector<std::string>& listParagraphs)
59 {
60  // blindly scan to the next \r or \n
61  std::string strParagraph;
62 
63  if (pszInput == NULL)
64  {
65  return;
66  }
67 
68  while (*pszInput)
69  {
70  while ((*pszInput != '\0') && (*pszInput != '\r') && (*pszInput != '\n'))
71  {
72  strParagraph += *pszInput;
73  pszInput++;
74  }
75 
76  listParagraphs.push_back(strParagraph);
77  strParagraph.clear();
78 
79  if (*pszInput == '\r')
80  {
81  pszInput++;
82  if (*pszInput == '\n')
83  {
84  pszInput++;
85  }
86  }
87  else if (*pszInput == '\n')
88  {
89  pszInput++;
90  }
91  }
92 }
93 
94 static void PrintParagraph(const char* psz, size_t width)
95 {
96  size_t indent = 0;
97  const char *pszProbe = psz;
98  std::vector<std::string> listWords;
99  bool fLineStart = true;
100  std::string strLine;
101  std::string strIndent;
102  size_t wordcount = 0;
103  size_t wordindex = 0;
104 
105  if (width <= 0)
106  {
107  return;
108  }
109 
110  if (psz==NULL)
111  {
112  return;
113  }
114 
115  while ((*pszProbe) && IsWhitespace(*pszProbe))
116  {
117  indent++;
118  pszProbe++;
119  }
120 
121  SplitParagraphIntoWords(psz, listWords);
122  wordcount = listWords.size();
123 
124  if (indent >= width)
125  {
126  indent = width-1;
127  }
128  for (size_t x = 0; x < indent; x++)
129  {
130  strIndent += ' ';
131  }
132 
133  while (wordindex < wordcount)
134  {
135 
136  if (fLineStart)
137  {
138  fLineStart = false;
139  strLine = strIndent;
140 
141  // we always consume a word and put it at the start of a line regardless of the space we have left
142  // one day we can consider doing hyphenation...
143  strLine += listWords[wordindex];
144  wordindex++;
145  }
146  else
147  {
148  // do we have enough room to fit including the space?
149  size_t newsize = strLine.size() + 1 + listWords[wordindex].size();
150 
151  if (newsize <= width)
152  {
153  strLine += ' ';
154  strLine += listWords[wordindex];
155  wordindex++;
156  }
157  else
158  {
159  // not a fit, we need to start a new line
160 
161  // flush whatever we have now
162  printf("%s\n", strLine.c_str());
163 
164  // flag that we got a new line starting
165  fLineStart = true;
166 
167  }
168  }
169 
170  }
171 
172  if ((strLine.size() > 0) || (wordcount == 0))
173  {
174  printf("%s\n", strLine.c_str());
175  }
176 
177 
178 
179 }
180 
181 void PrettyPrint(const char* pszInput, size_t width)
182 {
183  std::vector<std::string> listParagraphs;
184  size_t len;
185 
186  SplitInputIntoParagraphs(pszInput, listParagraphs);
187 
188  len = listParagraphs.size();
189  for (size_t x = 0; x < len; x++)
190  {
191  PrintParagraph(listParagraphs[x].c_str(), width);
192  }
193 }
194 
static void SplitInputIntoParagraphs(const char *pszInput, std::vector< std::string > &listParagraphs)
Definition: prettyprint.cpp:58
static bool IsWhitespace(char ch)
Definition: prettyprint.cpp:21
static void PrintParagraph(const char *psz, size_t width)
Definition: prettyprint.cpp:94
static void SplitParagraphIntoWords(const char *pszLine, std::vector< std::string > &listWords)
Definition: prettyprint.cpp:26
void PrettyPrint(const char *pszInput, size_t width)