Set up current working directory on File->Open
[gscope.git] / util.h
blob6262ff02cccd459ac101fcee8dde19c0b6e63928
1 /*
2 * (c) 2010 Cyrill Gorcunov, gorcunov@gmail.com
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or (at
7 * your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20 #ifndef UTIL_H
21 #define UTIL_H
23 #include <stdarg.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <ctype.h>
29 * Some bits are stolen from perf tool and Linux kernel :)
32 #ifdef __GNUC__
33 #define NORETURN __attribute__((__noreturn__))
34 #else
35 #define NORETURN
36 #ifndef __attribute__
37 #define __attribute__(x)
38 #endif
39 #endif
41 #define __stringify_1(x) #x
42 #define __stringify(x) __stringify_1(x)
44 extern void die(const char *err, ...) NORETURN __attribute__((format (printf, 1, 2)));
45 extern void die_perror(const char *s) NORETURN;
46 extern int error(const char *err, ...) __attribute__((format (printf, 1, 2)));
47 extern void warning(const char *err, ...) __attribute__((format (printf, 1, 2)));
48 extern void debug(const char *err, ...) __attribute__((format (printf, 1, 2)));
49 extern void set_die_routine(void (*routine)(const char *err, va_list params) NORETURN);
51 #define BUG() DIE_IF(1)
52 #define DIE_IF(cnd) \
53 do { \
54 if (cnd) \
55 die(" at (" __FILE__ ":" __stringify(__LINE__) "): " \
56 __stringify(cnd) "\n"); \
57 } while (0)
59 extern size_t strlcat(char *dest, const char *src, size_t count);
62 * Handy macros
64 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
66 static inline void *xzalloc(unsigned long size)
68 void *p = malloc(size);
69 DIE_IF(!p);
70 memset(p, 0, size);
71 return p;
74 static inline void *xalloc(unsigned long size)
76 void *p = malloc(size);
77 DIE_IF(!p);
78 return p;
81 static inline void *zalloc(unsigned long size)
83 void *p = malloc(size);
84 if(p)
85 memset(p, 0, size);
86 return p;
89 static inline void *xstrdup(char *str)
91 char *p = strdup(str);
92 DIE_IF(!p);
93 return p;
96 static inline void *memdup(void *src, size_t size)
98 void *p;
99 p = xalloc(size);
100 memcpy(p, src, size);
101 return p;
104 #define x_isspace(x) isspace((unsigned char)(x))
106 /* skip leading spaces */
107 static inline char *skip_spaces(const char *p)
109 if (p)
110 while (*p && x_isspace(*p))
111 p++;
112 return (char *)p;
115 /* skip trailing spaces */
116 static inline char *skip_spaces_rev(char *p)
118 if (p) {
119 size_t i = strlen(p);
120 if (i-- > 0) {
121 while (i && x_isspace(p[i]))
122 i--;
123 p[i + 1] = 0;
126 return (char *)p;
129 static inline char *skip_word(const char *p)
131 if (p)
132 while (*p && !x_isspace(*p))
133 p++;
134 return (char *)p;
138 * trims spaces around the word and returns
139 * pointer to the start of word, the p will
140 * be set the first character after the word
142 static inline char *trim_word(char **str)
144 char *start, *end;
146 if (!str)
147 return NULL;
149 start = skip_spaces(*str);
150 end = skip_word(start);
151 if (end != start) {
152 *end = 0;
153 *str = &end[1];
155 return start;
158 #endif /* UTIL_H */