cg_city_parsing.c (2796B)
1 /* Copyright (C) 2022 Université de Pau et des Pays de l'Adour UPPA 2 * Copyright (C) 2022 CNRS 3 * Copyright (C) 2022 Sorbonne Université 4 * Copyright (C) 2022 Université Paul Sabatier 5 * Copyright (C) 2022 |Meso|Star> (contact@meso-star.com) 6 * 7 * This program is free software: you can redistribute it and/or modify 8 * it under the terms of the GNU General Public License as published by 9 * the Free Software Foundation, either version 3 of the License, or 10 * (at your option) any later version. 11 * 12 * This program is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 * GNU General Public License for more details. 16 * 17 * You should have received a copy of the GNU General Public License 18 * along with this program. If not, see <http://www.gnu.org/licenses/>. */ 19 20 #include "cg.h" 21 #include "cg_city_parsing.h" 22 #include "cg_city_parsing_schemas.h" 23 #include "cg_ground.h" 24 #include "cg_city.h" 25 #include "cg_cyaml.h" 26 27 #include <star/scpr.h> 28 29 #include <rsys/rsys.h> 30 #include <rsys/logger.h> 31 #include <rsys/mem_allocator.h> 32 #include <rsys/clock_time.h> 33 34 #include <stdio.h> 35 36 res_T 37 parse_city 38 (const char* filename, 39 struct mem_allocator* allocator, 40 struct logger* logger, 41 const struct cyaml_config* config, 42 struct parsed_city** out_parsed) 43 { 44 res_T res = RES_OK; 45 struct parsed_city *parsed = NULL; 46 FILE* f; 47 cyaml_err_t err; 48 struct time t0, dt; 49 char buf[128]; 50 51 ASSERT(allocator && logger && filename && config && out_parsed); 52 53 time_current(&t0); 54 err = cyaml_load_file(filename, config, &city_schema, (void**)&parsed, NULL); 55 ERR(cyaml_err_to_res_T(err)); 56 57 parsed->allocator = allocator; 58 parsed->logger = logger; 59 str_init(allocator, &parsed->filename); 60 ERR(str_set(&parsed->filename, filename)); 61 62 time_sub(&dt, time_current(&dt), &t0); 63 time_dump(&dt, TIME_SEC | TIME_MSEC, NULL, buf, sizeof(buf)); 64 /* Log outcome */ 65 logger_print(logger, LOG_OUTPUT, 66 "City map file '%s' parsed: %u building(s) read in %s.\n", 67 filename, ((struct parsed_city*)parsed)->city_building_list_count, buf); 68 69 exit: 70 *out_parsed = parsed; 71 return res; 72 error: 73 logger_print(logger, LOG_ERROR, 74 "Error parsing city map file '%s'.\n", filename); 75 f = fopen(filename, "r"); 76 if(f) { 77 fclose(f); 78 } else { 79 logger_print(logger, LOG_ERROR, "Could not open file.\n"); 80 } 81 release_parsed_city(config, parsed); 82 parsed = NULL; 83 goto exit; 84 } 85 86 void 87 release_parsed_city 88 (const struct cyaml_config* config, 89 struct parsed_city* parsed) 90 { 91 cyaml_err_t err; 92 93 if(!parsed) return; 94 95 ASSERT(config); 96 97 str_release(&parsed->filename); 98 err = cyaml_free(config, &city_schema, parsed, 1); 99 CHK(RES_OK == cyaml_err_to_res_T(err)); 100 }