Ticket #157: mhl.002.diff
File mhl.002.diff, 13.8 KB (added by slavazanko, 15 years ago) |
---|
-
configure.ac
diff --git a/configure.ac b/configure.ac index 0066dbf..957e958 100644
a b dnl Keep this check close to the beginning, so that the users 25 25 dnl without any glib won't have their time wasted by other checks. 26 26 dnl 27 27 28 AC_DEFINE(WITH_GLIB, 1, [Use Glib functions]) 29 30 dnl TODO: make option --with-glib like this: 31 dnl 32 dnl AC_ARG_WITH(glib, 33 dnl [AS_HELP_STRING([--with-glib], [Use glib (default is yes)])], 34 dnl [with_glib=$enableval], 35 dnl [with_glib=yes]) 36 dnl 37 dnl if test x${with_glib} = xyes; then 38 dnl dnl some checking stuff 39 dnl AC_DEFINE(WITH_GLIB, 1, [Use Glib functions]) 40 dnl fi 41 42 28 43 AC_ARG_WITH(glib12, 29 44 [ --with-glib12 Force using glib 1.2.x [[no]]]) 30 45 -
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..20db6c0
- + 1 #ifndef __MHL_MEM 2 #define __MHL_MEM 3 4 #if defined _AIX && !defined REGEX_MALLOC 5 #pragma alloca 6 #endif 7 8 #include <config.h> 9 #include <memory.h> 10 #include <stdlib.h> 11 12 #ifdef __GNUC__ 13 # define alloca __builtin_alloca 14 #else 15 # ifdef _MSC_VER 16 # include <malloc.h> 17 # define alloca _alloca 18 # else 19 # if HAVE_ALLOCA_H 20 # include <alloca.h> 21 # else 22 # ifdef _AIX 23 #pragma alloca 24 # else 25 # ifndef alloca /* predefined by HP cc +Olibcalls */ 26 char *alloca (); 27 # endif 28 # endif 29 # endif 30 # endif 31 #endif 32 33 #ifndef HAVE_ALLOCA 34 # include <stdlib.h> 35 # if !defined(STDC_HEADERS) && defined(HAVE_MALLOC_H) 36 # include <malloc.h> 37 # endif 38 # define alloca malloc 39 #endif 40 41 #ifdef WITH_GLIB 42 # include <glib.h> 43 44 # if GLIB_MAJOR_VERSION >= 2 45 /* allocate a chunk of stack memory, uninitialized */ 46 # define mhl_mem_alloc_u(sz) (g_try_malloc(sz)) 47 48 /* allocate a chunk of stack memory, zeroed */ 49 # define mhl_mem_alloc_z(sz) (g_try_malloc0(sz)) 50 51 /* re-alloc memory chunk */ 52 # define mhl_mem_realloc(ptr,sz) (g_try_realloc(ptr,sz)) 53 54 # else 55 56 /* allocate a chunk of stack memory, uninitialized */ 57 # define mhl_mem_alloc_u(sz) (g_malloc(sz)) 58 59 /* allocate a chunk of stack memory, zeroed */ 60 # define mhl_mem_alloc_z(sz) (g_malloc0(sz)) 61 62 /* re-alloc memory chunk */ 63 # define mhl_mem_realloc(ptr,sz) (g_realloc(ptr,sz)) 64 65 # endif 66 67 /* allocate a chunk on stack - automatically free'd on function exit */ 68 # define mhl_stack_alloc(sz) (g_alloca(sz)) 69 70 #else 71 72 /* allocate a chunk of stack memory, uninitialized */ 73 # define mhl_mem_alloc_u(sz) (malloc(sz)) 74 75 /* allocate a chunk of stack memory, zeroed */ 76 # define mhl_mem_alloc_z(sz) (calloc(1,sz)) 77 78 /* re-alloc memory chunk */ 79 # define mhl_mem_realloc(ptr,sz) (realloc(ptr,sz)) 80 81 /* allocate a chunk on stack - automatically free'd on function exit */ 82 # define mhl_stack_alloc(sz) (alloca(sz)) 83 84 #endif 85 86 /* free a chunk of memory from stack, passing NULL does no harm */ 87 static inline void mhl_mem_free(void* ptr) 88 { 89 if (ptr) 90 #ifdef WITH_GLIB 91 g_free(ptr); 92 #else 93 free(ptr); 94 #endif 95 } 96 97 /* free an ptr and NULL it */ 98 #define MHL_PTR_FREE(ptr) do { mhl_mem_free(ptr); (ptr) = NULL; } while (0); 99 100 #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..fa99a7b
- + 1 #ifndef __MHL_STRING_H 2 #define __MHL_STRING_H 3 4 #include <ctype.h> 5 #include <stdarg.h> 6 #include "../mhl/memory.h" 7 8 #define mhl_str_dup(str) ((str ? strdup(str) : strdup(""))) 9 #define mhl_str_ndup(str,len) ((str ? strndup(str,len) : strdup(""))) 10 #define mhl_str_len(str) ((str ? strlen(str) : 0)) 11 12 static inline char* mhl_str_trim(char* str) 13 { 14 if (!str) return NULL; // NULL string ?! bail out. 15 16 // find the first non-space 17 char* start; for (start=str; ((*str) && (!isspace(*str))); str++); 18 19 // only spaces ? 20 if (!(*str)) { *str = 0; return str; } 21 22 // get the size (cannot be empty - catched above) 23 size_t _sz = strlen(str); 24 25 // find the proper end 26 char* end; 27 for (end=(str+_sz-1); ((end>str) && (isspace(*end))); end--); 28 end[1] = 0; // terminate, just to be sure 29 30 // if we have no leading spaces, just trucate 31 if (start==str) { end++; *end = 0; return str; } 32 33 34 // if it' only one char, dont need memmove for that 35 if (start==end) { str[0]=*start; str[1]=0; return str; } 36 37 // by here we have a (non-empty) region between start end end 38 memmove(str,start,(end-start+1)); 39 return str; 40 } 41 42 static inline void mhl_str_toupper(char* str) 43 { 44 if (str) 45 for (;*str;str++) 46 *str = toupper(*str); 47 } 48 49 #define __STR_CONCAT_MAX 32 50 /* _NEVER_ call this function directly ! */ 51 static inline char* __mhl_str_concat_hlp(const char* base, ...) 52 { 53 static const char* arg_ptr[__STR_CONCAT_MAX]; 54 static size_t arg_sz[__STR_CONCAT_MAX]; 55 int count = 0; 56 size_t totalsize = 0; 57 58 // first pass: scan through the params and count string sizes 59 va_list par; 60 61 if (base) 62 { 63 arg_ptr[0] = base; 64 arg_sz[0] = totalsize = strlen(base); 65 count = 1; 66 } 67 68 va_list args; 69 va_start(args,base); 70 char* a; 71 // note: we use ((char*)(1)) as terminator - NULL is a valid argument ! 72 while ((a = va_arg(args, char*))!=(char*)1) 73 { 74 // printf("a=%u\n", a); 75 if (a) 76 { 77 arg_ptr[count] = a; 78 arg_sz[count] = strlen(a); 79 totalsize += arg_sz[count]; 80 count++; 81 } 82 } 83 84 if (!count) 85 return mhl_str_dup(""); 86 87 // now as we know how much to copy, allocate the buffer 88 char* buffer = (char*)mhl_mem_alloc_u(totalsize+2); 89 char* current = buffer; 90 int x=0; 91 for (x=0; x<count; x++) 92 { 93 memcpy(current, arg_ptr[x], arg_sz[x]); 94 current += arg_sz[x]; 95 } 96 97 *current = 0; 98 return buffer; 99 } 100 101 #define mhl_str_concat(...) (__mhl_str_concat_hlp(__VA_ARGS__, (char*)(1))) 102 103 static inline char* mhl_str_reverse(char* ptr) 104 { 105 if (!ptr) return NULL; // missing string 106 if (!(ptr[0] && ptr[1])) return ptr; // empty or 1-ch string 107 108 size_t _sz = strlen(ptr); 109 char* start = ptr; 110 char* end = ptr+_sz-1; 111 112 while (start<end) 113 { 114 char c = *start; 115 *start = *end; 116 *end = c; 117 start++; 118 end--; 119 } 120 121 return ptr; 122 } 123 124 #endif