stardis-hbound-prog.c (3304B)
1 /* Copyright (C) 2018-2025 |Méso|Star> (contact@meso-star.com) 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 General Public License for more details. 12 * 13 * You should have received a copy of the GNU General Public License 14 * along with this program. If not, see <http://www.gnu.org/licenses/>. */ 15 16 #include "stardis-app.h" 17 #include "stardis-hbound-prog.h" 18 #include "stardis-fluid-prog.h" 19 #include "stardis-prog-properties.h" 20 #include "stardis-intface.h" 21 22 #include <rsys/rsys.h> 23 #include <rsys/mem_allocator.h> 24 #include <rsys/str.h> 25 26 #include <sdis.h> 27 28 #include <limits.h> 29 30 /******************************************************************************* 31 * Public Functions 32 ******************************************************************************/ 33 res_T 34 init_h_boundary_prog 35 (struct mem_allocator* allocator, 36 struct h_boundary_prog** dst) 37 { 38 res_T res = RES_OK; 39 int str_initialized = 0; 40 ASSERT(allocator && dst && *dst == NULL); 41 *dst = MEM_CALLOC(allocator, 1, sizeof(**dst)); 42 if(! *dst) { 43 res = RES_MEM_ERR; 44 goto error; 45 } 46 str_init(allocator, &(*dst)->name); 47 str_init(allocator, &(*dst)->prog_name); 48 str_initialized = 1; 49 (*dst)->mat_id = UINT_MAX; 50 end: 51 return res; 52 error: 53 if(str_initialized) { 54 str_release(&(*dst)->name); 55 str_release(&(*dst)->prog_name); 56 } 57 if(*dst) MEM_RM(allocator, *dst); 58 goto end; 59 } 60 61 void 62 release_h_boundary_prog 63 (struct h_boundary_prog* bound, 64 struct mem_allocator* allocator) 65 { 66 size_t i; 67 ASSERT(bound && allocator); 68 str_release(&bound->name); 69 str_release(&bound->prog_name); 70 if(bound->prog_data) 71 bound->release(bound->prog_data); 72 if(bound->possible_external_fluid) 73 release_fluid_prog(bound->possible_external_fluid, allocator); 74 for(i = 0; i < bound->argc; i++) MEM_RM(allocator, bound->argv[i]); 75 MEM_RM(allocator, bound->argv); 76 /* library_close call is managed at lib_data level */ 77 MEM_RM(allocator, bound); 78 } 79 80 res_T 81 str_print_h_boundary_prog 82 (struct str* str, 83 const struct description* desc) 84 { 85 res_T res = RES_OK; 86 const struct h_boundary_prog* b; 87 ASSERT(str && desc && DESC_IS_H(desc)); 88 89 b = desc->d.h_boundary_prog; 90 ASSERT(b->argc >= 1); /* At least one argument which is the program name */ 91 92 ERR(str_append_printf(str, 93 "programmed H boundary for %s '%s': lib='%s', " 94 "(using medium %u as external medium)", 95 (desc->type == DESC_BOUND_H_FOR_SOLID_PROG ? "solid" : "fluid"), 96 str_cget(&b->name), str_cget(&b->prog_name), 97 b->mat_id)); 98 if(b->argc > 1) { 99 size_t i; 100 ERR(str_append_printf(str, ", provided arguments:\n")); 101 for(i = 1; i < b->argc; i++) { 102 ERR(str_append_printf(str, (i+1 == b->argc ? "\t%s" : "\t%s\n"), b->argv[i])); 103 } 104 } 105 end: 106 return res; 107 error: 108 goto end; 109 } 110 111 double 112 h_bound_prog_get_hmax 113 (struct h_boundary_prog* h_boundary_props) 114 { 115 return h_boundary_props->hmax(h_boundary_props->prog_data); 116 }