star-4v_s

An invariant property of diffuse random walks
git clone git://git.meso-star.fr/star-4v_s.git
Log | Files | Refs | README | LICENSE

s4vs_args.c (2300B)


      1 /* Copyright (C) 2015-2018, 2021, 2024 |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 #define _POSIX_C_SOURCE 200809L /* getopt support */
     17 
     18 #include "s4vs_args.h"
     19 
     20 #include <rsys/cstr.h> /* string conversions */
     21 #include <unistd.h> /* getopt */
     22 
     23 /*******************************************************************************
     24  * Helper functions
     25  ******************************************************************************/
     26 static void
     27 usage(FILE* stream, char* name)
     28 {
     29   ASSERT(stream && name);
     30   fprintf(stream,
     31     "usage: %s [-n realisations_count] [-s scattering_coef] [file]\n",
     32     name);
     33 }
     34 
     35 /*******************************************************************************
     36  * Local functions
     37  ******************************************************************************/
     38 res_T
     39 s4vs_args_init(struct s4vs_args* args, int argc, char** argv)
     40 {
     41   int opt = 0;
     42   res_T res = RES_OK;
     43   ASSERT(args && argc && argv);
     44 
     45   *args = S4VS_ARGS_DEFAULT;
     46 
     47   while((opt = getopt(argc, argv, "n:s:")) != -1) {
     48     switch(opt) {
     49       case 'n':
     50         res = cstr_to_ulong(optarg, &args->nrealisations);
     51         if(res == RES_OK && args->nrealisations == 0) res = RES_BAD_ARG;
     52         break;
     53       case 's':
     54         res = cstr_to_double(optarg, &args->ks);
     55         if(res == RES_OK && args->ks < 0) res = RES_BAD_ARG;
     56         break;
     57       default: res = RES_BAD_ARG; break;
     58     }
     59     if(res != RES_OK) goto error;
     60   }
     61   if(optind < argc) args->filename = argv[optind];
     62 
     63 exit:
     64   return res;
     65 error:
     66   usage(stderr, argv[0]);
     67   goto exit;
     68 }
     69 
     70 void
     71 s4vs_args_release(struct s4vs_args* args)
     72 {
     73   /* Nothing to do */
     74   (void)args; /* Avoid "unused argument" warning */
     75 }