shtr_param.c (5057B)
1 /* Copyright (C) 2022, 2025, 2026 |Méso|Star> (contact@meso-star.com) 2 * Copyright (C) 2025, 2026 Université de Lorraine 3 * Copyright (C) 2022 Centre National de la Recherche Scientifique 4 * Copyright (C) 2022 Université Paul Sabatier 5 * 6 * This program is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation, either version 3 of the License, or 9 * (at your option) any later version. 10 * 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with this program. If not, see <http://www.gnu.org/licenses/>. */ 18 19 #include "shtr_c.h" 20 #include "shtr_param.h" 21 22 #include <rsys/cstr.h> 23 24 #define C_FORMAT_double "%g" 25 #define C_FORMAT_float "%g" 26 #define C_FORMAT_int "%d" 27 28 /******************************************************************************* 29 * Local functions 30 ******************************************************************************/ 31 #define DEFINE_PARSE_PARAM_FUNCTION(Type) \ 32 res_T \ 33 parse_param_##Type \ 34 (struct shtr* shtr, \ 35 const char* str, \ 36 const struct param_desc* desc, \ 37 Type* out_param) \ 38 { \ 39 Type param = 0; \ 40 res_T res = RES_OK; \ 41 ASSERT(shtr && desc && out_param); \ 42 ASSERT(desc->low < desc->upp \ 43 || (desc->low == desc->upp && desc->is_low_incl && desc->is_upp_incl));\ 44 \ 45 if(!str) { \ 46 ERROR(shtr, "%s:%lu: %s is missing.\n", \ 47 desc->path, (unsigned long)desc->line, desc->name); \ 48 res = RES_BAD_ARG; \ 49 goto error; \ 50 } \ 51 \ 52 res = cstr_to_##Type(str, ¶m); \ 53 if(res != RES_OK) { \ 54 ERROR(shtr, "%s:%lu: invalid %s `%s'.\n", \ 55 desc->path, (unsigned long)desc->line, desc->name, str); \ 56 res = RES_BAD_ARG; \ 57 goto error; \ 58 } \ 59 \ 60 if(param < desc->low || (param == desc->low && !desc->is_low_incl) \ 61 || param > desc->upp || (param == desc->upp && !desc->is_upp_incl)) { \ 62 ERROR(shtr, \ 63 "%s:%lu: invalid %s `%s'. It must be in " \ 64 "%c"CONCAT(C_FORMAT_, Type)", "CONCAT(C_FORMAT_, Type)"%c.\n", \ 65 desc->path, (unsigned long)desc->line, desc->name, str, \ 66 desc->is_low_incl ? '[' : ']', (Type)desc->low, \ 67 (Type)desc->upp, desc->is_upp_incl ? ']' : '['); \ 68 res = RES_BAD_ARG; \ 69 goto error; \ 70 } \ 71 \ 72 exit: \ 73 *out_param = param; \ 74 return res; \ 75 error: \ 76 goto exit; \ 77 } 78 DEFINE_PARSE_PARAM_FUNCTION(int) 79 DEFINE_PARSE_PARAM_FUNCTION(double) 80 #undef DEFINE_PARSE_PARAM_FUNCTION