Ticket #157: mhl.001.diff
File mhl.001.diff, 16.5 KB (added by anonymous, 15 years ago) |
---|
-
new file mhl/README
diff --git a/mhl/README b/mhl/README new file mode 100644 index 0000000..83fb3d7
- + 1 2 Micro helper library. 3 -- 4 5 This is a tiny library of helper functions/macros. 6 7 * MACRO-FUNC: macro w/ function syntax. (might become inline func) 8 * INLINE-FUNC: inline function (might become macro func) 9 * MACRO: strictly a macro (may never become a inline func) 10 11 -- 12 13 memory.h: Memory management functions 14 15 * mhl_mem_alloc_u(sz) [MACRO-FUNC] 16 17 Allocate sz bytes on stack, unitialized 18 19 * mhl_mem_alloc_z(sz) [INLINE-FUNC] 20 21 Allocate sz bytes on stack, zero'ed 22 23 * mhl_mem_free(ptr) [INLINE-FUNC] 24 25 Free chunk @ptr (MUST be allocated w/ mhl_mem_alloc_*()), 26 passing NULL is graciously allowed 27 28 * mhl_mem_realloc(ptr,newsize) -> returns newptr 29 30 Re-allocates a heap chunk, just like realloc() 31 32 * MHL_PTR_FREE(ptr) [MACRO-ONLY] 33 34 like mhl_mem_free(), but with ptr as a variable that gets cleared 35 (use this as shortcut to "mhl_mem_free(foo); foo = NULL") 36 37 string.h: String helpers 38 39 * mhl_str_dup(const char*s) -> char* 40 41 [MACRO-FUNC] Safe version of strdup(), when NULL passed, returns strdup("") 42 43 * mhl_str_ndup(const char* s) -> char* 44 45 [MACRO-FUNC] Safe version of strndup(), when NULL passed, returns strdup("") 46 47 * mhl_str_trim(char* s) -> char* 48 49 [INLINE-FUNC] Trims the string (removing leading and trailing whitespacs), 50 WITHIN the passed buffer, returning the string s itself. 51 When NULL passed returns NULL. 52 53 * mhl_str_toupper(char* s) -> char* 54 55 [INLINE-FUNC] Converts the string in passed buffer to uppercase, returns that 56 buffer. When NULL passed returns NULL. 57 58 * mhl_str_concat_1(const char* base, const char* one) -> char* 59 60 [INLINE-FUNC] Concatenates the string one onto the string base and returns the 61 result in a newly allocated buffer (free it w/ mhl_mem_free()). 62 For NULL strings, "" is assumed. 63 64 * mhl_str_concat_2(const char* base,const char* one,const char* two) -> char* 65 mhl_str_concat_3(const char* base,const char* one,const char* two,const char* three) -> char* 66 mhl_str_concat_4(const char* base,const char* one,const char* two,const char* three,const char* four) -> char* 67 mhl_str_concat_5(const char* base,const char* one,const char* two,const char* three,const char* four,const char* five) -> char* 68 mhl_str_concat_6(const char* base,const char* one,const char* two,const char* three,const char* four,const char* five,const char* six) -> char* 69 mhl_str_concat_7(const char* base,const char* one,const char* two,const char* three,const char* four,const char* five,const char* six,const char* seven) -> char* 70 71 [INLINE-FUNC] Like str_concat_1() but adding more strings. 72 73 * mhl_str_reverse(char* str) -> char* 74 75 [INLINE-FUNC] Reverses the string in passed buffer and returns the buffer ptr itself. 76 If NULL is passed, returns NULL. 77 78 shell_escape.h: Shell-style string escaping 79 80 * mhl_shell_escape_toesc(char c) -> bool 81 82 [MACRO-FUNC] returns true when given char has to be escaped 83 84 * mhl_shell_escape_nottoesc(char c) -> bool 85 86 [MACRO-FUNC] opposite of mhl_shell_escape_toesc() 87 88 * mhl_shell_escape_dup(const char* s) -> char* 89 90 [INLINE-FUNC] escapes an string and returns the result in a malloc()'ed chunk 91 Passing NULL returns an empty malloc()ed string. 92 93 * mhl_shell_unescape_buf(char* s) -> char* 94 95 [INLINE-FUNC] unescapes the string into given buffer (changes buffer!) and 96 returns ptr to the buffer itself. When NULL passed returns NULL. 97 98 env.h: Environment variable helpers 99 100 * mhl_getenv_dup(const char* n) -> char* 101 102 [MACRO-FUNC] like getenv() but returns an strdup()'ed copy. When NULL passed, 103 returns strdup("") -
new file mhl/env.h
diff --git a/mhl/env.h b/mhl/env.h new file mode 100644 index 0000000..4d02554
- + 1 #ifndef __MHL_ENV_H 2 #define __MHL_ENV_H 3 4 #define mhl_getenv_dup(name) (mhl_str_dup(name ? getenv(name) : "")) 5 6 #endif -
new file mhl/memory.h
diff --git a/mhl/memory.h b/mhl/memory.h new file mode 100644 index 0000000..b006177
- + 1 #ifndef __MHL_MEM 2 #define __MHL_MEM 3 4 #include <memory.h> 5 #include <stdlib.h> 6 7 /* allocate a chunk of stack memory, uninitialized */ 8 #define mhl_mem_alloc_u(sz) (malloc(sz)) 9 10 /* allocate a chunk of stack memory, zeroed */ 11 #define mhl_mem_alloc_z(sz) (calloc(1,sz)) 12 13 /* free a chunk of memory from stack, passing NULL does no harm */ 14 static inline void mhl_mem_free(void* ptr) 15 { 16 if (ptr) free(ptr); 17 } 18 19 /* free an ptr and NULL it */ 20 #define MHL_PTR_FREE(ptr) do { mhl_mem_free(ptr); (ptr) = NULL; } while (0); 21 22 /* allocate a chunk on stack - automatically free'd on function exit */ 23 #define mhl_stack_alloc(sz) (alloca(sz)) 24 25 /* re-alloc memory chunk */ 26 #define mhl_mem_realloc(ptr,sz) (realloc(ptr,sz)) 27 28 #endif -
new file mhl/shell_escape.h
diff --git a/mhl/shell_escape.h b/mhl/shell_escape.h new file mode 100644 index 0000000..2533388
- + 1 #ifndef __MHL_SHELL_ESCAPE_H 2 #define __MHL_SHELL_ESCAPE_H 3 4 /* Micro helper library: shell escaping functions */ 5 6 #include <string.h> 7 #include <stdlib.h> 8 9 #define mhl_shell_escape_toesc(x) \ 10 (((x)==' ')||((x)=='!')||((x)=='#')||((x)=='$')||((x)=='%')|| \ 11 ((x)=='(')||((x)==')')||((x)=='\'')||((x)=='&')||((x)=='~')|| \ 12 ((x)=='{')||((x)=='}')||((x)=='[')||((x)==']')||((x)=='`')|| \ 13 ((x)=='?')||((x)=='|')||((x)=='<')||((x)=='>')||((x)==';')|| \ 14 ((x)=='*')||((x)=='\\')||((x)=='"')) 15 16 #define mhl_shell_escape_nottoesc(x) \ 17 (((x)!=0) && (!mhl_shell_escape_toesc((x)))) 18 19 static inline char* mhl_shell_escape_dup(const char* src) 20 { 21 if ((src==NULL)||(!(*src))) 22 return strdup(""); 23 24 char* buffer = calloc(1, strlen(src)*2+2); 25 char* ptr = buffer; 26 27 /* look for the first char to escape */ 28 while (1) 29 { 30 char c; 31 /* copy over all chars not to escape */ 32 while ((c=(*src)) && mhl_shell_escape_nottoesc(c)) 33 { 34 *ptr = c; 35 ptr++; 36 src++; 37 } 38 39 /* at this point we either have an \0 or an char to escape */ 40 if (!c) 41 return buffer; 42 43 *ptr = '\\'; 44 ptr++; 45 *ptr = c; 46 ptr++; 47 src++; 48 } 49 } 50 51 /* shell-unescape within a given buffer (writing to it!) */ 52 static inline char* mhl_shell_unescape_buf(char* text) 53 { 54 if (!text) 55 return NULL; 56 57 // look for the first \ - that's quick skipover if there's nothing to escape 58 char* readptr = text; 59 while ((*readptr) && ((*readptr)!='\\')) readptr++; 60 if (!(*readptr)) return text; 61 62 // if we're here, we're standing on the first '\' 63 char* writeptr = readptr; 64 char c; 65 while ((c = *readptr)) 66 { 67 if (c=='\\') 68 { 69 readptr++; 70 switch ((c = *readptr)) 71 { 72 case 'n': (*writeptr) = '\n'; writeptr++; break; 73 case 'r': (*writeptr) = '\r'; writeptr++; break; 74 case 't': (*writeptr) = '\t'; writeptr++; break; 75 76 case ' ': 77 case '\\': 78 case '#': 79 case '$': 80 case '%': 81 case '(': 82 case ')': 83 case '[': 84 case ']': 85 case '{': 86 case '}': 87 case '<': 88 case '>': 89 case '!': 90 case '*': 91 case '?': 92 case '~': 93 case '`': 94 case '"': 95 case ';': 96 default: 97 (*writeptr) = c; writeptr++; break; 98 } 99 } 100 else // got a normal character 101 { 102 (*writeptr) = *readptr; 103 writeptr++; 104 } 105 readptr++; 106 } 107 *writeptr = 0; 108 109 return text; 110 } 111 112 #endif -
new file mhl/strhash.h
diff --git a/mhl/strhash.h b/mhl/strhash.h new file mode 100644 index 0000000..76c14e8
- + 1 #ifndef __MHL_STRHASH_H 2 #define __MHL_STRHASH_H 3 4 #include <hash.h> 5 #include "../mhl/memory.h" 6 7 static void __mhl_strhash_free_key(void* ptr) 8 { 9 mhl_mem_free(ptr); 10 } 11 12 static void __mhl_strhash_free_dummy(void* ptr) 13 { 14 } 15 16 typedef hash MHL_STRHASH; 17 18 #define MHL_STRHASH_DECLARE(n) MHL_STRHASH n; 19 20 #define MHL_STRHASH_INIT(h) \ 21 hash_initialise(h, 997U, \ 22 hash_hash_string, \ 23 hash_compare_string, \ 24 hash_copy_string, \ 25 __mhl_strhash_free_key, \ 26 __mhl_strhash_free_dummy) 27 28 #define MHL_STRHASH_DECLARE_INIT(n) \ 29 MHL_STRHASH_DECLARE(n); \ 30 MHL_STRHASH_INIT(&n); 31 32 #define MHL_STRHASH_DEINIT(ht) \ 33 hash_deinitialise(ht) 34 35 static inline void mhl_strhash_addkey(MHL_STRHASH* ht, const char* key, void* value) 36 { 37 hash_insert(ht, (char*)key, value); 38 } 39 40 static inline void* mhl_strhash_lookup(MHL_STRHASH* ht, const char* key) 41 { 42 void* retptr; 43 if (hash_retrieve(ht, (char*)key, &retptr)) 44 return retptr; 45 else 46 return NULL; 47 } 48 49 #endif -
new file mhl/string.h
diff --git a/mhl/string.h b/mhl/string.h new file mode 100644 index 0000000..338e169
- + 1 #ifndef __MHL_STRING_H 2 #define __MHL_STRING_H 3 4 #include <ctype.h> 5 #include "../mhl/memory.h" 6 7 #define mhl_str_dup(str) ((str ? strdup(str) : strdup(""))) 8 #define mhl_str_ndup(str,len) ((str ? strndup(str,len) : strdup(""))) 9 #define mhl_str_len(str) ((str ? strlen(str) : 0)) 10 11 static inline char* mhl_str_trim(char* str) 12 { 13 if (!str) return NULL; // NULL string ?! bail out. 14 15 // find the first non-space 16 char* start; for (start=str; ((*str) && (!isspace(*str))); str++); 17 18 // only spaces ? 19 if (!(*str)) { *str = 0; return str; } 20 21 // get the size (cannot be empty - catched above) 22 int _sz = strlen(str); 23 24 // find the proper end 25 char* end; 26 for (end=(str+_sz-1); ((end>str) && (isspace(*end))); end--); 27 end[1] = 0; // terminate, just to be sure 28 29 // if we have no leading spaces, just trucate 30 if (start==str) { end++; *end = 0; return str; } 31 32 33 // if it' only one char, dont need memmove for that 34 if (start==end) { str[0]=*start; str[1]=0; return str; } 35 36 // by here we have a (non-empty) region between start end end 37 memmove(str,start,(end-start+1)); 38 return str; 39 } 40 41 static inline void mhl_str_toupper(char* str) 42 { 43 if (str) 44 for (;*str;str++) 45 *str = toupper(*str); 46 } 47 48 /* concat 1 string to another and return as mhl_mem_alloc()'ed string */ 49 static inline char* mhl_str_concat_1(const char* base, const char* one) 50 { 51 if (!base) return mhl_str_dup(one); 52 if (!one) return mhl_str_dup(base); 53 54 int _sz_base = strlen(base); 55 int _sz_one = strlen(one); 56 57 char* buf = mhl_mem_alloc_u(_sz_base+_sz_one+2); 58 memcpy(buf, base, _sz_base); 59 memcpy(buf+_sz_base, one, _sz_one); 60 buf[_sz_base+_sz_one] = 0; 61 return buf; 62 } 63 64 /* concat 2 strings to another and return as mhl_mem_alloc()'ed string */ 65 static inline char* mhl_str_concat_2(const char* base, const char* one, const char* two) 66 { 67 if (!base) base = ""; 68 if (!one) one = ""; 69 if (!two) two = ""; 70 71 int _sz_base = strlen(base); 72 int _sz_one = strlen(one); 73 int _sz_two = strlen(two); 74 75 char* buf = mhl_mem_alloc_u(_sz_base+_sz_one+_sz_two+2); 76 char* ptr = buf; 77 memcpy(ptr, base, _sz_base); ptr+=_sz_base; 78 memcpy(ptr, one, _sz_one); ptr+=_sz_one; 79 memcpy(ptr, two, _sz_two); ptr+=_sz_two; 80 *ptr = 0; 81 return buf; 82 } 83 84 /* concat 3 strings to another and return as mhl_mem_alloc()'ed string */ 85 static inline char* mhl_str_concat_3(const char* base, const char* one, const char* two, const char* three) 86 { 87 if (!base) base = ""; 88 if (!one) one = ""; 89 if (!two) two = ""; 90 if (!three) three = ""; 91 92 int _sz_base = strlen(base); 93 int _sz_one = strlen(one); 94 int _sz_two = strlen(two); 95 int _sz_three = strlen(three); 96 97 char* buf = mhl_mem_alloc_u(_sz_base+_sz_one+_sz_two+_sz_three+2); 98 char* ptr = buf; 99 memcpy(ptr, base, _sz_base); ptr+=_sz_base; 100 memcpy(ptr, one, _sz_one); ptr+=_sz_one; 101 memcpy(ptr, two, _sz_two); ptr+=_sz_two; 102 memcpy(ptr, three, _sz_three); ptr+=_sz_three; 103 *ptr = 0; 104 return buf; 105 } 106 107 /* concat 4 strings to another and return as mhl_mem_alloc()'ed string */ 108 static inline char* mhl_str_concat_4(const char* base, const char* one, const char* two, const char* three, const char* four) 109 { 110 if (!base) base = ""; 111 if (!one) one = ""; 112 if (!two) two = ""; 113 if (!three) three = ""; 114 if (!four) four = ""; 115 116 int _sz_base = strlen(base); 117 int _sz_one = strlen(one); 118 int _sz_two = strlen(two); 119 int _sz_three = strlen(three); 120 int _sz_four = strlen(four); 121 122 char* buf = mhl_mem_alloc_u(_sz_base+_sz_one+_sz_two+_sz_three+_sz_four+2); 123 char* ptr = buf; 124 memcpy(ptr, base, _sz_base); ptr+=_sz_base; 125 memcpy(ptr, one, _sz_one); ptr+=_sz_one; 126 memcpy(ptr, two, _sz_two); ptr+=_sz_two; 127 memcpy(ptr, three, _sz_three); ptr+=_sz_three; 128 memcpy(ptr, four, _sz_four); ptr+=_sz_four; 129 *ptr = 0; 130 return buf; 131 } 132 133 /* concat 5 strings to another and return as mhl_mem_alloc()'ed string */ 134 static inline char* mhl_str_concat_5(const char* base, const char* one, const char* two, const char* three, const char* four, const char* five) 135 { 136 if (!base) base = ""; 137 if (!one) one = ""; 138 if (!two) two = ""; 139 if (!three) three = ""; 140 if (!four) four = ""; 141 if (!five) five = ""; 142 143 int _sz_base = strlen(base); 144 int _sz_one = strlen(one); 145 int _sz_two = strlen(two); 146 int _sz_three = strlen(three); 147 int _sz_four = strlen(four); 148 int _sz_five = strlen(five); 149 150 char* buf = mhl_mem_alloc_u(_sz_base+_sz_one+_sz_two+_sz_three+_sz_four+_sz_five+2); 151 char* ptr = buf; 152 memcpy(ptr, base, _sz_base); ptr+=_sz_base; 153 memcpy(ptr, one, _sz_one); ptr+=_sz_one; 154 memcpy(ptr, two, _sz_two); ptr+=_sz_two; 155 memcpy(ptr, three, _sz_three); ptr+=_sz_three; 156 memcpy(ptr, four, _sz_four); ptr+=_sz_four; 157 memcpy(ptr, five, _sz_five); ptr+=_sz_five; 158 *ptr = 0; 159 return buf; 160 } 161 162 /* concat 6 strings to another and return as mhl_mem_alloc()'ed string */ 163 static inline char* mhl_str_concat_6(const char* base, const char* one, const char* two, 164 const char* three, const char* four, const char* five, const char* six) 165 { 166 if (!base) base = ""; 167 if (!one) one = ""; 168 if (!two) two = ""; 169 if (!three) three = ""; 170 if (!four) four = ""; 171 if (!five) five = ""; 172 if (!six) six = ""; 173 174 int _sz_base = strlen(base); 175 int _sz_one = strlen(one); 176 int _sz_two = strlen(two); 177 int _sz_three = strlen(three); 178 int _sz_four = strlen(four); 179 int _sz_five = strlen(five); 180 int _sz_six = strlen(six); 181 182 char* buf = mhl_mem_alloc_u(_sz_base+_sz_one+_sz_two+_sz_three+_sz_four+_sz_five+_sz_six+2); 183 char* ptr = buf; 184 memcpy(ptr, base, _sz_base); ptr+=_sz_base; 185 memcpy(ptr, one, _sz_one); ptr+=_sz_one; 186 memcpy(ptr, two, _sz_two); ptr+=_sz_two; 187 memcpy(ptr, three, _sz_three); ptr+=_sz_three; 188 memcpy(ptr, four, _sz_four); ptr+=_sz_four; 189 memcpy(ptr, five, _sz_five); ptr+=_sz_five; 190 memcpy(ptr, six, _sz_six); ptr+=_sz_six; 191 *ptr = 0; 192 return buf; 193 } 194 195 /* concat 7 strings to another and return as mhl_mem_alloc()'ed string */ 196 static inline char* mhl_str_concat_7(const char* base, const char* one, const char* two, 197 const char* three, const char* four, const char* five, const char* six, const char* seven) 198 { 199 if (!base) base = ""; 200 if (!one) one = ""; 201 if (!two) two = ""; 202 if (!three) three = ""; 203 if (!four) four = ""; 204 if (!five) five = ""; 205 if (!six) six = ""; 206 if (!seven) seven = ""; 207 208 int _sz_base = strlen(base); 209 int _sz_one = strlen(one); 210 int _sz_two = strlen(two); 211 int _sz_three = strlen(three); 212 int _sz_four = strlen(four); 213 int _sz_five = strlen(five); 214 int _sz_six = strlen(six); 215 int _sz_seven = strlen(seven); 216 217 char* buf = mhl_mem_alloc_u(_sz_base+_sz_one+_sz_two+_sz_three+_sz_four+_sz_five+_sz_six+_sz_seven+2); 218 char* ptr = buf; 219 memcpy(ptr, base, _sz_base); ptr+=_sz_base; 220 memcpy(ptr, one, _sz_one); ptr+=_sz_one; 221 memcpy(ptr, two, _sz_two); ptr+=_sz_two; 222 memcpy(ptr, three, _sz_three); ptr+=_sz_three; 223 memcpy(ptr, four, _sz_four); ptr+=_sz_four; 224 memcpy(ptr, five, _sz_five); ptr+=_sz_five; 225 memcpy(ptr, six, _sz_six); ptr+=_sz_six; 226 memcpy(ptr, seven, _sz_seven); ptr+=_sz_seven; 227 *ptr = 0; 228 return buf; 229 } 230 231 static inline char* mhl_str_reverse(char* ptr) 232 { 233 if (!ptr) return NULL; // missing string 234 if (!(ptr[0] && ptr[1])) return ptr; // empty or 1-ch string 235 236 int _sz = strlen(ptr); 237 char* start = ptr; 238 char* end = ptr+_sz-1; 239 240 while (start<end) 241 { 242 char c = *start; 243 *start = *end; 244 *end = c; 245 start++; 246 end--; 247 } 248 249 return ptr; 250 } 251 252 #endif