php-internal-docs 8.4.8
Unofficial docs for php/php-src
Loading...
Searching...
No Matches
dba_inifile.c
Go to the documentation of this file.
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 | Author: Marcus Boerger <helly@php.net> |
14 +----------------------------------------------------------------------+
15 */
16
17#ifdef HAVE_CONFIG_H
18#include <config.h>
19#endif
20
21#include "php.h"
22
23#ifdef DBA_INIFILE
24#include "php_inifile.h"
25
26#include "libinifile/inifile.h"
27
28#ifdef HAVE_UNISTD_H
29#include <unistd.h>
30#endif
31#include <sys/types.h>
32#include <sys/stat.h>
33#include <fcntl.h>
34
35DBA_OPEN_FUNC(inifile)
36{
37 info->dbf = inifile_alloc(info->fp, info->mode == DBA_READER, info->flags&DBA_PERSISTENT);
38
39 return info->dbf ? SUCCESS : FAILURE;
40}
41
42DBA_CLOSE_FUNC(inifile)
43{
44 inifile *dba = info->dbf;
45
46 inifile_free(dba, info->flags&DBA_PERSISTENT);
47}
48
49DBA_FETCH_FUNC(inifile)
50{
51 inifile *dba = info->dbf;
52 val_type ini_val;
53 key_type ini_key;
54 zend_string *fetched_val = NULL;
55
56 if (!key) {
57 php_error_docref(NULL, E_WARNING, "No key specified");
58 return 0;
59 }
60 ini_key = inifile_key_split(ZSTR_VAL(key)); /* keylen not needed here */
61
62 ini_val = inifile_fetch(dba, &ini_key, skip);
63 inifile_key_free(&ini_key);
64 if (ini_val.value) {
65 fetched_val = zend_string_init(ini_val.value, strlen(ini_val.value), /* persistent */ false);
66 inifile_val_free(&ini_val);
67 }
68 return fetched_val;
69}
70
71DBA_UPDATE_FUNC(inifile)
72{
73 inifile *dba = info->dbf;
74 val_type ini_val;
75 int res;
76 key_type ini_key;
77
78 if (!key) {
79 php_error_docref(NULL, E_WARNING, "No key specified");
80 return 0;
81 }
82 ini_key = inifile_key_split(ZSTR_VAL(key)); /* keylen not needed here */
83
84 ini_val.value = ZSTR_VAL(val);
85
86 if (mode == 1) {
87 res = inifile_append(dba, &ini_key, &ini_val);
88 } else {
89 res = inifile_replace(dba, &ini_key, &ini_val);
90 }
91 inifile_key_free(&ini_key);
92 switch(res) {
93 case -1:
94 // TODO Check when this happens and confirm this can even happen
95 php_error_docref(NULL, E_WARNING, "Operation not possible");
96 return FAILURE;
97 default:
98 case 0:
99 return SUCCESS;
100 case 1:
101 return FAILURE;
102 }
103}
104
105DBA_EXISTS_FUNC(inifile)
106{
107 inifile *dba = info->dbf;
108 val_type ini_val;
109 key_type ini_key;
110
111 if (!key) {
112 php_error_docref(NULL, E_WARNING, "No key specified");
113 return 0;
114 }
115 ini_key = inifile_key_split(ZSTR_VAL(key)); /* keylen not needed here */
116
117 ini_val = inifile_fetch(dba, &ini_key, 0);
118 inifile_key_free(&ini_key);
119 if (ini_val.value) {
120 inifile_val_free(&ini_val);
121 return SUCCESS;
122 }
123 return FAILURE;
124}
125
126DBA_DELETE_FUNC(inifile)
127{
128 inifile *dba = info->dbf;
129 int res;
130 bool found = 0;
131 key_type ini_key;
132
133 if (!key) {
134 php_error_docref(NULL, E_WARNING, "No key specified");
135 return 0;
136 }
137 ini_key = inifile_key_split(ZSTR_VAL(key)); /* keylen not needed here */
138
139 res = inifile_delete_ex(dba, &ini_key, &found);
140
141 inifile_key_free(&ini_key);
142 return (res == -1 || !found ? FAILURE : SUCCESS);
143}
144
145DBA_FIRSTKEY_FUNC(inifile)
146{
147 inifile *dba = info->dbf;
148
149 if (inifile_firstkey(dba)) {
150 char *result = inifile_key_string(&dba->curr.key);
151 zend_string *key = zend_string_init(result, strlen(result), /* persistent */ false);
152 efree(result);
153 return key;
154 } else {
155 return NULL;
156 }
157}
158
159DBA_NEXTKEY_FUNC(inifile)
160{
161 inifile *dba = info->dbf;
162
163 if (!dba->curr.key.group && !dba->curr.key.name) {
164 return NULL;
165 }
166
167 if (inifile_nextkey(dba)) {
168 char *result = inifile_key_string(&dba->curr.key);
169 zend_string *key = zend_string_init(result, strlen(result), /* persistent */ false);
170 efree(result);
171 return key;
172 } else {
173 return NULL;
174 }
175}
176
177DBA_OPTIMIZE_FUNC(inifile)
178{
179 /* dummy */
180 return SUCCESS;
181}
182
183DBA_SYNC_FUNC(inifile)
184{
185 /* dummy */
186 return SUCCESS;
187}
188
189DBA_INFO_FUNC(inifile)
190{
191 return estrdup(inifile_version());
192}
193
194#endif
zend_string * res
Definition ffi.c:4692
zval * val
Definition ffi.c:4262
char * mode
#define NULL
Definition gdcache.h:45
#define SUCCESS
Definition hash_sha3.c:261
int inifile_append(inifile *dba, const key_type *key, const val_type *value)
Definition inifile.c:586
void inifile_free(inifile *dba, int persistent)
Definition inifile.c:100
int inifile_firstkey(inifile *dba)
Definition inifile.c:284
inifile * inifile_alloc(php_stream *fp, int readonly, int persistent)
Definition inifile.c:80
int inifile_delete_ex(inifile *dba, const key_type *key, bool *found)
Definition inifile.c:565
val_type inifile_fetch(inifile *dba, const key_type *key, int skip)
Definition inifile.c:241
char * inifile_key_string(const key_type *key)
Definition inifile.c:128
const char * inifile_version(void)
Definition inifile.c:41
int inifile_nextkey(inifile *dba)
Definition inifile.c:292
int inifile_replace(inifile *dba, const key_type *key, const val_type *value)
Definition inifile.c:572
void inifile_key_free(key_type *key)
Definition inifile.c:48
void inifile_val_free(val_type *val)
Definition inifile.c:61
key_type inifile_key_split(const char *group_name)
Definition inifile.c:111
PHPAPI ZEND_COLD void php_error_docref(const char *docref, int type, const char *format,...)
Definition main.c:1173
unsigned char key[REFLECTION_KEY_LEN]
line_type curr
Definition inifile.h:40
char * name
Definition inifile.h:22
char * group
Definition inifile.h:21
key_type key
Definition inifile.h:30
char * value
Definition inifile.h:26
#define efree(ptr)
Definition zend_alloc.h:155
#define estrdup(s)
Definition zend_alloc.h:164
strlen(string $string)
#define E_WARNING
Definition zend_errors.h:24
struct _zend_string zend_string
#define ZSTR_VAL(zstr)
Definition zend_string.h:68
@ FAILURE
Definition zend_types.h:61
bool result