Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Junxiao Shi | 1f481fa | 2017-01-26 15:14:43 +0000 | [diff] [blame] | 3 | * Copyright (c) 2014-2017, 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 | |
| 28 | namespace nfd { |
| 29 | namespace tools { |
Junxiao Shi | 331ade7 | 2016-08-19 14:07:19 +0000 | [diff] [blame] | 30 | namespace nfdc { |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 31 | |
| 32 | namespace xml { |
| 33 | |
| 34 | void |
| 35 | printHeader(std::ostream& os) |
| 36 | { |
| 37 | os << "<?xml version=\"1.0\"?>" |
| 38 | << "<nfdStatus xmlns=\"ndn:/localhost/nfd/status/1\">"; |
| 39 | } |
| 40 | |
| 41 | void |
| 42 | printFooter(std::ostream& os) |
| 43 | { |
| 44 | os << "</nfdStatus>"; |
| 45 | } |
| 46 | |
| 47 | std::ostream& |
| 48 | operator<<(std::ostream& os, const Text& text) |
| 49 | { |
| 50 | for (char ch : text.s) { |
| 51 | switch (ch) { |
| 52 | case '"': |
| 53 | os << """; |
| 54 | break; |
| 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 | default: |
| 68 | os << ch; |
| 69 | break; |
| 70 | } |
| 71 | } |
| 72 | return os; |
| 73 | } |
| 74 | |
| 75 | std::string |
| 76 | formatSeconds(time::seconds d) |
| 77 | { |
| 78 | return "PT" + to_string(d.count()) + "S"; |
| 79 | } |
| 80 | |
| 81 | std::string |
| 82 | formatTimestamp(time::system_clock::TimePoint t) |
| 83 | { |
| 84 | return time::toString(t, "%Y-%m-%dT%H:%M:%S%F"); |
| 85 | } |
| 86 | |
| 87 | } // namespace xml |
| 88 | |
| 89 | namespace text { |
| 90 | |
Junxiao Shi | 6c13562 | 2016-11-21 14:30:33 +0000 | [diff] [blame] | 91 | std::ostream& |
| 92 | operator<<(std::ostream& os, const Spaces& spaces) |
| 93 | { |
| 94 | for (int i = 0; i < spaces.nSpaces; ++i) { |
| 95 | os << ' '; |
| 96 | } |
| 97 | return os; |
| 98 | } |
| 99 | |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 100 | Separator::Separator(const std::string& first, const std::string& subsequent) |
| 101 | : m_first(first) |
| 102 | , m_subsequent(subsequent) |
| 103 | , m_count(0) |
| 104 | { |
| 105 | } |
| 106 | |
| 107 | Separator::Separator(const std::string& subsequent) |
| 108 | : Separator("", subsequent) |
| 109 | { |
| 110 | } |
| 111 | |
| 112 | std::ostream& |
| 113 | operator<<(std::ostream& os, Separator& sep) |
| 114 | { |
| 115 | if (++sep.m_count == 1) { |
| 116 | return os << sep.m_first; |
| 117 | } |
| 118 | return os << sep.m_subsequent; |
| 119 | } |
| 120 | |
Junxiao Shi | 1f481fa | 2017-01-26 15:14:43 +0000 | [diff] [blame] | 121 | ItemAttributes::ItemAttributes(bool wantMultiLine, int maxAttributeWidth) |
| 122 | : m_wantMultiLine(wantMultiLine) |
| 123 | , m_maxAttributeWidth(maxAttributeWidth) |
| 124 | , m_count(0) |
| 125 | { |
| 126 | } |
| 127 | |
| 128 | ItemAttributes::Attribute |
| 129 | ItemAttributes::operator()(const std::string& attribute) |
| 130 | { |
| 131 | ++m_count; |
| 132 | if (m_wantMultiLine) { |
| 133 | return {m_count > 1, |
| 134 | {m_maxAttributeWidth - static_cast<int>(attribute.size())}, |
| 135 | attribute}; |
| 136 | } |
| 137 | else { |
| 138 | return {false, |
| 139 | {m_count > 1 ? 1 : 0}, |
| 140 | attribute}; |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | std::string |
| 145 | ItemAttributes::end() const |
| 146 | { |
| 147 | return m_wantMultiLine ? "\n" : ""; |
| 148 | } |
| 149 | |
| 150 | std::ostream& |
| 151 | operator<<(std::ostream& os, const ItemAttributes::Attribute& attr) |
| 152 | { |
| 153 | if (attr.wantNewline) { |
| 154 | os << '\n'; |
| 155 | } |
| 156 | return os << attr.spaces << attr.attribute << '='; |
| 157 | } |
| 158 | |
| 159 | |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 160 | std::string |
| 161 | formatSeconds(time::seconds d, bool isLong) |
| 162 | { |
| 163 | return to_string(d.count()) + (isLong ? " seconds" : "s"); |
| 164 | } |
| 165 | |
| 166 | std::string |
| 167 | formatTimestamp(time::system_clock::TimePoint t) |
| 168 | { |
| 169 | return time::toIsoString(t); |
| 170 | } |
| 171 | |
| 172 | } // namespace text |
| 173 | |
Junxiao Shi | 331ade7 | 2016-08-19 14:07:19 +0000 | [diff] [blame] | 174 | } // namespace nfdc |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 175 | } // namespace tools |
| 176 | } // namespace nfd |