gnuastro-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[gnuastro-commits] master 56bd4e5 03/16: checkset func for some option v


From: Mohammad Akhlaghi
Subject: [gnuastro-commits] master 56bd4e5 03/16: checkset func for some option value allocations
Date: Wed, 24 Aug 2016 22:27:43 +0000 (UTC)

branch: master
commit 56bd4e58c4d5b0cbba4fcefa0104c43a2e4807f5
Author: Mohammad Akhlaghi <address@hidden>
Commit: Mohammad Akhlaghi <address@hidden>

    checkset func for some option value allocations
    
    The `gal_checkset_allocate_copy_set' was defined to simplify the option
    value allocation procedure and make the code more readable. It was
    implemented in some utilities, but strangely it was not used in others. So
    after a search through all `ui.c' and `args.h' files, it is now used in all
    the places where it should have been used.
---
 lib/gnuastro/commonargs.h |   14 ++--------
 src/TEMPLATE/main.h       |    2 +-
 src/TEMPLATE/ui.c         |   10 +------
 src/header/ui.c           |   10 +------
 src/imgcrop/ui.c          |   23 ++++-----------
 src/noisechisel/ui.c      |   68 ++++++++++-----------------------------------
 src/subtractsky/ui.c      |   66 ++++++++-----------------------------------
 7 files changed, 37 insertions(+), 156 deletions(-)

diff --git a/lib/gnuastro/commonargs.h b/lib/gnuastro/commonargs.h
index 7d85c60..8e5fe5d 100644
--- a/lib/gnuastro/commonargs.h
+++ b/lib/gnuastro/commonargs.h
@@ -248,19 +248,11 @@ gal_checkset_commonargs_cparse_opt(int key, char *arg, 
struct argp_state *state)
 
     /* Input/output: */
     case 'h':
-      errno=0;
-      cp->hdu=malloc(strlen(arg)+1);
-      if(cp->hdu==NULL) error(EXIT_FAILURE, 0, "space for hdu");
-      strcpy(cp->hdu, arg);
-      cp->hduset=1;
+      gal_checkset_allocate_copy_set(arg, &cp->hdu, &cp->hduset);
       break;
     case 'o':
-      errno=0;
-      cp->output=malloc(strlen(arg)+1);
-      if(cp->output==NULL) error(EXIT_FAILURE, 0, "space for output");
-      strcpy(cp->output, arg); /* This allocation is done so cp->output */
-      cp->outputset=1;         /* Can always be freed when set, because */
-      break;                   /* It usually needs modifications.       */
+      gal_checkset_allocate_copy_set(arg, &cp->output, &cp->outputset);
+      break;
     case 'D':
       cp->dontdelete=1;
       break;
diff --git a/src/TEMPLATE/main.h b/src/TEMPLATE/main.h
index 4ebd0f5..ff49659 100644
--- a/src/TEMPLATE/main.h
+++ b/src/TEMPLATE/main.h
@@ -48,7 +48,7 @@ along with Gnuastro. If not, see 
<http://www.gnu.org/licenses/>.
 
 struct uiparams
 {
-  char                     *inputname;  /* Name of input file.             */
+  char             *inputname;  /* Name of input file.             */
 };
 
 
diff --git a/src/TEMPLATE/ui.c b/src/TEMPLATE/ui.c
index 2f8352a..7cd2d1c 100644
--- a/src/TEMPLATE/ui.c
+++ b/src/TEMPLATE/ui.c
@@ -112,15 +112,7 @@ readconfig(char *filename, struct TEMPLATEparams *p)
 
       /* Inputs: */
       if(strcmp(name, "hdu")==0)
-        {
-          if(cp->hduset) continue;
-          errno=0;
-          cp->hdu=malloc(strlen(value)+1);
-          if(cp->hdu==NULL)
-            error(EXIT_FAILURE, errno, "space for HDU");
-          strcpy(cp->hdu, value);
-          cp->hduset=1;
-        }
+        gal_checkset_allocate_copy_set(value, &cp->hdu, &cp->hduset);
 
 
 
diff --git a/src/header/ui.c b/src/header/ui.c
index 19f1719..f2819aa 100644
--- a/src/header/ui.c
+++ b/src/header/ui.c
@@ -102,15 +102,7 @@ readconfig(char *filename, struct headerparams *p)
 
       /* Inputs: */
       if(strcmp(name, "hdu")==0)
