format-helpers.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
26 #include "format-helpers.hpp"
27 
28 namespace nfd {
29 namespace tools {
30 namespace nfdc {
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 << "&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 
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 
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 
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 
121 ItemAttributes::ItemAttributes(bool wantMultiLine, int maxAttributeWidth)
122  : m_wantMultiLine(wantMultiLine)
123  , m_maxAttributeWidth(maxAttributeWidth)
124  , m_count(0)
125 {
126 }
127 
129 ItemAttributes::operator()(const std::string& attribute)
130 {
131  return {*this, attribute};
132 }
133 
134 std::string
136 {
137  return m_wantMultiLine ? "\n" : "";
138 }
139 
140 std::ostream&
141 operator<<(std::ostream& os, const ItemAttributes::Attribute& attr)
142 {
143  ++attr.ia.m_count;
144  if (attr.ia.m_wantMultiLine) {
145  if (attr.ia.m_count > 1) {
146  os << '\n';
147  }
148  os << Spaces{attr.ia.m_maxAttributeWidth - static_cast<int>(attr.attribute.size())};
149  }
150  else {
151  if (attr.ia.m_count > 1) {
152  os << ' ';
153  }
154  }
155  return os << attr.attribute << '=';
156 }
157 
158 std::string
159 formatSeconds(time::seconds d, bool isLong)
160 {
161  return to_string(d.count()) + (isLong ? " seconds" : "s");
162 }
163 
164 std::string
165 formatTimestamp(time::system_clock::TimePoint t)
166 {
167  return time::toIsoString(t);
168 }
169 
170 } // namespace text
171 
172 } // namespace nfdc
173 } // namespace tools
174 } // namespace nfd
std::string formatSeconds(time::seconds d, bool isLong)
ItemAttributes(bool wantMultiLine=false, int maxAttributeWidth=0)
constructor
std::string formatTimestamp(time::system_clock::TimePoint t)
friend std::ostream & operator<<(std::ostream &os, Separator &sep)
void printHeader(std::ostream &os)
Copyright (c) 2014-2015, Regents of the University of California, Arizona Board of Regents...
Definition: algorithm.hpp:32
std::string formatTimestamp(time::system_clock::TimePoint t)
Attribute operator()(const std::string &attribute)
std::ostream & operator<<(std::ostream &os, const Text &text)
print XML text with special character represented as predefined entities
Separator(const std::string &first, const std::string &subsequent)
friend std::ostream & operator<<(std::ostream &os, const ItemAttributes::Attribute &attr)
int nSpaces
number of spaces; print nothing if negative
std::string formatSeconds(time::seconds d)
print different string on first and subsequent usage
print a number of whitespaces
void printFooter(std::ostream &os)