format-helpers.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2014-2022, Regents of the University of California,
4  * Arizona Board of Regents,
5  * Colorado State University,
6  * University Pierre & Marie Curie, Sorbonne University,
7  * Washington University in St. Louis,
8  * Beijing Institute of Technology,
9  * The University of Memphis.
10  *
11  * This file is part of NFD (Named Data Networking Forwarding Daemon).
12  * See AUTHORS.md for complete list of NFD authors and contributors.
13  *
14  * NFD is free software: you can redistribute it and/or modify it under the terms
15  * of the GNU General Public License as published by the Free Software Foundation,
16  * either version 3 of the License, or (at your option) any later version.
17  *
18  * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
19  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
20  * PURPOSE. See the GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License along with
23  * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
24  */
25 
26 #include "format-helpers.hpp"
27 
28 #include <iomanip>
29 #include <sstream>
30 
31 namespace nfd::tools::nfdc {
32 
33 namespace xml {
34 
35 void
36 printHeader(std::ostream& os)
37 {
38  os << "<?xml version=\"1.0\"?>"
39  << "<nfdStatus xmlns=\"ndn:/localhost/nfd/status/1\">";
40 }
41 
42 void
43 printFooter(std::ostream& os)
44 {
45  os << "</nfdStatus>";
46 }
47 
48 std::ostream&
49 operator<<(std::ostream& os, const Text& text)
50 {
51  for (char ch : text.s) {
52  switch (ch) {
53  case '"':
54  os << "&quot;";
55  break;
56  case '&':
57  os << "&amp;";
58  break;
59  case '\'':
60  os << "&apos;";
61  break;
62  case '<':
63  os << "&lt;";
64  break;
65  case '>':
66  os << "&gt;";
67  break;
68  default:
69  os << ch;
70  break;
71  }
72  }
73  return os;
74 }
75 
76 std::ostream&
77 operator<<(std::ostream& os, Flag v)
78 {
79  if (!v.flag) {
80  return os;
81  }
82  return os << '<' << v.elementName << "/>";
83 }
84 
85 std::string
86 formatDuration(time::nanoseconds d)
87 {
88  std::ostringstream str;
89 
90  if (d < 0_ns) {
91  str << "-";
92  }
93 
94  str << "PT";
95 
96  time::seconds seconds(time::duration_cast<time::seconds>(time::abs(d)));
97  time::milliseconds ms(time::duration_cast<time::milliseconds>(time::abs(d) - seconds));
98 
99  str << seconds.count();
100 
101  if (ms >= 1_ms) {
102  str << "." << std::setfill('0') << std::setw(3) << ms.count();
103  }
104 
105  str << "S";
106 
107  return str.str();
108 }
109 
110 std::string
111 formatTimestamp(time::system_clock::time_point t)
112 {
113  return time::toIsoExtendedString(t);
114 }
115 
116 } // namespace xml
117 
118 namespace text {
119 
120 std::ostream&
121 operator<<(std::ostream& os, const Spaces& spaces)
122 {
123  for (int i = 0; i < spaces.nSpaces; ++i) {
124  os << ' ';
125  }
126  return os;
127 }
128 
129 Separator::Separator(std::string_view first, std::string_view subsequent)
130  : m_first(first)
131  , m_subsequent(subsequent)
132 {
133 }
134 
135 Separator::Separator(std::string_view subsequent)
136  : Separator("", subsequent)
137 {
138 }
139 
140 std::ostream&
141 operator<<(std::ostream& os, Separator& sep)
142 {
143  if (++sep.m_count == 1) {
144  return os << sep.m_first;
145  }
146  return os << sep.m_subsequent;
147 }
148 
149 ItemAttributes::ItemAttributes(bool wantMultiLine, int maxAttributeWidth)
150  : m_wantMultiLine(wantMultiLine)
151  , m_maxAttributeWidth(maxAttributeWidth)
152 {
153 }
154 
156 ItemAttributes::operator()(const std::string& attribute)
157 {
158  return {*this, attribute};
159 }
160 
161 std::string
163 {
164  return m_wantMultiLine ? "\n" : "";
165 }
166 
167 std::ostream&
168 operator<<(std::ostream& os, const ItemAttributes::Attribute& attr)
169 {
170  ++attr.ia.m_count;
171  if (attr.ia.m_wantMultiLine) {
172  if (attr.ia.m_count > 1) {
173  os << '\n';
174  }
175  os << Spaces{attr.ia.m_maxAttributeWidth - static_cast<int>(attr.attribute.size())};
176  }
177  else {
178  if (attr.ia.m_count > 1) {
179  os << ' ';
180  }
181  }
182  return os << attr.attribute << '=';
183 }
184 
185 std::ostream&
186 operator<<(std::ostream& os, OnOff v)
187 {
188  return os << (v.flag ? "on" : "off");
189 }
190 
191 std::ostream&
192 operator<<(std::ostream& os, YesNo v)
193 {
194  return os << (v.flag ? "yes" : "no");
195 }
196 
197 std::string
198 formatTimestamp(time::system_clock::time_point t)
199 {
200  return time::toIsoString(t);
201 }
202 
203 } // namespace text
204 
205 } // namespace nfd::tools::nfdc
ItemAttributes(bool wantMultiLine=false, int maxAttributeWidth=0)
Constructor.
Attribute operator()(const std::string &attribute)
Print different string on first and subsequent usage.
Separator(std::string_view first, std::string_view subsequent)
std::string formatTimestamp(time::system_clock::time_point t)
std::ostream & operator<<(std::ostream &os, const Spaces &spaces)
std::string formatTimestamp(time::system_clock::time_point t)
std::string formatDuration(time::nanoseconds d)
void printFooter(std::ostream &os)
void printHeader(std::ostream &os)
std::ostream & operator<<(std::ostream &os, const Text &text)
Print XML text with special character represented as predefined entities.
Print boolean as 'on' or 'off'.
Print a number of whitespaces.
int nSpaces
number of spaces; print nothing if negative
Print boolean as 'yes' or 'no'.
Print true as an empty element and false as nothing.