aw.c (3005B)
1 /* Copyright (C) 2014-2017, 2020-2023 Vincent Forest (vaplv@free.fr) 2 * 3 * This program is free software: you can redistribute it and/or modify 4 * it under the terms of the GNU General Public License as published by 5 * the Free Software Foundation, either version 3 of the License, or 6 * (at your option) any later version. 7 * 8 * This program is distributed in the hope that it will be useful, 9 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * GNU Lesser General Public License for more details. 12 * 13 * You should have received a copy of the GNU Lesser General Public License 14 * along with this program. If not, see <http://www.gnu.org/licenses/>. */ 15 16 #define _POSIX_C_SOURCE 200112L /* strtok_r support */ 17 18 #include "aw_c.h" 19 #include <rsys/cstr.h> 20 #include <rsys/logger.h> 21 #include <rsys/mem_allocator.h> 22 #include <string.h> 23 24 /******************************************************************************* 25 * Helper functions 26 ******************************************************************************/ 27 static void 28 print_info(const char* msg, void* ctx) 29 { 30 fprintf(stderr, "%s%s", ctx ? (const char*)ctx : "info: ", msg); 31 } 32 33 static void 34 print_err(const char* msg, void* ctx) 35 { 36 fprintf(stderr, "%s%s", ctx ? (const char*)ctx : "error: ", msg); 37 } 38 39 static void 40 print_warn(const char* msg, void* ctx) 41 { 42 fprintf(stderr, "%s%s", ctx ? (const char*)ctx : "warning: ", msg); 43 } 44 45 /******************************************************************************* 46 * Local functions 47 ******************************************************************************/ 48 res_T 49 parse_doubleX 50 (double* dblX, 51 const unsigned int count_min, 52 const unsigned int count_max, 53 const double range_min, 54 const double range_max, 55 const double default_value, 56 char** tk_ctx) 57 { 58 size_t i; 59 ASSERT(dblX && count_min <= count_max && range_min <= range_max && tk_ctx); 60 ASSERT(default_value >= range_min && default_value <= range_max); 61 62 FOR_EACH(i, 0, count_max) { 63 char* real = strtok_r(NULL, " \t", tk_ctx); 64 res_T res = RES_OK; 65 66 if(!real) break; 67 68 res = cstr_to_double(real, dblX + i); 69 if(res != RES_OK) return res; 70 71 if(dblX[i] < range_min || dblX[i] > range_max) 72 return RES_BAD_ARG; 73 } 74 if(i < count_min) 75 return RES_BAD_ARG; 76 77 FOR_EACH(i, i, count_max) 78 dblX[i] = default_value; 79 80 return RES_OK; 81 } 82 83 res_T 84 setup_default_logger 85 (struct mem_allocator* allocator, 86 struct logger* logger, 87 const char* prefix_info, /* May be NULL */ 88 const char* prefix_error, /* May be NULL */ 89 const char* prefix_warning) /* May be NULL */ 90 { 91 res_T res = RES_OK; 92 ASSERT(logger); 93 res = logger_init(allocator, logger); 94 if(res != RES_OK) return res; 95 logger_set_stream(logger, LOG_OUTPUT, print_info, (void*)prefix_info); 96 logger_set_stream(logger, LOG_ERROR, print_err, (void*)prefix_error); 97 logger_set_stream(logger, LOG_WARNING, print_warn, (void*)prefix_warning); 98 return RES_OK; 99 } 100 101