diff --git a/mhl/env.h b/mhl/env.h
index 8d56337..6598e0c 100644
a
|
b
|
|
1 | | #ifndef __MHL_ENV_H |
2 | | #define __MHL_ENV_H |
| 1 | #ifndef MHL_ENV_H |
| 2 | #define MHL_ENV_H |
3 | 3 | |
4 | 4 | #include <mhl/string.h> |
5 | 5 | |
diff --git a/mhl/escape.h b/mhl/escape.h
index 2ec4e0d..118e082 100644
a
|
b
|
|
1 | | #ifndef __MHL_SHELL_ESCAPE_H |
2 | | #define __MHL_SHELL_ESCAPE_H |
| 1 | #ifndef MHL_ESCAPE_H |
| 2 | #define MHL_ESCAPE_H |
3 | 3 | |
4 | 4 | /* Micro helper library: shell escaping functions */ |
5 | 5 | |
… |
… |
static inline SHELL_ESCAPED_STR mhl_shell_escape_dup(const char* src) |
64 | 64 | /** Unescape paths or other strings for e.g the internal cd |
65 | 65 | shell-unescape within a given buffer (writing to it!) |
66 | 66 | |
67 | | /params const char * in |
| 67 | /params const char * src |
68 | 68 | string for unescaping |
69 | 69 | /returns |
70 | 70 | return unescaped string |
… |
… |
static inline char* mhl_shell_unescape_buf(char* text) |
113 | 113 | case '`': |
114 | 114 | case '"': |
115 | 115 | case ';': |
116 | | case '\0': /* end of line! malformed escape string */ |
| 116 | case '\0': /* end of string! malformed escape string */ |
117 | 117 | goto out; |
118 | 118 | default: |
119 | 119 | (*writeptr) = c; writeptr++; break; |
diff --git a/mhl/memory.h b/mhl/memory.h
index b006177..3268e93 100644
a
|
b
|
|
1 | | #ifndef __MHL_MEM |
2 | | #define __MHL_MEM |
| 1 | #ifndef MHL_MEMORY_H |
| 2 | #define MHL_MEMORY_H |
3 | 3 | |
4 | 4 | #include <memory.h> |
5 | 5 | #include <stdlib.h> |
… |
… |
static inline void mhl_mem_free(void* ptr) |
17 | 17 | } |
18 | 18 | |
19 | 19 | /* free an ptr and NULL it */ |
20 | | #define MHL_PTR_FREE(ptr) do { mhl_mem_free(ptr); (ptr) = NULL; } while (0); |
| 20 | #define MHL_PTR_FREE(ptr) do { mhl_mem_free(ptr); (ptr) = NULL; } while (0) |
21 | 21 | |
22 | 22 | /* allocate a chunk on stack - automatically free'd on function exit */ |
23 | 23 | #define mhl_stack_alloc(sz) (alloca(sz)) |
diff --git a/mhl/strhash.h b/mhl/strhash.h
index 1f19505..8ff17cb 100644
a
|
b
|
|
1 | | #ifndef __MHL_STRHASH_H |
2 | | #define __MHL_STRHASH_H |
| 1 | #ifndef MHL_STRHASH_H |
| 2 | #define MHL_STRHASH_H |
3 | 3 | |
4 | 4 | #include <hash.h> |
5 | 5 | #include <mhl/memory.h> |
diff --git a/mhl/string.h b/mhl/string.h
index ee74e1d..6431fdc 100644
a
|
b
|
|
1 | | #ifndef __MHL_STRING_H |
2 | | #define __MHL_STRING_H |
| 1 | #ifndef MHL_STRING_H |
| 2 | #define MHL_STRING_H |
3 | 3 | |
4 | 4 | #include <ctype.h> |
5 | 5 | #include <stdarg.h> |
… |
… |
|
10 | 10 | #define mhl_str_ndup(str,len) ((str ? strndup(str,len) : strdup(""))) |
11 | 11 | #define mhl_str_len(str) ((str ? strlen(str) : 0)) |
12 | 12 | |
| 13 | #define ISSPACE(c) isspace((unsigned char)(c)) |
| 14 | #define TOUPPER(c) toupper((unsigned char)(c)) |
| 15 | |
13 | 16 | static inline char * mhl_str_dup_range(const char * s_start, const char * s_bound) |
14 | 17 | { |
15 | 18 | return mhl_str_ndup(s_start, s_bound - s_start); |
… |
… |
static inline char* mhl_str_trim(char* str) |
20 | 23 | if (!str) return NULL; /* NULL string ?! bail out. */ |
21 | 24 | |
22 | 25 | /* find the first non-space */ |
23 | | char* start; for (start=str; ((*str) && (!isspace(*str))); str++); |
| 26 | char* start; for (start=str; ((*str) && (!ISSPACE(*str))); str++); |
24 | 27 | |
25 | 28 | /* only spaces ? */ |
26 | 29 | if (!(*str)) { *str = 0; return str; } |
27 | 30 | |
28 | | /* get the size (cannot be empty - catched above) */ |
| 31 | /* get the size (cannot be empty - caught above) */ |
29 | 32 | size_t _sz = strlen(str); |
30 | 33 | |
31 | 34 | /* find the proper end */ |
32 | 35 | char* end; |
33 | | for (end=(str+_sz-1); ((end>str) && (isspace(*end))); end--); |
| 36 | for (end=(str+_sz-1); ((end>str) && (ISSPACE(*end))); end--); |
34 | 37 | end[1] = 0; /* terminate, just to be sure */ |
35 | 38 | |
36 | 39 | /* if we have no leading spaces, just trucate */ |
37 | 40 | if (start==str) { end++; *end = 0; return str; } |
38 | 41 | |
39 | | /* if it' only one char, dont need memmove for that */ |
| 42 | /* if it's only one char, dont need memmove for that */ |
40 | 43 | if (start==end) { str[0]=*start; str[1]=0; return str; } |
41 | 44 | |
42 | | /* by here we have a (non-empty) region between start end end */ |
| 45 | /* by here we have a (non-empty) region between start and end */ |
43 | 46 | memmove(str,start,(end-start+1)); |
44 | 47 | return str; |
45 | 48 | } |
… |
… |
static inline void mhl_str_toupper(char* str) |
48 | 51 | { |
49 | 52 | if (str) |
50 | 53 | for (;*str;str++) |
51 | | *str = toupper(*str); |
| 54 | *str = TOUPPER(*str); |
52 | 55 | } |
53 | 56 | |
54 | 57 | #define __STR_CONCAT_MAX 32 |
… |
… |
static inline char* __mhl_str_concat_hlp(const char* base, ...) |
79 | 82 | arg_sz[count] = strlen(a); |
80 | 83 | totalsize += arg_sz[count]; |
81 | 84 | count++; |
| 85 | assert(count < __STR_CONCAT_MAX); |
82 | 86 | } |
83 | 87 | } |
84 | 88 | |
85 | 89 | if (!count) |
86 | 90 | return mhl_str_dup(""); |
87 | 91 | |
88 | | /* now as we know how much to copy, allocate the buffer */ |
89 | | char* buffer = (char*)mhl_mem_alloc_u(totalsize+2); |
| 92 | /* now that we know how much to copy, allocate the buffer */ |
| 93 | char* buffer = (char*)mhl_mem_alloc_u(totalsize+2); /* why +2? */ |
90 | 94 | char* current = buffer; |
91 | 95 | int x=0; |
92 | 96 | for (x=0; x<count; x++) |
… |
… |
static inline char * mhl_strmove(char * dest, const char * src) |
130 | 134 | { |
131 | 135 | size_t n = strlen (src) + 1; /* + '\0' */ |
132 | 136 | |
133 | | assert (dest<=src); |
| 137 | assert (dest<=src); /* strictly speaking, this invokes undefined behavior as soon as dest and src are pointers into different objects. */ |
134 | 138 | |
135 | 139 | return memmove(dest, src, n); |
136 | 140 | } |
… |
… |
static inline char* mhl_str_dir_plus_file(const char* dirname, const char* filen |
168 | 172 | return buffer; |
169 | 173 | } |
170 | 174 | |
171 | | #endif /* __MHL_STRING_H */ |
| 175 | #endif /* MHL_STRING_H */ |
diff --git a/mhl/types.h b/mhl/types.h
index 1f84002..977c27a 100644
a
|
b
|
|
4 | 4 | |
5 | 5 | */ |
6 | 6 | |
7 | | #ifndef __MHL_TYPES_H |
8 | | #define __MHL_TYPES_H |
| 7 | #ifndef MHL_TYPES_H |
| 8 | #define MHL_TYPES_H |
9 | 9 | |
10 | | typedef enum |
| 10 | #if !defined(__bool_true_false_are_defined) && !defined(false) && !defined(true) && !defined(bool) |
| 11 | typedef enum |
11 | 12 | { |
12 | 13 | false = 0, |
13 | 14 | true = 1 |
14 | 15 | } bool; |
| 16 | #endif |
15 | 17 | |
16 | 18 | #endif |