* new common/misc.c/
[gnokii.git] / common / misc.c
index d9c57ad..14bc240 100644 (file)
   $Id$
   
   $Log$
+  Revision 1.1.1.1.6.1  2001/11/25 23:04:51  short
+  * new common/misc.c/
+    * g{,v}asprintf() - "asprintf()" compatibility emulation
+    * ARRAY_LEN() - sizeof(x)/sizeof(*x)
+    * SAFE_STRNCPY{,_SIZEOF}() - strncpy with variable-size autodetection
+    * G_GNUC_PRINTF - GCC attribute from glib
+    * N_(x) - missing in localization macros
+
   Revision 1.1.1.1  2001/11/25 21:59:04  short
   :pserver:cvs@pserver.samba.org:/cvsroot - gnokii - Sun Nov 25 22:56 CET 2001
 
@@ -46,6 +54,7 @@
 */
 
 #include <string.h>
+#include <stdlib.h>
 #include "misc.h"
 
 int GetLine(FILE *File, char *Line, int count)
@@ -126,4 +135,49 @@ inline char *GetModel (const char *num)
        return (GetPhoneModel(num)->model);
 }
 
+#ifndef HAVE_VASPRINTF
+/* Adapted from snprintf(3) man page: */
+int gvasprintf(char **destp,const char *fmt,va_list ap)
+{
+int n,size=0x100;
+char *p,*pnew;
 
+       if (!(p=malloc(size))) {
+               *destp=NULL;
+               return(-1);
+               }
+       for (;;) {
+               /* Try to print in the allocated space. */
+               n=gvsprintf(p,size,fmt,ap);
+               /* If that worked, return the string. */
+               if (n>-1 && n<size) {
+                       *destp=p;
+                       return(n);
+                       }
+               /* Else try again with more space. */
+               if (n>-1)       /* glibc 2.1 */
+                       size=n+1;       /* precisely what is needed */
+               else            /* glibc 2.0 */
+                       size*=2;        /* twice the old size */
+               if (!(pnew=realloc(p,size))) {
+                       free(p);
+                       *destp=NULL;
+                       return(-1);
+                       }
+               p=pnew;
+       }
+}
+#endif
+
+#ifndef HAVE_ASPRINTF
+int gasprintf(char **destp,const char *fmt,...)
+{
+va_list ap;
+int r;
+
+       va_start(ap,fmt);
+       r=gvasprintf(destp,fmt,ap);
+       va_end(ap);
+       return(r);
+}
+#endif