Added some string utility functions.
[ashd.git] / lib / utils.h
index d74d09d..3c9800c 100644 (file)
@@ -1,10 +1,12 @@
 #ifndef _UTILS_H
 #define _UTILS_H
 
+#include <stdarg.h>
+
 #define smalloc(size) ({void *__result__; ((__result__ = malloc(size)) == NULL)?({exit(-1); (void *)0;}):__result__;})
 #define srealloc(ptr, size) ({void *__result__; ((__result__ = realloc((ptr), (size))) == NULL)?({exit(-1); (void *)0;}):__result__;})
 #define szmalloc(size) memset(smalloc(size), 0, size)
-#define sstrdup(str) ({char *__strbuf__ = (str); strcpy(smalloc(strlen(__strbuf__) + 1), __strbuf__);})
+#define sstrdup(str) ({const char *__strbuf__ = (str); strcpy(smalloc(strlen(__strbuf__) + 1), __strbuf__);})
 #define omalloc(o) ((o) = szmalloc(sizeof(*(o))))
 
 #define bufinit(buf) memset(&(buf), 0, sizeof(buf))
@@ -23,12 +25,19 @@ do { \
     memcpy((buf).b + (buf).d, (new), (__bufcat_size__) * sizeof(*((buf).b))); \
     (buf).d += __bufcat_size__; \
 } while(0)
-#define bufcatstr2(buf, str) \
+#define bufcatstr(buf, str) \
 do { \
     char *__buf__; \
     __buf__ = (str); \
     bufcat((buf), __buf__, strlen(__buf__)); \
 } while(0)
+#define bufcatstr2(buf, str) \
+do { \
+    char *__buf__; \
+    __buf__ = (str); \
+    bufcat((buf), __buf__, strlen(__buf__) + 1); \
+} while(0)
+#define bufeat(buf, len) memmove((buf).b, (buf).b + (len), (buf).d -= (len))
 
 struct buffer {
     void *b;
@@ -48,5 +57,9 @@ struct charvbuf {
 };
 
 void _sizebuf(struct buffer *buf, size_t wanted, size_t el);
+char *decstr(char **p, size_t *len);
+char *vsprintf2(char *format, va_list al);
+char *sprintf2(char *format, ...);
+char *sprintf3(char *format, ...);
 
 #endif