rngrd_lint.c (4079B)
1 /* Copyright (C) 2022, 2023, 2025 Centre National de la Recherche Scientifique 2 * Copyright (C) 2022, 2023, 2025 Institut Pierre-Simon Laplace 3 * Copyright (C) 2022, 2023, 2025 Institut de Physique du Globe de Paris 4 * Copyright (C) 2022, 2023, 2025, 2026 |Méso|Star> (contact@meso-star.com) 5 * Copyright (C) 2022, 2023, 2025 Observatoire de Paris 6 * Copyright (C) 2022, 2023, 2025 Université de Reims Champagne-Ardenne 7 * Copyright (C) 2022, 2023, 2025 Université de Versaille Saint-Quentin 8 * Copyright (C) 2022, 2023, 2025 Université Paul Sabatier 9 * 10 * This program is free software: you can redistribute it and/or modify 11 * it under the terms of the GNU General Public License as published by 12 * the Free Software Foundation, either version 3 of the License, or 13 * (at your option) any later version. 14 * 15 * This program is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 * GNU General Public License for more details. 19 * 20 * You should have received a copy of the GNU General Public License 21 * along with this program. If not, see <http://www.gnu.org/licenses/>. */ 22 23 #include "rngrd.h" 24 25 #include <rsys/mem_allocator.h> 26 27 #include <getopt.h> 28 29 struct args { 30 struct rngrd_create_args rngrd; 31 int validate; 32 int quit; 33 }; 34 #define ARGS_DEFAULT__ {RNGRD_CREATE_ARGS_DEFAULT__, 0, 0} 35 static const struct args ARGS_DEFAULT = ARGS_DEFAULT__; 36 37 /******************************************************************************* 38 * Helper functions 39 ******************************************************************************/ 40 static void 41 usage(FILE* stream) 42 { 43 fprintf(stream, 44 "usage: rngrd-lint [-hVv] -M material_list -m geometry -p properties\n"); 45 } 46 47 static void 48 args_release(struct args* args) 49 { 50 ASSERT(args); 51 *args = ARGS_DEFAULT; 52 } 53 54 static res_T 55 args_init(struct args* args, int argc, char** argv) 56 { 57 res_T res = RES_OK; 58 int opt; 59 ASSERT(args && argc && argv); 60 61 *args = ARGS_DEFAULT; 62 63 while((opt = getopt(argc, argv, "hM:m:n:p:Vv")) != -1) { 64 switch(opt) { 65 case 'h': 66 usage(stdout); 67 args_release(args); 68 args->quit = 1; 69 goto exit; 70 case 'M': args->rngrd.mtllst_filename = optarg; break; 71 case 'm': args->rngrd.smsh_filename = optarg; break; 72 case 'n': args->rngrd.name = optarg; break; 73 case 'p': args->rngrd.props_filename = optarg; break; 74 case 'V': args->validate = 1; break; 75 case 'v': args->rngrd.verbose = 1; break; 76 default: res = RES_BAD_ARG; break; 77 } 78 if(res != RES_OK) { 79 if(optarg) { 80 fprintf(stderr, "%s: invalid option args `%s' -- `%c'\n", 81 argv[0], optarg, opt); 82 } 83 goto error; 84 } 85 } 86 #define MANDATORY(Cond, Name, Opt) { \ 87 if(!(Cond)) { \ 88 fprintf(stderr, "rngrd-lint: missing %s -- option '-%c'\n", (Name), (Opt)); \ 89 res = RES_BAD_ARG; \ 90 goto error; \ 91 } \ 92 } (void)0 93 MANDATORY(args->rngrd.mtllst_filename, "material list", 'M'); 94 MANDATORY(args->rngrd.smsh_filename, "mesh", 'm'); 95 MANDATORY(args->rngrd.props_filename, "properties", 'p'); 96 #undef MANDATORY 97 98 exit: 99 return res; 100 error: 101 usage(stderr); 102 goto exit; 103 } 104 105 /******************************************************************************* 106 * Main function 107 ******************************************************************************/ 108 int 109 main(int argc, char** argv) 110 { 111 struct args args = ARGS_DEFAULT; 112 struct rngrd* rngrd = NULL; 113 res_T res = RES_OK; 114 int err = 0; 115 116 res = args_init(&args, argc, argv); 117 if(res != RES_OK) goto error; 118 if(args.quit) goto exit; 119 120 res = rngrd_create(&args.rngrd, &rngrd); 121 if(res != RES_OK) goto error; 122 123 if(args.validate) { 124 res = rngrd_validate(rngrd); 125 if(res != RES_OK) goto error; 126 } 127 128 exit: 129 args_release(&args); 130 if(rngrd) RNGRD(ref_put(rngrd)); 131 if(mem_allocated_size() !=0) { 132 fprintf(stderr, "Memory leaks: %lu bytes\n", 133 (unsigned long)mem_allocated_size()); 134 err = -1; 135 } 136 return err; 137 error: 138 err = -1; 139 goto exit; 140 }