size_t -> gsize
authorshort <>
Thu, 12 Dec 2002 04:03:40 +0000 (04:03 +0000)
committershort <>
Thu, 12 Dec 2002 04:03:40 +0000 (04:03 +0000)
+captive_printf_alloca()

src/libcaptive/include/captive/macros.h

index 96d1ce6..39fe1b6 100644 (file)
@@ -26,6 +26,7 @@
 #include <stdarg.h>
 #include <string.h>    /* for memset() */
 #include <glib/galloca.h>
+#include <glib/gmessages.h>
 
 
 G_BEGIN_DECLS
@@ -142,7 +143,7 @@ G_BEGIN_DECLS
  * Value %NULL is returned iff @n==%0;
  */
 #define captive_newn_alloca(objp,n) ({ \
-               size_t _captive_newn_alloca_n=(n); \
+               gsize _captive_newn_alloca_n=(n); \
                /* Fix 'g_alloca(0)!=NULL': */ \
                ((objp)=(!_captive_newn_alloca_n ? NULL : g_alloca(sizeof(*(objp))*_captive_newn_alloca_n))); \
                })
@@ -225,6 +226,37 @@ G_BEGIN_DECLS
  */
 #define CAPTIVE_MEMZERO(objp) (memset((objp),0,sizeof(*(objp))))
 
+
+/**
+ * captive_printf_alloca:
+ * @format: Format string. See the sprintf() documentation.
+ * @args: Arguments for @format. See the sprintf() documentation.
+ *
+ * Format the given format string @format as in sprintf().
+ * Output buffer is allocated automatically and it does not need to be deallocated
+ * manually as it is managed by g_alloca().
+ *
+ * @Returns: Formatted output string located in g_alloca() memory.
+ */
+#define captive_printf_alloca(format,args...) ({ \
+               gsize _captive_printf_alloca_size=captive_nv_printf_string_upper_bound((format) , ## args); \
+               gchar *_captive_printf_alloca_r=g_alloca(_captive_printf_alloca_size); \
+               g_snprintf(_captive_printf_alloca_r,_captive_printf_alloca_size,(format) , ## args); \
+               (const gchar *)_captive_printf_alloca_r; \
+               })
+static inline gsize captive_nv_printf_string_upper_bound(const gchar *format,...) G_GNUC_PRINTF(1,0);
+static inline gsize captive_nv_printf_string_upper_bound(const gchar *format,...)
+{
+va_list ap;
+gsize r;
+
+       va_start(ap,format);
+       r=g_printf_string_upper_bound(format,ap);
+       va_end(ap);
+       return r;
+}
+
+
 G_END_DECLS