archive_string.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*-
  2. * Copyright (c) 2003-2007 Tim Kientzle
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
  15. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  16. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  17. * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
  18. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  19. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  20. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  21. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  23. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. *
  25. * $FreeBSD: src/lib/libarchive/archive_string.h,v 1.13 2008/12/06 05:56:43 kientzle Exp $
  26. *
  27. */
  28. #ifndef ARCHIVE_STRING_H_INCLUDED
  29. #define ARCHIVE_STRING_H_INCLUDED
  30. #include <stdarg.h>
  31. #ifdef HAVE_STDLIB_H
  32. #include <stdlib.h> /* required for wchar_t on some systems */
  33. #endif
  34. #ifdef HAVE_STRING_H
  35. #include <string.h>
  36. #endif
  37. #ifdef HAVE_WCHAR_H
  38. #include <wchar.h>
  39. #endif
  40. /*
  41. * Basic resizable/reusable string support a la Java's "StringBuffer."
  42. *
  43. * Unlike sbuf(9), the buffers here are fully reusable and track the
  44. * length throughout.
  45. *
  46. * Note that all visible symbols here begin with "__archive" as they
  47. * are internal symbols not intended for anyone outside of this library
  48. * to see or use.
  49. */
  50. struct archive_string {
  51. char *s; /* Pointer to the storage */
  52. size_t length; /* Length of 's' */
  53. size_t buffer_length; /* Length of malloc-ed storage */
  54. };
  55. /* Initialize an archive_string object on the stack or elsewhere. */
  56. #define archive_string_init(a) \
  57. do { (a)->s = NULL; (a)->length = 0; (a)->buffer_length = 0; } while(0)
  58. /* Append a C char to an archive_string, resizing as necessary. */
  59. struct archive_string *
  60. __archive_strappend_char(struct archive_string *, char);
  61. #define archive_strappend_char __archive_strappend_char
  62. /* Convert a wide-char string to UTF-8 and append the result. */
  63. struct archive_string *
  64. __archive_strappend_w_utf8(struct archive_string *, const wchar_t *);
  65. #define archive_strappend_w_utf8 __archive_strappend_w_utf8
  66. /* Convert a wide-char string to current locale and append the result. */
  67. /* Returns NULL if conversion fails. */
  68. struct archive_string *
  69. __archive_strappend_w_mbs(struct archive_string *, const wchar_t *);
  70. #define archive_strappend_w_mbs __archive_strappend_w_mbs
  71. /* Basic append operation. */
  72. struct archive_string *
  73. __archive_string_append(struct archive_string *as, const char *p, size_t s);
  74. /* Copy one archive_string to another */
  75. void
  76. __archive_string_copy(struct archive_string *dest, struct archive_string *src);
  77. #define archive_string_copy(dest, src) \
  78. __archive_string_copy(dest, src)
  79. /* Concatenate one archive_string to another */
  80. void
  81. __archive_string_concat(struct archive_string *dest, struct archive_string *src);
  82. #define archive_string_concat(dest, src) \
  83. __archive_string_concat(dest, src)
  84. /* Ensure that the underlying buffer is at least as large as the request. */
  85. struct archive_string *
  86. __archive_string_ensure(struct archive_string *, size_t);
  87. #define archive_string_ensure __archive_string_ensure
  88. /* Append C string, which may lack trailing \0. */
  89. /* The source is declared void * here because this gets used with
  90. * "signed char *", "unsigned char *" and "char *" arguments.
  91. * Declaring it "char *" as with some of the other functions just
  92. * leads to a lot of extra casts. */
  93. struct archive_string *
  94. __archive_strncat(struct archive_string *, const void *, size_t);
  95. #define archive_strncat __archive_strncat
  96. /* Append a C string to an archive_string, resizing as necessary. */
  97. #define archive_strcat(as,p) __archive_string_append((as),(p),strlen(p))
  98. /* Copy a C string to an archive_string, resizing as necessary. */
  99. #define archive_strcpy(as,p) \
  100. ((as)->length = 0, __archive_string_append((as), (p), p == NULL ? 0 : strlen(p)))
  101. /* Copy a C string to an archive_string with limit, resizing as necessary. */
  102. #define archive_strncpy(as,p,l) \
  103. ((as)->length=0, archive_strncat((as), (p), (l)))
  104. /* Return length of string. */
  105. #define archive_strlen(a) ((a)->length)
  106. /* Set string length to zero. */
  107. #define archive_string_empty(a) ((a)->length = 0)
  108. /* Release any allocated storage resources. */
  109. void __archive_string_free(struct archive_string *);
  110. #define archive_string_free __archive_string_free
  111. /* Like 'vsprintf', but resizes the underlying string as necessary. */
  112. void __archive_string_vsprintf(struct archive_string *, const char *,
  113. va_list);
  114. #define archive_string_vsprintf __archive_string_vsprintf
  115. void __archive_string_sprintf(struct archive_string *, const char *, ...);
  116. #define archive_string_sprintf __archive_string_sprintf
  117. /* Allocates a fresh buffer and converts as (assumed to be UTF-8) into it.
  118. * Returns NULL if conversion failed in any way. */
  119. wchar_t *__archive_string_utf8_w(struct archive_string *as);
  120. #endif