sources/format.h

changeset 60
268721e6be20
parent 15
33da84af4bba
child 66
bd28a5730fd0
equal deleted inserted replaced
59:00a084f8ed26 60:268721e6be20
34 #include "geometry.h" 34 #include "geometry.h"
35 35
36 #define FORMAT_OVERLOAD(...) \ 36 #define FORMAT_OVERLOAD(...) \
37 inline FUNCTION make_format_argument (__VA_ARGS__ a) -> String 37 inline FUNCTION make_format_argument (__VA_ARGS__ a) -> String
38 38
39 // 39 // -------------------------------------------------------------------------------------------------
40 // ------------------------------------------------------------------------------------------------- 40 //
41 //
42
43 FORMAT_OVERLOAD (String) { return a; } 41 FORMAT_OVERLOAD (String) { return a; }
44 FORMAT_OVERLOAD (char) { return String (a); } 42 FORMAT_OVERLOAD (char) { return String (a); }
45 FORMAT_OVERLOAD (int) { return String::from_number (a); } 43 FORMAT_OVERLOAD (int) { return String::from_number (a); }
46 FORMAT_OVERLOAD (long int) { return String::from_number (a); } 44 FORMAT_OVERLOAD (long int) { return String::from_number (a); }
47 FORMAT_OVERLOAD (double) { return String::from_number (a); } 45 FORMAT_OVERLOAD (double) { return String::from_number (a); }
105 result.sprintf ("{(%d, %d), (%dx%d)}", a.x, a.y, a.width, a.height); 103 result.sprintf ("{(%d, %d), (%dx%d)}", a.x, a.y, a.width, a.height);
106 return result; 104 return result;
107 } 105 }
108 106
109 // ------------------------------------------------------------------------------------------------- 107 // -------------------------------------------------------------------------------------------------
108 //
110 // Formats the given string with the given args. 109 // Formats the given string with the given args.
111 // 110 //
112 FUNCTION format_args (const String& fmtstr, const Vector<String>& args) -> String; 111 FUNCTION format_args (const String& fmtstr, const Vector<String>& args) -> String;
113 112
114 // ------------------------------------------------------------------------------------------------- 113 // -------------------------------------------------------------------------------------------------
114 //
115 // Expands the given arguments into a vector of strings. 115 // Expands the given arguments into a vector of strings.
116 // 116 //
117 template<typename T, typename... RestTypes> FUNCTION 117 template<typename T, typename... RestTypes> FUNCTION
118 expand_format_arguments (Vector<String>& data, const T& arg, const RestTypes& ... rest) -> void 118 expand_format_arguments (Vector<String>& data, const T& arg, const RestTypes& ... rest) -> void
119 { 119 {
156 expand_format_arguments (args, raw_args...); 156 expand_format_arguments (args, raw_args...);
157 return format_args (fmtstr, args); 157 return format_args (fmtstr, args);
158 } 158 }
159 159
160 // ------------------------------------------------------------------------------------------------- 160 // -------------------------------------------------------------------------------------------------
161 //
161 // This is an overload of format() where no arguments are supplied. 162 // This is an overload of format() where no arguments are supplied.
162 // It returns the formatter string as-is. 163 // It returns the formatter string as-is.
163 // 164 //
164 static String format (const String& fmtstr) __attribute__ ((unused)); 165 static String format (const String& fmtstr) __attribute__ ((unused));
165 static String // FUNCTION 166 static String // FUNCTION
167 { 168 {
168 return fmtstr; 169 return fmtstr;
169 } 170 }
170 171
171 // ------------------------------------------------------------------------------------------------- 172 // -------------------------------------------------------------------------------------------------
173 //
172 // Prints the formatting result to the given file handle 174 // Prints the formatting result to the given file handle
173 // 175 //
174 template<typename... Args> FUNCTION 176 template<typename... Args> FUNCTION
175 print_to (FILE* fp, const String& fmtstr, Args const& ...args) -> void 177 print_to (FILE* fp, const String& fmtstr, Args const& ...args) -> void
176 { 178 {
177 std::fprintf (fp, "%s", format (fmtstr, args...).chars()); 179 std::fprintf (fp, "%s", format (fmtstr, args...).chars());
178 } 180 }
179 181
180 // ------------------------------------------------------------------------------------------------- 182 // -------------------------------------------------------------------------------------------------
183 //
181 // Appends the formatting result to the given filename, if opening it succeeds 184 // Appends the formatting result to the given filename, if opening it succeeds
182 // 185 //
183 template<typename... argtypes> 186 template<typename... argtypes>
184 void print_to (const String& filename, const String& fmtstr, const argtypes&... args) 187 void print_to (const String& filename, const String& fmtstr, const argtypes&... args)
185 { 188 {
191 fclose (handle); 194 fclose (handle);
192 } 195 }
193 } 196 }
194 197
195 // ------------------------------------------------------------------------------------------------- 198 // -------------------------------------------------------------------------------------------------
199 //
196 // Prints the formatting result to the console 200 // Prints the formatting result to the console
197 // 201 //
198 template<typename... argtypes> 202 template<typename... argtypes>
199 void print (const String& fmtstr, const argtypes&... args) 203 void print (const String& fmtstr, const argtypes&... args)
200 { 204 {

mercurial