[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,
- [gnuastro-commits] master updated (0b9d8f0 -> 463dd0c), Mohammad Akhlaghi, 2016/08/12
- [gnuastro-commits] master 93afc48 3/6: Macro to read numbers in txtarray is now a function,
Mohammad Akhlaghi <=
- [gnuastro-commits] master 5d17cfa 2/6: New macro to check columns for input catalogs, Mohammad Akhlaghi, 2016/08/12
- [gnuastro-commits] master 231a69e 5/6: New function to replace non-NaN pixels in arraymanip, Mohammad Akhlaghi, 2016/08/12
- [gnuastro-commits] master 0fe062a 1/6: Two indentations corrected, Mohammad Akhlaghi, 2016/08/12
- [gnuastro-commits] master 463dd0c 6/6: MakeProfiles can now use input image as canvas, Mohammad Akhlaghi, 2016/08/12
- [gnuastro-commits] master 799c83b 4/6: Flat profiles in MakeProfiles are given values, Mohammad Akhlaghi, 2016/08/12