30 #include "../include/autoconf.h"
45 const char *s1 = *(
const char *
const *)p1;
46 const char *s2 = *(
const char *
const *)p2;
48 return(strcmp(s1, s2));
74 int load_dir(
const char *dir,
char ***namelist,
int skip_dirs)
78 int entries = 0, entry_size = 0;
87 while ((d =
readdir(dp)) != NULL) {
89 snprintf(
name,
sizeof(
name),
"%s/%s", dir, d->d_name);
91 if (S_ISDIR(sb.st_mode)) {
96 if (entries == entry_size) {
98 rn = realloc(rn,
sizeof(
char *)*entry_size);
136 for (style_map =
styles; style_map != NULL; style_map = style_map->
next) {
137 if (!strcmp(style_name, style_map->
path)) {
182 char style_file_path[256];
183 char style_file_full_path[256];
185 struct stat file_stat;
186 int i, only_subdirs = 0;
189 if (strstr(dirname,
"..") != NULL || (stylename != NULL && strstr(stylename,
"..") != NULL))
193 if (stylename && strlen(stylename) > 0) {
194 snprintf(style_file_path,
sizeof(style_file_path),
"%s/%s", dirname, stylename);
196 strlcpy(style_file_path, dirname,
sizeof(style_file_path));
200 snprintf(style_file_full_path,
sizeof(style_file_full_path),
"%s/maps%s",
settings.
datadir, style_file_path);
201 if (stat(style_file_full_path, &file_stat) == 0
202 && !S_ISDIR(file_stat.st_mode)) {
205 if (style_map == NULL) {
208 char style_dir_full_path[256];
211 snprintf(style_dir_full_path,
sizeof(style_dir_full_path),
"%s/maps%s",
settings.
datadir, style_file_path);
216 n =
load_dir(style_dir_full_path, &namelist, 1);
218 n =
load_dir(style_dir_full_path, &namelist, 0);
231 if (difficulty == -1) {
237 p = strchr(style_file_path,
'\0');
238 snprintf(p, style_file_path+
sizeof(style_file_path)-p,
"/%s", namelist[
RANDOM()%n]);
242 int min_dist = 32000, min_index = -1;
245 for (i = 0; i < n; i++) {
247 char *mfile_name = strrchr(namelist[i],
'_');
248 if (mfile_name == NULL) {
253 for (
q = 0;
q < n;
q++) {
259 dist = abs(difficulty - atoi(mfile_name + 1));
260 if (dist < min_dist) {
268 p = strchr(style_file_path,
'\0');
269 snprintf(p, style_file_path+
sizeof(style_file_path)-p,
"/%s", namelist[min_index]);
272 for (i = 0; i < n; i++) {
305 }
while (new_obj == NULL && limit < 1000);
306 return HEAD(new_obj);
326 LOG(
llevDebug,
"free_style_maps: Freed %d maps\n", style_maps);