-        {
-          if(cp->hduset) continue;
-          errno=0;
-          cp->hdu=malloc(strlen(value)+1);
-          if(cp->hdu==NULL)
-            error(EXIT_FAILURE, errno, "space for HDU");
-          strcpy(cp->hdu, value);
-          cp->hduset=1;
-        }
+        gal_checkset_allocate_copy_set(value, &cp->hdu, &cp->hduset);
 
 
 
diff --git a/src/imgcrop/ui.c b/src/imgcrop/ui.c
index 82679ff..61cc70f 100644
--- a/src/imgcrop/ui.c
+++ b/src/imgcrop/ui.c
@@ -217,25 +217,12 @@ readconfig(char *filename, struct imgcropparams *p)
           up->checkcenterset=1;
         }
       else if(strcmp(name, "output")==0)
-        {
-          if(cp->outputset) continue;
-          errno=0;
-          cp->output=malloc(strlen(value)+1);
-          if(cp->output==NULL)
-            error(EXIT_FAILURE, errno, "space for output");
-          strcpy(cp->output, value);
-          cp->outputset=1;
-        }
+        gal_checkset_allocate_copy_set(value, &cp->output, &cp->outputset);
+
       else if(strcmp(name, "suffix")==0)
-        {
-          if(up->suffixset) continue;
-          errno=0;
-          p->suffix=malloc(strlen(value)+1);
-          if(p->suffix==NULL)
-            error(EXIT_FAILURE, errno, "space for prefix");
-          strcpy(p->suffix, value);
-          up->suffixset=1;
-        }
+        gal_checkset_allocate_copy_set(value, &p->suffix, &up->suffixset);
+
+
 
       /* Read options common to all programs */
       GAL_CONFIGFILES_READ_COMMONOPTIONS_FROM_CONF
diff --git a/src/noisechisel/ui.c b/src/noisechisel/ui.c
index afa1971..3df6e97 100644
--- a/src/noisechisel/ui.c
+++ b/src/noisechisel/ui.c
@@ -102,55 +102,22 @@ readconfig(char *filename, struct noisechiselparams *p)
 
       /* Inputs: */
       if(strcmp(name, "hdu")==0)
-        {
-          if(cp->hduset) continue;
-          errno=0;
-          cp->hdu=malloc(strlen(value)+1);
-          if(cp->hdu==NULL)
-            error(EXIT_FAILURE, errno, "space for HDU");
-          strcpy(cp->hdu, value);
-          cp->hduset=1;
-        }
+        gal_checkset_allocate_copy_set(value, &cp->hdu, &cp->hduset);
+
       else if(strcmp(name, "mask")==0)
-        {
-          if(up->masknameset) continue;
-          errno=0;
-          up->maskname=malloc(strlen(value)+1);
-          if(up->maskname==NULL)
-            error(EXIT_FAILURE, errno, "space for mask name");
-          strcpy(up->maskname, value);
-          up->masknameset=1;
-        }
+        gal_checkset_allocate_copy_set(value, &up->maskname,
+                                       &up->masknameset);
+
       else if(strcmp(name, "mhdu")==0)
-        {
-          if(up->mhduset) continue;
-          errno=0;
-          up->mhdu=malloc(strlen(value)+1);
-          if(up->mhdu==NULL)
-            error(EXIT_FAILURE, errno, "space for mask HDU");
-          strcpy(up->mhdu, value);
-          up->mhduset=1;
-        }
+        gal_checkset_allocate_copy_set(value, &up->mhdu, &up->mhduset);
+
       else if(strcmp(name, "kernel")==0)
-        {
-          if(up->kernelnameset) continue;
-          errno=0;
-          up->kernelname=malloc(strlen(value)+1);
-          if(up->kernelname==NULL)
-            error(EXIT_FAILURE, errno, "space for kernel name");
-          strcpy(up->kernelname, value);
-          up->kernelnameset=1;
-        }
+        gal_checkset_allocate_copy_set(value, &up->kernelname,
+                                       &up->kernelnameset);
+
       else if(strcmp(name, "khdu")==0)
