cpp
[m6w6/ext-psi] / src / types / impl_def_val.c
1 /*******************************************************************************
2 Copyright (c) 2016, Michael Wallner <mike@php.net>.
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7
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.
13
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 *******************************************************************************/
25
26 #include "php_psi_stdinc.h"
27 #include "data.h"
28
29 #include <assert.h>
30
31 struct psi_impl_def_val *psi_impl_def_val_init(token_t t, const char *text)
32 {
33 struct psi_impl_def_val *def = calloc(1, sizeof(*def));
34
35 def->type = t;
36 def->text = text ? strdup(text) : NULL;
37
38 return def;
39 }
40
41 void psi_impl_def_val_free(struct psi_impl_def_val **def_ptr)
42 {
43 if (*def_ptr) {
44 struct psi_impl_def_val *def = *def_ptr;
45
46 *def_ptr = NULL;
47 if (def->token) {
48 free(def->token);
49 }
50 switch (def->type) {
51 case PSI_T_STRING:
52 case PSI_T_QUOTED_STRING:
53 if (def->ival.zend.str) {
54 zend_string_release(def->ival.zend.str);
55 }
56 break;
57 }
58 if (def->text) {
59 free(def->text);
60 }
61 free(def);
62 }
63 }
64
65 bool psi_impl_def_val_validate(struct psi_data *data,
66 struct psi_impl_def_val *def, token_t type_t, const char *type_name)
67 {
68 if (def->type != PSI_T_NULL && def->text) {
69 switch (type_t) {
70 case PSI_T_BOOL:
71 def->ival.zend.bval = def->type == PSI_T_TRUE ? 1 : 0;
72 break;
73 case PSI_T_INT:
74 def->ival.zend.lval = zend_atol(def->text, strlen(def->text));
75 break;
76 case PSI_T_FLOAT:
77 case PSI_T_DOUBLE:
78 def->ival.dval = zend_strtod(def->text, NULL);
79 break;
80 case PSI_T_STRING:
81 /* used for consts */
82 def->ival.zend.str = zend_string_init(def->text, strlen(def->text), 1);
83 break;
84 case PSI_T_QUOTED_STRING:
85 def->ival.zend.str = zend_string_init(&def->text[1], strlen(def->text) - 2, 1);
86 break;
87 default:
88 data->error(data, def->token, PSI_WARNING,
89 "Invalid default value type '%s', expected one of bool, int, double, string.",
90 type_name);
91 return false;
92 }
93 }
94 return true;
95 }
96
97 void psi_impl_def_val_dump(int fd, struct psi_impl_def_val *val) {
98 switch (val->type) {
99 case PSI_T_BOOL:
100 dprintf(fd, "%s", val->ival.zend.bval ? "true" : "false");
101 break;
102 case PSI_T_INT:
103 dprintf(fd, "%ld", val->ival.zend.lval);
104 break;
105 case PSI_T_FLOAT:
106 case PSI_T_DOUBLE:
107 dprintf(fd, "%f", val->ival.dval);
108 break;
109 case PSI_T_STRING:
110 dprintf(fd, "\"%s\"", val->ival.zend.str->val);
111 break;
112 case PSI_T_QUOTED_STRING:
113 dprintf(fd, "\"%s\"", val->text);
114 break;
115 default:
116 if (val->text) {
117 dprintf(fd, "%s", val->text);
118 } else {
119 assert(0);
120 }
121 break;
122 }
123 }