# include # include "ppc_evolution.h" # include "ppc_fetch_line.h" # include "ppc_ll.h" # include "ppc_xmalloc.h" # define BUFLEN 1024 static int get_world_dimens ( struct world *world, char *str, int lineno ); static int get_eden_dimens ( struct world *world, char *str, int lineno ); static int get_plant_energy ( struct world *world, char *str, int lineno ); static int get_reproduction_threshold ( struct world *world, char *str, int lineno ); static struct animal *get_animal_specs ( char *str, int lineno ); static char *fetch_line_aux ( char *buf, int buflen, FILE *stream, int *lineno ); int read_wdf ( struct world *world ); /******************************************************************************/ static int get_world_dimens ( struct world *world, char *str, int lineno ) /******************************************************************************/ /* Purpose: get_world_dimens() reads the world dimensions from the WDF file. Licensing: This code is distributed under the MIT license. Modified: 02 May 2024 Author: John Burkardt Reference: Rouben Rostamian, Programming Projects in C for Students of Engineering, Science, and Mathematics, SIAM, 2014, ISBN: 978-1-611973-49-5 Input: struct world *world: a pointer to the world structure. char *str: the text of the line being read. int lineno: the line number. Output: struct world *world: a pointer to the world structure. The height and width parameters of the world have been stored. int value: 1 for success, 0 for failure. */ { int value; if ( sscanf ( str, "World %d %d", &world->world_h, &world->world_w ) == 2 ) { value = 1; } else { fprintf ( stderr, "stdin:line %d: expected World dimensions here.\n", lineno ); value = 0; } return value; } /******************************************************************************/ static int get_eden_dimens ( struct world *world, char *str, int lineno ) /******************************************************************************/ /* Purpose: get_eden_dimens() reads the Eden dimensions from the WDF file. Licensing: This code is distributed under the MIT license. Modified: 02 May 2024 Author: John Burkardt Reference: Rouben Rostamian, Programming Projects in C for Students of Engineering, Science, and Mathematics, SIAM, 2014, ISBN: 978-1-611973-49-5 Input: struct world *world: a pointer to the world structure. char *str: the text of the line being read. int lineno: the line number. Output: struct world *world: a pointer to the world structure. The height and width parameters of Eden have been stored. int value: 1 for success, 0 for failure. */ { int value; if ( sscanf ( str, "Eden %d %d", &world->eden_h, &world->eden_w ) == 2 ) { value = 1; } else { fprintf ( stderr, "stdin:line %d: expected Eden dimensions here.\n", lineno ); value = 0; } return value; } /******************************************************************************/ static int get_plant_energy ( struct world *world, char *str, int lineno ) /******************************************************************************/ /* Purpose: get_plant_energy() reads the plant energy from the WDF file. Licensing: This code is distributed under the MIT license. Modified: 02 May 2024 Author: John Burkardt Reference: Rouben Rostamian, Programming Projects in C for Students of Engineering, Science, and Mathematics, SIAM, 2014, ISBN: 978-1-611973-49-5 Input: struct world *world: a pointer to the world structure. char *str: the text of the line being read. int lineno: the line number. Output: struct world *world: a pointer to the world structure. The plant energy has been stored. int value: 1 for success, 0 for failure. */ { int value; if ( sscanf ( str, "plant_energy %d", &world->plant_energy ) == 1 ) { value = 1; } else { fprintf ( stderr, "stdin:line %d: expected plant energy here.\n", lineno ); value = 0; } return value; } /******************************************************************************/ static int get_reproduction_threshold ( struct world *world, char *str, int lineno ) /******************************************************************************/ /* Purpose: get_reproduction_threshold() reads the reproduction threshold from the WDF file. Licensing: This code is distributed under the MIT license. Modified: 02 May 2024 Author: John Burkardt Reference: Rouben Rostamian, Programming Projects in C for Students of Engineering, Science, and Mathematics, SIAM, 2014, ISBN: 978-1-611973-49-5 Input: struct world *world: a pointer to the world structure. char *str: the text of the line being read. int lineno: the line number. Output: struct world *world: a pointer to the world structure. The reproduction threshold has been stored. int value: 1 for success, 0 for failure. */ { int value; if ( sscanf ( str, "reproduction_threshold %d", &world->reproduction_threshold ) == 1 ) { value = 1; } else { fprintf ( stderr, "stdin:line %d: expected reproduction threshold here.\n", lineno ); value = 0; } return value; } /******************************************************************************/ static struct animal *get_animal_specs ( char *str, int lineno ) /******************************************************************************/ { struct animal *animal; int d; int e; int genes[8]; int i; int j; int r; r = sscanf ( str, "( %d %d ) %d %d | %d %d %d %d %d %d %d %d |", &i, &j, &d, &e, &genes[0], &genes[1], &genes[2], &genes[3], &genes[4], &genes[5], &genes[6], &genes[7] ); if ( r == 12 ) { animal = xmalloc ( sizeof ( animal ) ); animal->i = i; animal->j = j; animal->d = d; animal->e = e; for ( int k = 0; k < 8; k++ ) { animal->genes[k] = genes[k]; } return animal; } else { fprintf ( stderr, "stdin:line %d: expected animal description here.\n", lineno ); return NULL; } } /******************************************************************************/ static char *fetch_line_aux ( char *buf, int buflen, FILE *stream, int *lineno ) /******************************************************************************/ { char *s = fetch_line ( buf, buflen, stream, lineno ); if ( s == NULL ) { fprintf ( stderr, "stdin:line %d: premature end of input.\n", *lineno ); } return s; } /******************************************************************************/ int read_wdf ( struct world *world ) /******************************************************************************/ { struct animal *animal; int animal_count = 0; char buf[BUFLEN]; int lineno = 0; int result; char *s; result = ( s = fetch_line_aux ( buf, BUFLEN, stdin, &lineno ) ) && get_world_dimens ( world, s, lineno ) && ( s = fetch_line_aux ( buf, BUFLEN, stdin, &lineno ) ) && get_eden_dimens ( world, s, lineno ) && ( s = fetch_line_aux ( buf, BUFLEN, stdin, &lineno ) ) && get_plant_energy ( world, s, lineno ) && ( s = fetch_line_aux ( buf, BUFLEN, stdin, &lineno ) ) && get_reproduction_threshold ( world, s, lineno ); if ( ! result ) { return 0; } while ( ( s = fetch_line ( buf, BUFLEN, stdin, &lineno ) ) != NULL ) { if ( ( animal = get_animal_specs ( s, lineno ) ) == NULL ) { return 0; } else { world->herd = ll_push ( world->herd, animal ); animal_count = animal_count + 1; } } /* Oh the lengths that clever people will go to! */ fprintf ( stderr, "# %d animal%s read from the input\n", animal_count, animal_count == 1 ? "" : "s" ); return 1; }