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

commit 74503ba38a7397b5bf3413f1fbd9a3662caa2c8b
parent fb230c8aa8888b0ad3fe89d1527cbfcfd7313103
Author: Eduardo Fontana Lazzari <edufonlaz@gmail.com>
Date:   Wed, 19 Mar 2025 14:53:16 +0100

Split parse_direction_distribution into multiple functions

The function was too long and contained many nested if statements,
making it difficult to read.

Also, ensure that all pointers are initialized to NULL.

Diffstat:
Msrc/sphin_brdf.c | 6+++---
Msrc/sphin_sensor.c | 10+++++-----
Msrc/sphin_source.c | 130++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-----------------
Msrc/sphin_surface.c | 6+++---
4 files changed, 114 insertions(+), 38 deletions(-)

diff --git a/src/sphin_brdf.c b/src/sphin_brdf.c @@ -97,9 +97,9 @@ parse_brdf char* value, struct sphin_brdf** out_brdf) { - char* brdf_type; - char* str_reflectivity; - char* token_ptr; + char* brdf_type = NULL; + char* str_reflectivity = NULL; + char* token_ptr = NULL; double reflectivity; struct sphin_brdf* brdf = NULL; res_T res = RES_OK; diff --git a/src/sphin_sensor.c b/src/sphin_sensor.c @@ -139,11 +139,11 @@ parse_sensor const char* name, struct sphin_sensor** out_sensor) { - char* keyword; - char* token; - char* token_ptr; - char* value; - struct sphin_sensor* sensor; + char* keyword = NULL; + char* token = NULL; + char* token_ptr = NULL; + char* value = NULL; + struct sphin_sensor* sensor = NULL; struct str line; res_T res = RES_OK; diff --git a/src/sphin_source.c b/src/sphin_source.c @@ -100,17 +100,98 @@ release_source(ref_T* address) } static res_T +parse_lambertian_direction_distribution + (struct sphin_source_direction_distribution* dir_dist, + struct txtrdr* txtrdr, + char* token) +{ + char* token_ptr = NULL; + + ASSERT(NULL != dir_dist); + ASSERT(NULL != txtrdr); + + (void)txtrdr; + + token = strtok_r(token, " \t", &token_ptr); + if (NULL != token) { return RES_BAD_ARG; } + + dir_dist->type = SPHIN_SOURCE_DIRECTION_ISOTROPIC; + return RES_OK; +} + +static res_T +parse_collim_direction_distribution + (struct sphin_source_direction_distribution* dir_dist, + struct txtrdr* txtrdr, + char* token) +{ + char* str_direction = NULL; + char* token_ptr = NULL; + double direction[3]; + res_T res = RES_OK; + + ASSERT(NULL != dir_dist); + ASSERT(NULL != txtrdr); + + if (NULL == token) { res = RES_BAD_ARG; goto error; } + + dir_dist->type = SPHIN_SOURCE_DIRECTION_COLLIM; + str_direction = strtok_r(token, " \t", &token_ptr); + if (0 == strcmp(str_direction, "NORMAL")){ + direction[0] = direction[1] = direction[2] = 0; + } + else{ + /* TODO implement parsing of a vector, taking into account the formatting + * in the input file to be decided */ + res = RES_BAD_ARG; goto error; + } + + d3_set(dir_dist->collim.direction, direction); + +exit: + return res; +error: + goto exit; +} + +static res_T +parse_cos_pow_n_direction_distribution + (struct sphin_source_direction_distribution* dir_dist, + struct txtrdr* txtrdr, + char* token) +{ + char* str_collimation_degree = NULL; + char* token_ptr = NULL; + double collimation_degree; + res_T res = RES_OK; + + ASSERT(NULL != dir_dist); + ASSERT(NULL != txtrdr); + + if (NULL == token) { res = RES_BAD_ARG; goto error; } + + dir_dist->type = SPHIN_SOURCE_DIRECTION_COS_POW_N; + + str_collimation_degree = strtok_r(token, " \t", &token_ptr); + res = cstr_to_double(str_collimation_degree, &collimation_degree); + if (RES_OK != res) { res = RES_BAD_ARG; goto error; } + if (collimation_degree < 0) { res = RES_BAD_ARG; goto error; } + dir_dist->cos_pow_n.collimation_degree = collimation_degree; + +exit: + return res; +error: + goto exit; +} + +static res_T parse_direction_distribution (struct sphin_source* source, struct txtrdr* txtrdr, char* value) { - char* direction_distribution_type; - char* str_collimation_degree; - char* str_direction; - char* token_ptr; - double collimation_degree; - double direction[3] = {0, 0, 0}; + char* direction_distribution_type = NULL; + char* token_ptr = NULL; res_T res = RES_OK; ASSERT(NULL != source); @@ -124,34 +205,29 @@ parse_direction_distribution /* Lambertian source */ if (0 == strcmp(direction_distribution_type, "LAMBERT")){ - source->direction_distribution.type = SPHIN_SOURCE_DIRECTION_ISOTROPIC; + res = parse_lambertian_direction_distribution( + &source->direction_distribution, + txtrdr, + token_ptr); } /* Collimated source */ else if (0 == strcmp(direction_distribution_type, "COLLIM")){ - source->direction_distribution.type = SPHIN_SOURCE_DIRECTION_COLLIM; - str_direction = strtok_r(NULL, " \t", &token_ptr); - if (0 == strcmp(str_direction, "NORMAL")){ - direction[0] = direction[1] = direction[2] = 0; - } - else{ - /* TODO implement parsing of a vector, taking into account the formatting - * in the input file to be decided */ - res = RES_BAD_ARG; goto error; - } - d3_set(source->direction_distribution.collim.direction, direction); + res = parse_collim_direction_distribution( + &source->direction_distribution, + txtrdr, + token_ptr); } /* Cos^n model of direction distribution */ else if (0 == strcmp(direction_distribution_type, "COS_POW_N")){ - source->direction_distribution.type = SPHIN_SOURCE_DIRECTION_COS_POW_N; - str_collimation_degree = strtok_r(NULL, " \t", &token_ptr); - res = cstr_to_double(str_collimation_degree, &collimation_degree); - if (RES_OK != res) { res = RES_BAD_ARG; goto error; } - if (collimation_degree < 0) { res = RES_BAD_ARG; goto error; } - source->direction_distribution.cos_pow_n.collimation_degree = collimation_degree; + res = parse_cos_pow_n_direction_distribution( + &source->direction_distribution, + txtrdr, + token_ptr); } else { res = RES_BAD_ARG; goto error; } + if (RES_OK != res) {goto error;} res = txtrdr_read_line(txtrdr); if (RES_OK != res) { goto error; } @@ -168,9 +244,9 @@ parse_flux_density struct txtrdr* txtrdr, char* value) { - char* flux_density_unit; - char* str_flux_density; - char* token_ptr; + char* flux_density_unit = NULL; + char* str_flux_density = NULL; + char* token_ptr = NULL; double flux_density; res_T res = RES_OK; diff --git a/src/sphin_surface.c b/src/sphin_surface.c @@ -98,8 +98,8 @@ release_surface(ref_T* address) size_t i, ngeometries; struct sphin* sphin = NULL; struct sphin_brdf* brdf = NULL; - struct sphin_geometry** geometries= NULL; - struct sphin_sensor* sensor =NULL; + struct sphin_geometry** geometries = NULL; + struct sphin_sensor* sensor = NULL; struct sphin_source* source = NULL; struct sphin_surface* surface = NULL; ASSERT(NULL != address); @@ -144,7 +144,7 @@ parse_geometry struct txtrdr* txtrdr, char* value) { - struct sphin_geometry* geom; + struct sphin_geometry* geom = NULL; res_T res = RES_OK; ASSERT(NULL != surface);