comparison src/audacious/strings.c @ 4606:6b76f8589f5d

Added a temporary function str_assert_utf8() for finding points in code where string data should be UTF-8 but might not be. In case of valid UTF-8 a copy of string is returned. If string is NOT valid UTF-8, a call backtrace is printed and str_to_utf() is called instead.
author Matti Hamalainen <ccr@tnsp.org>
date Wed, 04 Jun 2008 23:17:47 +0300
parents 6e323e395886
children 829c30fc87ba
comparison
equal deleted inserted replaced
4605:9df19c4900ba 4606:6b76f8589f5d
295 replace with '?' */ 295 replace with '?' */
296 return str_to_utf8_fallback(str); 296 return str_to_utf8_fallback(str);
297 } 297 }
298 298
299 299
300 /* This function is here to ASSERT that a given string IS valid UTF-8.
301 * If it is, a copy of the string is returned (use g_free() to deallocate it.)
302 *
303 * However, if the string is NOT valid UTF-8, a warning is printed and a
304 * callstack backtrace is printed in order to see where the problem occured.
305 *
306 * This is a temporary measure for removing useless str_to_utf8 etc. calls
307 * and will be eventually removed...
308 * -- ccr
309 */
310 #include <execinfo.h>
311
312 gchar *
313 str_assert_utf8(const gchar * str)
314 {
315 /* NULL in NULL out */
316 if (!str)
317 return NULL;
318
319 /* already UTF-8? */
320 if (!g_utf8_validate(str, -1, NULL)) {
321 gint i, nsymbols;
322 const gint nsymmax = 50;
323 void *addrbuf[nsymmax];
324 gchar **symbols;
325 nsymbols = backtrace(addrbuf, nsymmax);
326 symbols = backtrace_symbols(addrbuf, nsymbols);
327
328 fprintf(stderr, "WARNING! String '%s' was not UTF-8! Backtrace (%d):\n", str, nsymbols);
329
330 for (i = 0; i < nsymbols; i++)
331 fprintf(stderr, "#%d > %s\n", i, symbols[i]);
332
333 free(symbols);
334
335 return str_to_utf8(str);
336 } else
337 return g_strdup(str);
338 }
339
340
300 const gchar * 341 const gchar *
301 str_skip_chars(const gchar * str, const gchar * chars) 342 str_skip_chars(const gchar * str, const gchar * chars)
302 { 343 {
303 while (strchr(chars, *str)) 344 while (strchr(chars, *str))
304 str++; 345 str++;