improved type validation
[m6w6/ext-psi] / src / types / impl_def_val.c
index 61c4e8ba6ef275ca61eac4b4690cde8f21eae0d8..69b5db4ca067321a3541d5025295d85789842671 100644 (file)
@@ -33,7 +33,7 @@ struct psi_impl_def_val *psi_impl_def_val_init(token_t t, const char *text)
        struct psi_impl_def_val *def = calloc(1, sizeof(*def));
 
        def->type = t;
-       def->text = strdup(text);
+       def->text = text ? strdup(text) : NULL;
 
        return def;
 }
@@ -49,24 +49,24 @@ void psi_impl_def_val_free(struct psi_impl_def_val **def_ptr)
                }
                switch (def->type) {
                case PSI_T_STRING:
-                       assert(0);
-                       /* no break */
                case PSI_T_QUOTED_STRING:
                        if (def->ival.zend.str) {
                                zend_string_release(def->ival.zend.str);
                        }
                        break;
                }
-               free(def->text);
+               if (def->text) {
+                       free(def->text);
+               }
                free(def);
        }
 }
 
 bool psi_impl_def_val_validate(struct psi_data *data,
-               struct psi_impl_def_val *def, struct psi_impl_type *type)
+               struct psi_impl_def_val *def, token_t type_t, const char *type_name)
 {
-       if (def->type != PSI_T_NULL) {
-               switch (type->type) {
+       if (def->type != PSI_T_NULL && def->text) {
+               switch (type_t) {
                case PSI_T_BOOL:
                        def->ival.zend.bval = def->type == PSI_T_TRUE ? 1 : 0;
                        break;
@@ -78,15 +78,13 @@ bool psi_impl_def_val_validate(struct psi_data *data,
                        def->ival.dval = zend_strtod(def->text, NULL);
                        break;
                case PSI_T_STRING:
-                       assert(0);
-                       /* no break */
                case PSI_T_QUOTED_STRING:
-                       def->ival.zend.str = zend_string_init(&def->text[1], strlen(def->text) - 2, 1);
+                       def->ival.zend.str = zend_string_init(def->text, strlen(def->text), 1);
                        break;
                default:
                        data->error(data, def->token, PSI_WARNING,
                                        "Invalid default value type '%s', expected one of bool, int, double, string.",
-                                       type->name);
+                                       type_name);
                        return false;
                }
        }
@@ -95,13 +93,28 @@ bool psi_impl_def_val_validate(struct psi_data *data,
 
 void psi_impl_def_val_dump(int fd, struct psi_impl_def_val *val) {
        switch (val->type) {
+       case PSI_T_BOOL:
+               dprintf(fd, "%s", val->ival.zend.bval ? "true" : "false");
+               break;
+       case PSI_T_INT:
+               dprintf(fd, "%ld", val->ival.zend.lval);
+               break;
+       case PSI_T_FLOAT:
+       case PSI_T_DOUBLE:
+               dprintf(fd, "%f", val->ival.dval);
+               break;
        case PSI_T_STRING:
-               assert(0);
-               /* no break */
+               dprintf(fd, "\"%s\"", val->ival.zend.str->val);
+               break;
        case PSI_T_QUOTED_STRING:
                dprintf(fd, "\"%s\"", val->text);
                break;
        default:
-               dprintf(fd, "%s", val->text);
+               if (val->text) {
+                       dprintf(fd, "%s", val->text);
+               } else {
+                       assert(0);
+               }
+               break;
        }
 }