--- /dev/null
+/*
+Safe string-to-* conversion
+
+Copyright (C) 2013 W. Trevor King
+
+This program is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#ifndef _CONFIG_H_
+#define _CONFIG_H_
+
+#define HAVE_COMPLEX /* enable safe_strtoc() */
+
+#endif /* _CONFIG_H_ */
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
-#include <stdlib.h> /* for strtod(), exit(), EXIT_* */
+#include <stdlib.h> /* for strtod(), exit(), EXIT_*, free() */
#include <stdio.h> /* for stderr, *printf(), perror() */
#include <errno.h> /* for errno */
-#include "safe-strto.h" /* check safe_strto*() declarations */
+#include "config.h" /* enabling safe_strtoc() */
+#ifdef HAVE_COMPLEX
+# include <string.h> /* for strdup(), strtok_r() */
+# include <complex.h> /* for the complex type qualifier */
+#endif /* HAVE_COMPLEX */
+#include "safe-strto.h" /* check safe_strto*() declarations */
long int safe_strtol(const char *str, int base, const char *name)
{
}
return x;
}
+
+#ifdef HAVE_COMPLEX
+
+double complex safe_strtoc(const char *str, const char *name)
+{
+ char *real, *imag, *tail, *saveptr;
+ char *local_str;
+ double complex z;
+
+ local_str = strdup(str);
+ if (!local_str) {
+ fprintf(stderr, "could not duplicate %s for %s\n", str, name);
+ perror("strdup");
+ exit(EXIT_FAILURE);
+ }
+ real = strtok_r(local_str, ",", &saveptr);
+ imag = strtok_r(NULL, ",", &saveptr);
+ tail = strtok_r(NULL, ",", &saveptr);
+ if (!real || !imag || tail) { /* '', 'x', or 'x,y,z...' */
+ fprintf(stderr, "invalid value for %s (%s), use 'REAL,IMAGE'\n",
+ str, name);
+ free(local_str);
+ exit(EXIT_FAILURE);
+ }
+ z = safe_strtod(real, name) + I * safe_strtod(imag, name);
+ free(local_str);
+ return z;
+}
+
+#endif /* HAVE_COMPLEX */
long int safe_strtol(const char *str, int base, const char *name);
double safe_strtod(const char *str, const char *name);
+#ifdef HAVE_COMPLEX
+double complex safe_strtoc(const char *str, const char *name);
+#endif /* HAVE_COMPLEX */
+
#endif /* _SAFE_STRTO_H_ */