-        {
-          if(up->khduset) continue;
-          errno=0;
-          up->khdu=malloc(strlen(value)+1);
-          if(up->khdu==NULL)
-            error(EXIT_FAILURE, errno, "space for kernel HDU");
-          strcpy(up->khdu, value);
-          up->khduset=1;
-        }
+        gal_checkset_allocate_copy_set(value, &up->khdu, &up->khduset);
+
       else if(strcmp(name, "skysubtracted")==0)
         {
           if(up->skysubtractedset) continue;
@@ -177,15 +144,8 @@ readconfig(char *filename, struct noisechiselparams *p)
 
       /* Outputs */
       else if(strcmp(name, "output")==0)
-        {
-          if(cp->outputset) continue;
-          errno=0;
-          cp->output=malloc(strlen(value)+1);
-          if(cp->output==NULL)
-            error(EXIT_FAILURE, errno, "space for output");
-          strcpy(cp->output, value);
-          cp->outputset=1;
-        }
+        gal_checkset_allocate_copy_set(value, &cp->output, &cp->outputset);
+
       else if(strcmp(name, "grownclumps")==0)
         {
           if(up->grownclumpsset) continue;
diff --git a/src/subtractsky/ui.c b/src/subtractsky/ui.c
index d73f9e5..eb68215 100644
--- a/src/subtractsky/ui.c
+++ b/src/subtractsky/ui.c
@@ -102,69 +102,27 @@ readconfig(char *filename, struct subtractskyparams *p)
 
       /* Inputs: */
       if(strcmp(name, "hdu")==0)
-        {
-          if(cp->hduset) continue;
-          errno=0;
-          cp->hdu=malloc(strlen(value)+1);
-          if(cp->hdu==NULL)
-            error(EXIT_FAILURE, errno, "space for HDU");
-          strcpy(cp->hdu, value);
-          cp->hduset=1;
-        }
+        gal_checkset_allocate_copy_set(value, &cp->hdu, &cp->hduset);
+
       else if(strcmp(name, "mask")==0)
-        {
-          if(up->masknameset) continue;
-          errno=0;
-          up->maskname=malloc(strlen(value)+1);
-          if(up->maskname==NULL)
-            error(EXIT_FAILURE, errno, "space for mask name");
-          strcpy(up->maskname, value);
-          up->masknameset=1;
-        }
+        gal_checkset_allocate_copy_set(value, &up->maskname,
+                                       &up->masknameset);
+
       else if(strcmp(name, "mhdu")==0)
-        {
-          if(up->mhduset) continue;
-          errno=0;
-          up->mhdu=malloc(strlen(value)+1);
-          if(up->mhdu==NULL)
-            error(EXIT_FAILURE, errno, "space for mask HDU");
-          strcpy(up->mhdu, value);
-          up->mhduset=1;
-        }
+        gal_checkset_allocate_copy_set(value, &up->mhdu, &up->mhduset);
+
       else if(strcmp(name, "kernel")==0)
-        {
-          if(up->kernelnameset) continue;
-          errno=0;
-          up->kernelname=malloc(strlen(value)+1);
-          if(up->kernelname==NULL)
-            error(EXIT_FAILURE, errno, "space for kernel name");
-          strcpy(up->kernelname, value);
-          up->kernelnameset=1;
-        }
+        gal_checkset_allocate_copy_set(value, &up->kernelname,
+                                       &up->kernelnameset);
+
       else if(strcmp(name, "khdu")==0)
-        {
-          if(up->khduset) continue;
-          errno=0;
-          up->khdu=malloc(strlen(value)+1);
-          if(up->khdu==NULL)
-            error(EXIT_FAILURE, errno, "mpace for kernel HDU");
-          strcpy(up->khdu, value);
-          up->khduset=1;
-        }
+        gal_checkset_allocate_copy_set(value, &up->khdu, &up->khduset);
 
 
 
       /* Outputs */
       else if(strcmp(name, "output")==0)
-        {
-          if(cp->outputset) continue;
-          errno=0;
-          cp->output=malloc(strlen(value)+1);
-          if(cp->output==NULL)
-            error(EXIT_FAILURE, errno, "space for output");
-          strcpy(cp->output, value);
-          cp->outputset=1;
-        }
+        gal_checkset_allocate_copy_set(value, &cp->output, &cp->outputset);
 
 
       /* Mesh grid: */



reply via email to

[Prev in Thread] Current Thread [Next in Thread]