Clean a dictionary of unwanted keys Used to make sure only valid keys are in output presets
| 2532 | // Clean a dictionary of unwanted keys |
| 2533 | // Used to make sure only valid keys are in output presets |
| 2534 | static void |
| 2535 | dict_clean(hb_value_t *dict, hb_value_t *template) |
| 2536 | { |
| 2537 | hb_value_t *tmp = hb_value_dup(dict); |
| 2538 | hb_dict_iter_t iter; |
| 2539 | const char *key; |
| 2540 | hb_value_t *val; |
| 2541 | hb_value_t *template_val; |
| 2542 | hb_value_type_t template_type, val_type; |
| 2543 | const char *preset_name = NULL; |
| 2544 | |
| 2545 | val = hb_dict_get(dict, "PresetName"); |
| 2546 | if (val != NULL) |
| 2547 | preset_name = hb_value_get_string(val); |
| 2548 | |
| 2549 | // Remove keys that are not in the template and translate compatible |
| 2550 | // data types to the types used in the template. |
| 2551 | for (iter = hb_dict_iter_init(tmp); |
| 2552 | iter != HB_DICT_ITER_DONE; |
| 2553 | iter = hb_dict_iter_next(tmp, iter)) |
| 2554 | { |
| 2555 | key = hb_dict_iter_key(iter); |
| 2556 | val = hb_dict_iter_value(iter); |
| 2557 | val_type = hb_value_type(val); |
| 2558 | |
| 2559 | template_val = hb_dict_get(template, key); |
| 2560 | template_type = hb_value_type(template_val); |
| 2561 | if (template_val == NULL) |
| 2562 | { |
| 2563 | // Unknown key. These can be keys used privately by the |
| 2564 | // frontend. So don't make noise about them. |
| 2565 | hb_dict_remove(dict, key); |
| 2566 | } |
| 2567 | else if (val_type != template_type) |
| 2568 | { |
| 2569 | if (val_type == HB_VALUE_TYPE_DICT || |
| 2570 | val_type == HB_VALUE_TYPE_ARRAY || |
| 2571 | template_type == HB_VALUE_TYPE_DICT || |
| 2572 | template_type == HB_VALUE_TYPE_ARRAY) |
| 2573 | { |
| 2574 | hb_error("Preset %s: Incompatible value types for key %s. " |
| 2575 | "Dropping.", preset_name, key); |
| 2576 | hb_dict_remove(dict, key); |
| 2577 | } |
| 2578 | else if (hb_value_is_number(val) && |
| 2579 | hb_value_is_number(template_val)) |
| 2580 | { |
| 2581 | // Silently convert compatible numbers |
| 2582 | hb_value_t *v; |
| 2583 | v = hb_value_xform(val, template_type); |
| 2584 | hb_dict_set(dict, key, v); |
| 2585 | } |
| 2586 | else |
| 2587 | { |
| 2588 | hb_value_t *v; |
| 2589 | hb_error("Preset %s: Incorrect value type for key %s. " |
| 2590 | "Converting.", preset_name, key); |
| 2591 | v = hb_value_xform(val, template_type); |
no test coverage detected