safe-strto-example.c: Add a pared-down argv-parsing example util/safe-strto
authorW. Trevor King <wking@tremily.us>
Mon, 11 Feb 2013 16:07:56 +0000 (11:07 -0500)
committerW. Trevor King <wking@tremily.us>
Mon, 11 Feb 2013 16:42:06 +0000 (11:42 -0500)
Makefile
safe-strto-example.c [new file with mode: 0644]

index af2d15bbbc003d4a322be1236b70dfab14e2abb0..3a4d7534053bf7ba555773c28a9fa3f4e7b6110e 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -26,7 +26,7 @@ VERSION = 1
 RELEASE = $(COURSE)-$(PACKAGE)-$(VERSION)
 RUN_PROGRAM = hello_world
 SCRIPTS =
-C_PROGRAMS = hello_world
+C_PROGRAMS = hello_world safe-strto-example
 CXX_PROGRAMS = goodbye_world
 PROGRAMS = $(C_PROGRAMS) $(CXX_PROGRAMS)
 
@@ -64,6 +64,10 @@ TAR = tar
 #   http://www.gnu.org/software/make/manual/html_node/Rules.html
 all: $(PROGRAMS)
 
+# Dependency declarations
+safe-strto-example_OBJECTS = safe-strto-example.o safe-strto.o
+safe-strto-example.o safe-strto.o: config.h safe-strto.h
+
 # target: help - display callable targets
 # Use `grep` to search this file for target comments
 help:
diff --git a/safe-strto-example.c b/safe-strto-example.c
new file mode 100644 (file)
index 0000000..0c1d4e9
--- /dev/null
@@ -0,0 +1,93 @@
+/*
+Demonstrate argument parsing with safe-strto.h
+
+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/>.
+*/
+
+#include <stdlib.h>       /* for exit(), EXIT_* */
+#include <stdio.h>        /* for stderr, *printf() */
+#include <getopt.h>       /* for getopt_long() */
+#include <complex.h>      /* for the complex type qualifier */
+#include "config.h"       /* enabling safe_strtoc() */
+#include "safe-strto.h"   /* for safe_strtol() and safe_strtoc() */
+
+static struct option long_options[] = {
+       {"my-long",    required_argument, 0, 'l' },
+       {"my-double",  required_argument, 0, 'd' },
+#ifdef HAVE_COMPLEX
+       {"my-complex", required_argument, 0, 'c' },
+#endif  /* HAVE_COMPLEX */
+       {}
+};
+
+#ifdef HAVE_COMPLEX
+static char *short_options = "l:d:c:";
+#else
+static char *short_options = "l:d:";
+#endif  /* HAVE_COMPLEX */
+
+#ifdef HAVE_COMPLEX
+void parse_args(int argc, char **argv, long *my_long, double *my_double,
+                double complex *my_complex)
+#else
+void parse_args(int argc, char **argv, long *my_long, double *my_double)
+#endif  /* HAVE_COMPLEX */
+{
+       int ch;
+
+       while (1) {
+               ch = getopt_long(argc, argv, short_options, long_options, NULL);
+               if (ch == -1)
+                       break;
+               switch(ch) {
+               case 'l':
+                       *my_long = safe_strtol(optarg, 10, "my-int");
+                       break;
+               case 'd':
+                       *my_double = safe_strtod(optarg, "my-double");
+                       break;
+#ifdef HAVE_COMPLEX
+               case 'c':
+                       *my_complex = safe_strtoc(optarg, "my-complex");
+                       break;
+#endif  /* HAVE_COMPLEX */
+               case '?':
+                       exit(EXIT_FAILURE);
+               default:
+                       printf("?? getopt returned character code 0%o ??\n", ch);
+               }
+       }
+       return;
+}
+
+int main(int argc, char **argv) {
+       long int my_long = 0;
+       double my_double = 0;
+#ifdef HAVE_COMPLEX
+       double complex my_complex = 0;
+
+       parse_args(argc, argv, &my_long, &my_double, &my_complex);
+#else
+       parse_args(argc, argv, &my_long, &my_double);
+#endif /* HAVE_COMPLEX */
+       printf("my_long:    %ld\n", my_long);
+       printf("my_double:  %g\n", my_double);
+#ifdef HAVE_COMPLEX
+       printf("my_complex: %g + %gi\n", creal(my_complex), cimag(my_complex));
+#endif  /* HAVE_COMPLEX */
+
+       return EXIT_SUCCESS;
+}