comparison src/strhelp.h @ 3:8b4c49c92451

Add initial implementation that handles choices
author Andre Heinecke <andre.heinecke@intevation.de>
date Tue, 22 Mar 2016 10:39:19 +0100
parents
children 53090844eddd
comparison
equal deleted inserted replaced
2:4926d626fe15 3:8b4c49c92451
1 /* Copyright (C) 2015 by ETH Zürich
2 * Software engineering by Intevation GmbH
3 *
4 * This file is Free Software under the GNU GPL (v>=2)
5 * and comes with ABSOLUTELY NO WARRANTY!
6 * See LICENSE.txt for details.
7 */
8 #ifndef STRHELP_H
9 #define STRHELP_H
10
11 #ifdef __cplusplus
12 extern "C" {
13 #endif
14
15 #include <stdbool.h>
16 #include <stddef.h>
17
18 /**
19 * @file strhelp.h
20 * @brief Helper functions for c strings and memory management
21 * @details strhelp contains terminating memory allocation functions and
22 * some conveniance functions to work with c strings or arrays of c
23 * strings.
24 */
25
26 /** @def To avoid that a compiler optimizes certain memset calls away */
27 #define wipememory2(_ptr,_set,_len) do { \
28 volatile char *_vptr=(volatile char *)(_ptr); \
29 size_t _vlen=(_len); \
30 while(_vlen) { *_vptr=(_set); _vptr++; _vlen--; } \
31 } while(0)
32 /** @def To avoid that a compiler optimizes certain memset calls away */
33 #define wipememory(_ptr,_len) wipememory2(_ptr,0,_len)
34
35
36 void *xmalloc( size_t n );
37 /** @brief like malloc but initalizes the values with 0 */
38 void *xmalloc0( size_t n );
39 void *xrealloc( void *a, size_t n );
40 void *xcalloc( size_t n, size_t m );
41 char *xstrndup( const char *string, const size_t len );
42 void xfree ( void *p );
43
44 /**
45 * @brief Terminating variant of asprintf
46 *
47 * This function behaves exactly like asprintf(3) but will terminate
48 * when an error occures (usally that means that memoy allocation
49 * failed).
50 */
51 int xasprintf (char **strp, const char *fmt, ...);
52
53 /**
54 * @brief Returns the length of the given %NULL-terminated
55 * string array str_array.
56 * @param[in] str_array a %NULL-terminated array of strings
57 * @returns length of str_array.
58 */
59 unsigned int strv_length (char **str_array);
60
61 /**
62 * @brief append a string to a NULL terminated array of strings.
63 *
64 * @param[in,out] pArray pointer to the NULL terminated list of string pointers.
65 * @param[in] string pointer to the string to append to the list.
66 * @param[in] len length of the string to append to the list
67 */
68 void strv_append (char ***pArray, const char *string, const size_t len);
69
70 /**
71 * @brief append a string to another string.
72 *
73 * @param[in,out] pDst pointer to the string to be extended.
74 * @param[in,out] dst_len length of the dst string. Will be modified.
75 * @param[in] appendage pointer to the string to append.
76 * @param[in] len length of the string to append.
77 */
78 void str_append_str (char **pDst, size_t *dst_len, const char *appendage,
79 const size_t len);
80
81 /**
82 * @brief Frees the given %NULL-terminated string array.
83 * @param[in,out] str_array a %NULL-terminated array of strings
84 */
85 void strv_free (char **str_array);
86
87 /**
88 * @brief Checks whether two strings exactly match
89 * @param[in] s1 the first string
90 * @param[in] s2 the second string
91 * @returns true if s1 and s2 are equal
92 */
93 bool str_equal (char *s1, char *s2);
94
95 /**
96 * @brief Checks whether s2 exactly matches the beginning of s1.
97 * @param[in] s1 the string who's beginning is searched
98 * @param[in] s2 the string which is searched for
99 * @returns true if s1 starts with s2, false otherwise
100 */
101 bool str_starts_with (char *s1, char *s2);
102
103 /**
104 * @brief Trims all white space from the start and end of string.
105 * @details the start of the string is trimmed by setting *s to the
106 * first non white space character. The end is trimmed by setting the
107 * first character after the last non white space character to \0.
108 * @param[in,out] s ponter to the string to strip
109 */
110 bool str_trim (char **s);
111
112 /** @brief decode base64 encoded data
113 *
114 * The memory allocated for dest needs to be free'd by the
115 * caller.
116 *
117 * _Input warning:_
118 * If the input contains invalid base64 characters an error
119 * is returned.
120 *
121 * If the input is invalid base64 but consists of valid
122 * base64 characters _no error_ is returned and dst contains
123 * the valid input up to the error.
124 *
125 * @param [out] dst Pointer to the destination. Needs to be NULL
126 * @param [out] dst_size Size allocated for the destination.
127 * @param [in] src Pointer to the base64 encoded data.
128 * @param [in] src_size Size of the encoded data.
129 *
130 * @returns 0 on success a polarssl error or -1 otherwise
131 */
132 int str_base64_decode(char **dst, size_t *dst_size, char *src,
133 size_t src_size);
134
135 #ifdef WIN32
136
137 /** @brief convert a utf8 string to utf16 wchar
138 *
139 * @param[in] string utf8 string. Must be at least len characters long.
140 * @param[in] len number of characters to be converted.
141 *
142 * @returns pointer to a newly allocated wchar array. NULL on error.
143 *
144 **/
145 wchar_t *utf8_to_wchar (const char *string, size_t len);
146
147 /** @brief convert a local 8 bit (acp) string to utf16 wchar
148 *
149 * @param[in] string acp string. Must be at least len characters long.
150 * @param[in] len number of characters to be converted.
151 *
152 * @returns pointer to a newly allocated wchar array. NULL on error.
153 *
154 **/
155 wchar_t *acp_to_wchar (const char *string, size_t len);
156
157 /** @brief convert a utf16 string to utf8
158 *
159 * @param[in] string utf16 string. Must be at least len characters long.
160 * @param[in] len number of characters to be converted.
161 *
162 * @returns pointer to a newly allocated char array. NULL on error.
163 *
164 **/
165 char *wchar_to_utf8 (const wchar_t *string, size_t len);
166 #endif
167
168 #ifdef __cplusplus
169 }
170 #endif
171
172 #endif
173
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)