Merge from vendor branch LESS:
[dragonfly.git] / contrib / libarchive-2 / libarchive / archive_string.h
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.9 2007/05/29 01:00:19 kientzle Exp $
26  *
27  */
28
29 #ifndef ARCHIVE_STRING_H_INCLUDED
30 #define ARCHIVE_STRING_H_INCLUDED
31
32 #include <stdarg.h>
33 #ifdef HAVE_STRING_H
34 #include <string.h>
35 #endif
36
37 /*
38  * Basic resizable/reusable string support a la Java's "StringBuffer."
39  *
40  * Unlike sbuf(9), the buffers here are fully reusable and track the
41  * length throughout.
42  *
43  * Note that all visible symbols here begin with "__archive" as they
44  * are internal symbols not intended for anyone outside of this library
45  * to see or use.
46  */
47
48 struct archive_string {
49         char    *s;  /* Pointer to the storage */
50         size_t   length; /* Length of 's' */
51         size_t   buffer_length; /* Length of malloc-ed storage */
52 };
53
54 /* Initialize an archive_string object on the stack or elsewhere. */
55 #define archive_string_init(a)  \
56         do { (a)->s = NULL; (a)->length = 0; (a)->buffer_length = 0; } while(0)
57
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
63 /* Append a char to an archive_string using UTF8. */
64 struct archive_string *
65 __archive_strappend_char_UTF8(struct archive_string *, int);
66 #define archive_strappend_char_UTF8 __archive_strappend_char_UTF8
67
68 /* Append an integer in the specified base (2 <= base <= 16). */
69 struct archive_string *
70 __archive_strappend_int(struct archive_string *as, int d, int base);
71 #define archive_strappend_int __archive_strappend_int
72
73 /* Basic append operation. */
74 struct archive_string *
75 __archive_string_append(struct archive_string *as, const char *p, size_t s);
76
77 /* Copy one archive_string to another */
78 void
79 __archive_string_copy(struct archive_string *dest, struct archive_string *src);
80 #define archive_string_copy(dest, src) \
81         __archive_string_copy(dest, src)
82
83 /* Ensure that the underlying buffer is at least as large as the request. */
84 struct archive_string *
85 __archive_string_ensure(struct archive_string *, size_t);
86 #define archive_string_ensure __archive_string_ensure
87
88 /* Append C string, which may lack trailing \0. */
89 struct archive_string *
90 __archive_strncat(struct archive_string *, const char *, size_t);
91 #define archive_strncat  __archive_strncat
92
93 /* Append a C string to an archive_string, resizing as necessary. */
94 #define archive_strcat(as,p) __archive_string_append((as),(p),strlen(p))
95
96 /* Copy a C string to an archive_string, resizing as necessary. */
97 #define archive_strcpy(as,p) \
98         ((as)->length = 0, __archive_string_append((as), (p), strlen(p)))
99
100 /* Copy a C string to an archive_string with limit, resizing as necessary. */
101 #define archive_strncpy(as,p,l) \
102         ((as)->length=0, archive_strncat((as), (p), (l)))
103
104 /* Return length of string. */
105 #define archive_strlen(a) ((a)->length)
106
107 /* Set string length to zero. */
108 #define archive_string_empty(a) ((a)->length = 0)
109
110 /* Release any allocated storage resources. */
111 void    __archive_string_free(struct archive_string *);
112 #define archive_string_free  __archive_string_free
113
114 /* Like 'vsprintf', but resizes the underlying string as necessary. */
115 void    __archive_string_vsprintf(struct archive_string *, const char *,
116             va_list);
117 #define archive_string_vsprintf __archive_string_vsprintf
118
119 #endif