blob: c7e595b3cd3b2e87e11258aa9a5f15f7609623f1 [file] [log] [blame]
Junxiao Shi38f4ce92016-08-04 10:01:52 +00001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2014-2016, 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
28namespace nfd {
29namespace tools {
30namespace nfd_status {
31
32namespace xml {
33
34void
35printHeader(std::ostream& os)
36{
37 os << "<?xml version=\"1.0\"?>"
38 << "<nfdStatus xmlns=\"ndn:/localhost/nfd/status/1\">";
39}
40
41void
42printFooter(std::ostream& os)
43{
44 os << "</nfdStatus>";
45}
46
47std::ostream&
48operator<<(std::ostream& os, const Text& text)
49{
50 for (char ch : text.s) {
51 switch (ch) {
52 case '"':
53 os << "&quot;";
54 break;
55 case '&':
56 os << "&amp;";
57 break;
58 case '\'':
59 os << "&apos;";
60 break;
61 case '<':
62 os << "&lt;";
63 break;
64 case '>':
65 os << "&gt;";
66 break;
67 default:
68 os << ch;
69 break;
70 }
71 }
72 return os;
73}
74
75std::string
76formatSeconds(time::seconds d)
77{
78 return "PT" + to_string(d.count()) + "S";
79}
80
81std::string
82formatTimestamp(time::system_clock::TimePoint t)
83{
84 return time::toString(t, "%Y-%m-%dT%H:%M:%S%F");
85}
86
87} // namespace xml
88
89namespace text {
90
91Separator::Separator(const std::string& first, const std::string& subsequent)
92 : m_first(first)
93 , m_subsequent(subsequent)
94 , m_count(0)
95{
96}
97
98Separator::Separator(const std::string& subsequent)
99 : Separator("", subsequent)
100{
101}
102
103std::ostream&
104operator<<(std::ostream& os, Separator& sep)
105{
106 if (++sep.m_count == 1) {
107 return os << sep.m_first;
108 }
109 return os << sep.m_subsequent;
110}
111
112std::string
113formatSeconds(time::seconds d, bool isLong)
114{
115 return to_string(d.count()) + (isLong ? " seconds" : "s");
116}
117
118std::string
119formatTimestamp(time::system_clock::TimePoint t)
120{
121 return time::toIsoString(t);
122}
123
124} // namespace text
125
126} // namespace nfd_status
127} // namespace tools
128} // namespace nfd