blob: a329102d933d39a226dcb1c8942c9a2c8088a9f8 [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/*
3 * Copyright (c) 2014-2018, 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
78std::string
Eric Newberryde332452018-01-30 11:45:32 -070079formatDuration(time::nanoseconds d)
Junxiao Shi38f4ce92016-08-04 10:01:52 +000080{
Eric Newberryde332452018-01-30 11:45:32 -070081 std::ostringstream str;
82
83 if (d < 0_ns) {
84 str << "-";
85 }
86
87 str << "PT";
88
89 time::seconds seconds(time::duration_cast<time::seconds>(time::abs(d)));
90 time::milliseconds ms(time::duration_cast<time::milliseconds>(time::abs(d) - seconds));
91
92 str << seconds.count();
93
94 if (ms >= 1_ms) {
95 str << "." << std::setfill('0') << std::setw(3) << ms.count();
96 }
97
98 str << "S";
99
100 return str.str();
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000101}
102
103std::string
104formatTimestamp(time::system_clock::TimePoint t)
105{
106 return time::toString(t, "%Y-%m-%dT%H:%M:%S%F");
107}
108
109} // namespace xml
110
111namespace text {
112
Junxiao Shi6c135622016-11-21 14:30:33 +0000113std::ostream&
114operator<<(std::ostream& os, const Spaces& spaces)
115{
116 for (int i = 0; i < spaces.nSpaces; ++i) {
117 os << ' ';
118 }
119 return os;
120}
121
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000122Separator::Separator(const std::string& first, const std::string& subsequent)
123 : m_first(first)
124 , m_subsequent(subsequent)
125 , m_count(0)
126{
127}
128
129Separator::Separator(const std::string& subsequent)
130 : Separator("", subsequent)
131{
132}
133
134std::ostream&
135operator<<(std::ostream& os, Separator& sep)
136{
137 if (++sep.m_count == 1) {
138 return os << sep.m_first;
139 }
140 return os << sep.m_subsequent;
141}
142
Junxiao Shi1f481fa2017-01-26 15:14:43 +0000143ItemAttributes::ItemAttributes(bool wantMultiLine, int maxAttributeWidth)
144 : m_wantMultiLine(wantMultiLine)
145 , m_maxAttributeWidth(maxAttributeWidth)
146 , m_count(0)
147{
148}
149
150ItemAttributes::Attribute
151ItemAttributes::operator()(const std::string& attribute)
152{
Junxiao Shi056815e2017-01-29 16:39:19 +0000153 return {*this, attribute};
Junxiao Shi1f481fa2017-01-26 15:14:43 +0000154}
155
156std::string
157ItemAttributes::end() const
158{
159 return m_wantMultiLine ? "\n" : "";
160}
161
162std::ostream&
163operator<<(std::ostream& os, const ItemAttributes::Attribute& attr)
164{
Junxiao Shi056815e2017-01-29 16:39:19 +0000165 ++attr.ia.m_count;
166 if (attr.ia.m_wantMultiLine) {
167 if (attr.ia.m_count > 1) {
168 os << '\n';
169 }
170 os << Spaces{attr.ia.m_maxAttributeWidth - static_cast<int>(attr.attribute.size())};
Junxiao Shi1f481fa2017-01-26 15:14:43 +0000171 }
Junxiao Shi056815e2017-01-29 16:39:19 +0000172 else {
173 if (attr.ia.m_count > 1) {
174 os << ' ';
175 }
176 }
177 return os << attr.attribute << '=';
Junxiao Shi1f481fa2017-01-26 15:14:43 +0000178}
179
Junxiao Shi8dc473a2018-03-02 15:10:18 +0000180std::ostream&
181operator<<(std::ostream& os, OnOff v)
182{
183 return os << (v.flag ? "on" : "off");
184}
185
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000186std::string
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000187formatTimestamp(time::system_clock::TimePoint t)
188{
189 return time::toIsoString(t);
190}
191
192} // namespace text
193
Junxiao Shi331ade72016-08-19 14:07:19 +0000194} // namespace nfdc
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000195} // namespace tools
196} // namespace nfd