Sun, 13 Jan 2013 14:16:45 +0100
server can change uid
/* * File: sstring.c * Author: olaf * * Created on 17. Juni 2010, 13:27 */ #include <stdlib.h> #include <strings.h> #include <stdarg.h> #include "string.h" sstr_t sstr (char *s) { sstr_t string; string.ptr = s; string.length = strlen(s); return string; } sstr_t sstrn (char *s, size_t n) { sstr_t string; string.ptr = s; string.length = n; return string; } size_t sstrnlen (size_t n, sstr_t s, ...) { va_list ap; size_t size = s.length; va_start(ap, s); for (int i=0;i<n-1;i++) { sstr_t str = va_arg(ap, sstr_t); size += str.length; } va_end(ap); return size; } sstr_t sstrcat (sstr_t s, ...) { va_list ap; va_start(ap, s); s.ptr[0] = 0; sstr_t str = va_arg (ap, sstr_t); while (str.ptr != NULL) { s.ptr = strncat (s.ptr, str.ptr, s.length); str = va_arg (ap, sstr_t); } va_end(ap); return s; } sstr_t sstrncat (size_t n, sstr_t s, sstr_t c1, ...) { va_list ap; va_start(ap, c1); s.ptr[0] = 0; size_t len = s.length; size_t cplen = c1.length > len ? len : c1.length; char *ptr = s.ptr; memcpy(ptr, c1.ptr, cplen); len -= cplen; ptr += cplen; for (int i=0;i<n-1;i++) { sstr_t str = va_arg (ap, sstr_t); cplen = str.length > len ? len : str.length; if(cplen <= 0) { va_end(ap); return s; } memcpy(ptr, str.ptr, cplen); len -= cplen; ptr += cplen; } va_end(ap); return s; } sstr_t sstrsubs (sstr_t s, size_t start) { return sstrsubsl (s, start, s.length-start); } sstr_t sstrsubsl (sstr_t s, size_t start, size_t length) { sstr_t new_sstr; if (start < 0 || start >= s.length || length < 0) { return s; } if (length > s.length-start) { length = s.length-start; } new_sstr.ptr = &s.ptr[start]; new_sstr.length = length; return new_sstr; } sstr_t* sstrsplit(sstr_t s, sstr_t d, size_t *n) { if (d.length == 0) { return NULL; } sstr_t* result; size_t nmax = *n; *n = 1; /* special case: exact match - no processing needed */ if (s.length == d.length && strncmp(s.ptr, d.ptr, s.length) == 0) { result = malloc(sizeof(sstr_t)); result[0] = sstrn("", 0); return result; } sstr_t sv = sstrdup(s); for (int i = 0 ; i < s.length ; i++) { if (sv.ptr[i] == d.ptr[0]) { _Bool match = 1; for (int j = 1 ; j < d.length ; j++) { if (j+i < s.length) { match &= (sv.ptr[i+j] == d.ptr[j]); } else { match = 0; break; } } if (match) { (*n)++; for (int j = 0 ; j < d.length ; j++) { sv.ptr[i+j] = 0; } i += d.length; } } if ((*n) == nmax) break; } result = malloc(sizeof(sstr_t) * (*n)); char *pptr = sv.ptr; for (int i = 0 ; i < *n ; i++) { size_t l = strlen(pptr); char* ptr = malloc(l + 1); memcpy(ptr, pptr, l); ptr[l] = 0; result[i] = sstrn(ptr, l); pptr += l + d.length; } free(sv.ptr); return result; } int sstrcmp(sstr_t s1, sstr_t s2) { return strncmp(s1.ptr, s2.ptr, s1.length>s2.length ? s2.length: s1.length); } sstr_t sstrdup(sstr_t s) { sstr_t newstring; newstring.ptr = (char*) malloc(s.length + 1); if (newstring.ptr != NULL) { newstring.length = s.length; newstring.ptr[newstring.length] = 0; memcpy(newstring.ptr, s.ptr, s.length); } return newstring; } // webserver extension sstr_t sstrtrim(sstr_t string) { sstr_t newstr = string; int nsoff = 0; int l = 1; for(int i=0;i<string.length;i++) { char c = string.ptr[i]; if(l) { /* leading whitespace */ if(c > 32) { l = 0; nsoff = i; newstr.ptr = &string.ptr[i]; newstr.length = string.length - nsoff; } } else { /* trailing whitespace */ if(c > 32) { newstr.length = (i - nsoff) + 1; } } } return newstr; } sstr_t sstrdup_mp(UcxMempool *pool, sstr_t s) { sstr_t newstring; newstring.ptr = (char*)ucx_mempool_malloc(pool, s.length + 1); if (newstring.ptr != NULL) { newstring.length = s.length; newstring.ptr[newstring.length] = 0; memcpy(newstring.ptr, s.ptr, s.length); } return newstring; } sstr_t sstrdup_pool(pool_handle_t *pool, sstr_t s) { sstr_t newstring; newstring.ptr = (char*)pool_malloc(pool, s.length + 1); if (newstring.ptr != NULL) { newstring.length = s.length; newstring.ptr[newstring.length] = 0; memcpy(newstring.ptr, s.ptr, s.length); } return newstring; }