//===================================================================
//=========================== util.c ================================
//===================================================================
-void *util_memory_a (unsigned int, unsigned int, const char *);
-void util_memory_d (void *, unsigned int, const char *);
-void util_meminfo ();
+void *util_memory_a (unsigned int, unsigned int, const char *);
+void util_memory_d (void *, unsigned int, const char *);
+void util_meminfo ();
-char *util_strdup (const char *);
-char *util_strrq (char *);
-char *util_strrnl (char *);
-void util_debug (const char *, const char *, ...);
-int util_getline (char **, size_t *, FILE *);
-void util_endianswap(void *, int, int);
+char *util_strdup (const char *);
+char *util_strrq (char *);
+char *util_strrnl (char *);
+void util_debug (const char *, const char *, ...);
+int util_getline (char **, size_t *, FILE *);
+void util_endianswap (void *, int, int);
uint32_t util_crc32(const char *, int, register const short);
#endif
/* Builds vector type (usefull for inside structures) */
-#define VECTOR_TYPE(T,N) \
- T* N##_data = NULL; \
- long N##_elements = 0; \
+#define VECTOR_TYPE(T,N) \
+ T* N##_data = NULL; \
+ long N##_elements = 0; \
long N##_allocated = 0
/* Builds vector add */
-#define VECTOR_CORE(T,N) \
- int N##_add(T element) { \
- if (N##_elements == N##_allocated) { \
- if (N##_allocated == 0) { \
- N##_allocated = 12; \
- } else { \
- N##_allocated *= 2; \
- } \
- void *temp = mem_a(N##_allocated * sizeof(T)); \
- if (!temp) { \
- mem_d(temp); \
- return -1; \
- } \
- memcpy(temp, N##_data, (N##_elements * sizeof(T))); \
- mem_d(N##_data); \
- N##_data = (T*)temp; \
- } \
- N##_data[N##_elements] = element; \
- return N##_elements++; \
+#define VECTOR_CORE(T,N) \
+ int N##_add(T element) { \
+ if (N##_elements == N##_allocated) { \
+ if (N##_allocated == 0) { \
+ N##_allocated = 12; \
+ } else { \
+ N##_allocated *= 2; \
+ } \
+ void *temp = mem_a(N##_allocated * sizeof(T)); \
+ if (!temp) { \
+ mem_d(temp); \
+ return -1; \
+ } \
+ memcpy(temp, N##_data, (N##_elements * sizeof(T))); \
+ mem_d(N##_data); \
+ N##_data = (T*)temp; \
+ } \
+ N##_data[N##_elements] = element; \
+ return N##_elements++; \
+ } \
+ int N##_put(T* elements, size_t len) { \
+ len --; \
+ elements--; \
+ while (N##_add(*++elements) != -1 && len--); \
+ return N##_elements; \
}
/* Builds a full vector inspot */
#define VECTOR_MAKE(T,N) \
VECTOR_TYPE(T,N); \
VECTOR_CORE(T,N)
/* Builds a vector add function pointer for inside structures */
-#define VECTOR_IMPL(T,N) \
- int (*N##_add)(T)
+#define VECTOR_IMPL(T,N) int (*N##_add)(T)
//===================================================================
//=========================== code.c ================================
*pos = '\0';
return (ret = pos - *lineptr);
}
+
+/*
+ * Strechy string buffer (for easy string creation) -- this is fast, just
+ * say no to strict aliasing.
+ */
+//#define util_stringbuf_add(a,v) ((((a)==0 ||((int*)(a)-2)[1]+(1)>=((int*)(a)-2)[0])?util_stringbuf_grow((void**)&(a),(1),sizeof(*(a))):0),(a)[((int*)(a)-2)[1]++]=(v))
+//#define util_stringbuf_len(a) ((a)? ((int*)(a)-2)[1]:0)
+//#define util_stringbuf_del(a) ((a)?free (((int*)(a)-2)),0:0)
+void *util_stringbuf_grow(void **a, int in, int it) {
+ int m = *a ? 2 * ((int*)(*a)-2)[0]+in : in+1;
+ void *p = realloc(*a ? ((int*)(*a)-2) : 0, it * m + sizeof(int)*2);
+ if (p) {
+ if (!*a) ((int*)p)[1] = 0;
+ *a = (void*)((int*)p+2);
+ ((int*)(*a)-2)[0] = m;
+ }
+ return *a;
+}