Import of libarchive and bsdtar 1.3.1
[dragonfly.git] / contrib / libarchive-1.3.1 / libarchive / archive_string.h
1 /*-
2  * Copyright (c) 2003-2004 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  *    in this position and unchanged.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  *
26  * $FreeBSD: src/lib/libarchive/archive_string.h,v 1.6 2005/01/16 22:13:51 kientzle Exp $
27  *
28  */
29
30 #ifndef ARCHIVE_STRING_H_INCLUDED
31 #define ARCHIVE_STRING_H_INCLUDED
32
33 #include <stdarg.h>
34 #include <string.h>
35
36 /*
37  * Basic resizable/reusable string support a la Java's "StringBuffer."
38  *
39  * Unlike sbuf(9), the buffers here are fully reusable and track the
40  * length throughout.
41  *
42  * Note that all visible symbols here begin with "__archive" as they
43  * are internal symbols not intended for anyone outside of this library
44  * to see or use.
45  */
46
47 struct archive_string {
48         char    *s;  /* Pointer to the storage */
49         size_t   length; /* Length of 's' */
50         size_t   buffer_length; /* Length of malloc-ed storage */
51 };
52
53 /* Initialize an archive_string object on the stack or elsewhere. */
54 #define archive_string_init(a)  \
55         do { (a)->s = NULL; (a)->length = 0; (a)->buffer_length = 0; } while(0)
56
57 /* Append a C char to an archive_string, resizing as necessary. */
58 struct archive_string *
59 __archive_strappend_char(struct archive_string *, char);
60 #define archive_strappend_char __archive_strappend_char
61
62 /* Append a char to an archive_string using UTF8. */
63 struct archive_string *
64 __archive_strappend_char_UTF8(struct archive_string *, int);
65 #define archive_strappend_char_UTF8 __archive_strappend_char_UTF8
66
67 /* Append an integer in the specified base (2 <= base <= 16). */
68 struct archive_string *
69 __archive_strappend_int(struct archive_string *as, int d, int base);
70 #define archive_strappend_int __archive_strappend_int
71
72 /* Basic append operation. */
73 struct archive_string *
74 __archive_string_append(struct archive_string *as, const char *p, size_t s);
75
76 /* Ensure that the underlying buffer is at least as large as the request. */
77 struct archive_string *
78 __archive_string_ensure(struct archive_string *, size_t);
79 #define archive_string_ensure __archive_string_ensure
80
81 /* Append C string, which may lack trailing \0. */
82 struct archive_string *
83 __archive_strncat(struct archive_string *, const char *, size_t);
84 #define archive_strncat  __archive_strncat
85
86 /* Append a C string to an archive_string, resizing as necessary. */
87 #define archive_strcat(as,p) __archive_string_append((as),(p),strlen(p))
88
89 /* Copy a C string to an archive_string, resizing as necessary. */
90 #define archive_strcpy(as,p) \
91         ((as)->length = 0, __archive_string_append((as), (p), strlen(p)))
92
93 /* Copy a C string to an archive_string with limit, resizing as necessary. */
94 #define archive_strncpy(as,p,l) \
95         ((as)->length=0, archive_strncat((as), (p), (l)))
96
97 /* Return length of string. */
98 #define archive_strlen(a) ((a)->length)
99
100 /* Set string length to zero. */
101 #define archive_string_empty(a) ((a)->length = 0)
102
103 /* Release any allocated storage resources. */
104 void    __archive_string_free(struct archive_string *);
105 #define archive_string_free  __archive_string_free
106
107 /* Like 'vsprintf', but resizes the underlying string as necessary. */
108 void    __archive_string_vsprintf(struct archive_string *, const char *,
109             va_list);
110 #define archive_string_vsprintf __archive_string_vsprintf
111
112 #endif