test_rngrd.c (4830B)
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 |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 print_help(const char* cmd) 42 { 43 ASSERT(cmd); 44 printf( 45 "Usage: %s -m mesh -p pops -M matlst [ option ... ]\n" 46 "Test the Rad-Net GRound library\n\n", cmd); 47 printf( 48 " -h display this help and exit\n"); 49 printf( 50 " -M matlst path toward the list of ground materials (rnsl(5))\n"); 51 printf( 52 " -m mesh path toward the ground mesh (smsh(5))\n"); 53 printf( 54 " -n name ground name\n"); 55 printf( 56 " -p props path toward the ground properties (rnsp(5))\n"); 57 printf( 58 " -V validate data\n"); 59 printf( 60 " -v make the program verbose\n"); 61 printf("\n"); 62 printf( 63 "This is free software released under the GNU GPL license, version 3 or\n" 64 "later. You are free to change or redistribute it under certain\n" 65 "conditions <http://gnu.org.licenses/gpl.html>\n"); 66 } 67 68 static void 69 args_release(struct args* args) 70 { 71 ASSERT(args); 72 *args = ARGS_DEFAULT; 73 } 74 75 static res_T 76 args_init(struct args* args, int argc, char** argv) 77 { 78 res_T res = RES_OK; 79 int opt; 80 ASSERT(args && argc && argv); 81 82 *args = ARGS_DEFAULT; 83 84 while((opt = getopt(argc, argv, "hM:m:n:p:Vv")) != -1) { 85 switch(opt) { 86 case 'h': 87 print_help(argv[0]); 88 args_release(args); 89 args->quit = 1; 90 goto exit; 91 case 'M': args->rngrd.mtllst_filename = optarg; break; 92 case 'm': args->rngrd.smsh_filename = optarg; break; 93 case 'n': args->rngrd.name = optarg; break; 94 case 'p': args->rngrd.props_filename = optarg; break; 95 case 'V': args->validate = 1; break; 96 case 'v': args->rngrd.verbose = 1; break; 97 default: res = RES_BAD_ARG; break; 98 } 99 if(res != RES_OK) { 100 if(optarg) { 101 fprintf(stderr, "%s: invalid option args `%s' -- `%c'\n", 102 argv[0], optarg, opt); 103 } 104 goto error; 105 } 106 } 107 if(!args->rngrd.smsh_filename) { 108 fprintf(stderr, "Mesh filename is missing -- option `-m'\n"); 109 res = RES_BAD_ARG; 110 goto error; 111 } 112 if(!args->rngrd.props_filename) { 113 fprintf(stderr, "Properties filename is missing -- option `-p'\n"); 114 res = RES_BAD_ARG; 115 goto error; 116 } 117 if(!args->rngrd.mtllst_filename) { 118 fprintf(stderr, "The material list filename is missing -- option `-M'\n"); 119 res = RES_BAD_ARG; 120 goto error; 121 } 122 123 exit: 124 return res; 125 error: 126 args_release(args); 127 goto exit; 128 } 129 130 /******************************************************************************* 131 * Main function 132 ******************************************************************************/ 133 int 134 main(int argc, char** argv) 135 { 136 struct args args = ARGS_DEFAULT; 137 struct rngrd* rngrd = NULL; 138 res_T res = RES_OK; 139 int err = 0; 140 141 res = args_init(&args, argc, argv); 142 if(res != RES_OK) goto error; 143 if(args.quit) goto exit; 144 145 res = rngrd_create(&args.rngrd, &rngrd); 146 if(res != RES_OK) goto error; 147 148 if(args.validate) { 149 res = rngrd_validate(rngrd); 150 if(res != RES_OK) goto error; 151 } 152 153 exit: 154 args_release(&args); 155 if(rngrd) RNGRD(ref_put(rngrd)); 156 if(mem_allocated_size() !=0) { 157 fprintf(stderr, "Memory leaks: %lu bytes\n", 158 (unsigned long)mem_allocated_size()); 159 err = -1; 160 } 161 return err; 162 error: 163 err = -1; 164 goto exit; 165 }