Commit | Line | Data |
---|---|---|
f7d82350 SR |
1 | /* |
2 | * Copyright (C) 2010 Red Hat Inc, Steven Rostedt <srostedt@redhat.com> | |
3 | * | |
4 | * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
5 | * This program is free software; you can redistribute it and/or | |
6 | * modify it under the terms of the GNU Lesser General Public | |
7 | * License as published by the Free Software Foundation; | |
8 | * version 2.1 of the License (not later!) | |
9 | * | |
10 | * This program is distributed in the hope that it will be useful, | |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | * GNU Lesser General Public License for more details. | |
14 | * | |
15 | * You should have received a copy of the GNU Lesser General Public | |
16 | * License along with this program; if not, write to the Free Software | |
17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
18 | * | |
19 | * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
20 | */ | |
21 | #ifndef __UTIL_H | |
22 | #define __UTIL_H | |
23 | ||
24 | #include <ctype.h> | |
25 | ||
668fe01f SR |
26 | /* Can be overridden */ |
27 | void die(const char *fmt, ...); | |
28 | void *malloc_or_die(unsigned int size); | |
29 | void warning(const char *fmt, ...); | |
30 | void pr_stat(const char *fmt, ...); | |
31 | void vpr_stat(const char *fmt, va_list ap); | |
32 | ||
33 | /* Always available */ | |
34 | void __die(const char *fmt, ...); | |
35 | void __warning(const char *fmt, ...); | |
36 | void __pr_stat(const char *fmt, ...); | |
37 | ||
38 | void __vdie(const char *fmt, ...); | |
39 | void __vwarning(const char *fmt, ...); | |
40 | void __vpr_stat(const char *fmt, ...); | |
41 | ||
e1aa7c30 NK |
42 | #define min(x, y) ({ \ |
43 | typeof(x) _min1 = (x); \ | |
44 | typeof(y) _min2 = (y); \ | |
45 | (void) (&_min1 == &_min2); \ | |
46 | _min1 < _min2 ? _min1 : _min2; }) | |
47 | ||
f7d82350 SR |
48 | static inline char *strim(char *string) |
49 | { | |
50 | char *ret; | |
51 | ||
52 | if (!string) | |
53 | return NULL; | |
54 | while (*string) { | |
55 | if (!isspace(*string)) | |
56 | break; | |
57 | string++; | |
58 | } | |
59 | ret = string; | |
60 | ||
61 | string = ret + strlen(ret) - 1; | |
62 | while (string > ret) { | |
63 | if (!isspace(*string)) | |
64 | break; | |
65 | string--; | |
66 | } | |
67 | string[1] = 0; | |
68 | ||
69 | return ret; | |
70 | } | |
71 | ||
72 | static inline int has_text(const char *text) | |
73 | { | |
74 | if (!text) | |
75 | return 0; | |
76 | ||
77 | while (*text) { | |
78 | if (!isspace(*text)) | |
79 | return 1; | |
80 | text++; | |
81 | } | |
82 | ||
83 | return 0; | |
84 | } | |
85 | ||
86 | #endif |