comparison src/strhelp.h @ 0:147b08bc7d64

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