sstr.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. * Copyright (C) 2005 iptelorg GmbH
  3. *
  4. * This file is part of ser, a free SIP server.
  5. *
  6. * ser is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version
  10. *
  11. * For a license to use the ser software under conditions
  12. * other than those described here, or to purchase support for this
  13. * software, please contact iptel.org by e-mail at the following addresses:
  14. * [email protected]
  15. *
  16. * ser is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  24. */
  25. #ifndef __SIMPLE_STR_H
  26. #define __SIMPLE_STR_H
  27. #include <cds/memory.h>
  28. #ifdef __cplusplus
  29. extern "C" {
  30. #endif
  31. /* If compiled for SER, use ser internal strings ! */
  32. #ifdef SER
  33. #include "str.h"
  34. typedef str str_t;
  35. #else
  36. typedef struct {
  37. char *s;
  38. int len;
  39. } str_t;
  40. #define STR_STATIC_INIT(v) {(v), sizeof(v) - 1}
  41. #endif
  42. #define FMT_STR(str) (str).len,((str).s ? (str).s : "")
  43. #define str_len(ptr) ((ptr)?(ptr)->len:0)
  44. /** transalate zero-terminated string to str_t (both uses the input buffer!)*/
  45. str_t zt2str(char *str);
  46. /** returns 1 if the string is empty */
  47. int is_str_empty(const str_t *s);
  48. /** duplicate string into given destination (data region is newly allocated) */
  49. int str_dup_impl(str_t* dst, const str_t* src);
  50. int str_dup_dbg(str_t* dst, const str_t* src, const char *file, int line);
  51. /*#define str_dup(dst,src) str_dup_dbg(dst,src,__FILE__,__LINE__)*/
  52. #define str_dup(dst,src) str_dup_impl(dst,src)
  53. /** duplicate string into newly allocated destination (data and str structure are newly allocated) */
  54. str_t *str_dup_new(const str_t* src);
  55. /** duplicate zero-terminated string */
  56. int str_dup_zt(str_t* dst, const char* src);
  57. /** duplicate zero-terminated string to zero-terminated string */
  58. char *zt_strdup(const char*src);
  59. /** frees string content if allocated */
  60. /* void str_free_content(str_t *s); */
  61. #define str_free_content(str) do { if (str != NULL) { \
  62. if (((str)->len > 0) && ((str)->s)) cds_free((str)->s);\
  63. (str)->len = 0; \
  64. (str)->s = 0; \
  65. } } while (0)
  66. /** frees string content if allocated and then the string itself */
  67. /* void str_free(str_t *s); */
  68. #define str_free(str) do { if (str != NULL) { \
  69. if (((str)->len > 0) && ((str)->s)) cds_free((str)->s);\
  70. cds_free(str); \
  71. } } while (0)
  72. /* clears string content */
  73. #define str_clear(str) do { if (str != NULL) { \
  74. (str)->len = 0; \
  75. (str)->s = 0; \
  76. } } while (0)
  77. /** case sensitive comparation - returns 0 if equal, nonzero otherwise */
  78. int str_case_equals(const str_t *a, const str_t *b);
  79. /** case insensitive comparation - returns 0 if equal, nonzero otherwise */
  80. int str_nocase_equals(const str_t *a, const str_t *b);
  81. /** compare str_t and zero terminated string */
  82. int str_cmp_zt(const str_t *a, const char *b); /* renamed sz_cmp */
  83. /** is b prefix of a */
  84. int str_prefix(const str_t *a, const str_t *b); /* ss_start */
  85. /* #define ss_cmp(const str_t *a, const str_t *b) ((a->len == b->len)?sz_cmp(a, b->s):(-1)) */
  86. /* void str_clear(str_t *s); */
  87. /** locate character in string */
  88. char *str_strchr(const str_t *s, char c);
  89. /** locate string in string */
  90. char *str_str(const str_t *s, const str_t *search_for);
  91. /* creates new string as concatenation of a and b */
  92. int str_concat(str_t *dst, str_t *a, str_t *b);
  93. int replace_str(const str_t *src, str_t *dst, const str_t *sample, const str_t *value);
  94. /** Copies string into another one. The destination string buffer
  95. * MUST be allocated in needed size! */
  96. #define str_cpy(dst, src) do { \
  97. memcpy((dst)->s, (src)->s, (src)->len); \
  98. (dst)->len = (src)->len; \
  99. } while (0)
  100. /* pointer after given string - often used when strings
  101. * allocated together with data structure holding them */
  102. #define after_str_ptr(ss) ((ss)->s + (ss)->len)
  103. /*
  104. * Append a string app with length app_len
  105. * to the end of string str which is a str* pointer
  106. * the buffer must be large enough
  107. */
  108. #define str_append(str, app, app_len) \
  109. do { \
  110. memcpy((str)->s + (str)->len, (app), (app_len)); \
  111. (str)->len += (app_len); \
  112. } while(0)
  113. #ifdef __cplusplus
  114. }
  115. #endif
  116. #endif