gnuastro-commits
[Top][All Lists]
Advanced

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

[gnuastro-commits] master 93afc48 3/6: Macro to read numbers in txtarray


From: Mohammad Akhlaghi
Subject: [gnuastro-commits] master 93afc48 3/6: Macro to read numbers in txtarray is now a function
Date: Fri, 12 Aug 2016 23:58:02 +0000 (UTC)

branch: master
commit 93afc48eba481a9d72abb055c21efd1c77631d37
Author: Mohammad Akhlaghi <address@hidden>
Commit: Mohammad Akhlaghi <address@hidden>

    Macro to read numbers in txtarray is now a function
    
    The CONVERTANDSAVE macro in `lib/txtarray.c' was changed to a new
    `convertandsave' function. This makes debugging and checking of the results
    much more easier. Also some comments were added for further clarification.
    
    This was mainly done as part of the work on task #14115 ("MakeProfiles flat
    profiles have a given value"), since the input catalog must contain NaN
    values. Ultimately there was no problem here, but the code is now easier to
    read and debug.
---
 lib/txtarray.c |   48 +++++++++++++++++++++++++++++++-----------------
 1 file changed, 31 insertions(+), 17 deletions(-)

diff --git a/lib/txtarray.c b/lib/txtarray.c
index a8d816b..638bd79 100644
--- a/lib/txtarray.c
+++ b/lib/txtarray.c
@@ -142,18 +142,24 @@ savetolog(FILE **log, char *filename, size_t lineno, 
size_t s0,
 
 
 /* Macro functions: */
-#define CONVERTANDSAVE {                                                \
-    errno=0; tailptr=NULL;                                              \
-  array[ts0*s1+ts1]=strtod(token, &tailptr);                            \
-  if(errno)                                                             \
-    error_at_line(EXIT_FAILURE, errno, "%s", lineno, token, filename);  \
-  if(*tailptr!='\0')                                                    \
-    {                                                                   \
-      savetolog(&log, filename, lineno, ts0, ts1, token);               \
-      array[ts0*s1+ts1]=NAN;                                            \
-    }                                                                   \
-  ++ts1;                                                                \
-  }
+double
+convertandsave(char *token, size_t lineno, char *filename, FILE *log,
+               size_t ts0, size_t ts1)
+{
+  double dout;
+  char *tailptr=NULL;
+
+  errno=0;
+  dout=strtod(token, &tailptr);
+  if(errno)
+    error_at_line(EXIT_FAILURE, errno, "%s", lineno, token, filename);
+  if(*tailptr!='\0')
+    {
+      savetolog(&log, filename, lineno, ts0, ts1, token);
+      dout=NAN;
+    }
+  return dout;
+}
 
 
 
@@ -163,7 +169,7 @@ void
 filltable(char *filename, double *array, size_t s0, size_t s1)
 {
   FILE *fp, *log=NULL;
-  char *line=NULL, *token, *tailptr;
+  char *line=NULL, *token;
   size_t len=200, lineno=0, ts0, ts1;
 
   /* Open the file: */
@@ -201,9 +207,15 @@ filltable(char *filename, double *array, size_t s0, size_t 
s1)
         continue;
 
       /* Convert the first token and put it into the array: */
-      CONVERTANDSAVE;
-
-      /* Read the rest of the tokens: */
+      array[ts0*s1+ts1]=convertandsave(token, lineno, filename,
+                                       log, ts0, ts1);
+      ++ts1;
+
+      /* Read the rest of the tokens in this line. We had to do the first
+         token separately because of the nature of strtok: it can only be
+         used recursively after one proper call, see the GNU C Library
+         documentation for a complete explanation (this is a C standard
+         function, but GNU C Library has a wonderful manual). */
       while( (token=strtok(NULL, GAL_TXTARRAY_DELIMITERS))!=NULL )
         {
           if(ts1>=s1)
@@ -211,7 +223,9 @@ filltable(char *filename, double *array, size_t s0, size_t 
s1)
                           "Too many columns on this line. The number of "
                           "columns should be the same as the first row "
                           "of the table.");
-          CONVERTANDSAVE;
+          array[ts0*s1+ts1]=convertandsave(token, lineno, filename,
+                                           log, ts0, ts1);
+          ++ts1;
         }
       if(ts1<s1-1)                /* It should be s1-1. */
         error_at_line(EXIT_FAILURE, 0, filename, lineno,



reply via email to

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