sstr.h

00001 /* 
00002  * Copyright (C) 2005 iptelorg GmbH
00003  *
00004  * This file is part of ser, a free SIP server.
00005  *
00006  * ser is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; either version 2 of the License, or
00009  * (at your option) any later version
00010  *
00011  * For a license to use the ser software under conditions
00012  * other than those described here, or to purchase support for this
00013  * software, please contact iptel.org by e-mail at the following addresses:
00014  *    info@iptel.org
00015  *
00016  * ser is distributed in the hope that it will be useful,
00017  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019  * GNU General Public License for more details.
00020  *
00021  * You should have received a copy of the GNU General Public License
00022  * along with this program; if not, write to the Free Software
00023  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00024  */
00025 
00026 #ifndef __SIMPLE_STR_H
00027 #define __SIMPLE_STR_H
00028 
00029 #include <cds/memory.h>
00030 
00031 #ifdef __cplusplus
00032 extern "C" {
00033 #endif
00034 
00035 /* If compiled for SER, use ser internal strings ! */
00036 #ifdef SER
00037 
00038 #include "str.h"        
00039 typedef str str_t;
00040 
00041 #else
00042 
00043 typedef struct {
00044         char *s;
00045         int len;
00046 } str_t;
00047 
00048 #define STR_STATIC_INIT(v) {(v), sizeof(v) - 1}
00049 
00050 #endif
00051 
00052 #define FMT_STR(str)    (str).len,((str).s ? (str).s : "")      
00053 
00054 #define str_len(ptr)    ((ptr)?(ptr)->len:0)
00055 
00057 str_t zt2str(char *str);
00058 
00060 int is_str_empty(const str_t *s);
00061 
00063 int str_dup_impl(str_t* dst, const str_t* src);
00064 int str_dup_dbg(str_t* dst, const str_t* src, const char *file, int line);
00065 /*#define str_dup(dst,src)      str_dup_dbg(dst,src,__FILE__,__LINE__)*/
00066 #define str_dup(dst,src)        str_dup_impl(dst,src)
00067 
00068 
00070 str_t *str_dup_new(const str_t* src);
00071 
00073 int str_dup_zt(str_t* dst, const char* src);
00074 
00076 char *zt_strdup(const char*src);
00077 
00079 /* void str_free_content(str_t *s); */
00080 #define str_free_content(str)   do { if (str != NULL) { \
00081                 if (((str)->len > 0) && ((str)->s)) cds_free((str)->s);\
00082                 (str)->len = 0; \
00083                 (str)->s = 0; \
00084         } } while (0)
00085 
00087 /* void str_free(str_t *s); */
00088 #define str_free(str)   do { if (str != NULL) { \
00089                 if (((str)->len > 0) && ((str)->s)) cds_free((str)->s);\
00090                 cds_free(str); \
00091         } } while (0)
00092 
00093 /* clears string content */
00094 #define str_clear(str)  do { if (str != NULL) { \
00095                 (str)->len = 0; \
00096                 (str)->s = 0; \
00097         } } while (0)
00098 
00099 
00101 int str_case_equals(const str_t *a, const str_t *b);
00103 int str_nocase_equals(const str_t *a, const str_t *b);
00104 
00106 int str_cmp_zt(const str_t *a, const char *b); /* renamed sz_cmp */
00107 
00109 int str_prefix(const str_t *a, const str_t *b); /* ss_start */
00110 
00111 /* #define ss_cmp(const str_t *a, const str_t *b) ((a->len == b->len)?sz_cmp(a, b->s):(-1)) */
00112 
00113 /* void str_clear(str_t *s); */
00114 
00116 char *str_strchr(const str_t *s, char c);
00117 
00119 char *str_str(const str_t *s, const str_t *search_for);
00120 
00121 /* creates new string as concatenation of a and b */
00122 int str_concat(str_t *dst, str_t *a, str_t *b);
00123 
00124 int replace_str(const str_t *src, str_t *dst, const str_t *sample, const str_t *value);
00125 
00128 #define str_cpy(dst, src) do { \
00129         memcpy((dst)->s, (src)->s, (src)->len); \
00130         (dst)->len = (src)->len; \
00131         } while (0)
00132 
00133 /* pointer after given string - often used when strings
00134  * allocated together with data structure holding them */
00135 #define after_str_ptr(ss)       ((ss)->s + (ss)->len)
00136 
00137 /*
00138  * Append a string app with length app_len
00139  * to the end of string str which is a str* pointer
00140  * the buffer must be large enough
00141  */
00142 #define str_append(str, app, app_len)                    \
00143     do {                                                 \
00144         memcpy((str)->s + (str)->len, (app), (app_len)); \
00145         (str)->len += (app_len);                         \
00146     } while(0)
00147 
00148 #ifdef __cplusplus
00149 }
00150 #endif
00151 
00152 #endif