src/format.h

changeset 119
bdf8d46c145f
child 124
a7b769a0e537
equal deleted inserted replaced
118:e3361cf7cbf4 119:bdf8d46c145f
1 /*
2 Copyright 2012-2014 Santeri Piippo
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions
7 are met:
8
9 1. Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
11 2. Redistributions in binary form must reproduce the above copyright
12 notice, this list of conditions and the following disclaimer in the
13 documentation and/or other materials provided with the distribution.
14 3. The name of the author may not be used to endorse or promote products
15 derived from this software without specific prior written permission.
16
17 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #ifndef BOTC_FORMAT_H
30 #define BOTC_FORMAT_H
31
32 #include "string.h"
33 #include "list.h"
34
35 class FormatArgument
36 {
37 public:
38 FormatArgument (const String& a) : m_text (a) {}
39 FormatArgument (char a) : m_text (a) {}
40 FormatArgument (int a) : m_text (String::fromNumber (a)) {}
41 FormatArgument (long a) : m_text (String::fromNumber (a)) {}
42 FormatArgument (const char* a) : m_text (a) {}
43
44 FormatArgument (void* a)
45 {
46 m_text.sprintf ("%p", a);
47 }
48
49 FormatArgument (const void* a)
50 {
51 m_text.sprintf ("%p", a);
52 }
53
54 template<class T> FormatArgument (const List<T>& list)
55 {
56 if (list.isEmpty())
57 {
58 m_text = "{}";
59 return;
60 }
61
62 m_text = "{ ";
63
64 for (const T& a : list)
65 {
66 if (&a != &list[0])
67 m_text += ", ";
68
69 m_text += FormatArgument (a).text();
70 }
71
72 m_text += " }";
73 }
74
75 inline const String& text() const
76 {
77 return m_text;
78 }
79
80 private:
81 String m_text;
82 };
83
84 #ifndef IN_IDE_PARSER
85 # ifdef DEBUG
86 # define devf(...) PrintTo (stderr, __VA_ARGS__)
87 # define dvalof( A ) PrintTo (stderr, "value of '%1' = %2\n", #A, A)
88 # else
89 # define devf(...)
90 # define dvalof( A )
91 # endif // DEBUG
92 #else
93 // print something in debug builds
94 void devf (void, ...);
95
96 // print the value of @a
97 void dvalof (void a);
98 #endif // IN_IDE_PARSER
99
100
101 /**
102 * Formats the given string with the given args.
103 *
104 * @param fmtstr Formatter string to process.
105 * @param args Args to format with the string.
106 * @see format()
107 */
108 String formatArgs (const String& fmtstr, const std::vector<String>& args);
109
110 /**
111 * Expands the given arguments into a vector of strings.
112 *
113 * @param data Where to insert the strings.
114 * @param arg First argument to process
115 * @param rest... Rest of the arguments.
116 */
117 template<typename T, typename... RestTypes>
118 void expandFormatArguments (std::vector<String>& data, const T& arg, const RestTypes& ... rest)
119 {
120 data.push_back (FormatArgument (arg).text());
121 expandFormatArguments (data, rest...);
122 }
123
124 /**
125 * This is an overload of @c ExpandFormatArguments for end-of-args support.
126 */
127 static void expandFormatArguments (std::vector<String>& data) __attribute__ ( (unused));
128 static void expandFormatArguments (std::vector<String>& data)
129 {
130 (void) data;
131 }
132
133 /**
134 * Formats the given formatter string and args and returns the string.
135 * This is essentially a modernized sprintf.
136 *
137 * Args in the format string take the form %n where n is a digit. The argument
138 * will be expanded to the nth argument passed. This is essentially Qt's
139 * QString::arg() syntax. Note: %0 is invalid.
140 *
141 * Arguments can be passed a modifier which takes the form of a character
142 * just before the digit. Currently supported modifiers are s, d and x.
143 *
144 * - s: The argument will expand into "s" if it would've not expanded into "1"
145 * otherwise. If it would have expanded into "1" it will expand into an
146 * empty string.
147 *
148 * - d: The argument expands into the numeric form of the first character of
149 * its previous expansion. Use this to get numeric forms of @c char
150 * arguments.
151 *
152 * - x: The numeric argument will be represented in hexadecimal notation. This
153 * will work if the argument is a string representing a number. If the
154 * argument did not expand into a number in the first place, 0 is used
155 * (and 0x0 is printed).
156 *
157 * @param fmtstr Formatter string to process
158 * @param raw_args Arguments for the formatter string.
159 * @return the formatted string.
160 * @see Print
161 * @see PrintTo
162 */
163 template<typename... argtypes>
164 String format (const String& fmtstr, const argtypes&... raw_args)
165 {
166 std::vector<String> args;
167 expandFormatArguments (args, raw_args...);
168 assert (args.size() == sizeof... (raw_args));
169 return formatArgs (fmtstr, args);
170 }
171
172 /**
173 * This is an overload of @c format where no arguments are supplied.
174 * @return the formatter string as-is.
175 */
176 static String format (const String& fmtstr) __attribute__ ( (unused));
177 static String format (const String& fmtstr)
178 {
179 return fmtstr;
180 }
181
182 /**
183 * Processes the given formatter string using @c format and prints it to the
184 * specified file pointer.
185 *
186 * @param fp File pointer to print the formatted string to
187 * @param fmtstr Formatter string for @c format
188 * @param args Arguments for @c fmtstr
189 */
190 template<typename... argtypes>
191 void printTo (FILE* fp, const String& fmtstr, const argtypes&... args)
192 {
193 fprintf (fp, "%s", format (fmtstr, args...).c_str());
194 }
195
196 /**
197 * Processes the given formatter string using @c format and prints the result to
198 * @c stdout.
199 *
200 * @param fmtstr Formatter string for @c format
201 * @param args Arguments for @c fmtstr
202 */
203 template<typename... argtypes>
204 void print (const String& fmtstr, const argtypes&... args)
205 {
206 printTo (stdout, fmtstr, args...);
207 }
208
209 /**
210 * Throws an std::runtime_error with the processed formatted string. The program
211 * execution terminates after a call to this function as the exception is first
212 * caught in @c main which prints the error to stderr and then exits.
213 *
214 * @param fmtstr The formatter string of the error.
215 * @param args The args to the formatter string.
216 * @see Format
217 */
218 template<typename... argtypes>
219 void error (const String& fmtstr, const argtypes&... args)
220 {
221 error (format (fmtstr, args...));
222 }
223
224 /**
225 * An overload of @c Error with no string formatting in between.
226 *
227 * @param msg The error message.
228 */
229 void error (String msg);
230
231 #endif // BOTC_FORMAT_H

mercurial