comparison src/map.c @ 38:83ae0e13c416

core: improve parsing of strings in map.c
author David Demelier <markand@malikania.fr>
date Wed, 15 Jan 2020 20:44:17 +0100
parents 783841af4033
children 3996f873a54b
comparison
equal deleted inserted replaced
37:c33c8e9a89f6 38:83ae0e13c416
26 #include "texture.h" 26 #include "texture.h"
27 #include "sprite.h" 27 #include "sprite.h"
28 28
29 #include <SDL.h> 29 #include <SDL.h>
30 30
31 #define MAXLEN(v) STRINGIFY(v) 31 /* Create %<v>c string literal for scanf */
32 #define STRINGIFY(v) "%" #v "s" 32 #define MAX_F(v) MAX_F_(v)
33 #define MAX_F_(v) "%" #v "c"
33 34
34 static void 35 static void
35 parse_layer(struct map *map, const char *line, FILE *fp) 36 parse_layer(struct map *map, const char *line, FILE *fp)
36 { 37 {
37 char layer_name[32]; 38 char layer_name[32 + 1] = { 0 };
38 struct map_layer *layer; 39 struct map_layer *layer;
39 size_t amount, current; 40 size_t amount, current;
40 41
41 /* Determine layer. */ 42 /* Determine layer. */
42 if (sscanf(line, "layer|%32s", layer_name) <= 0) 43 if (sscanf(line, "layer|%32s", layer_name) <= 0)
78 79
79 static void 80 static void
80 parse(struct map *map, const char *line, FILE *fp) 81 parse(struct map *map, const char *line, FILE *fp)
81 { 82 {
82 if (strncmp(line, "title", 5) == 0) 83 if (strncmp(line, "title", 5) == 0)
83 sscanf(line, "title|" MAXLEN(MAP_TITLE_MAX), map->title); 84 sscanf(line, "title|" MAX_F(MAP_TITLE_MAX), map->title);
84 else if (strncmp(line, "width", 5) == 0) 85 else if (strncmp(line, "width", 5) == 0)
85 sscanf(line, "width|%hu", &map->width); 86 sscanf(line, "width|%hu", &map->width);
86 else if (strncmp(line, "height", 6) == 0) 87 else if (strncmp(line, "height", 6) == 0)
87 sscanf(line, "height|%hu", &map->height); 88 sscanf(line, "height|%hu", &map->height);
88 else if (strncmp(line, "tilewidth", 9) == 0) 89 else if (strncmp(line, "tilewidth", 9) == 0)