src/fios.cpp
changeset 10310 ca2eb5811a07
parent 10301 76966696a338
child 10405 fb6f4c4476a6
equal deleted inserted replaced
10309:a3519caf1001 10310:ca2eb5811a07
   214 	/* A parent directory link exists if we are not in the root directory */
   214 	/* A parent directory link exists if we are not in the root directory */
   215 	if (!FiosIsRoot(_fios_path) && mode != SLD_NEW_GAME) {
   215 	if (!FiosIsRoot(_fios_path) && mode != SLD_NEW_GAME) {
   216 		fios = _fios_items.Append();
   216 		fios = _fios_items.Append();
   217 		fios->type = FIOS_TYPE_PARENT;
   217 		fios->type = FIOS_TYPE_PARENT;
   218 		fios->mtime = 0;
   218 		fios->mtime = 0;
   219 		ttd_strlcpy(fios->name, "..", lengthof(fios->name));
   219 		strecpy(fios->name, "..", lastof(fios->name));
   220 		ttd_strlcpy(fios->title, ".. (Parent directory)", lengthof(fios->title));
   220 		strecpy(fios->title, ".. (Parent directory)", lastof(fios->title));
   221 	}
   221 	}
   222 
   222 
   223 	/* Show subdirectories */
   223 	/* Show subdirectories */
   224 	if (mode != SLD_NEW_GAME && (dir = ttd_opendir(_fios_path)) != NULL) {
   224 	if (mode != SLD_NEW_GAME && (dir = ttd_opendir(_fios_path)) != NULL) {
   225 		while ((dirent = readdir(dir)) != NULL) {
   225 		while ((dirent = readdir(dir)) != NULL) {
   226 			ttd_strlcpy(d_name, FS2OTTD(dirent->d_name), sizeof(d_name));
   226 			strecpy(d_name, FS2OTTD(dirent->d_name), lastof(d_name));
   227 
   227 
   228 			/* found file must be directory, but not '.' or '..' */
   228 			/* found file must be directory, but not '.' or '..' */
   229 			if (FiosIsValidFile(_fios_path, dirent, &sb) && (sb.st_mode & S_IFDIR) &&
   229 			if (FiosIsValidFile(_fios_path, dirent, &sb) && (sb.st_mode & S_IFDIR) &&
   230 					(!FiosIsHiddenFile(dirent) || strncasecmp(d_name, PERSONAL_DIR, strlen(d_name)) == 0) &&
   230 					(!FiosIsHiddenFile(dirent) || strncasecmp(d_name, PERSONAL_DIR, strlen(d_name)) == 0) &&
   231 					strcmp(d_name, ".") != 0 && strcmp(d_name, "..") != 0) {
   231 					strcmp(d_name, ".") != 0 && strcmp(d_name, "..") != 0) {
   232 				fios = _fios_items.Append();
   232 				fios = _fios_items.Append();
   233 				fios->type = FIOS_TYPE_DIR;
   233 				fios->type = FIOS_TYPE_DIR;
   234 				fios->mtime = 0;
   234 				fios->mtime = 0;
   235 				ttd_strlcpy(fios->name, d_name, lengthof(fios->name));
   235 				strecpy(fios->name, d_name, lastof(fios->name));
   236 				snprintf(fios->title, lengthof(fios->title), "%s" PATHSEP " (Directory)", d_name);
   236 				snprintf(fios->title, lengthof(fios->title), "%s" PATHSEP " (Directory)", d_name);
   237 				str_validate(fios->title);
   237 				str_validate(fios->title);
   238 			}
   238 			}
   239 		}
   239 		}
   240 		closedir(dir);
   240 		closedir(dir);
   255 	dir = ttd_opendir(_fios_path);
   255 	dir = ttd_opendir(_fios_path);
   256 	if (dir != NULL) {
   256 	if (dir != NULL) {
   257 		while ((dirent = readdir(dir)) != NULL) {
   257 		while ((dirent = readdir(dir)) != NULL) {
   258 			char fios_title[64];
   258 			char fios_title[64];
   259 			char *t;
   259 			char *t;
   260 			ttd_strlcpy(d_name, FS2OTTD(dirent->d_name), sizeof(d_name));
   260 			strecpy(d_name, FS2OTTD(dirent->d_name), lastof(d_name));
   261 
   261 
   262 			if (!FiosIsValidFile(_fios_path, dirent, &sb) || !(sb.st_mode & S_IFREG) || FiosIsHiddenFile(dirent)) continue;
   262 			if (!FiosIsValidFile(_fios_path, dirent, &sb) || !(sb.st_mode & S_IFREG) || FiosIsHiddenFile(dirent)) continue;
   263 
   263 
   264 			/* File has no extension, skip it */
   264 			/* File has no extension, skip it */
   265 			if ((t = strrchr(d_name, '.')) == NULL) continue;
   265 			if ((t = strrchr(d_name, '.')) == NULL) continue;
   268 			FiosType type = callback_proc(mode, d_name, t, fios_title);
   268 			FiosType type = callback_proc(mode, d_name, t, fios_title);
   269 			if (type != FIOS_TYPE_INVALID) {
   269 			if (type != FIOS_TYPE_INVALID) {
   270 				fios = _fios_items.Append();
   270 				fios = _fios_items.Append();
   271 				fios->mtime = sb.st_mtime;
   271 				fios->mtime = sb.st_mtime;
   272 				fios->type = type;
   272 				fios->type = type;
   273 				ttd_strlcpy(fios->name, d_name, lengthof(fios->name));
   273 				strecpy(fios->name, d_name, lastof(fios->name));
   274 
   274 
   275 				/* Some callbacks want to lookup the title of the file. Allow that.
   275 				/* Some callbacks want to lookup the title of the file. Allow that.
   276 				 * If we just copy the title from the filename, strip the extension */
   276 				 * If we just copy the title from the filename, strip the extension */
   277 				t = (fios_title[0] == '\0') ? *t = '\0', d_name : fios_title;
   277 				t = (fios_title[0] == '\0') ? *t = '\0', d_name : fios_title;
   278 				ttd_strlcpy(fios->title, t, lengthof(fios->title));
   278 				strecpy(fios->title, t, lastof(fios->title));
   279 				str_validate(fios->title);
   279 				str_validate(fios->title);
   280 			}
   280 			}
   281 		}
   281 		}
   282 		closedir(dir);
   282 		closedir(dir);
   283 	}
   283 	}