]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - lib/libarchive/archive_string.h
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.git] / lib / 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$
26  *
27  */
28
29 #ifndef ARCHIVE_STRING_H_INCLUDED
30 #define ARCHIVE_STRING_H_INCLUDED
31
32 #include <stdarg.h>
33 #ifdef HAVE_STDLIB_H
34 #include <stdlib.h>  /* required for wchar_t on some systems */
35 #endif
36 #ifdef HAVE_STRING_H
37 #include <string.h>
38 #endif
39 #ifdef HAVE_WCHAR_H
40 #include <wchar.h>
41 #endif
42
43 /*
44  * Basic resizable/reusable string support a la Java's "StringBuffer."
45  *
46  * Unlike sbuf(9), the buffers here are fully reusable and track the
47  * length throughout.
48  *
49  * Note that all visible symbols here begin with "__archive" as they
50  * are internal symbols not intended for anyone outside of this library
51  * to see or use.
52  */
53
54 struct archive_string {
55         char    *s;  /* Pointer to the storage */
56         size_t   length; /* Length of 's' */
57         size_t   buffer_length; /* Length of malloc-ed storage */
58 };
59
60 /* Initialize an archive_string object on the stack or elsewhere. */
61 #define archive_string_init(a)  \
62         do { (a)->s = NULL; (a)->length = 0; (a)->buffer_length = 0; } while(0)
63
64 /* Append a C char to an archive_string, resizing as necessary. */
65 struct archive_string *
66 __archive_strappend_char(struct archive_string *, char);
67 #define archive_strappend_char __archive_strappend_char
68
69 /* Append an integer in the specified base (2 <= base <= 16). */
70 struct archive_string *
71 __archive_strappend_int(struct archive_string *as, int d, int base);
72 #define archive_strappend_int __archive_strappend_int
73
74 /* Convert a wide-char string to UTF-8 and append the result. */
75 struct archive_string *
76 __archive_strappend_w_utf8(struct archive_string *, const wchar_t *);
77 #define archive_strappend_w_utf8        __archive_strappend_w_utf8
78
79 /* Convert a wide-char string to current locale and append the result. */
80 /* Returns NULL if conversion fails. */
81 struct archive_string *
82 __archive_strappend_w_mbs(struct archive_string *, const wchar_t *);
83 #define archive_strappend_w_mbs __archive_strappend_w_mbs
84
85 /* Basic append operation. */
86 struct archive_string *
87 __archive_string_append(struct archive_string *as, const char *p, size_t s);
88
89 /* Copy one archive_string to another */
90 void
91 __archive_string_copy(struct archive_string *dest, struct archive_string *src);
92 #define archive_string_copy(dest, src) \
93         __archive_string_copy(dest, src)
94
95 /* Concatenate one archive_string to another */
96 void
97 __archive_string_concat(struct archive_string *dest, struct archive_string *src);
98 #define archive_string_concat(dest, src) \
99         __archive_string_concat(dest, src)
100
101 /* Ensure that the underlying buffer is at least as large as the request. */
102 struct archive_string *
103 __archive_string_ensure(struct archive_string *, size_t);
104 #define archive_string_ensure __archive_string_ensure
105
106 /* Append C string, which may lack trailing \0. */
107 struct archive_string *
108 __archive_strncat(struct archive_string *, const char *, size_t);
109 #define archive_strncat  __archive_strncat
110
111 /* Append a C string to an archive_string, resizing as necessary. */
112 #define archive_strcat(as,p) __archive_string_append((as),(p),strlen(p))
113
114 /* Copy a C string to an archive_string, resizing as necessary. */
115 #define archive_strcpy(as,p) \
116         ((as)->length = 0, __archive_string_append((as), (p), p == NULL ? 0 : strlen(p)))
117
118 /* Copy a C string to an archive_string with limit, resizing as necessary. */
119 #define archive_strncpy(as,p,l) \
120         ((as)->length=0, archive_strncat((as), (p), (l)))
121
122 /* Return length of string. */
123 #define archive_strlen(a) ((a)->length)
124
125 /* Set string length to zero. */
126 #define archive_string_empty(a) ((a)->length = 0)
127
128 /* Release any allocated storage resources. */
129 void    __archive_string_free(struct archive_string *);
130 #define archive_string_free  __archive_string_free
131
132 /* Like 'vsprintf', but resizes the underlying string as necessary. */
133 void    __archive_string_vsprintf(struct archive_string *, const char *,
134             va_list);
135 #define archive_string_vsprintf __archive_string_vsprintf
136
137 void    __archive_string_sprintf(struct archive_string *, const char *, ...);
138 #define archive_string_sprintf  __archive_string_sprintf
139
140 /* Allocates a fresh buffer and converts as (assumed to be UTF-8) into it.
141  * Returns NULL if conversion failed in any way. */
142 wchar_t *__archive_string_utf8_w(struct archive_string *as);
143
144
145 #endif