1 // rTorrent - BitTorrent client
2 // Copyright (C) 2005-2011, Jari Sundell
3 //
4 // This program is free software; you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation; either version 2 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 //
18 // In addition, as a special exception, the copyright holders give
19 // permission to link the code of portions of this program with the
20 // OpenSSL library under certain conditions as described in each
21 // individual source file, and distribute linked combinations
22 // including the two.
23 //
24 // You must obey the GNU General Public License in all respects for
25 // all of the code used other than OpenSSL. If you modify file(s)
26 // with this exception, you may extend this exception to your version
27 // of the file(s), but you are not obligated to do so. If you do not
28 // wish to do so, delete this exception statement from your version.
29 // If you delete this exception statement from all source files in the
30 // program, then also delete it here.
31 //
32 // Contact: Jari Sundell <jaris@ifi.uio.no>
33 //
34 // Skomakerveien 33
35 // 3185 Skoppum, NORWAY
36
37 #ifndef RTORRENT_DISPLAY_UTILS_H
38 #define RTORRENT_DISPLAY_UTILS_H
39
40 #include <ctime>
41 #include <cstdio>
42 #include <string>
43
44 namespace core {
45 class Download;
46 }
47
48 namespace utils {
49 class Timer;
50 }
51
52 namespace torrent {
53 class ClientInfo;
54 class Entry;
55 }
56
57 class Control;
58
59 namespace display {
60
61 char* print_string(char* first, char* last, char* str);
62
63 char* print_hhmmss(char* first, char* last, time_t t);
64 char* print_hhmmss_local(char* first, char* last, time_t t);
65 char* print_ddhhmm(char* first, char* last, time_t t);
66 char* print_ddmmyyyy(char* first, char* last, time_t t);
67
68 char* print_download_title(char* first, char* last, core::Download* d);
69 char* print_download_info_full(char* first, char* last, core::Download* d);
70 char* print_download_status(char* first, char* last, core::Download* d);
71
72 char* print_download_column_compact(char* first, char* last);
73 char* print_download_info_compact(char* first, char* last, core::Download* d);
74
75 char* print_download_time_left(char* first, char* last, core::Download* d);
76 char* print_download_percentage_done(char* first, char* last, core::Download* d);
77
78 char* print_client_version(char* first, char* last, const torrent::ClientInfo& clientInfo);
79
80 char* print_entry_tags(char* first, char* last);
81 char* print_entry_file(char* first, char* last, const torrent::Entry& entry);
82
83 char* print_status_info(char* first, char* last);
84 char* print_status_extra(char* first, char* last);
85
86 inline char*
print_buffer(char * first,char * last,const char * format)87 print_buffer(char* first, char* last, const char* format) {
88 if (first >= last)
89 return first;
90
91 // Adding 'i' format to suppress a GCC warning.
92 int s = snprintf(first, last - first, format, 0);
93
94 if (s < 0)
95 return first;
96 else
97 return std::min(first + s, last);
98 }
99
100 template <typename Arg1>
101 inline char*
print_buffer(char * first,char * last,const char * format,const Arg1 & arg1)102 print_buffer(char* first, char* last, const char* format, const Arg1& arg1) {
103 if (first >= last)
104 return first;
105
106 int s = snprintf(first, last - first, format, arg1);
107
108 if (s < 0)
109 return first;
110 else
111 return std::min(first + s, last);
112 }
113
114 template <typename Arg1, typename Arg2>
115 inline char*
print_buffer(char * first,char * last,const char * format,const Arg1 & arg1,const Arg2 & arg2)116 print_buffer(char* first, char* last, const char* format, const Arg1& arg1, const Arg2& arg2) {
117 if (first >= last)
118 return first;
119
120 int s = snprintf(first, last - first, format, arg1, arg2);
121
122 if (s < 0)
123 return first;
124 else
125 return std::min(first + s, last);
126 }
127
128 template <typename Arg1, typename Arg2, typename Arg3>
129 inline char*
print_buffer(char * first,char * last,const char * format,const Arg1 & arg1,const Arg2 & arg2,const Arg3 & arg3)130 print_buffer(char* first, char* last, const char* format, const Arg1& arg1, const Arg2& arg2, const Arg3& arg3) {
131 if (first >= last)
132 return first;
133
134 int s = snprintf(first, last - first, format, arg1, arg2, arg3);
135
136 if (s < 0)
137 return first;
138 else
139 return std::min(first + s, last);
140 }
141
142 template <typename Arg1, typename Arg2, typename Arg3, typename Arg4>
143 inline char*
print_buffer(char * first,char * last,const char * format,const Arg1 & arg1,const Arg2 & arg2,const Arg3 & arg3,const Arg4 & arg4)144 print_buffer(char* first, char* last, const char* format, const Arg1& arg1, const Arg2& arg2, const Arg3& arg3, const Arg4& arg4) {
145 if (first >= last)
146 return first;
147
148 int s = snprintf(first, last - first, format, arg1, arg2, arg3, arg4);
149
150 if (s < 0)
151 return first;
152 else
153 return std::min(first + s, last);
154 }
155
156 template <typename Arg1, typename Arg2, typename Arg3, typename Arg4, typename Arg5>
157 inline char*
print_buffer(char * first,char * last,const char * format,const Arg1 & arg1,const Arg2 & arg2,const Arg3 & arg3,const Arg4 & arg4,const Arg5 & arg5)158 print_buffer(char* first, char* last, const char* format, const Arg1& arg1, const Arg2& arg2, const Arg3& arg3, const Arg4& arg4, const Arg5& arg5) {
159 if (first >= last)
160 return first;
161
162 int s = snprintf(first, last - first, format, arg1, arg2, arg3, arg4, arg5);
163
164 if (s < 0)
165 return first;
166 else
167 return std::min(first + s, last);
168 }
169
170 template <typename Arg1, typename Arg2, typename Arg3, typename Arg4, typename Arg5, typename Arg6>
171 inline char*
print_buffer(char * first,char * last,const char * format,const Arg1 & arg1,const Arg2 & arg2,const Arg3 & arg3,const Arg4 & arg4,const Arg5 & arg5,const Arg6 & arg6)172 print_buffer(char* first, char* last, const char* format, const Arg1& arg1, const Arg2& arg2, const Arg3& arg3, const Arg4& arg4, const Arg5& arg5, const Arg6& arg6) {
173 if (first >= last)
174 return first;
175
176 int s = snprintf(first, last - first, format, arg1, arg2, arg3, arg4, arg5, arg6);
177
178 if (s < 0)
179 return first;
180 else
181 return std::min(first + s, last);
182 }
183
184 }
185
186 #endif
187