1 /*******************************************************************************
2 Copyright (c) 2016, Michael Wallner <mike@php.net>.
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
8 * Redistributions of source code must retain the above copyright notice,
9 this list of conditions and the following disclaimer.
10 * Redistributions in binary form must reproduce the above copyright
11 notice, this list of conditions and the following disclaimer in the
12 documentation and/or other materials provided with the distribution.
14 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
18 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
20 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
21 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
22 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 *******************************************************************************/
26 #include "php_psi_stdinc.h"
31 struct psi_impl_def_val
*psi_impl_def_val_init(token_t t
, void *data
)
33 struct psi_impl_def_val
*def
= calloc(1, sizeof(*def
));
35 switch ((def
->type
= t
)) {
40 case PSI_T_QUOTED_STRING
:
41 /* immediate upgrade */
42 def
->type
= PSI_T_STRING
;
46 def
->ival
.zend
.str
= zend_string_init(data
, strlen(data
), 1);
61 void psi_impl_def_val_free(struct psi_impl_def_val
**def_ptr
)
64 struct psi_impl_def_val
*def
= *def_ptr
;
72 psi_num_exp_free(&def
->data
.num
);
76 if (def
->ival
.zend
.str
) {
77 zend_string_release(def
->ival
.zend
.str
);
87 bool psi_impl_def_val_validate(struct psi_data
*data
,
88 struct psi_impl_def_val
*def
, struct psi_impl_type
*cmp
,
89 struct psi_validate_scope
*scope
)
91 if (def
->type
== PSI_T_NULL
) {
97 def
->ival
.zend
.bval
= def
->type
== PSI_T_TRUE
? 1 : 0;
102 if (def
->type
== PSI_T_NUMBER
) {
103 token_t typ
= psi_num_exp_exec(def
->data
.num
, &def
->ival
, NULL
, scope
->defs
);
107 def
->ival
.dval
= def
->ival
.fval
;
110 def
->type
= PSI_T_FLOAT
;
111 cmp
->type
= PSI_T_FLOAT
;
112 strcpy(cmp
->name
, "float");
115 def
->type
= PSI_T_INT
;
116 cmp
->type
= PSI_T_INT
;
117 strcpy(cmp
->name
, "int");
120 psi_num_exp_free(&def
->data
.num
);
126 if (def
->type
== PSI_T_NUMBER
) {
127 def
->type
= PSI_T_INT
;
128 def
->ival
.zend
.lval
= psi_num_exp_get_long(def
->data
.num
, NULL
, scope
->defs
);
129 psi_num_exp_free(&def
->data
.num
);
131 if (def
->type
== PSI_T_INT
) {
138 if (def
->type
== PSI_T_NUMBER
) {
139 def
->type
= PSI_T_DOUBLE
;
140 def
->ival
.dval
= psi_num_exp_get_double(def
->data
.num
, NULL
, scope
->defs
);
141 psi_num_exp_free(&def
->data
.num
);
143 if (def
->type
== PSI_T_DOUBLE
) {
149 if (def
->type
== PSI_T_STRING
) {
155 data
->error(data
, def
->token
, PSI_WARNING
,
156 "Invalid default value type '%s', "
157 "expected one of bool, int, float, string.",
164 void psi_impl_def_val_dump(int fd
, struct psi_impl_def_val
*val
) {
170 dprintf(fd
, "false");
173 dprintf(fd
, "%s", val
->ival
.zend
.bval
? "true" : "false");
176 dprintf(fd
, "%ld", val
->ival
.zend
.lval
);
180 dprintf(fd
, "%" PRIdval
, val
->ival
.dval
);
183 dprintf(fd
, "\"%s\"", val
->ival
.zend
.str
->val
);