raising the head after a three-weeks refactoring
[m6w6/ext-psi] / src / types / impl_def_val.c
index 0d176f309eea9845d6ff2222243ef761c6d4f98a..61c4e8ba6ef275ca61eac4b4690cde8f21eae0d8 100644 (file)
  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *******************************************************************************/
 
-#ifdef HAVE_CONFIG_H
-# include "config.h"
-#else
-# include "php_config.h"
-#endif
+#include "php_psi_stdinc.h"
+#include "data.h"
 
-#include <stdlib.h>
-#include <stdio.h>
+#include <assert.h>
 
-#include "impl_def_val.h"
+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));
 
-void free_impl_def_val(impl_def_val *def) {
-       free(def->text);
-       free(def);
-}
-
-impl_def_val *init_impl_def_val(token_t t, const char *text) {
-       impl_def_val *def = calloc(1, sizeof(*def));
        def->type = t;
        def->text = strdup(text);
+
        return def;
 }
 
-void dump_impl_def_val(int fd, impl_def_val *val) {
-       if (val->type == PSI_T_QUOTED_STRING) {
+void psi_impl_def_val_free(struct psi_impl_def_val **def_ptr)
+{
+       if (*def_ptr) {
+               struct psi_impl_def_val *def = *def_ptr;
+
+               *def_ptr = NULL;
+               if (def->token) {
+                       free(def->token);
+               }
+               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);
+               free(def);
+       }
+}
+
+bool psi_impl_def_val_validate(struct psi_data *data,
+               struct psi_impl_def_val *def, struct psi_impl_type *type)
+{
+       if (def->type != PSI_T_NULL) {
+               switch (type->type) {
+               case PSI_T_BOOL:
+                       def->ival.zend.bval = def->type == PSI_T_TRUE ? 1 : 0;
+                       break;
+               case PSI_T_INT:
+                       def->ival.zend.lval = zend_atol(def->text, strlen(def->text));
+                       break;
+               case PSI_T_FLOAT:
+               case PSI_T_DOUBLE:
+                       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);
+                       break;
+               default:
+                       data->error(data, def->token, PSI_WARNING,
+                                       "Invalid default value type '%s', expected one of bool, int, double, string.",
+                                       type->name);
+                       return false;
+               }
+       }
+       return true;
+}
+
+void psi_impl_def_val_dump(int fd, struct psi_impl_def_val *val) {
+       switch (val->type) {
+       case PSI_T_STRING:
+               assert(0);
+               /* no break */
+       case PSI_T_QUOTED_STRING:
                dprintf(fd, "\"%s\"", val->text);
-       } else {
+               break;
+       default:
                dprintf(fd, "%s", val->text);
        }
-
 }