1 /* |
|
2 Copyright 2014, 2015 Teemu 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. Neither the name of the copyright holder nor the names of its |
|
15 contributors may be used to endorse or promote products derived from |
|
16 this software without specific prior written permission. |
|
17 |
|
18 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
19 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
|
20 TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A |
|
21 PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER |
|
22 OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
|
23 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
|
24 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
|
25 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
|
26 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
|
27 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
|
28 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
29 */ |
|
30 |
|
31 #pragma once |
|
32 #include "mystring.h" |
|
33 #include "basics.h" |
|
34 #include "geometry.h" |
|
35 |
|
36 #define FORMAT_OVERLOAD(...) \ |
|
37 inline String make_format_argument (__VA_ARGS__ a) |
|
38 |
|
39 // ------------------------------------------------------------------------------------------------- |
|
40 // |
|
41 FORMAT_OVERLOAD (String) { return a; } |
|
42 FORMAT_OVERLOAD (char) { return String (a); } |
|
43 FORMAT_OVERLOAD (short int) { return String::from_number (a); } |
|
44 FORMAT_OVERLOAD (int) { return String::from_number (a); } |
|
45 FORMAT_OVERLOAD (long int) { return String::from_number (a); } |
|
46 FORMAT_OVERLOAD (double) { return String::from_number (a); } |
|
47 FORMAT_OVERLOAD (unsigned short int) { return String::from_number (a); } |
|
48 FORMAT_OVERLOAD (unsigned int) { return String::from_number (a); } |
|
49 FORMAT_OVERLOAD (unsigned long int) { return String::from_number (a); } |
|
50 FORMAT_OVERLOAD (const char*) { return a; } |
|
51 FORMAT_OVERLOAD (std::nullptr_t) { (void) a; return "<null pointer>"; } |
|
52 FORMAT_OVERLOAD (bool) { return a ? "true" : "false"; } |
|
53 |
|
54 FORMAT_OVERLOAD (const void*) |
|
55 { |
|
56 String result; |
|
57 result.sprintf ("%p", a); |
|
58 return result; |
|
59 } |
|
60 |
|
61 template<typename T, typename C> |
|
62 FORMAT_OVERLOAD (const Container<T, C>&) |
|
63 { |
|
64 String result; |
|
65 |
|
66 if (a.is_empty()) |
|
67 return "{}"; |
|
68 |
|
69 result = "{"; |
|
70 |
|
71 for (auto it = a.begin(); it != a.end(); ++it) |
|
72 { |
|
73 if (it != a.begin()) |
|
74 result += ", "; |
|
75 |
|
76 result += make_format_argument (*it); |
|
77 } |
|
78 |
|
79 result += "}"; |
|
80 return result; |
|
81 } |
|
82 |
|
83 FORMAT_OVERLOAD (Color) |
|
84 { |
|
85 static const char* colorstrings[] = |
|
86 { |
|
87 "BLACK", "RED", "GREEN", "YELLOW", |
|
88 "BLUE", "MAGENTA", "CYAN", "WHITE", "DEFAULT" |
|
89 }; |
|
90 |
|
91 static_assert (sizeof colorstrings / sizeof *colorstrings == NUM_COLORS, |
|
92 "colostrings[] has an incorrect amount of elements"); |
|
93 |
|
94 if (int (a) >= 0 and int (a) < NUM_COLORS) |
|
95 return colorstrings[int (a)]; |
|
96 |
|
97 return "???"; |
|
98 } |
|
99 |
|
100 FORMAT_OVERLOAD (Position) |
|
101 { |
|
102 return String ("(") + a.x + ", " + a.y + ")"; |
|
103 } |
|
104 |
|
105 FORMAT_OVERLOAD (Size) |
|
106 { |
|
107 return String ("(") + a.width + "x" + a.height + ")"; |
|
108 } |
|
109 |
|
110 FORMAT_OVERLOAD (Rectangle) |
|
111 { |
|
112 String result; |
|
113 result.sprintf ("{(%d, %d), (%dx%d)}", a.x, a.y, a.width, a.height); |
|
114 return result; |
|
115 } |
|
116 |
|
117 // ------------------------------------------------------------------------------------------------- |
|
118 // |
|
119 // Formats the given string with the given args. |
|
120 // |
|
121 FUNCTION format_args (const String& fmtstr, const Vector<String>& args) -> String; |
|
122 |
|
123 // ------------------------------------------------------------------------------------------------- |
|
124 // |
|
125 // Expands the given arguments into a vector of strings. |
|
126 // |
|
127 template<typename T, typename... RestTypes> |
|
128 void expand_format_arguments (Vector<String>& data, const T& arg, const RestTypes& ... rest) |
|
129 { |
|
130 data.append (make_format_argument (arg)); |
|
131 expand_format_arguments (data, rest...); |
|
132 } |
|
133 |
|
134 static void expand_format_arguments (Vector<String>&) __attribute__((unused)); |
|
135 static void expand_format_arguments (Vector<String>&) {} |
|
136 |
|
137 // ------------------------------------------------------------------------------------------------- |
|
138 // |
|
139 // Formats the given formatter string and args and returns the string. |
|
140 // This is essentially a modernized sprintf. |
|
141 // |
|
142 // Args in the format string take the form %n where n is a digit. The argument |
|
143 // will be expanded to the nth argument passed. This is essentially Qt's |
|
144 // QString::arg() syntax. Note: %0 is invalid. |
|
145 // |
|
146 // Arguments can be passed a modifier which takes the form of a character |
|
147 // just before the digit. Currently supported modifiers are s, d and x. |
|
148 // |
|
149 // - s: The argument will expand into "s" if it would've not expanded into "1" |
|
150 // otherwise. If it would have expanded into "1" it will expand into an |
|
151 // empty string. |
|
152 // |
|
153 // - d: The argument expands into the numeric form of the first character of |
|
154 // its previous expansion. Use this to get numeric forms of @c char |
|
155 // arguments. |
|
156 // |
|
157 // - x: The numeric argument will be represented in hexadecimal notation. This |
|
158 // will work if the argument is a string representing a number. If the |
|
159 // argument did not expand into a number in the first place, 0 is used |
|
160 // and 0x0 is printed. |
|
161 // |
|
162 template<typename... argtypes> |
|
163 String format (const String& fmtstr, const argtypes&... raw_args) |
|
164 { |
|
165 Vector<String> args; |
|
166 expand_format_arguments (args, raw_args...); |
|
167 return format_args (fmtstr, args); |
|
168 } |
|
169 |
|
170 // ------------------------------------------------------------------------------------------------- |
|
171 // |
|
172 // This is an overload of format() where no arguments are supplied. |
|
173 // It returns the formatter string as-is. |
|
174 // |
|
175 static String format (const String& fmtstr) __attribute__ ((unused)); |
|
176 static String format (const String& fmtstr) // -> String |
|
177 { |
|
178 return fmtstr; |
|
179 } |
|
180 |
|
181 // ------------------------------------------------------------------------------------------------- |
|
182 // |
|
183 // Prints the formatting result to the given file handle |
|
184 // |
|
185 template<typename... Args> |
|
186 void print_to (FILE* fp, const String& fmtstr, Args const& ...args) |
|
187 { |
|
188 std::fprintf (fp, "%s", format (fmtstr, args...).chars()); |
|
189 } |
|
190 |
|
191 // ------------------------------------------------------------------------------------------------- |
|
192 // |
|
193 // Appends the formatting result to the given filename, if opening it succeeds |
|
194 // |
|
195 template<typename... argtypes> |
|
196 void print_to (const String& filename, const String& fmtstr, const argtypes&... args) |
|
197 { |
|
198 FILE* handle; |
|
199 |
|
200 if ((handle = fopen (filename, "a"))) |
|
201 { |
|
202 print_to (handle, fmtstr, args...); |
|
203 fclose (handle); |
|
204 } |
|
205 } |
|