diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2023-03-27 00:36:00 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-03-27 00:36:00 +0100 |
| commit | 3d765a385b4345035433e6c361e6db4f1f07f75b (patch) | |
| tree | 1af11a7074a0716400ca48ae3082d545c9e911e8 /challenge-209/duncan-c-white/C/parseints.c | |
| parent | 711607e7c4455a272429fc62c97a8bafc4ca16a3 (diff) | |
| parent | 07744bc404fe0be15f6a50ab42d298df77464b81 (diff) | |
| download | perlweeklychallenge-club-3d765a385b4345035433e6c361e6db4f1f07f75b.tar.gz perlweeklychallenge-club-3d765a385b4345035433e6c361e6db4f1f07f75b.tar.bz2 perlweeklychallenge-club-3d765a385b4345035433e6c361e6db4f1f07f75b.zip | |
Merge pull request #7807 from dcw803/master
imported my solutions to this week's tasks, task 1 in Perl and C, task 2 in Perl…
Diffstat (limited to 'challenge-209/duncan-c-white/C/parseints.c')
| -rw-r--r-- | challenge-209/duncan-c-white/C/parseints.c | 114 |
1 files changed, 114 insertions, 0 deletions
diff --git a/challenge-209/duncan-c-white/C/parseints.c b/challenge-209/duncan-c-white/C/parseints.c new file mode 100644 index 0000000000..80408d3382 --- /dev/null +++ b/challenge-209/duncan-c-white/C/parseints.c @@ -0,0 +1,114 @@ +// Simple routine to parse one or more arguments, +// looking for individual +ints or comma-separated +// lists of +ints. +// + +#include <stdio.h> +#include <stdlib.h> +#include <stdbool.h> +#include <string.h> +#include <ctype.h> +#include <assert.h> + +#include "args.h" +#include "printarray.h" +#include "parseints.h" + +typedef struct +{ + int nel; // current number of elements + int maxel; // maximum number of elements allocated + int *list; // malloc()d list of integers +} intlist; + + +// +// intlist il.. then initialize il.. then: +// add_one( element, &il ); +// +static void add_one( int x, intlist *p ) +{ + if( p->nel > p->maxel ) + { + p->maxel += 128; + p->list = realloc( p->list, p->maxel ); + assert( p->list != NULL ); + } + #if 0 + if( debug ) + { + printf( "PIA: appending %d to result at " + "pos %d\n", x, p->nel ); + } + #endif + p->list[p->nel++] = x; +} + + +// +// intlist il.. then initialize il.. then: +// add_one_arg( argstr, &il ); +// +static void add_one_arg( char *argstr, intlist *p ) +{ + int x; + if( !check_unsigned_int(argstr,&x) ) + { + fprintf( stderr, "PIA: arg %s must be +int\n", argstr ); + exit(1); + } + add_one( x, p ); +} + + +// +// int nel; +// int *ilist = parse_int_args( argc, argv, argno, &nel ); +// process all arguments argv[argno..argc-1], extracting either +// single ints or comma-separated lists of ints from those arguments, +// accumulate all integers in a dynarray list, storing the total number +// of elements in nel. This list must be freed by the caller. +// Note that the list of elements used to be terminated by a -1 value, +// but I've commented this out from now on. +// +int *parse_int_args( int argc, char **argv, int argno, int *nel ) +{ + int *result = malloc( 128 * sizeof(int) ); + assert( result != NULL ); + intlist il = { 0, 128, result }; + + #if 0 + if( debug ) + { + printf( "PIA: parsing ints from args %d..%d\n", argno, argc-1 ); + } + #endif + for( int i=argno; i<argc; i++ ) + { + assert( strlen(argv[i]) < 1024 ); + char copy[1024]; + strcpy( copy, argv[i] ); + char *com; + char *s; + for( s=copy; (com = strchr(s,',')) != NULL; s=com+1 ) + { + *com = '\0'; + add_one_arg( s, &il ); + } + add_one_arg( s, &il ); + } + + //add_one( -1, &il ); + + #if 0 + if( debug ) + { + printf( "PIA: final list is " ); + print_int_array( 80, il.nel, il.list, ',', stdout ); + putchar( '\n' ); + } + #endif + + *nel = il.nel; + return il.list; +} |
