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_refractive_index.c (5919B)


      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_refractive_index.h"
     30 #include "sphin_spectral_property.h"
     31 
     32 #include <rsys/mem_allocator.h>
     33 #include <rsys/ref_count.h>
     34 #include <rsys/str.h>
     35 #include <rsys/text_reader.h>
     36 
     37 /*******************************************************************************
     38  * Helper functions
     39  ******************************************************************************/
     40 static res_T
     41 refractive_index_create
     42   (struct sphin* sphin,
     43    struct sphin_refractive_index** out_refractive_index)
     44 {
     45   struct sphin_refractive_index* refr_ind = NULL;
     46   res_T res = RES_OK;
     47 
     48   ASSERT(NULL != out_refractive_index);
     49   ASSERT(NULL != sphin);
     50 
     51   refr_ind = MEM_CALLOC
     52     (sphin->allocator, 1, sizeof(struct sphin_refractive_index));
     53   if (NULL == refr_ind) { res = RES_MEM_ERR; goto error; }
     54   ref_init(&refr_ind->ref);
     55   SPHIN(ref_get(sphin));
     56   refr_ind->sphin = sphin;
     57   refr_ind->n_real = NULL;
     58   refr_ind->n_imag = NULL;
     59 
     60 exit:
     61   *out_refractive_index = refr_ind;
     62   return res;
     63 error:
     64   if (NULL != refr_ind) {
     65     SPHIN(refractive_index_ref_put(refr_ind));
     66     refr_ind = NULL;
     67   }
     68   goto exit;
     69 }
     70 
     71 static void
     72 release_refractive_index
     73   (ref_T* address)
     74 {
     75   struct sphin_refractive_index* refr_ind = NULL;
     76   struct sphin* sphin  = NULL;
     77 
     78   ASSERT(NULL != address);
     79 
     80   refr_ind = CONTAINER_OF(address, struct sphin_refractive_index, ref);
     81   sphin = refr_ind->sphin;
     82   if (NULL != refr_ind->n_real) {
     83     SPHIN(spectral_property_ref_put(refr_ind->n_real));
     84   }
     85   if (NULL != refr_ind->n_imag) {
     86     SPHIN(spectral_property_ref_put(refr_ind->n_imag));
     87   }
     88   sphin = refr_ind->sphin;
     89   MEM_RM(sphin->allocator, refr_ind);
     90   SPHIN(ref_put(sphin));
     91 }
     92 
     93 /*******************************************************************************
     94  * Local functions
     95  ******************************************************************************/
     96 res_T
     97 parse_refractive_index
     98   (struct sphin* sphin,
     99    struct txtrdr* txtrdr,
    100    struct sphin_refractive_index** out_refractive_index)
    101 {
    102   char* token = NULL;
    103   char* token_ptr = NULL;
    104   char* keyword = NULL;
    105   char* filename = NULL;
    106   struct sphin_refractive_index* refr_ind = NULL;
    107   struct str line;
    108   res_T res = RES_OK;
    109 
    110   ASSERT(NULL != sphin);
    111   ASSERT(NULL != txtrdr);
    112   ASSERT(NULL != out_refractive_index);
    113 
    114   str_init(sphin->allocator, &line);
    115 
    116   res = refractive_index_create(sphin, &refr_ind);
    117   if (RES_OK != res) { goto error; }
    118 
    119   res = txtrdr_read_line(txtrdr);
    120   if (RES_OK != res) { goto error; }
    121 
    122   while (NULL != txtrdr_get_line(txtrdr)) {
    123     res = str_set(&line, txtrdr_get_cline(txtrdr));
    124     if (RES_OK != res) { goto error; }
    125 
    126     token = strtok_r(str_get(&line), ":", &token_ptr);
    127     if (NULL == token){ res = RES_BAD_ARG; goto error; }
    128     keyword = trim_keyword(token);
    129     filename = trim_keyword(token_ptr);
    130 
    131     if (0 == strcmp(keyword, "n_real")) {
    132       res = parse_spectral_property(sphin, filename, &refr_ind->n_real);
    133       if (RES_OK != res) { goto error; }
    134       res = txtrdr_read_line(txtrdr);
    135       if (RES_OK != res) { goto error; }
    136     }
    137     else if (0 == strcmp(keyword, "n_imag")) {
    138       res = parse_spectral_property(sphin, filename, &refr_ind->n_imag);
    139       if (RES_OK != res) { goto error; }
    140       res = txtrdr_read_line(txtrdr);
    141       if (RES_OK != res) { goto error; }
    142     }
    143     else {
    144       break;
    145     }
    146   }
    147 
    148 exit:
    149   str_release(&line);
    150   *out_refractive_index = refr_ind;
    151   return res;
    152 error:
    153   if (NULL != refr_ind) {
    154     SPHIN(refractive_index_ref_put(refr_ind));
    155   }
    156   refr_ind = NULL;
    157   goto exit;
    158 }
    159 
    160 /*******************************************************************************
    161  * Exported functions
    162  ******************************************************************************/
    163 res_T
    164 sphin_refractive_index_ref_get
    165   (struct sphin_refractive_index* refr_ind)
    166 {
    167   if (NULL == refr_ind) {
    168     return RES_BAD_ARG;
    169   }
    170   ref_get(&refr_ind->ref);
    171   return RES_OK;
    172 }
    173 
    174 res_T
    175 sphin_refractive_index_ref_put
    176   (struct sphin_refractive_index* refr_ind)
    177 {
    178   if (NULL == refr_ind) {
    179     return RES_BAD_ARG;
    180   }
    181   ref_put(&refr_ind->ref, release_refractive_index);
    182   return RES_OK;
    183 }
    184 
    185 res_T
    186 sphin_refractive_index_get_n_real
    187   (struct sphin_refractive_index* refr_ind,
    188    struct sphin_spectral_property** n_real)
    189 {
    190   if (NULL == refr_ind || NULL == n_real) {
    191     return RES_BAD_ARG;
    192   }
    193   *n_real = refr_ind->n_real;
    194   return RES_OK;
    195 }
    196 
    197 res_T
    198 sphin_refractive_index_get_n_imag
    199   (struct sphin_refractive_index* refr_ind,
    200    struct sphin_spectral_property** n_imag)
    201 {
    202   if (NULL == refr_ind || NULL == n_imag) {
    203     return RES_BAD_ARG;
    204   }
    205   *n_imag = refr_ind->n_imag;
    206   return RES_OK;
    207 }