blob: 1810f3016cfdebf21a11f7c79c802baeffd0663c [file] [log] [blame]
Junxiao Shi38f4ce92016-08-04 10:01:52 +00001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Eric Newberryde332452018-01-30 11:45:32 -07002/*
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -04003 * Copyright (c) 2014-2022, Regents of the University of California,
Junxiao Shi38f4ce92016-08-04 10:01:52 +00004 * 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
Eric Newberryde332452018-01-30 11:45:32 -070028#include <iomanip>
29#include <sstream>
30
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040031namespace nfd::tools::nfdc {
Junxiao Shi38f4ce92016-08-04 10:01:52 +000032
33namespace xml {
34
35void
36printHeader(std::ostream& os)
37{
38 os << "<?xml version=\"1.0\"?>"
39 << "<nfdStatus xmlns=\"ndn:/localhost/nfd/status/1\">";
40}
41
42void
43printFooter(std::ostream& os)
44{
45 os << "</nfdStatus>";
46}
47
48std::ostream&
49operator<<(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
Junxiao Shi7a36ac72018-03-21 15:23:22 +000076std::ostream&
77operator<<(std::ostream& os, Flag v)
78{
79 if (!v.flag) {
80 return os;
81 }
82 return os << '<' << v.elementName << "/>";
83}
84
Junxiao Shi38f4ce92016-08-04 10:01:52 +000085std::string
Eric Newberryde332452018-01-30 11:45:32 -070086formatDuration(time::nanoseconds d)
Junxiao Shi38f4ce92016-08-04 10:01:52 +000087{
Eric Newberryde332452018-01-30 11:45:32 -070088 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();
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000108}
109
110std::string
Davide Pesavento412c9822021-07-02 00:21:05 -0400111formatTimestamp(time::system_clock::time_point t)
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000112{
Davide Pesavento412c9822021-07-02 00:21:05 -0400113 return time::toIsoExtendedString(t);
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000114}
115
116} // namespace xml
117
118namespace text {
119
Junxiao Shi6c135622016-11-21 14:30:33 +0000120std::ostream&
121operator<<(std::ostream& os, const Spaces& spaces)
122{
123 for (int i = 0; i < spaces.nSpaces; ++i) {
124 os << ' ';
125 }
126 return os;
127}
128
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400129Separator::Separator(std::string_view first, std::string_view subsequent)
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000130 : m_first(first)
131 , m_subsequent(subsequent)
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000132{
133}
134
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400135Separator::Separator(std::string_view subsequent)
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000136 : Separator("", subsequent)
137{
138}
139
140std::ostream&
141operator<<(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
Junxiao Shi1f481fa2017-01-26 15:14:43 +0000149ItemAttributes::ItemAttributes(bool wantMultiLine, int maxAttributeWidth)
150 : m_wantMultiLine(wantMultiLine)
151 , m_maxAttributeWidth(maxAttributeWidth)
Junxiao Shi1f481fa2017-01-26 15:14:43 +0000152{
153}
154
155ItemAttributes::Attribute
156ItemAttributes::operator()(const std::string& attribute)
157{
Junxiao Shi056815e2017-01-29 16:39:19 +0000158 return {*this, attribute};
Junxiao Shi1f481fa2017-01-26 15:14:43 +0000159}
160
161std::string
162ItemAttributes::end() const
163{
164 return m_wantMultiLine ? "\n" : "";
165}
166
167std::ostream&
168operator<<(std::ostream& os, const ItemAttributes::Attribute& attr)
169{
Junxiao Shi056815e2017-01-29 16:39:19 +0000170 ++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())};
Junxiao Shi1f481fa2017-01-26 15:14:43 +0000176 }
Junxiao Shi056815e2017-01-29 16:39:19 +0000177 else {
178 if (attr.ia.m_count > 1) {
179 os << ' ';
180 }
181 }
182 return os << attr.attribute << '=';
Junxiao Shi1f481fa2017-01-26 15:14:43 +0000183}
184
Junxiao Shi8dc473a2018-03-02 15:10:18 +0000185std::ostream&
186operator<<(std::ostream& os, OnOff v)
187{
188 return os << (v.flag ? "on" : "off");
189}
190
Junxiao Shia4d7fe02018-07-20 06:51:41 -0600191std::ostream&
192operator<<(std::ostream& os, YesNo v)
193{
194 return os << (v.flag ? "yes" : "no");
195}
196
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000197std::string
Davide Pesavento412c9822021-07-02 00:21:05 -0400198formatTimestamp(time::system_clock::time_point t)
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000199{
200 return time::toIsoString(t);
201}
202
203} // namespace text
204
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400205} // namespace nfd::tools::nfdc