initial revision
[ucgine.git] / tools / cfzy / libconfizery / cfzy_string.h
1 /*
2  * Copyright (c) 2013, Olivier MATZ <zer0@droids-corp.org>
3  * All rights reserved.
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  *     * Redistributions of source code must retain the above copyright
8  *       notice, this list of conditions and the following disclaimer.
9  *     * Redistributions in binary form must reproduce the above copyright
10  *       notice, this list of conditions and the following disclaimer in the
11  *       documentation and/or other materials provided with the distribution.
12  *     * Neither the name of the University of California, Berkeley nor the
13  *       names of its contributors may be used to endorse or promote products
14  *       derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
17  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
20  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27
28 /**
29  * @file
30  * String-related tools
31  */
32
33 #ifndef CFZY_STRING_H
34 #define CFZY_STRING_H
35
36 /**
37  * Append a formatted string in an allocated buffer
38  *
39  * This function acts as a asprintf, with some differences:
40  * - *but can already contain a string, in this case, the new string
41  *   is appended.
42  * - *strp is set to NULL on error, like in BSD's asprintf
43  *
44  * @param strp
45  *   Pointer to a (char *), must not be NULL. If *strp is NULL a new
46  *   buffer is allocated to store the string. Else, if *strp is not
47  *   NULL, it must point to a valid string in a buffer that was
48  *   previously allocated using malloc.
49  * @param fmt
50  *   Format string, followed by other arguments, like in printf
51  * @return
52  *   The number of bytes printed. If memory allocation wasn't
53  *   possible, or some other error occurs, these functions will return
54  *   -1, and the contents of strp is set to NULL.
55  **/
56 int cfzy_string_asprintf(char **buf, const char *fmt, ...);
57
58 /**
59  * Remove quotes on a string
60  *
61  * If the buffer is surrounded by quotes (simple or double), this
62  * function will allocate a new string and remove the quotes properly,
63  * also deleting the backslash in occurences of \\, \" or \'. If the
64  * buffer does not start by a quote, the function just return a
65  * duplicate of the input string.
66  *
67  * On sucess, the value pointer by eatlen is updated to the len of
68  * consumed bytes in the input buffer, including quotes if any, but
69  * not including \0. On error *eatlen is undefined.
70  *
71  * @param buf
72  *   string buffer
73  * @param eatlen
74  *   pointer to an int where the number of consumed bytes is written
75  * @return
76  *   the unquoted string (allocated), or NULL on error
77  */
78 char *cfzy_string_unquote(const char *buf, unsigned *eatlen);
79
80 /**
81  * Quote a string with double quotes
82  *
83  * Allocate a new string which is a copy of the first one, surrounded
84  * by quotes and quotes of initial string are escaped with a
85  * backslash.
86  *
87  * @param src
88  *   input string
89  * @return
90  *   the quoted string (allocated), or NULL on error
91  */
92 char *cfzy_string_quote(const char *src);
93
94 #endif /* CFZY_STRING */