sgas_lint.c (3841B)
1 /* Copyright (C) 2025, 2026 |Méso|Star> (contact@meso-star.com) 2 * Copyright (C) 2025, 2026 Université de Lorraine 3 * 4 * This program is free software: you can redistribute it and/or modify 5 * it under the terms of the GNU General Public License as published by 6 * the Free Software Foundation, either version 3 of the License, or 7 * (at your option) any later version. 8 * 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * 14 * You should have received a copy of the GNU General Public License 15 * along with this program. If not, see <http://www.gnu.org/licenses/>. */ 16 17 #define _POSIX_C_SOURCE 200112L /* getopt support */ 18 19 #include "sgas.h" 20 21 #include <rsys/mem_allocator.h> 22 23 #include <stdio.h> 24 #include <unistd.h> /* getopt */ 25 26 struct args { 27 char* mesh; /* Path to a tetrahedral mesh in smsh(5) format */ 28 29 /* Path to the list of time varying atrtp(5) files. 30 * NULL <=> read from standard input */ 31 char* therm_props_list; 32 33 int verbose; /* Verbosity level */ 34 int quit; 35 }; 36 #define ARGS_DEFAULT__ {0} 37 static const struct args ARGS_DEFAULT = ARGS_DEFAULT__; 38 39 struct cmd { 40 struct args args; 41 struct sgas* gas; 42 }; 43 #define CMD_NULL__ {0} 44 static const struct cmd CMD_NULL = CMD_NULL__; 45 46 /******************************************************************************* 47 * Helper functions 48 ******************************************************************************/ 49 static void 50 usage(const char* name, FILE* stream) 51 { 52 fprintf(stream, "usage: %s [-hv] -m mesh [therm_props_list]\n", name); 53 } 54 55 static res_T 56 args_init(struct args* args, int argc, char** argv) 57 { 58 int opt = 0; 59 res_T res = RES_OK; 60 ASSERT(args && argv); 61 62 *args = ARGS_DEFAULT; 63 64 while((opt = getopt(argc, argv, "hm:v")) != -1) { 65 switch(opt) { 66 case 'h': 67 usage(argv[0], stdout); 68 args->quit = 1; 69 goto exit; 70 case 'm': args->mesh = optarg; break; 71 case 'v': args->verbose += (args->verbose < 3); break; 72 default: res = RES_BAD_ARG; break; 73 } 74 if(res != RES_OK) { 75 if(optarg) { 76 fprintf(stderr, "%s: invalid option argument '%s' -- '%c'\n", 77 argv[0], optarg, opt); 78 } 79 goto error; 80 } 81 } 82 83 if(optind < argc) args->therm_props_list = argv[optind]; 84 85 if(!args->mesh) { 86 res = RES_BAD_ARG; 87 goto error; 88 } 89 90 exit: 91 return res; 92 error: 93 usage(argv[0], stderr); 94 goto exit; 95 } 96 97 static void 98 cmd_release(struct cmd* cmd) 99 { 100 ASSERT(cmd); 101 if(cmd->gas) SGAS(ref_put(cmd->gas)); 102 } 103 104 static res_T 105 cmd_init(struct cmd* cmd, const struct args* args) 106 { 107 struct sgas_create_args gas_args = SGAS_CREATE_ARGS_DEFAULT; 108 res_T res = RES_OK; 109 ASSERT(cmd && args); 110 111 cmd->args = *args; 112 113 gas_args.mesh.name = args->mesh; 114 if(args->therm_props_list) { 115 gas_args.therm_props_list.name = args->therm_props_list; 116 } else { 117 gas_args.therm_props_list.name = "stdin"; 118 gas_args.therm_props_list.stream = stdin; 119 } 120 gas_args.verbose = args->verbose; 121 122 if((res = sgas_create(&gas_args, &cmd->gas)) != RES_OK) goto error; 123 124 exit: 125 return res; 126 error: 127 cmd_release(cmd); 128 *cmd = CMD_NULL; 129 goto exit; 130 } 131 132 /******************************************************************************* 133 * The program 134 ******************************************************************************/ 135 int 136 main(int argc, char** argv) 137 { 138 struct args args = ARGS_DEFAULT; 139 struct cmd cmd = CMD_NULL; 140 int err = 0; 141 res_T res = RES_OK; 142 143 if((res = args_init(&args, argc, argv)) != RES_OK) goto error; 144 if(args.quit) goto exit; 145 146 if((res = cmd_init(&cmd, &args)) != RES_OK) goto error; 147 148 exit: 149 cmd_release(&cmd); 150 CHK(mem_allocated_size() == 0); 151 return err; 152 error: 153 err = 1; 154 goto exit; 155 }