Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Eric Newberry | de33245 | 2018-01-30 11:45:32 -0700 | [diff] [blame] | 2 | /* |
Davide Pesavento | 412c982 | 2021-07-02 00:21:05 -0400 | [diff] [blame^] | 3 | * Copyright (c) 2014-2021, Regents of the University of California, |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 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 | |
Eric Newberry | de33245 | 2018-01-30 11:45:32 -0700 | [diff] [blame] | 28 | #include <iomanip> |
| 29 | #include <sstream> |
| 30 | |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 31 | namespace nfd { |
| 32 | namespace tools { |
Junxiao Shi | 331ade7 | 2016-08-19 14:07:19 +0000 | [diff] [blame] | 33 | namespace nfdc { |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 34 | |
| 35 | namespace xml { |
| 36 | |
| 37 | void |
| 38 | printHeader(std::ostream& os) |
| 39 | { |
| 40 | os << "<?xml version=\"1.0\"?>" |
| 41 | << "<nfdStatus xmlns=\"ndn:/localhost/nfd/status/1\">"; |
| 42 | } |
| 43 | |
| 44 | void |
| 45 | printFooter(std::ostream& os) |
| 46 | { |
| 47 | os << "</nfdStatus>"; |
| 48 | } |
| 49 | |
| 50 | std::ostream& |
| 51 | operator<<(std::ostream& os, const Text& text) |
| 52 | { |
| 53 | for (char ch : text.s) { |
| 54 | switch (ch) { |
| 55 | case '"': |
| 56 | os << """; |
| 57 | break; |
| 58 | case '&': |
| 59 | os << "&"; |
| 60 | break; |
| 61 | case '\'': |
| 62 | os << "'"; |
| 63 | break; |
| 64 | case '<': |
| 65 | os << "<"; |
| 66 | break; |
| 67 | case '>': |
| 68 | os << ">"; |
| 69 | break; |
| 70 | default: |
| 71 | os << ch; |
| 72 | break; |
| 73 | } |
| 74 | } |
| 75 | return os; |
| 76 | } |
| 77 | |
Junxiao Shi | 7a36ac7 | 2018-03-21 15:23:22 +0000 | [diff] [blame] | 78 | std::ostream& |
| 79 | operator<<(std::ostream& os, Flag v) |
| 80 | { |
| 81 | if (!v.flag) { |
| 82 | return os; |
| 83 | } |
| 84 | return os << '<' << v.elementName << "/>"; |
| 85 | } |
| 86 | |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 87 | std::string |
Eric Newberry | de33245 | 2018-01-30 11:45:32 -0700 | [diff] [blame] | 88 | formatDuration(time::nanoseconds d) |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 89 | { |
Eric Newberry | de33245 | 2018-01-30 11:45:32 -0700 | [diff] [blame] | 90 | 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 Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | std::string |
Davide Pesavento | 412c982 | 2021-07-02 00:21:05 -0400 | [diff] [blame^] | 113 | formatTimestamp(time::system_clock::time_point t) |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 114 | { |
Davide Pesavento | 412c982 | 2021-07-02 00:21:05 -0400 | [diff] [blame^] | 115 | return time::toIsoExtendedString(t); |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | } // namespace xml |
| 119 | |
| 120 | namespace text { |
| 121 | |
Junxiao Shi | 6c13562 | 2016-11-21 14:30:33 +0000 | [diff] [blame] | 122 | std::ostream& |
| 123 | operator<<(std::ostream& os, const Spaces& spaces) |
| 124 | { |
| 125 | for (int i = 0; i < spaces.nSpaces; ++i) { |
| 126 | os << ' '; |
| 127 | } |
| 128 | return os; |
| 129 | } |
| 130 | |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 131 | Separator::Separator(const std::string& first, const std::string& subsequent) |
| 132 | : m_first(first) |
| 133 | , m_subsequent(subsequent) |
| 134 | , m_count(0) |
| 135 | { |
| 136 | } |
| 137 | |
| 138 | Separator::Separator(const std::string& subsequent) |
| 139 | : Separator("", subsequent) |
| 140 | { |
| 141 | } |
| 142 | |
| 143 | std::ostream& |
| 144 | operator<<(std::ostream& os, Separator& sep) |
| 145 | { |
| 146 | if (++sep.m_count == 1) { |
| 147 | return os << sep.m_first; |
| 148 | } |
| 149 | return os << sep.m_subsequent; |
| 150 | } |
| 151 | |
Junxiao Shi | 1f481fa | 2017-01-26 15:14:43 +0000 | [diff] [blame] | 152 | ItemAttributes::ItemAttributes(bool wantMultiLine, int maxAttributeWidth) |
| 153 | : m_wantMultiLine(wantMultiLine) |
| 154 | , m_maxAttributeWidth(maxAttributeWidth) |
| 155 | , m_count(0) |
| 156 | { |
| 157 | } |
| 158 | |
| 159 | ItemAttributes::Attribute |
| 160 | ItemAttributes::operator()(const std::string& attribute) |
| 161 | { |
Junxiao Shi | 056815e | 2017-01-29 16:39:19 +0000 | [diff] [blame] | 162 | return {*this, attribute}; |
Junxiao Shi | 1f481fa | 2017-01-26 15:14:43 +0000 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | std::string |
| 166 | ItemAttributes::end() const |
| 167 | { |
| 168 | return m_wantMultiLine ? "\n" : ""; |
| 169 | } |
| 170 | |
| 171 | std::ostream& |
| 172 | operator<<(std::ostream& os, const ItemAttributes::Attribute& attr) |
| 173 | { |
Junxiao Shi | 056815e | 2017-01-29 16:39:19 +0000 | [diff] [blame] | 174 | ++attr.ia.m_count; |
| 175 | if (attr.ia.m_wantMultiLine) { |
| 176 | if (attr.ia.m_count > 1) { |
| 177 | os << '\n'; |
| 178 | } |
| 179 | os << Spaces{attr.ia.m_maxAttributeWidth - static_cast<int>(attr.attribute.size())}; |
Junxiao Shi | 1f481fa | 2017-01-26 15:14:43 +0000 | [diff] [blame] | 180 | } |
Junxiao Shi | 056815e | 2017-01-29 16:39:19 +0000 | [diff] [blame] | 181 | else { |
| 182 | if (attr.ia.m_count > 1) { |
| 183 | os << ' '; |
| 184 | } |
| 185 | } |
| 186 | return os << attr.attribute << '='; |
Junxiao Shi | 1f481fa | 2017-01-26 15:14:43 +0000 | [diff] [blame] | 187 | } |
| 188 | |
Junxiao Shi | 8dc473a | 2018-03-02 15:10:18 +0000 | [diff] [blame] | 189 | std::ostream& |
| 190 | operator<<(std::ostream& os, OnOff v) |
| 191 | { |
| 192 | return os << (v.flag ? "on" : "off"); |
| 193 | } |
| 194 | |
Junxiao Shi | a4d7fe0 | 2018-07-20 06:51:41 -0600 | [diff] [blame] | 195 | std::ostream& |
| 196 | operator<<(std::ostream& os, YesNo v) |
| 197 | { |
| 198 | return os << (v.flag ? "yes" : "no"); |
| 199 | } |
| 200 | |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 201 | std::string |
Davide Pesavento | 412c982 | 2021-07-02 00:21:05 -0400 | [diff] [blame^] | 202 | formatTimestamp(time::system_clock::time_point t) |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 203 | { |
| 204 | return time::toIsoString(t); |
| 205 | } |
| 206 | |
| 207 | } // namespace text |
| 208 | |
Junxiao Shi | 331ade7 | 2016-08-19 14:07:19 +0000 | [diff] [blame] | 209 | } // namespace nfdc |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 210 | } // namespace tools |
| 211 | } // namespace nfd |