47677a8b9171496214f7084523fc41fc0f669103
[ccc.git] / util.h
1 #ifndef UTIL_H
2 #define UTIL_H
3
4 #include <stdarg.h>
5 #include <stdbool.h>
6 #include <stdio.h>
7
8 struct list { void *el; struct list *tail; };
9 struct list *list_cons(void *el, struct list *tail);
10 void list_free(struct list *head, void (*freefun)(void *));
11 void **list_to_array(struct list *list, int *num, bool reverse);
12 int list_length(struct list *head);
13
14 void die(const char *msg, ...);
15 void pdie(const char *msg);
16
17 /* if buf == NULL, a fresh buffer is allocated */
18 char *escape_char(char c, char *buf, bool str);
19 /* unescaped character will be in position 0 and the rest from position 1 on */
20 char *unescape_char(char *c);
21 /* Remove the last and first character from the string */
22 char *trimquotes(char *c);
23 void pindent(int indent, FILE *out);
24
25 void safe_fprintf(FILE *out, const char *msg, ...);
26 void *safe_malloc(size_t size);
27 void *safe_strdup(const char *c);
28 FILE *safe_fopen(const char *path, const char *mode);
29 void safe_fclose(FILE *file);
30
31 #endif