# include # include "ppc_evolution.h" # include "ppc_evolution_io.h" # include "ppc_fetch_line.h" # include "ppc_ll.h" # include "ppc_xmalloc.h" # define BUFLEN 1024 /******************************************************************************/ 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; } /******************************************************************************/ 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; } /******************************************************************************/ 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; } /******************************************************************************/ 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; } /******************************************************************************/ struct animal *get_animal_specs ( char *str, int lineno ) /******************************************************************************/ /* Purpose: get_animal_specs() reads animal specifications from the WDF file. Licensing: This code is distributed under the MIT license. Modified: 24 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: char *str: the text of the line being read. int lineno: the line number. Output: struct animal *animal: the animal specification that was read. */ { 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 ) { fprintf ( stderr, "stdin:line %d: expected animal description here.\n", lineno ); return NULL; } 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; } /******************************************************************************/ char *fetch_line_aux ( char *buf, int buflen, FILE *stream, int *lineno ) /******************************************************************************/ /* Purpose: fetch_line_aux() is a helper function. Discussion: The function fetch_line() returns NULL when the input is exhausted. But in the context of reading a WDF, if the end of input occurs before all the world parameters have been retrieved, then this is an error condition. We call fetch_line() in cases where no more input is acceptable, or we call fetch_line_aux() if more input must be coming. Licensing: This code is distributed under the MIT license. Modified: 24 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: char *buf: the input buffer. int buflen: the size of the input buffer. FILE *stream: the source of input. int *lineno: the line number of the input. Output: char *s: the text of the next input line. */ { 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 ) /******************************************************************************/ /* Purpose: read_wdf() reads a world definition file (wdf). Discussion: The file is presumed to be linked to the standard input device. Licensing: This code is distributed under the MIT license. Modified: 24 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 */ { struct animal *animal; int animal_count; char buf[BUFLEN]; int lineno = 0; int result; char *s; s = fetch_line_aux ( buf, BUFLEN, stdin, &lineno ); if ( s == NULL ) { printf ( "read_wdf(): Fatal error!\n" ); return 0; } result = get_world_dimens ( world, s, lineno ); if ( ! result ) { printf ( "read_wdf(): Fatal error!\n" ); return 0; } s = fetch_line_aux ( buf, BUFLEN, stdin, &lineno ); if ( s == NULL ) { printf ( "read_wdf(): Fatal error!\n" ); return 0; } result = get_eden_dimens ( world, s, lineno ); if ( ! result ) { printf ( "read_wdf(): Fatal error!\n" ); return 0; } s = fetch_line_aux ( buf, BUFLEN, stdin, &lineno ); if ( s == NULL ) { printf ( "read_wdf(): Fatal error!\n" ); return 0; } result = get_plant_energy ( world, s, lineno ); if ( ! result ) { printf ( "read_wdf(): Fatal error!\n" ); return 0; } s = fetch_line_aux ( buf, BUFLEN, stdin, &lineno ); if ( s == NULL ) { printf ( "read_wdf(): Fatal error!\n" ); return 0; } result = get_reproduction_threshold ( world, s, lineno ); if ( ! result ) { printf ( "read_wdf(): Fatal error!\n" ); return 0; } animal_count = 0; world->herd = NULL; while ( ( s = fetch_line ( buf, BUFLEN, stdin, &lineno ) ) != NULL ) { animal = get_animal_specs ( s, lineno ); if ( animal == NULL ) { return 0; } world->herd = ll_push ( world->herd, animal ); animal_count = animal_count + 1; } fprintf ( stderr, "# %d animals read from the input\n", animal_count ); return 1; } /******************************************************************************/ int write_wdf ( struct world *world, char *filename ) /******************************************************************************/ /* Purpose: write_wdf() writes an evolution world to a world definition file (wdf). Licensing: This code is distributed under the MIT license. Modified: 24 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 */ { FILE *output; output = fopen ( filename, "wt" ); fprintf ( output, "# A world definition file for the evolution simulator.\n" ); fprintf ( output, "World %d %d\n", world->world_h, world->world_w ); fprintf ( output, "Eden %d %d\n", world->eden_h, world->eden_w ); fprintf ( output, "plant_energy %d\n", world->plant_energy ); fprintf ( output, "reproduction_threshold %d\n", world->reproduction_threshold ); fprintf ( output, "\n" ); fprintf ( output, "# Animals\n" ); for ( conscell *p = world->herd; p != NULL; p = p->next ) { struct animal *animal = p->data; fprintf ( output, "(%d %d) %d %d | %d %d %d %d %d %d %d %d |\n", animal->i, animal->j, animal->d, animal->e, animal->genes[0], animal->genes[1],animal->genes[2], animal->genes[3], animal->genes[4], animal->genes[5],animal->genes[6], animal->genes[7] ); } fclose ( output ); return 0; }