solparser_image.c (3959B)
1 /* Copyright (C) 2018-2026 |Meso|Star> (contact@meso-star.com) 2 * Copyright (C) 2016-2018 CNRS 3 * 4 * This program is free software: you can redistribute it and/or modify 5 * it under the terms of the GNU General Public License as published by 6 * the Free Software Foundation, either version 3 of the License, or 7 * (at your option) any later version. 8 * 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * 14 * You should have received a copy of the GNU General Public License 15 * along with this program. If not, see <http://www.gnu.org/licenses/>. */ 16 17 #include "solparser_c.h" 18 19 /******************************************************************************* 20 * Local functions 21 ******************************************************************************/ 22 res_T 23 parse_image 24 (struct solparser* parser, 25 yaml_document_t* doc, 26 const yaml_node_t* img, 27 struct solparser_image_id* out_iimg) 28 { 29 enum { PATH }; 30 struct solparser_image* solimg = NULL; 31 size_t isolimg = SIZE_MAX; 32 intptr_t i, n; 33 int mask = 0; /* Register the parsed attributes */ 34 res_T res = RES_OK; 35 ASSERT(parser && doc && img &&out_iimg); 36 37 if(img->type != YAML_MAPPING_NODE) { 38 log_err(parser, img, "expect a mapping of image parameters.\n"); 39 res = RES_BAD_ARG; 40 goto error; 41 } 42 43 /* Allocate an image */ 44 isolimg = darray_image_size_get(&parser->images); 45 res = darray_image_resize(&parser->images, isolimg + 1); 46 if(res != RES_OK) { 47 log_err(parser, img, "could not allocate the image.\n"); 48 goto error; 49 } 50 solimg = darray_image_data_get(&parser->images) + isolimg; 51 52 n = img->data.mapping.pairs.top - img->data.mapping.pairs.start; 53 FOR_EACH(i, 0, n) { 54 yaml_node_t* key; 55 yaml_node_t* val; 56 57 key = yaml_document_get_node(doc, img->data.mapping.pairs.start[i].key); 58 val = yaml_document_get_node(doc, img->data.mapping.pairs.start[i].value); 59 if(key->type != YAML_SCALAR_NODE) { 60 log_err(parser, key, "expect image parameters.\n"); 61 goto error; 62 } 63 #define SETUP_MASK(Flag, Name) { \ 64 if(mask & BIT(Flag)) { \ 65 log_err(parser, key, \ 66 "the image parameter `"Name"' is already defined.\n"); \ 67 res = RES_BAD_ARG; \ 68 goto error; \ 69 } \ 70 mask |= BIT(Flag); \ 71 } (void)0 72 if(!strcmp((char*)key->data.scalar.value, "path")) { 73 SETUP_MASK(PATH, "path"); 74 res = parse_string(parser, val, &solimg->filename); 75 } else { 76 log_err(parser, key, "unknown image parameter `%s'.\n", 77 key->data.scalar.value); 78 res = RES_BAD_ARG; 79 goto error; 80 } 81 if(res != RES_OK) { 82 log_node(parser, key); 83 goto error; 84 } 85 #undef SETUP_MASK 86 } 87 88 #define CHECK_PARAM(Flag, Name) \ 89 if(!(mask & BIT(Flag))) { \ 90 log_err(parser, img, \ 91 "the image parameter `"Name"' is missing.\n"); \ 92 res = RES_BAD_ARG; \ 93 goto error; \ 94 } (void)0 95 CHECK_PARAM(PATH, "path"); 96 #undef CHECK_PARAM 97 98 exit: 99 out_iimg->i = isolimg; 100 return res; 101 error: 102 if(solimg) { 103 darray_image_pop_back(&parser->images); 104 isolimg = SIZE_MAX; 105 } 106 goto exit; 107 } 108