Loading...
Searching...
No Matches
format-helpers.hpp
Go to the documentation of this file.
1/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
3 * Copyright (c) 2014-2024, 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#ifndef NFD_TOOLS_NFDC_FORMAT_HELPERS_HPP
27#define NFD_TOOLS_NFDC_FORMAT_HELPERS_HPP
28
29#include "core/common.hpp"
30
31namespace nfd::tools::nfdc {
32
33namespace xml {
34
35void
36printHeader(std::ostream& os);
37
38void
39printFooter(std::ostream& os);
40
41struct Text
42{
43 const std::string& s;
44};
45
48std::ostream&
49operator<<(std::ostream& os, const Text& text);
50
53struct Flag
54{
55 const char* elementName;
56 bool flag;
57};
58
59std::ostream&
60operator<<(std::ostream& os, Flag v);
61
66std::string
67formatDuration(time::nanoseconds d);
68
73std::string
74formatTimestamp(time::system_clock::time_point t);
75
76} // namespace xml
77
78namespace text {
79
82struct Spaces
83{
84 int nSpaces;
85};
86
87std::ostream&
88operator<<(std::ostream& os, const Spaces& spaces);
89
100class Separator : noncopyable
101{
102public:
103 Separator(std::string_view first, std::string_view subsequent);
104
105 explicit
106 Separator(std::string_view subsequent);
107
108 int
109 getCount() const
110 {
111 return m_count;
112 }
113
114private:
115 const std::string m_first;
116 const std::string m_subsequent;
117 int m_count = 0;
118
119 friend std::ostream& operator<<(std::ostream& os, Separator& sep);
120};
121
122std::ostream&
123operator<<(std::ostream& os, Separator& sep);
124
141class ItemAttributes : noncopyable
142{
143public:
148 explicit
149 ItemAttributes(bool wantMultiLine = false, int maxAttributeWidth = 0);
150
152 {
154 std::string attribute;
155 };
156
161 operator()(const std::string& attribute);
162
163 std::string
164 end() const;
165
166private:
167 const bool m_wantMultiLine;
168 const int m_maxAttributeWidth;
169 int m_count = 0;
170
171 friend std::ostream& operator<<(std::ostream& os, const ItemAttributes::Attribute& attr);
172};
173
174std::ostream&
175operator<<(std::ostream& os, const ItemAttributes::Attribute& attr);
176
179struct OnOff
180{
181 bool flag;
182};
183
184std::ostream&
185operator<<(std::ostream& os, OnOff v);
186
189struct YesNo
190{
191 bool flag;
192};
193
194std::ostream&
195operator<<(std::ostream& os, YesNo v);
196
197namespace detail {
198
199template<typename DurationT>
200std::string
201getTimeUnit(bool isLong);
202
203template<>
204inline std::string
206{
207 return isLong ? "nanoseconds" : "ns";
208}
209
210template<>
211inline std::string
213{
214 return isLong ? "microseconds" : "us";
215}
216
217template<>
218inline std::string
220{
221 return isLong ? "milliseconds" : "ms";
222}
223
224template<>
225inline std::string
227{
228 return isLong ? "seconds" : "s";
229}
230
231template<>
232inline std::string
234{
235 return isLong ? "minutes" : "m";
236}
237
238template<>
239inline std::string
240getTimeUnit<time::hours>(bool isLong)
241{
242 return isLong ? "hours" : "h";
243}
244
245template<>
246inline std::string
247getTimeUnit<time::days>(bool isLong)
248{
249 return isLong ? "days" : "d";
250}
251
252} // namespace detail
253
254template<typename OutputPrecision>
255std::string
256formatDuration(time::nanoseconds d, bool isLong = false)
257{
258 return std::to_string(time::duration_cast<OutputPrecision>(d).count()) +
259 (isLong ? " " : "") + detail::getTimeUnit<OutputPrecision>(isLong);
260}
261
262std::string
263formatTimestamp(time::system_clock::time_point t);
264
265} // namespace text
266
267} // namespace nfd::tools::nfdc
268
269#endif // NFD_TOOLS_NFDC_FORMAT_HELPERS_HPP
Attribute operator()(const std::string &attribute)
friend std::ostream & operator<<(std::ostream &os, const ItemAttributes::Attribute &attr)
Print different string on first and subsequent usage.
friend std::ostream & operator<<(std::ostream &os, Separator &sep)
std::string getTimeUnit(bool isLong)
std::string getTimeUnit< time::microseconds >(bool isLong)
std::string getTimeUnit< time::days >(bool isLong)
std::string getTimeUnit< time::seconds >(bool isLong)
std::string getTimeUnit< time::milliseconds >(bool isLong)
std::string getTimeUnit< time::nanoseconds >(bool isLong)
std::string getTimeUnit< time::hours >(bool isLong)
std::string getTimeUnit< time::minutes >(bool isLong)
std::string formatTimestamp(time::system_clock::time_point t)
std::ostream & operator<<(std::ostream &os, const Spaces &spaces)
std::string formatDuration(time::nanoseconds d, bool isLong=false)
std::string formatTimestamp(time::system_clock::time_point t)
std::string formatDuration(time::nanoseconds d)
void printFooter(std::ostream &os)
void printHeader(std::ostream &os)
std::ostream & operator<<(std::ostream &os, const Text &text)
Print XML text with special character represented as predefined entities.
Print boolean as 'on' or 'off'.
Print a number of whitespaces.
int nSpaces
number of spaces; print nothing if negative
Print boolean as 'yes' or 'no'.
Print true as an empty element and false as nothing.