xref: /php-src/main/php_ini_builder.h (revision d87ba95a)
1 /*
2    +----------------------------------------------------------------------+
3    | Copyright (c) The PHP Group                                          |
4    +----------------------------------------------------------------------+
5    | This source file is subject to version 3.01 of the PHP license,      |
6    | that is bundled with this package in the file LICENSE, and is        |
7    | available through the world-wide-web at the following url:           |
8    | https://www.php.net/license/3_01.txt                                 |
9    | If you did not receive a copy of the PHP license and are unable to   |
10    | obtain it through the world-wide-web, please send a note to          |
11    | license@php.net so we can mail you a copy immediately.               |
12    +----------------------------------------------------------------------+
13    | Authors: Max Kellermann <max.kellermann@ionos.com>                   |
14    +----------------------------------------------------------------------+
15 */
16 
17 #ifndef PHP_INI_BUILDER_H
18 #define PHP_INI_BUILDER_H
19 
20 #include "php.h"
21 
22 /**
23  * A class which helps with constructing INI entries from the command
24  * line.
25  */
26 struct php_ini_builder {
27 	char *value;
28 	size_t length;
29 };
30 
BEGIN_EXTERN_C()31 BEGIN_EXTERN_C()
32 
33 static inline void php_ini_builder_init(struct php_ini_builder *b)
34 {
35 	b->value = NULL;
36 	b->length = 0;
37 }
38 
php_ini_builder_deinit(struct php_ini_builder * b)39 static inline void php_ini_builder_deinit(struct php_ini_builder *b)
40 {
41 	free(b->value);
42 }
43 
44 /**
45  * Null-terminate the buffer and return it.
46  */
php_ini_builder_finish(struct php_ini_builder * b)47 static inline char *php_ini_builder_finish(struct php_ini_builder *b)
48 {
49 	if (b->value != NULL) {
50 		/* null-terminate the string */
51 		b->value[b->length] = '\0';
52 	}
53 
54 	return b->value;
55 }
56 
57 /**
58  * Make room for more data.
59  *
60  * @param delta the number of bytes to be appended
61  */
php_ini_builder_realloc(struct php_ini_builder * b,size_t delta)62 static inline void php_ini_builder_realloc(struct php_ini_builder *b, size_t delta)
63 {
64 	/* reserve enough space for the null terminator */
65 	b->value = realloc(b->value, b->length + delta + 1);
66 }
67 
68 /**
69  * Prepend a string.
70  *
71  * @param src the source string
72  * @param length the size of the source string
73  */
74 PHPAPI void php_ini_builder_prepend(struct php_ini_builder *b, const char *src, size_t length);
75 
76 #define php_ini_builder_prepend_literal(b, l) php_ini_builder_prepend(b, l, strlen(l))
77 
78 /**
79  * Append an unquoted name/value pair.
80  */
81 PHPAPI void php_ini_builder_unquoted(struct php_ini_builder *b, const char *name, size_t name_length, const char *value, size_t value_length);
82 
83 /**
84  * Append a quoted name/value pair.
85  */
86 PHPAPI void php_ini_builder_quoted(struct php_ini_builder *b, const char *name, size_t name_length, const char *value, size_t value_length);
87 
88 /**
89  * Parse an INI entry from the command-line option "--define".
90  */
91 PHPAPI void php_ini_builder_define(struct php_ini_builder *b, const char *arg);
92 
93 END_EXTERN_C()
94 
95 #endif
96