star-phor-input

File format for describing photoreactor configurations
git clone https://www.edstar.cnrs.fr/git/star-phor-input.git
Log | Files | Refs | README | LICENSE

sphin_sensor_volume.c (5869B)


      1 /* Copyright (C) 2024-2026 Centre National de la Recherche Scientifique
      2  * Copyright (C) 2024-2026 Clermont Auvergne INP
      3  * Copyright (C) 2024-2026 INSA Lyon
      4  * Copyright (C) 2024-2026 Institut Mines Télécom Albi-Carmaux
      5  * Copyright (C) 2024-2026 Institut National Polytechnique de Toulouse
      6  * Copyright (C) 2024-2026 |Méso|Star> (contact@meso-star.com)
      7  * Copyright (C) 2024-2026 PhotonLyX (info@photonlyx.com)
      8  * Copyright (C) 2024-2026 Université de Lorraine
      9  * Copyright (C) 2024-2026 Université Paul Sabatier
     10  * Copyright (C) 2024-2026 Université Toulouse - Jean Jaurès
     11  *
     12  * This program is free software: you can redistribute it and/or modify
     13  * it under the terms of the GNU General Public License as published by
     14  * the Free Software Foundation, either version 3 of the License, or
     15  * (at your option) any later version.
     16  *
     17  * This program is distributed in the hope that it will be useful,
     18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
     20  * GNU General Public License for more details.
     21  *
     22  * You should have received a copy of the GNU General Public License
     23  * along with this program. If not, see <http://www.gnu.org/licenses/>. */
     24 #define _POSIX_C_SOURCE 200112L /* for strtok_r support */
     25 
     26 #include "sphin.h"
     27 #include "sphin_c.h"
     28 #include "sphin_config.h"
     29 #include "sphin_sensor.h"
     30 #include "sphin_sensor_volume.h"
     31 
     32 #include <rsys/cstr.h>
     33 #include <rsys/mem_allocator.h>
     34 #include <rsys/ref_count.h>
     35 #include <rsys/rsys.h>
     36 #include <rsys/str.h>
     37 #include <rsys/text_reader.h>
     38 
     39 struct txtrdr;
     40 
     41 struct sphin_sensor_volume {
     42   struct str name;
     43   double response_function;
     44 
     45   struct sphin* sphin;
     46   ref_T ref;
     47 };
     48 
     49 /*******************************************************************************
     50  * Helper functions
     51  ******************************************************************************/
     52 static res_T
     53 sensor_volume_create
     54   (struct sphin* sphin,
     55    const char* name,
     56    struct sphin_sensor_volume** out_sensor_volume)
     57 {
     58   struct sphin_sensor_volume* sensor_volume = NULL;
     59   res_T res = RES_OK;
     60 
     61   ASSERT(NULL != sphin);
     62   ASSERT(NULL != out_sensor_volume);
     63   ASSERT(NULL != name);
     64   ASSERT('\0' != name[0]); /* Name can't be empty */
     65 
     66   sensor_volume = MEM_CALLOC
     67     (sphin->allocator, 1, sizeof(struct sphin_sensor_volume));
     68   if (NULL == sensor_volume) { res = RES_MEM_ERR; goto error; }
     69   ref_init(&sensor_volume->ref);
     70   SPHIN(ref_get(sphin));
     71   sensor_volume->sphin = sphin;
     72 
     73   str_init(sphin->allocator, &sensor_volume->name);
     74   res = str_set(&sensor_volume->name, name);
     75   if (RES_OK != res) { goto error; }
     76 
     77 exit:
     78   *out_sensor_volume = sensor_volume;
     79   return res;
     80 error:
     81   if (NULL != sensor_volume) {
     82     SPHIN(sensor_volume_ref_put(sensor_volume));
     83     sensor_volume = NULL;
     84   }
     85   goto exit;
     86 }
     87 
     88 static void
     89 release_sensor_volume
     90   (ref_T* address)
     91 {
     92   struct sphin_sensor_volume* sensor_volume = NULL;
     93   struct sphin* sphin = NULL;
     94 
     95   ASSERT(NULL != address);
     96   sensor_volume = CONTAINER_OF(address, struct sphin_sensor_volume, ref);
     97   str_release(&sensor_volume->name);
     98   sphin = sensor_volume->sphin;
     99   MEM_RM(sphin->allocator, sensor_volume);
    100   SPHIN(ref_put(sphin));
    101 }
    102 
    103 /*******************************************************************************
    104  * Local functions
    105  ******************************************************************************/
    106 res_T
    107 parse_sensor_volume
    108   (struct sphin* sphin,
    109    struct txtrdr* txtrdr,
    110    const char* name,
    111    struct sphin_sensor_volume** out_sensor_volume)
    112 {
    113   char* keyword = NULL;
    114   char* token = NULL;
    115   char* token_ptr = NULL;
    116   char* value = NULL;
    117   struct sphin_sensor_volume* sensor_volume = NULL;
    118   struct str line;
    119   res_T res = RES_OK;
    120 
    121   ASSERT(NULL != sphin);
    122   ASSERT(NULL != txtrdr);
    123   ASSERT(NULL != name);
    124   ASSERT(NULL != out_sensor_volume);
    125 
    126   str_init(sphin->allocator, &line);
    127 
    128   res = sensor_volume_create(sphin, name, &sensor_volume);
    129   if (RES_OK != res) { goto error; }
    130 
    131   res = txtrdr_read_line(txtrdr);
    132   if (RES_OK != res) { goto error; }
    133 
    134   while (NULL != txtrdr_get_line(txtrdr)) {
    135     res = str_set(&line, txtrdr_get_cline(txtrdr));
    136     if (RES_OK != res) { goto error; }
    137 
    138     /* Parse keyword */
    139     token = strtok_r(str_get(&line), ":", &token_ptr);
    140     if (NULL == token){ res = RES_BAD_ARG; goto error; }
    141     keyword = trim_keyword(token);
    142     if (NULL == keyword){ res = RES_BAD_ARG; goto error; }
    143 
    144     /* Parse value */
    145     token = strtok_r(NULL, "", &token_ptr);
    146     value = token;
    147     /* Parse response function */
    148     if (0 == strcmp(keyword, "response_function")){
    149       res =  parse_response_function(&sensor_volume->response_function,
    150                                      txtrdr, value);
    151     }
    152     else {
    153       break;
    154     }
    155     if (RES_OK != res) { goto error; }
    156   }
    157 
    158 exit:
    159   str_release(&line);
    160   *out_sensor_volume = sensor_volume;
    161   return res;
    162 error:
    163   if (sensor_volume != NULL){
    164     SPHIN(sensor_volume_ref_put(sensor_volume));
    165     sensor_volume = NULL;
    166   }
    167   goto exit;
    168 }
    169 
    170 /*******************************************************************************
    171  * Exported functions
    172  ******************************************************************************/
    173 res_T
    174 sphin_sensor_volume_ref_get
    175   (struct sphin_sensor_volume* sensor_volume)
    176 {
    177   if (NULL == sensor_volume) {
    178     return RES_BAD_ARG;
    179   }
    180   ref_get(&sensor_volume->ref);
    181   return RES_OK;
    182 }
    183 
    184 res_T
    185 sphin_sensor_volume_ref_put
    186   (struct sphin_sensor_volume* sensor_volume)
    187 {
    188   if (NULL == sensor_volume) {
    189     return RES_BAD_ARG;
    190   }
    191   ref_put(&sensor_volume->ref, release_sensor_volume);
    192   return RES_OK;
    193 }
    194 
    195 res_T
    196 sphin_sensor_volume_get_response_function
    197   (struct sphin_sensor_volume* sensor_volume,
    198    double* response_function)
    199 {
    200   if (NULL == sensor_volume || NULL == response_function){
    201     return RES_BAD_ARG;
    202   }
    203 
    204   *response_function = sensor_volume->response_function;
    205   return RES_OK;
    206 }