[vlc-devel] [PATCH] Win32: wrappers to work around %z printf limitations

Pierre Ynard linkfanel at yahoo.fr
Mon Mar 9 12:17:32 CET 2009


This patch works around unsupported %z modifiers, by using a wrapper
around *printf functions that patches the format string on the fly. The
format string is duplicated, and "%z"'s are replaced by "%l"'s.


diff --git a/include/vlc_fixups.h b/include/vlc_fixups.h
index 87626e8..23887a1 100644
--- a/include/vlc_fixups.h
+++ b/include/vlc_fixups.h
@@ -1,7 +1,7 @@
 /*****************************************************************************
  * fixups.h: portability fixups included from config.h
  *****************************************************************************
- * Copyright © 1998-2008 the VideoLAN project
+ * Copyright © 1998-2009 the VideoLAN project
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -38,6 +38,120 @@ static inline char *strdup (const char *str)
 }
 #endif
 
+#ifdef WIN32
+/* Windows' printf doesn't support %z modifiers, thus we need to rewrite
+ * the format string in a wrapper. */
+# include <string.h>
+static inline char *vlc_fix_format_string (const char *format)
+{
+    char *fmt, *f;
+    if (strstr (format, "%z") == NULL)
+        return NULL;
+
+    fmt = strdup (format);
+    if (fmt == NULL)
+        return NULL;
+
+    while ((f = strstr (fmt, "%z")) != NULL)
+    {
+       f[1] = 'l';
+    }
+    return fmt;
+}
+
+# include <stdlib.h>
+# include <stdio.h>
+# include <stdarg.h>
+
+static inline int vlc_vprintf (const char *format, va_list ap)
+{
+    char *fmt = vlc_fix_format_string (format);
+    int ret = vprintf (fmt ? fmt : format, ap);
+    free (fmt);
+    return ret;
+}
+# define vprintf vlc_vprintf
+
+static inline int vlc_vfprintf (FILE *stream, const char *format, va_list ap)
+{
+    char *fmt = vlc_fix_format_string (format);
+    int ret = vfprintf (stream, fmt ? fmt : format, ap);
+    free (fmt);
+    return ret;
+}
+# define vfprintf vlc_vfprintf
+
+static inline int vlc_vsprintf (char *str, const char *format, va_list ap)
+{
+    char *fmt = vlc_fix_format_string (format);
+    int ret = vsprintf (str, fmt ? fmt : format, ap);
+    free (fmt);
+    return ret;
+}
+# define vsprintf vlc_vsprintf
+
+static inline int vlc_vsnprintf (char *str, size_t size, const char *format, va_list ap)
+{
+    char *fmt = vlc_fix_format_string (format);
+    int ret = vsnprintf (str, size, fmt ? fmt : format, ap);
+    free (fmt);
+    return ret;
+}
+# define vsnprintf vlc_vsnprintf
+
+static inline int vlc_printf (const char *format, ...)
+{
+    va_list ap;
+    int ret;
+    va_start (ap, format);
+    ret = vprintf (format, ap);
+    va_end (ap);
+    return ret;
+}
+# define printf(...) vlc_printf(__VA_ARGS__)
+
+static inline int vlc_fprintf (FILE *stream, const char *format, ...)
+{
+    va_list ap;
+    int ret;
+    va_start (ap, format);
+    ret = vfprintf (stream, format, ap);
+    va_end (ap);
+    return ret;
+}
+# define fprintf vlc_fprintf
+
+static inline int vlc_sprintf (char *str, const char *format, ...)
+{
+    va_list ap;
+    int ret;
+    va_start (ap, format);
+    ret = vsprintf (str, format, ap);
+    va_end (ap);
+    return ret;
+}
+# define sprintf vlc_sprintf
+
+static inline int vlc_snprintf (char *str, size_t size, const char *format, ...)
+{
+    va_list ap;
+    int ret;
+    va_start (ap, format);
+    ret = vsnprintf (str, size, format, ap);
+    va_end (ap);
+    return ret;
+}
+# define snprintf vlc_snprintf
+
+/* Make sure we don't use flawed vasprintf or asprintf either */
+# ifdef HAVE_VASPRINTF
+#  undef HAVE_VASPRINTF
+# endif
+# ifdef HAVE_ASPRINTF
+#  undef HAVE_ASPRINTF
+# endif
+#endif
+
 #ifndef HAVE_VASPRINTF
 # include <stdio.h>
 # include <stdlib.h>


Regards,

-- 
Pierre Ynard
"Une âme dans un corps, c'est comme un dessin sur une feuille de papier."



More information about the vlc-devel mailing list