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