blob: 4c7badd002e7b111a1abb62e10c7dc14a73f3c7c [file] [log] [blame]
Junxiao Shi38f4ce92016-08-04 10:01:52 +00001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2014-2016, 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
Junxiao Shi331ade72016-08-19 14:07:19 +000026#ifndef NFD_TOOLS_NFDC_FORMAT_HELPERS_HPP
27#define NFD_TOOLS_NFDC_FORMAT_HELPERS_HPP
Junxiao Shi38f4ce92016-08-04 10:01:52 +000028
Junxiao Shi9f5b01d2016-08-05 03:54:28 +000029#include "core/common.hpp"
Junxiao Shi38f4ce92016-08-04 10:01:52 +000030
31namespace nfd {
32namespace tools {
Junxiao Shi331ade72016-08-19 14:07:19 +000033namespace nfdc {
Junxiao Shi38f4ce92016-08-04 10:01:52 +000034
35namespace xml {
36
37void
38printHeader(std::ostream& os);
39
40void
41printFooter(std::ostream& os);
42
43struct Text
44{
45 const std::string& s;
46};
47
48/** \brief print XML text with special character represented as predefined entities
49 */
50std::ostream&
51operator<<(std::ostream& os, const Text& text);
52
53std::string
54formatSeconds(time::seconds d);
55
56template<typename DURATION>
57std::string
58formatDuration(DURATION d)
59{
60 return formatSeconds(time::duration_cast<time::seconds>(d));
61}
62
63std::string
64formatTimestamp(time::system_clock::TimePoint t);
65
66} // namespace xml
67
68namespace text {
69
Junxiao Shi6c135622016-11-21 14:30:33 +000070/** \brief print a number of whitespaces
71 */
72struct Spaces
73{
74 int nSpaces; ///< number of spaces; print nothing if negative
75};
76
77std::ostream&
78operator<<(std::ostream& os, const Spaces& spaces);
79
Junxiao Shi38f4ce92016-08-04 10:01:52 +000080/** \brief print different string on first and subsequent usage
81 *
82 * \code
83 * Separator sep(",");
84 * for (int i = 1; i <= 3; ++i) {
85 * os << sep << i;
86 * }
87 * // prints: 1,2,3
88 * \endcode
89 */
90class Separator
91{
92public:
93 Separator(const std::string& first, const std::string& subsequent);
94
95 explicit
96 Separator(const std::string& subsequent);
97
Junxiao Shic143afe2016-09-20 13:04:51 +000098 int
99 getCount() const
100 {
101 return m_count;
102 }
103
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000104private:
105 std::string m_first;
106 std::string m_subsequent;
107 int m_count;
108
109 friend std::ostream& operator<<(std::ostream& os, Separator& sep);
110};
111
112std::ostream&
113operator<<(std::ostream& os, Separator& sep);
114
115std::string
116formatSeconds(time::seconds d, bool isLong = false);
117
118template<typename DURATION>
119std::string
120formatDuration(DURATION d, bool isLong = false)
121{
122 return formatSeconds(time::duration_cast<time::seconds>(d), isLong);
123}
124
125std::string
126formatTimestamp(time::system_clock::TimePoint t);
127
128} // namespace text
129
Junxiao Shi331ade72016-08-19 14:07:19 +0000130} // namespace nfdc
Junxiao Shi38f4ce92016-08-04 10:01:52 +0000131} // namespace tools
132} // namespace nfd
133
Junxiao Shi331ade72016-08-19 14:07:19 +0000134#endif // NFD_TOOLS_NFDC_FORMAT_HELPERS_HPP