sources/format.h

changeset 5
146825d63b9a
parent 1
4dd5bde4e777
child 10
3874575d924d
equal deleted inserted replaced
4:ec5387e121fa 5:146825d63b9a
103 String result; 103 String result;
104 result.sprintf ("{(%d, %d), (%dx%d)}", a.x, a.y, a.width, a.height); 104 result.sprintf ("{(%d, %d), (%dx%d)}", a.x, a.y, a.width, a.height);
105 return result; 105 return result;
106 } 106 }
107 107
108 // 108 // -------------------------------------------------------------------------------------------------
109 // -------------------------------------------------------------------------------------------------
110 //
111 // Formats the given string with the given args. 109 // Formats the given string with the given args.
112 // 110 //
113 auto format_args (const String& fmtstr, const Vector<String>& args) -> String; 111 FUNCTION format_args (const String& fmtstr, const Vector<String>& args) -> String;
114 112
115 // 113 // -------------------------------------------------------------------------------------------------
116 // -------------------------------------------------------------------------------------------------
117 //
118 // Expands the given arguments into a vector of strings. 114 // Expands the given arguments into a vector of strings.
119 // 115 //
120 template<typename T, typename... RestTypes> 116 template<typename T, typename... RestTypes> FUNCTION
121 auto expand_format_arguments (Vector<String>& data, const T& arg, const RestTypes& ... rest) -> void 117 expand_format_arguments (Vector<String>& data, const T& arg, const RestTypes& ... rest) -> void
122 { 118 {
123 data.append (make_format_argument (arg).text()); 119 data.append (make_format_argument (arg).text());
124 expand_format_arguments (data, rest...); 120 expand_format_arguments (data, rest...);
125 } 121 }
126 122
127 static void expand_format_arguments (Vector<String>&) __attribute__( (unused)); 123 static void expand_format_arguments (Vector<String>&) __attribute__((unused));
128 static void expand_format_arguments (Vector<String>&) {} 124 static void expand_format_arguments (Vector<String>&) {}
129 125
130 //
131 // ------------------------------------------------------------------------------------------------- 126 // -------------------------------------------------------------------------------------------------
132 // 127 //
133 // Formats the given formatter string and args and returns the string. 128 // Formats the given formatter string and args and returns the string.
134 // This is essentially a modernized sprintf. 129 // This is essentially a modernized sprintf.
135 // 130 //
151 // - x: The numeric argument will be represented in hexadecimal notation. This 146 // - x: The numeric argument will be represented in hexadecimal notation. This
152 // will work if the argument is a string representing a number. If the 147 // will work if the argument is a string representing a number. If the
153 // argument did not expand into a number in the first place, 0 is used 148 // argument did not expand into a number in the first place, 0 is used
154 // and 0x0 is printed. 149 // and 0x0 is printed.
155 // 150 //
156 template<typename... argtypes> 151 template<typename... argtypes> FUNCTION
157 String format (const String& fmtstr, const argtypes&... raw_args) 152 format (const String& fmtstr, const argtypes&... raw_args) -> String
158 { 153 {
159 Vector<String> args; 154 Vector<String> args;
160 expand_format_arguments (args, raw_args...); 155 expand_format_arguments (args, raw_args...);
161 return format_args (fmtstr, args); 156 return format_args (fmtstr, args);
162 } 157 }
163 158
164 // 159 // -------------------------------------------------------------------------------------------------
165 // -------------------------------------------------------------------------------------------------
166 //
167 // This is an overload of format() where no arguments are supplied. 160 // This is an overload of format() where no arguments are supplied.
168 // It returns the formatter string as-is. 161 // It returns the formatter string as-is.
169 // 162 //
170 static String format (const String& fmtstr) __attribute__ ((unused)); 163 static String format (const String& fmtstr) __attribute__ ((unused));
171 static String format (const String& fmtstr) 164 static String // FUNCTION
165 format (const String& fmtstr) // -> String
172 { 166 {
173 return fmtstr; 167 return fmtstr;
174 } 168 }
175 169
176 // 170 // -------------------------------------------------------------------------------------------------
177 // ------------------------------------------------------------------------------------------------- 171 // Prints the formatting result to the given file handle
178 // 172 //
179 173 template<typename... Args> FUNCTION
180 template<typename... Args> 174 print_to (FILE* fp, const String& fmtstr, Args const& ...args) -> void
181 void print_to (FILE* fp, const String& fmtstr, Args const& ...args)
182 { 175 {
183 std::fprintf (fp, "%s", format (fmtstr, args...).chars()); 176 std::fprintf (fp, "%s", format (fmtstr, args...).chars());
184 } 177 }
185 178
186 // 179 // -------------------------------------------------------------------------------------------------
187 // ------------------------------------------------------------------------------------------------- 180 // Appends the formatting result to the given filename, if opening it succeeds
188 // 181 //
189
190 template<typename... argtypes> 182 template<typename... argtypes>
191 void print_to (const String& filename, const String& fmtstr, const argtypes&... args) 183 void print_to (const String& filename, const String& fmtstr, const argtypes&... args)
192 { 184 {
193 FILE* handle; 185 FILE* handle;
194 186
197 print_to (handle, fmtstr, args...); 189 print_to (handle, fmtstr, args...);
198 fclose (handle); 190 fclose (handle);
199 } 191 }
200 } 192 }
201 193
202 // 194 // -------------------------------------------------------------------------------------------------
203 // ------------------------------------------------------------------------------------------------- 195 // Prints the formatting result to stdout
204 // 196 //
205
206 template<typename... argtypes> 197 template<typename... argtypes>
207 void print (const String& fmtstr, const argtypes&... args) 198 void print (const String& fmtstr, const argtypes&... args)
208 { 199 {
209 print_to (stdout, fmtstr, args...); 200 print_to (stdout, fmtstr, args...);
210 } 201 }
211
212 #define PRINT_TO_LOG(...) \
213 { \
214 print_to (LOGFILE, "%1:%2: ", __PRETTY_FUNCTION__, __LINE__); \
215 print_to (LOGFILE, __VA_ARGS__); \
216 }

mercurial