olaf@20: /* olaf@20: * File: sstring.h olaf@20: * Author: olaf olaf@20: * olaf@20: * Created on 17. Juni 2010, 13:26 olaf@20: */ olaf@20: olaf@20: #ifndef _SSTRING_H olaf@20: #define _SSTRING_H olaf@20: universe@69: #include "ucx.h" universe@38: #include universe@38: universe@42: /* use macros for literals only */ universe@74: #define S(s) { (char*)s, sizeof(s)-1 } universe@74: #define ST(s) sstrn((char*)s, sizeof(s)-1) olaf@20: olaf@20: #ifdef __cplusplus olaf@20: extern "C" { olaf@20: #endif olaf@20: olaf@20: typedef struct sstring { olaf@20: char *ptr; olaf@20: size_t length; olaf@20: } sstr_t; olaf@20: olaf@20: /* olaf@20: * creates a new sstr_t from a null terminated string olaf@20: * olaf@20: * s null terminated string olaf@20: */ olaf@68: sstr_t sstr(char *s); olaf@20: olaf@20: /* olaf@20: * creates a new sstr_t from a string and length olaf@20: * olaf@20: * s string olaf@20: * n length of string olaf@20: */ olaf@68: sstr_t sstrn(char *s, size_t n); olaf@20: olaf@20: olaf@20: /* olaf@20: * gets the length of n sstr_t strings olaf@20: * olaf@20: * n number of strings olaf@20: * s string olaf@20: * ... strings olaf@20: */ olaf@68: size_t sstrnlen(size_t n, sstr_t s, ...); olaf@20: olaf@20: olaf@20: /* olaf@20: * concatenates n strings olaf@20: * olaf@20: * n number of strings olaf@20: * s new string with enough memory allocated olaf@20: * ... strings olaf@20: */ olaf@68: sstr_t sstrncat(size_t n, sstr_t s, sstr_t c1, ...); olaf@20: olaf@20: olaf@20: /* olaf@20: * olaf@20: */ olaf@68: sstr_t sstrsubs(sstr_t s, size_t start); olaf@20: olaf@20: /* olaf@20: * olaf@20: */ olaf@68: sstr_t sstrsubsl(sstr_t s, size_t start, size_t length); olaf@20: universe@39: /* universe@39: * splits s into n parts universe@39: * universe@39: * s the string to split universe@39: * d the delimiter string universe@39: * n the maximum size of the resulting list universe@39: * a size of 0 indicates an unbounded list size universe@39: * the actual size of the list will be stored here universe@39: * universe@39: * Hint: use this value to avoid dynamic reallocation of the result list universe@39: * universe@39: * Returns a list of the split strings universe@39: * NOTE: this list needs to be freed manually after usage universe@39: * universe@39: * Returns NULL on error universe@39: */ olaf@68: sstr_t* sstrsplit(sstr_t s, sstr_t d, size_t *n); olaf@20: olaf@68: int sstrcmp(sstr_t s1, sstr_t s2); olaf@20: olaf@68: sstr_t sstrdup(sstr_t s); olaf@20: olaf@96: sstr_t sstrtrim(sstr_t string); olaf@96: olaf@20: #ifdef __cplusplus olaf@20: } olaf@20: #endif olaf@20: olaf@20: #endif /* _SSTRING_H */