Released version 3-2015061300
[notion.git] / libtu / misc.h
blob3b547e01d89e1fef6ac59abd45393fc51b3019ea
1 /*
2 * libtu/misc.h
4 * Copyright (c) Tuomo Valkonen 1999-2005.
6 * You may distribute and modify this library under the terms of either
7 * the Clarified Artistic License or the GNU LGPL, version 2.1 or later.
8 */
10 #ifndef LIBTU_MISC_H
11 #define LIBTU_MISC_H
13 #include <stdlib.h>
14 #include <stdio.h>
15 #include <assert.h>
16 #include "types.h"
18 #define ALLOC(X) (X*)malloczero(sizeof(X))
19 #define ALLOC_N(X, N) (X*)malloczero(sizeof(X)*(N))
20 #define REALLOC_N(PTR, X, S, N) (X*)remalloczero(PTR, sizeof(X)*(S), sizeof(X)*(N))
22 #define FREE(X) do{if(X!=NULL)free(X);}while(0)
24 #define XOR(X, Y) (((X)==0) != ((Y)==0))
26 /* UNUSED macro, for function argument */
27 #ifdef __GNUC__
28 # define UNUSED(x) UNUSED_ ## x __attribute__((__unused__))
29 #else
30 # define UNUSED(x) UNUSED_ ## x
31 #endif
33 extern void* malloczero(size_t size);
34 extern void* remalloczero(void *ptr, size_t oldsize, size_t newsize);
36 extern char* scopy(const char *p);
37 extern char* scopyn(const char *p, size_t n);
38 extern char* scat(const char *p1, const char *p2);
39 extern char* scatn(const char *p1, ssize_t n1, const char *p2, ssize_t n2);
40 extern char* scat3(const char *p1, const char *p2, const char *p3);
41 extern void stripws(char *p);
42 extern const char *libtu_strcasestr(const char *str, const char *ptn);
44 extern const char* simple_basename(const char *name);
46 /* I dislike fread and fwrite... */
47 extern bool readf(FILE *fd, void *buf, size_t n);
48 extern bool writef(FILE *fd, const void *buf, size_t n);
50 #endif /* LIBTU_MISC_H */