2
0

str.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. * Copyright (C) 2001-2003 FhG Fokus
  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 str_h
  26. #define str_h
  27. /** @defgroup str_string Counted-Length Strings
  28. * @{
  29. *
  30. * Implementation of counted-length strings. In SER and its modules, strings
  31. * are often stored in the ::str structure. In addition to the pointer
  32. * pointing to the first character of the string, the structure also contains
  33. * the length of the string.
  34. *
  35. * @section motivation Motivation
  36. * Storing the length of the string together with the pointer to the string
  37. * has two advantages. First, it makes many string operations faster because
  38. * it is not necessary to count the number of characters at runtime. Second,
  39. * the pointer can point to arbitrary substrings within a SIP message (which
  40. * itself is stored as one long string spanning the whole message) without the
  41. * need to make a zero-terminated copy of it.
  42. *
  43. * @section drawbacks Drawbacks
  44. * Note well that the fact that string stored
  45. * using this data structure are not zero terminated makes them a little
  46. * incovenient to use with many standard libc string functions, because these
  47. * usually expect the input to be zero-terminated. In this case you have to
  48. * either make a zero-terminated copy or inject the terminating zero behind
  49. * the actuall string (if possible). Note that injecting a zero terminating
  50. * characters is considered to be dangerous.
  51. */
  52. /** @file
  53. * This header field defines the ::str data structure that is used across
  54. * SER sources to store counted-length strings. The file also defines several
  55. * convenience macros.
  56. */
  57. /** Data structure used across SER sources to store counted-length strings.
  58. * This is the data structure that is used to store counted-length
  59. * strings in SER core and modules.
  60. */
  61. struct _str{
  62. char* s; /**< Pointer to the first character of the string */
  63. int len; /**< Length of the string */
  64. };
  65. /** Data structure used across SER sources to store counted-length strings.
  66. * @see _str
  67. */
  68. typedef struct _str str;
  69. /** Initializes static ::str string with string literal.
  70. * This is a convenience macro that can be used to initialize
  71. * static ::str strings with string literals like this:
  72. * \code static str var = STR_STATIC_INIT("some_string"); \endcode
  73. * @param v is a string literal
  74. * @sa STR_NULL
  75. */
  76. #define STR_STATIC_INIT(v) {(v), sizeof(v) - 1}
  77. /* kamailio compatibility macro (same thing as above) */
  78. #define str_init(v) STR_STATIC_INIT(v)
  79. /** Initializes ::str string with NULL pointer and zero length.
  80. * This is a convenience macro that can be used to initialize
  81. * ::str string variable to NULL string with zero length:
  82. * \code str var = STR_NULL; \endcode
  83. * @sa STR_STATIC_INIT
  84. */
  85. #define STR_NULL {0, 0}
  86. /** Formats ::str string for use in printf-like functions.
  87. * This is a macro that prepares a ::str string for use in functions which
  88. * use printf-like formatting strings. This macro is necessary because
  89. * ::str strings do not have to be zero-terminated and thus it is necessary
  90. * to provide printf-like functions with the number of characters in the
  91. * string manually. Here is an example how to use the macro:
  92. * \code printf("%.*s\n", STR_FMT(var));\endcode Note well that the correct
  93. * sequence in the formatting string is %.*, see the man page of printf for
  94. * more details.
  95. */
  96. #define STR_FMT(_pstr_) \
  97. ((_pstr_ != (str *)0) ? (_pstr_)->len : 0), \
  98. ((_pstr_ != (str *)0) ? (_pstr_)->s : "")
  99. /** Compares two ::str strings.
  100. * This macro implements comparison of two strings represented using ::str
  101. * structures. First it compares the lengths of both string and if and only
  102. * if they are same then both strings are compared using memcmp.
  103. * @param x is first string to be compared
  104. * @param y is second string to be compared
  105. * @return 1 if strings are same, 0 otherwise
  106. */
  107. #define STR_EQ(x,y) (((x).len == (y).len) && \
  108. (memcmp((x).s, (y).s, (x).len) == 0))
  109. /** @} */
  110. /** Appends a sufffix
  111. * @param orig is the original string
  112. * @param suffix is the suffix string
  113. * @param dest is the result ::str of appending suffix to orig
  114. * @return 0 if ok -1 if error
  115. * remember to free the dest->s private memory
  116. */
  117. int str_append(str *orig, str *suffix, str *dest);
  118. #endif