blob: 63f241766ff194e9a9f3cb62c38e050b448c3d6d [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
Junxiao Shi38f4ce92016-08-04 10:01:52 +000031namespace nfd {
32namespace tools {
Junxiao Shi331ade72016-08-19 14:07:19 +000033namespace nfdc {
Junxiao Shi38f4ce92016-08-04 10:01:52 +000034
35namespace xml {
36
37void
38printHeader(std::ostream& os)
39{
40 os << "<?xml version=\"1.0\"?>"
41 << "<nfdStatus xmlns=\"ndn:/localhost/nfd/status/1\">";
42}
43
44void
45printFooter(std::ostream& os)
46{
47 os << "</nfdStatus>";
48}
49
50std::ostream&
51operator<<(std::ostream& os, const Text& text)
52{
53 for (char ch : text.s) {
54 switch (ch) {
55 case '"':
56 os << "&quot;";
57 break;
58 case '&':
59 os << "&amp;";
60 break;
61 case '\'':
62 os << "&apos;";
63 break;
64 case '<':
65 os << "&lt;";
66 break;
67 case '>':
68 os << "&gt;";
69 break;
70 default:
71 os << ch;
72 break;
73 }
74 }
75 return os;
76}
77
Junxiao Shi7a36ac72018-03-21 15:23:22 +000078std::ostream&
79operator<<(std::ostream& os, Flag v)
80{
81 if (!v.flag) {
82 return os;
83 }
84 return os << '<' << v.elementName << "/>";
85}
86
Junxiao Shi38f4ce92016-08-04 10:01:52 +000087std::string
Eric Newberryde332452018-01-30 11:45:32 -070088formatDuration(time::nanoseconds d)
Junxiao Shi38f4ce92016-08-04 10:01:52 +000089{
Eric Newberryde332452018-01-30 11:45:32 -070090 std::ostringstream str;
91
92 if (d < 0_ns) {
93 str << "-";
94 }
95
96 str << "PT";
97
98 time::seconds seconds(time::duration_cast<time::seconds>(time::abs(d)));
99 time::milliseconds ms(time::duration_cast<time::milliseconds>(time::abs(d) - seconds));
100
101 str << seconds.count();
102
103 if (ms >= 1_ms) {
104 str << "." << std::setfill('0') << std::setw(3) << ms.count();
105 }
106
107 str << "S";
108
109 return str.str();
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000110}
111
112std::string
Davide Pesavento412c9822021-07-02 00:21:05 -0400113formatTimestamp(time::system_clock::time_point t)
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000114{
Davide Pesavento412c9822021-07-02 00:21:05 -0400115 return time::toIsoExtendedString(t);
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000116}
117
118} // namespace xml
119
120namespace text {
121
Junxiao Shi6c135622016-11-21 14:30:33 +0000122std::ostream&
123operator<<(std::ostream& os, const Spaces& spaces)
124{
125 for (int i = 0; i < spaces.nSpaces; ++i) {
126 os << ' ';
127 }
128 return os;
129}
130
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400131Separator::Separator(std::string_view first, std::string_view subsequent)
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000132 : m_first(first)
133 , m_subsequent(subsequent)
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000134{
135}
136
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400137Separator::Separator(std::string_view subsequent)
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000138 : Separator("", subsequent)
139{
140}
141
142std::ostream&
143operator<<(std::ostream& os, Separator& sep)
144{
145 if (++sep.m_count == 1) {
146 return os << sep.m_first;
147 }
148 return os << sep.m_subsequent;
149}
150
Junxiao Shi1f481fa2017-01-26 15:14:43 +0000151ItemAttributes::ItemAttributes(bool wantMultiLine, int maxAttributeWidth)
152 : m_wantMultiLine(wantMultiLine)
153 , m_maxAttributeWidth(maxAttributeWidth)
Junxiao Shi1f481fa2017-01-26 15:14:43 +0000154{
155}
156
157ItemAttributes::Attribute
158ItemAttributes::operator()(const std::string& attribute)
159{
Junxiao Shi056815e2017-01-29 16:39:19 +0000160 return {*this, attribute};
Junxiao Shi1f481fa2017-01-26 15:14:43 +0000161}
162
163std::string
164ItemAttributes::end() const
165{
166 return m_wantMultiLine ? "\n" : "";
167}
168
169std::ostream&
170operator<<(std::ostream& os, const ItemAttributes::Attribute& attr)
171{
Junxiao Shi056815e2017-01-29 16:39:19 +0000172 ++attr.ia.m_count;
173 if (attr.ia.m_wantMultiLine) {
174 if (attr.ia.m_count > 1) {
175 os << '\n';
176 }
177 os << Spaces{attr.ia.m_maxAttributeWidth - static_cast<int>(attr.attribute.size())};
Junxiao Shi1f481fa2017-01-26 15:14:43 +0000178 }
Junxiao Shi056815e2017-01-29 16:39:19 +0000179 else {
180 if (attr.ia.m_count > 1) {
181 os << ' ';
182 }
183 }
184 return os << attr.attribute << '=';
Junxiao Shi1f481fa2017-01-26 15:14:43 +0000185}
186
Junxiao Shi8dc473a2018-03-02 15:10:18 +0000187std::ostream&
188operator<<(std::ostream& os, OnOff v)
189{
190 return os << (v.flag ? "on" : "off");
191}
192
Junxiao Shia4d7fe02018-07-20 06:51:41 -0600193std::ostream&
194operator<<(std::ostream& os, YesNo v)
195{
196 return os << (v.flag ? "yes" : "no");
197}
198
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000199std::string
Davide Pesavento412c9822021-07-02 00:21:05 -0400200formatTimestamp(time::system_clock::time_point t)
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000201{
202 return time::toIsoString(t);
203}
204
205} // namespace text
206
Junxiao Shi331ade72016-08-19 14:07:19 +0000207} // namespace nfdc
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000208} // namespace tools
209} // namespace nfd