/** * Walk the contents of the given path, executing fn over each real file for * which file_predicate returns true. * The path MAY end in slash if its a directory */ iERR visit_files(char *parentpath , char *filename , FILE_PREDICATE_FN file_predicate , LOOP_FN fn , char *fn_name ) { iENTER; DIR *dir; BOOL skip; char *fullfilepath, *localfile; char fullfilepathbuffer[MAX_TEMP_STRING]; struct dirent *d; fullfilepath = test_concat_filename(fullfilepathbuffer, MAX_TEMP_STRING, parentpath, filename); // Are we visiting a directory or a regular file? dir = DIR_OPEN(fullfilepath); if (dir) { IF_PRINT("STARTING %s over %s\n\n", fn_name, fullfilepath); while ((d = DIR_NEXT(dir)) != NULL) { localfile = d->d_name; // Ignore magic files in the directory listing. if ( strcmp(localfile, "." ) != 0 && strcmp(localfile, "..") != 0) { IONDEBUG(visit_files(fullfilepath, localfile, file_predicate, fn, fn_name), "visit files"); } } DIR_CLOSE(dir); IF_PRINT("\nFINISHED %s over %s\n\n", fn_name, localfile); } else { skip = is_always_skipped(fullfilepath); if (!skip && file_predicate) { skip = ((*file_predicate)(filename)) != TRUE; } if (!skip) { IONDEBUG(test_one_file(fullfilepath, fn, fn_name), "test one file"); } else { // Skipping the file IF_PRINT("*** SKIPPING %s\n", fullfilepath); } } iRETURN; }
iERR test_one_file(char *pathname, LOOP_FN fn, char *fn_name) { iENTER; FILE *fstream = NULL; ION_STREAM *f_ion_stream = NULL; hREADER reader; long size; char *buffer; long result; // ------------ testing ion_reader_open_stream ---------------- fstream = fopen(pathname, "rb"); if (!fstream) { printf("\nERROR: can't open file %s\n", pathname); IONDEBUG(IERR_CANT_FIND_FILE, pathname); } IF_PRINT("\nProcessing file %s\n", pathname); IONDEBUG(ion_stream_open_file_in(fstream, &f_ion_stream), "opening ion stream reader"); IONDEBUG(ion_reader_open(&reader, f_ion_stream, NULL), "opening reader"); IONDEBUG2(((*fn)(reader)), fn_name, pathname); IONDEBUG(ion_reader_close(reader), "closing reader"); IONDEBUG(ion_stream_close(f_ion_stream), "closing ion stream reader"); fclose(fstream); // ------------ testing ion_reader_open_buffer ---------------- // obtain file size: fstream = fopen(pathname, "rb"); fseek (fstream, 0, SEEK_END); size = ftell (fstream); rewind(fstream); // Set position indicator to the beginning buffer = (char*) malloc(size); result = fread (buffer, 1, size, fstream); // copy the file into the buffer: fclose (fstream); IONDEBUG(ion_reader_open_buffer(&reader, (BYTE *)buffer, result, NULL), "opening buffer reader"); IONDEBUG2(((*fn)(reader)), fn_name, pathname); IONDEBUG(ion_reader_close(reader), "closing reader"); free (buffer); iRETURN; }
static error_flag save_node(FILE *file, map_s *map, node_s *node, node_s *default_values, int number_of_defaults) { int i; int _default; char **node_names; char *temporary = malloc(32); if(file == NULL || map == NULL || node == NULL || default_values == NULL) return BAD_FUNCTION_ARGUMENT; for(i = 0; i < map->number_of_nodes; i++) if(node == &(map->nodes[i])) break; for(_default = 0; _default < number_of_defaults; _default++) { if(node->type == default_values[_default].type) break; // If it can't find the correct type, then use the first one. if(_default == number_of_defaults - 1) _default = 0; } YAML_START_OF_STREAM(file); fprintf(file, "Name: %s &node%d\n" "Type: %s\n", node->name, i, node->type); if(node->owner > 0) fprintf(file, "Owner: Player %d\n", node->owner); else if(node->owner < 0) fprintf(file, "Owner: AI %d\n", -node->owner); else fputs("Owner: Neutral\n", file); if(node->planet_health != default_values[_default].planet_health) fprintf(file, "Planet health: %ld\n", node->planet_health); if(node->shield_health != default_values[_default].shield_health) fprintf(file, "Shield health: %ld\n", node->shield_health); fputs("\nBools:\n", file); IF_PRINT(node->bools.has_shield, default_values[_default].bools.has_shield, "Has shield: ") IF_PRINT(node->bools.is_a_starting_planet, default_values[_default].bools.is_a_starting_planet, "Is a starting planet: ") IF_PRINT(node->bools.is_colonized, default_values[_default].bools.is_colonized, "Is colonized: ") IF_PRINT(node->bools.is_colonizable, default_values[_default].bools.is_colonizable, "Is colonizable: ") IF_PRINT(node->bools.is_destroyable, default_values[_default].bools.is_destroyable, "Is destroyable: ") IF_PRINT(node->bools.is_in_FOW, default_values[_default].bools.is_in_FOW, "Is in FOW: ") IF_PRINT(node->bools.is_visible, default_values[_default].bools.is_visible, "Is visible: ") fputs("\nConnected nodes:\n", file); if(node->number_of_connections == 0) { node_names = malloc(sizeof(node_names)); *node_names = malloc(5); strncpy(*node_names, "none", 5); YAML_print_list(file, node_names, 1, 1 ); free(*node_names); free(node_names); } else { node_names = get_node_names(map, node->connected_nodes, node->number_of_connections); YAML_print_list(file, node_names, node->number_of_connections, 1); for(i = 0; i < node->number_of_connections; i++) free(node_names[i]); free(node_names); } fputs("\n", file); return SUCCESS; }