OpenTTD
fios.cpp
Go to the documentation of this file.
1 /* $Id: fios.cpp 27896 2017-08-20 21:04:21Z frosch $ */
2 
3 /*
4  * This file is part of OpenTTD.
5  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
6  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
7  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
8  */
9 
15 #include "stdafx.h"
16 #include "fios.h"
17 #include "fileio_func.h"
18 #include "tar_type.h"
19 #include "screenshot.h"
20 #include "string_func.h"
21 #include <sys/stat.h>
22 
23 #ifndef WIN32
24 # include <unistd.h>
25 #endif /* WIN32 */
26 
27 #include "table/strings.h"
28 
29 #include "safeguards.h"
30 
31 /* Variables to display file lists */
32 static char *_fios_path;
33 static const char *_fios_path_last;
34 SortingBits _savegame_sort_order = SORT_BY_DATE | SORT_DESCENDING;
35 
36 /* OS-specific functions are taken from their respective files (win32/unix/os2 .c) */
37 extern bool FiosIsRoot(const char *path);
38 extern bool FiosIsValidFile(const char *path, const struct dirent *ent, struct stat *sb);
39 extern bool FiosIsHiddenFile(const struct dirent *ent);
40 extern void FiosGetDrives(FileList &file_list);
41 extern bool FiosGetDiskFreeSpace(const char *path, uint64 *tot);
42 
43 /* get the name of an oldstyle savegame */
44 extern void GetOldSaveGameName(const char *file, char *title, const char *last);
45 
52 int CDECL CompareFiosItems(const FiosItem *da, const FiosItem *db)
53 {
54  int r = 0;
55 
56  if ((_savegame_sort_order & SORT_BY_NAME) == 0 && da->mtime != db->mtime) {
57  r = da->mtime < db->mtime ? -1 : 1;
58  } else {
59  r = strcasecmp(da->title, db->title);
60  }
61 
62  if (_savegame_sort_order & SORT_DESCENDING) r = -r;
63  return r;
64 }
65 
66 FileList::~FileList()
67 {
68  this->Clear();
69 }
70 
77 {
78  this->Clear();
79 
80  assert(fop == SLO_LOAD || fop == SLO_SAVE);
81  switch (abstract_filetype) {
82  case FT_NONE:
83  break;
84 
85  case FT_SAVEGAME:
86  FiosGetSavegameList(fop, *this);
87  break;
88 
89  case FT_SCENARIO:
90  FiosGetScenarioList(fop, *this);
91  break;
92 
93  case FT_HEIGHTMAP:
94  FiosGetHeightmapList(fop, *this);
95  break;
96 
97  default:
98  NOT_REACHED();
99  }
100 }
101 
108 const FiosItem *FileList::FindItem(const char *file)
109 {
110  for (const FiosItem *item = this->Begin(); item != this->End(); item++) {
111  if (strcmp(file, item->name) == 0) return item;
112  if (strcmp(file, item->title) == 0) return item;
113  }
114 
115  /* If no name matches, try to parse it as number */
116  char *endptr;
117  int i = strtol(file, &endptr, 10);
118  if (file == endptr || *endptr != '\0') i = -1;
119 
120  if (IsInsideMM(i, 0, this->Length())) return this->Get(i);
121 
122  /* As a last effort assume it is an OpenTTD savegame and
123  * that the ".sav" part was not given. */
124  char long_file[MAX_PATH];
125  seprintf(long_file, lastof(long_file), "%s.sav", file);
126  for (const FiosItem *item = this->Begin(); item != this->End(); item++) {
127  if (strcmp(long_file, item->name) == 0) return item;
128  if (strcmp(long_file, item->title) == 0) return item;
129  }
130 
131  return NULL;
132 }
133 
141 StringID FiosGetDescText(const char **path, uint64 *total_free)
142 {
143  *path = _fios_path;
144  return FiosGetDiskFreeSpace(*path, total_free) ? STR_SAVELOAD_BYTES_FREE : STR_ERROR_UNABLE_TO_READ_DRIVE;
145 }
146 
152 const char *FiosBrowseTo(const FiosItem *item)
153 {
154  switch (item->type) {
155  case FIOS_TYPE_DRIVE:
156 #if defined(WINCE)
157  seprintf(_fios_path, _fios_path_last, PATHSEP "");
158 #elif defined(WIN32) || defined(__OS2__)
159  seprintf(_fios_path, _fios_path_last, "%c:" PATHSEP, item->title[0]);
160 #endif
161  break;
162 
163  case FIOS_TYPE_INVALID:
164  break;
165 
166  case FIOS_TYPE_PARENT: {
167  /* Check for possible NULL ptr (not required for UNIXes, but AmigaOS-alikes) */
168  char *s = strrchr(_fios_path, PATHSEPCHAR);
169  if (s != NULL && s != _fios_path) {
170  s[0] = '\0'; // Remove last path separator character, so we can go up one level.
171  }
172  s = strrchr(_fios_path, PATHSEPCHAR);
173  if (s != NULL) {
174  s[1] = '\0'; // go up a directory
175 #if defined(__MORPHOS__) || defined(__AMIGAOS__)
176  /* On MorphOS or AmigaOS paths look like: "Volume:directory/subdirectory" */
177  } else if ((s = strrchr(_fios_path, ':')) != NULL) {
178  s[1] = '\0';
179 #endif
180  }
181  break;
182  }
183 
184  case FIOS_TYPE_DIR:
185  strecat(_fios_path, item->name, _fios_path_last);
186  strecat(_fios_path, PATHSEP, _fios_path_last);
187  break;
188 
189  case FIOS_TYPE_DIRECT:
190  seprintf(_fios_path, _fios_path_last, "%s", item->name);
191  break;
192 
193  case FIOS_TYPE_FILE:
194  case FIOS_TYPE_OLDFILE:
195  case FIOS_TYPE_SCENARIO:
196  case FIOS_TYPE_OLD_SCENARIO:
197  case FIOS_TYPE_PNG:
198  case FIOS_TYPE_BMP:
199  return item->name;
200  }
201 
202  return NULL;
203 }
204 
213 static void FiosMakeFilename(char *buf, const char *path, const char *name, const char *ext, const char *last)
214 {
215  const char *period;
216 
217  /* Don't append the extension if it is already there */
218  period = strrchr(name, '.');
219  if (period != NULL && strcasecmp(period, ext) == 0) ext = "";
220 #if defined(__MORPHOS__) || defined(__AMIGAOS__)
221  if (path != NULL) {
222  unsigned char sepchar = path[(strlen(path) - 1)];
223 
224  if (sepchar != ':' && sepchar != '/') {
225  seprintf(buf, last, "%s" PATHSEP "%s%s", path, name, ext);
226  } else {
227  seprintf(buf, last, "%s%s%s", path, name, ext);
228  }
229  } else {
230  seprintf(buf, last, "%s%s", name, ext);
231  }
232 #else
233  seprintf(buf, last, "%s" PATHSEP "%s%s", path, name, ext);
234 #endif
235 }
236 
243 void FiosMakeSavegameName(char *buf, const char *name, const char *last)
244 {
245  const char *extension = (_game_mode == GM_EDITOR) ? ".scn" : ".sav";
246 
247  FiosMakeFilename(buf, _fios_path, name, extension, last);
248 }
249 
256 void FiosMakeHeightmapName(char *buf, const char *name, const char *last)
257 {
258  char ext[5];
259  ext[0] = '.';
260  strecpy(ext + 1, GetCurrentScreenshotExtension(), lastof(ext));
261 
262  FiosMakeFilename(buf, _fios_path, name, ext, last);
263 }
264 
270 bool FiosDelete(const char *name)
271 {
272  char filename[512];
273 
274  FiosMakeSavegameName(filename, name, lastof(filename));
275  return unlink(filename) == 0;
276 }
277 
278 typedef FiosType fios_getlist_callback_proc(SaveLoadOperation fop, const char *filename, const char *ext, char *title, const char *last);
279 
283 class FiosFileScanner : public FileScanner {
285  fios_getlist_callback_proc *callback_proc;
287 public:
294  FiosFileScanner(SaveLoadOperation fop, fios_getlist_callback_proc *callback_proc, FileList &file_list) :
295  fop(fop), callback_proc(callback_proc), file_list(file_list)
296  {}
297 
298  /* virtual */ bool AddFile(const char *filename, size_t basepath_length, const char *tar_filename);
299 };
300 
307 bool FiosFileScanner::AddFile(const char *filename, size_t basepath_length, const char *tar_filename)
308 {
309  const char *ext = strrchr(filename, '.');
310  if (ext == NULL) return false;
311 
312  char fios_title[64];
313  fios_title[0] = '\0'; // reset the title;
314 
315  FiosType type = this->callback_proc(this->fop, filename, ext, fios_title, lastof(fios_title));
316  if (type == FIOS_TYPE_INVALID) return false;
317 
318  for (const FiosItem *fios = file_list.Begin(); fios != file_list.End(); fios++) {
319  if (strcmp(fios->name, filename) == 0) return false;
320  }
321 
322  FiosItem *fios = file_list.Append();
323 #ifdef WIN32
324  struct _stat sb;
325  if (_tstat(OTTD2FS(filename), &sb) == 0) {
326 #else
327  struct stat sb;
328  if (stat(filename, &sb) == 0) {
329 #endif
330  fios->mtime = sb.st_mtime;
331  } else {
332  fios->mtime = 0;
333  }
334 
335  fios->type = type;
336  strecpy(fios->name, filename, lastof(fios->name));
337 
338  /* If the file doesn't have a title, use its filename */
339  const char *t = fios_title;
340  if (StrEmpty(fios_title)) {
341  t = strrchr(filename, PATHSEPCHAR);
342  t = (t == NULL) ? filename : (t + 1);
343  }
344  strecpy(fios->title, t, lastof(fios->title));
345  str_validate(fios->title, lastof(fios->title));
346 
347  return true;
348 }
349 
350 
358 static void FiosGetFileList(SaveLoadOperation fop, fios_getlist_callback_proc *callback_proc, Subdirectory subdir, FileList &file_list)
359 {
360  struct stat sb;
361  struct dirent *dirent;
362  DIR *dir;
363  FiosItem *fios;
364  int sort_start;
365  char d_name[sizeof(fios->name)];
366 
367  file_list.Clear();
368 
369  /* A parent directory link exists if we are not in the root directory */
370  if (!FiosIsRoot(_fios_path)) {
371  fios = file_list.Append();
372  fios->type = FIOS_TYPE_PARENT;
373  fios->mtime = 0;
374  strecpy(fios->name, "..", lastof(fios->name));
375  strecpy(fios->title, ".. (Parent directory)", lastof(fios->title));
376  }
377 
378  /* Show subdirectories */
379  if ((dir = ttd_opendir(_fios_path)) != NULL) {
380  while ((dirent = readdir(dir)) != NULL) {
381  strecpy(d_name, FS2OTTD(dirent->d_name), lastof(d_name));
382 
383  /* found file must be directory, but not '.' or '..' */
384  if (FiosIsValidFile(_fios_path, dirent, &sb) && S_ISDIR(sb.st_mode) &&
385  (!FiosIsHiddenFile(dirent) || strncasecmp(d_name, PERSONAL_DIR, strlen(d_name)) == 0) &&
386  strcmp(d_name, ".") != 0 && strcmp(d_name, "..") != 0) {
387  fios = file_list.Append();
388  fios->type = FIOS_TYPE_DIR;
389  fios->mtime = 0;
390  strecpy(fios->name, d_name, lastof(fios->name));
391  seprintf(fios->title, lastof(fios->title), "%s" PATHSEP " (Directory)", d_name);
392  str_validate(fios->title, lastof(fios->title));
393  }
394  }
395  closedir(dir);
396  }
397 
398  /* Sort the subdirs always by name, ascending, remember user-sorting order */
399  {
400  SortingBits order = _savegame_sort_order;
401  _savegame_sort_order = SORT_BY_NAME | SORT_ASCENDING;
402  QSortT(file_list.files.Begin(), file_list.files.Length(), CompareFiosItems);
403  _savegame_sort_order = order;
404  }
405 
406  /* This is where to start sorting for the filenames */
407  sort_start = file_list.Length();
408 
409  /* Show files */
410  FiosFileScanner scanner(fop, callback_proc, file_list);
411  if (subdir == NO_DIRECTORY) {
412  scanner.Scan(NULL, _fios_path, false);
413  } else {
414  scanner.Scan(NULL, subdir, true, true);
415  }
416 
417  QSortT(file_list.Get(sort_start), file_list.Length() - sort_start, CompareFiosItems);
418 
419  /* Show drives */
420  FiosGetDrives(file_list);
421 
422  file_list.Compact();
423 }
424 
433 static void GetFileTitle(const char *file, char *title, const char *last, Subdirectory subdir)
434 {
435  char buf[MAX_PATH];
436  strecpy(buf, file, lastof(buf));
437  strecat(buf, ".title", lastof(buf));
438 
439  FILE *f = FioFOpenFile(buf, "r", subdir);
440  if (f == NULL) return;
441 
442  size_t read = fread(title, 1, last - title, f);
443  assert(title + read <= last);
444  title[read] = '\0';
445  str_validate(title, last);
446  FioFCloseFile(f);
447 }
448 
460 FiosType FiosGetSavegameListCallback(SaveLoadOperation fop, const char *file, const char *ext, char *title, const char *last)
461 {
462  /* Show savegame files
463  * .SAV OpenTTD saved game
464  * .SS1 Transport Tycoon Deluxe preset game
465  * .SV1 Transport Tycoon Deluxe (Patch) saved game
466  * .SV2 Transport Tycoon Deluxe (Patch) saved 2-player game */
467 
468  /* Don't crash if we supply no extension */
469  if (ext == NULL) return FIOS_TYPE_INVALID;
470 
471  if (strcasecmp(ext, ".sav") == 0) {
472  GetFileTitle(file, title, last, SAVE_DIR);
473  return FIOS_TYPE_FILE;
474  }
475 
476  if (fop == SLO_LOAD) {
477  if (strcasecmp(ext, ".ss1") == 0 || strcasecmp(ext, ".sv1") == 0 ||
478  strcasecmp(ext, ".sv2") == 0) {
479  if (title != NULL) GetOldSaveGameName(file, title, last);
480  return FIOS_TYPE_OLDFILE;
481  }
482  }
483 
484  return FIOS_TYPE_INVALID;
485 }
486 
494 {
495  static char *fios_save_path = NULL;
496  static char *fios_save_path_last = NULL;
497 
498  if (fios_save_path == NULL) {
499  fios_save_path = MallocT<char>(MAX_PATH);
500  fios_save_path_last = fios_save_path + MAX_PATH - 1;
501  FioGetDirectory(fios_save_path, fios_save_path_last, SAVE_DIR);
502  }
503 
504  _fios_path = fios_save_path;
505  _fios_path_last = fios_save_path_last;
506 
508 }
509 
521 static FiosType FiosGetScenarioListCallback(SaveLoadOperation fop, const char *file, const char *ext, char *title, const char *last)
522 {
523  /* Show scenario files
524  * .SCN OpenTTD style scenario file
525  * .SV0 Transport Tycoon Deluxe (Patch) scenario
526  * .SS0 Transport Tycoon Deluxe preset scenario */
527  if (strcasecmp(ext, ".scn") == 0) {
528  GetFileTitle(file, title, last, SCENARIO_DIR);
529  return FIOS_TYPE_SCENARIO;
530  }
531 
532  if (fop == SLO_LOAD) {
533  if (strcasecmp(ext, ".sv0") == 0 || strcasecmp(ext, ".ss0") == 0 ) {
534  GetOldSaveGameName(file, title, last);
535  return FIOS_TYPE_OLD_SCENARIO;
536  }
537  }
538 
539  return FIOS_TYPE_INVALID;
540 }
541 
549 {
550  static char *fios_scn_path = NULL;
551  static char *fios_scn_path_last = NULL;
552 
553  /* Copy the default path on first run or on 'New Game' */
554  if (fios_scn_path == NULL) {
555  fios_scn_path = MallocT<char>(MAX_PATH);
556  fios_scn_path_last = fios_scn_path + MAX_PATH - 1;
557  FioGetDirectory(fios_scn_path, fios_scn_path_last, SCENARIO_DIR);
558  }
559 
560  _fios_path = fios_scn_path;
561  _fios_path_last = fios_scn_path_last;
562 
563  char base_path[MAX_PATH];
564  FioGetDirectory(base_path, lastof(base_path), SCENARIO_DIR);
565 
566  Subdirectory subdir = (fop == SLO_LOAD && strcmp(base_path, _fios_path) == 0) ? SCENARIO_DIR : NO_DIRECTORY;
567  FiosGetFileList(fop, &FiosGetScenarioListCallback, subdir, file_list);
568 }
569 
570 static FiosType FiosGetHeightmapListCallback(SaveLoadOperation fop, const char *file, const char *ext, char *title, const char *last)
571 {
572  /* Show heightmap files
573  * .PNG PNG Based heightmap files
574  * .BMP BMP Based heightmap files
575  */
576 
577  FiosType type = FIOS_TYPE_INVALID;
578 
579 #ifdef WITH_PNG
580  if (strcasecmp(ext, ".png") == 0) type = FIOS_TYPE_PNG;
581 #endif /* WITH_PNG */
582 
583  if (strcasecmp(ext, ".bmp") == 0) type = FIOS_TYPE_BMP;
584 
585  if (type == FIOS_TYPE_INVALID) return FIOS_TYPE_INVALID;
586 
587  TarFileList::iterator it = _tar_filelist[SCENARIO_DIR].find(file);
588  if (it != _tar_filelist[SCENARIO_DIR].end()) {
589  /* If the file is in a tar and that tar is not in a heightmap
590  * directory we are for sure not supposed to see it.
591  * Examples of this are pngs part of documentation within
592  * collections of NewGRFs or 32 bpp graphics replacement PNGs.
593  */
594  bool match = false;
595  Searchpath sp;
596  FOR_ALL_SEARCHPATHS(sp) {
597  char buf[MAX_PATH];
598  FioAppendDirectory(buf, lastof(buf), sp, HEIGHTMAP_DIR);
599 
600  if (strncmp(buf, it->second.tar_filename, strlen(buf)) == 0) {
601  match = true;
602  break;
603  }
604  }
605 
606  if (!match) return FIOS_TYPE_INVALID;
607  }
608 
609  GetFileTitle(file, title, last, HEIGHTMAP_DIR);
610 
611  return type;
612 }
613 
620 {
621  static char *fios_hmap_path = NULL;
622  static char *fios_hmap_path_last = NULL;
623 
624  if (fios_hmap_path == NULL) {
625  fios_hmap_path = MallocT<char>(MAX_PATH);
626  fios_hmap_path_last = fios_hmap_path + MAX_PATH - 1;
627  FioGetDirectory(fios_hmap_path, fios_hmap_path_last, HEIGHTMAP_DIR);
628  }
629 
630  _fios_path = fios_hmap_path;
631  _fios_path_last = fios_hmap_path_last;
632 
633  char base_path[MAX_PATH];
634  FioGetDirectory(base_path, lastof(base_path), HEIGHTMAP_DIR);
635 
636  Subdirectory subdir = strcmp(base_path, _fios_path) == 0 ? HEIGHTMAP_DIR : NO_DIRECTORY;
637  FiosGetFileList(fop, &FiosGetHeightmapListCallback, subdir, file_list);
638 }
639 
644 const char *FiosGetScreenshotDir()
645 {
646  static char *fios_screenshot_path = NULL;
647 
648  if (fios_screenshot_path == NULL) {
649  fios_screenshot_path = MallocT<char>(MAX_PATH);
650  FioGetDirectory(fios_screenshot_path, fios_screenshot_path + MAX_PATH - 1, SCREENSHOT_DIR);
651  }
652 
653  return fios_screenshot_path;
654 }
655 
656 #if defined(ENABLE_NETWORK)
657 #include "network/network_content.h"
658 #include "3rdparty/md5/md5.h"
659 
662  uint32 scenid;
663  uint8 md5sum[16];
664  char filename[MAX_PATH];
665 
666  bool operator == (const ScenarioIdentifier &other) const
667  {
668  return this->scenid == other.scenid &&
669  memcmp(this->md5sum, other.md5sum, sizeof(this->md5sum)) == 0;
670  }
671 
672  bool operator != (const ScenarioIdentifier &other) const
673  {
674  return !(*this == other);
675  }
676 };
677 
681 class ScenarioScanner : protected FileScanner, public SmallVector<ScenarioIdentifier, 8> {
682  bool scanned;
683 public:
685  ScenarioScanner() : scanned(false) {}
686 
691  void Scan(bool rescan)
692  {
693  if (this->scanned && !rescan) return;
694 
695  this->FileScanner::Scan(".id", SCENARIO_DIR, true, true);
696  this->scanned = true;
697  }
698 
699  /* virtual */ bool AddFile(const char *filename, size_t basepath_length, const char *tar_filename)
700  {
701  FILE *f = FioFOpenFile(filename, "r", SCENARIO_DIR);
702  if (f == NULL) return false;
703 
705  int fret = fscanf(f, "%i", &id.scenid);
706  FioFCloseFile(f);
707  if (fret != 1) return false;
708  strecpy(id.filename, filename, lastof(id.filename));
709 
710  Md5 checksum;
711  uint8 buffer[1024];
712  char basename[MAX_PATH];
713  size_t len, size;
714 
715  /* open the scenario file, but first get the name.
716  * This is safe as we check on extension which
717  * must always exist. */
718  strecpy(basename, filename, lastof(basename));
719  *strrchr(basename, '.') = '\0';
720  f = FioFOpenFile(basename, "rb", SCENARIO_DIR, &size);
721  if (f == NULL) return false;
722 
723  /* calculate md5sum */
724  while ((len = fread(buffer, 1, (size > sizeof(buffer)) ? sizeof(buffer) : size, f)) != 0 && size != 0) {
725  size -= len;
726  checksum.Append(buffer, len);
727  }
728  checksum.Finish(id.md5sum);
729 
730  FioFCloseFile(f);
731 
732  this->Include(id);
733  return true;
734  }
735 };
736 
739 
746 const char *FindScenario(const ContentInfo *ci, bool md5sum)
747 {
748  _scanner.Scan(false);
749 
750  for (ScenarioIdentifier *id = _scanner.Begin(); id != _scanner.End(); id++) {
751  if (md5sum ? (memcmp(id->md5sum, ci->md5sum, sizeof(id->md5sum)) == 0)
752  : (id->scenid == ci->unique_id)) {
753  return id->filename;
754  }
755  }
756 
757  return NULL;
758 }
759 
766 bool HasScenario(const ContentInfo *ci, bool md5sum)
767 {
768  return (FindScenario(ci, md5sum) != NULL);
769 }
770 
775 {
776  _scanner.Scan(true);
777 }
778 
779 #endif /* ENABLE_NETWORK */
FiosType
Elements of a file system that are recognized.
Definition: fileio_type.h:69
bool AddFile(const char *filename, size_t basepath_length, const char *tar_filename)
Try to add a fios item set with the given filename.
Definition: fios.cpp:307
AbstractFileType
The different abstract types of files that the system knows about.
Definition: fileio_type.h:18
Basic data to distinguish a scenario.
Definition: fios.cpp:661
uint32 unique_id
Unique ID; either GRF ID or shortname.
Definition: tcp_content.h:77
Scanner to find the unique IDs of scenarios.
Definition: fios.cpp:681
FiosType FiosGetSavegameListCallback(SaveLoadOperation fop, const char *file, const char *ext, char *title, const char *last)
Callback for FiosGetFileList.
Definition: fios.cpp:460
static char * strecat(char *dst, const char *src, const char *last)
Appends characters from one string to another.
Definition: depend.cpp:99
const char * FS2OTTD(const TCHAR *name)
Convert to OpenTTD&#39;s encoding from that of the local environment.
Definition: win32.cpp:613
Subdirectory
The different kinds of subdirectories OpenTTD uses.
Definition: fileio_type.h:110
void FioFCloseFile(FILE *f)
Close a file in a safe way.
Definition: fileio.cpp:342
Structs, typedefs and macros used for TAR file handling.
void Clear()
Remove all items from the list.
Definition: fios.h:179
static bool IsInsideMM(const T x, const uint min, const uint max)
Checks if a value is in an interval.
Definition: math_func.hpp:266
uint8 md5sum[16]
MD5 checksum of file.
Definition: fios.cpp:663
int CDECL seprintf(char *str, const char *last, const char *format,...)
Safer implementation of snprintf; same as snprintf except:
Definition: string.cpp:398
uint Scan(const char *extension, Subdirectory sd, bool tars=true, bool recursive=true)
Scan for files with the given extension in the given search path.
Definition: fileio.cpp:1428
#define FOR_ALL_SEARCHPATHS(sp)
Iterator for all the search paths.
Definition: fileio_func.h:49
Subdirectory of scenario for heightmaps.
Definition: fileio_type.h:115
Subdirectory for all screenshots.
Definition: fileio_type.h:125
const T * Begin() const
Get the pointer to the first item (const)
const FiosItem * Begin() const
Get a pointer to the first file information.
Definition: fios.h:132
void FiosGetSavegameList(SaveLoadOperation fop, FileList &file_list)
Get a list of savegames.
Definition: fios.cpp:493
Functions for Standard In/Out file operations.
#define lastof(x)
Get the last element of an fixed size array.
Definition: depend.cpp:50
Simple vector template class.
Functions to make screenshots.
Searchpath
Types of searchpaths OpenTTD might use.
Definition: fileio_type.h:133
uint32 scenid
ID for the scenario (generated by content).
Definition: fios.cpp:662
void FiosGetScenarioList(SaveLoadOperation fop, FileList &file_list)
Get a list of scenarios.
Definition: fios.cpp:548
const T * End() const
Get the pointer behind the last valid item (const)
File is being saved.
Definition: fileio_type.h:52
Deals with finding savegames.
Definition: fios.h:98
StringID FiosGetDescText(const char **path, uint64 *total_free)
Get descriptive texts.
Definition: fios.cpp:141
Helper for scanning for files with a given name.
Definition: fileio_func.h:73
const FiosItem * FindItem(const char *file)
Find file information of a file by its name from the file list.
Definition: fios.cpp:108
SmallVector< FiosItem, 32 > files
The list of files.
Definition: fios.h:193
old or new savegame
Definition: fileio_type.h:20
old or new scenario
Definition: fileio_type.h:21
bool HasScenario(const ContentInfo *ci, bool md5sum)
Check whether we&#39;ve got a given scenario based on its unique ID.
Definition: fios.cpp:766
FiosFileScanner(SaveLoadOperation fop, fios_getlist_callback_proc *callback_proc, FileList &file_list)
Create the scanner.
Definition: fios.cpp:294
uint Length() const
Get the number of files in the list.
Definition: fios.h:123
Functions related to low-level strings.
Base directory for all scenarios.
Definition: fileio_type.h:114
bool AddFile(const char *filename, size_t basepath_length, const char *tar_filename)
Add a file with the given filename.
Definition: fios.cpp:699
const FiosItem * End() const
Get a pointer behind the last file information.
Definition: fios.h:141
const char * FindScenario(const ContentInfo *ci, bool md5sum)
Find a given scenario based on its unique ID.
Definition: fios.cpp:746
static ScenarioScanner _scanner
Scanner for scenarios.
Definition: fios.cpp:738
bool scanned
Whether we&#39;ve already scanned.
Definition: fios.cpp:682
void FiosMakeSavegameName(char *buf, const char *name, const char *last)
Make a save game or scenario filename from a name.
Definition: fios.cpp:243
nothing to do
Definition: fileio_type.h:19
uint Length() const
Get the number of items in the list.
bool operator!=(const MultiMapIterator< Tmap_iter1, Tlist_iter1, Tkey, Tvalue1, Tcompare > &iter1, const MultiMapIterator< Tmap_iter2, Tlist_iter2, Tkey, Tvalue2, Tcompare > &iter2)
Inverse of operator==().
Definition: multimap.hpp:222
Definition: win32.cpp:98
void str_validate(char *str, const char *last, StringValidationSettings settings)
Scans the string for valid characters and if it finds invalid ones, replaces them with a question mar...
Definition: string.cpp:184
static void FiosMakeFilename(char *buf, const char *path, const char *name, const char *ext, const char *last)
Construct a filename from its components in destination buffer buf.
Definition: fios.cpp:213
FILE * FioFOpenFile(const char *filename, const char *mode, Subdirectory subdir, size_t *filesize)
Opens a OpenTTD file somewhere in a personal or global directory.
Definition: fileio.cpp:474
A path without any base directory.
Definition: fileio_type.h:127
Definition of base types and functions in a cross-platform compatible way.
A number of safeguards to prevent using unsafe methods.
void Scan(bool rescan)
Scan, but only if it&#39;s needed.
Definition: fios.cpp:691
void ScanScenarios()
Force a (re)scan of the scenarios.
Definition: fios.cpp:774
Base directory for all savegames.
Definition: fileio_type.h:112
bool operator==(const MultiMapIterator< Tmap_iter1, Tlist_iter1, Tkey, Tvalue1, Tcompare > &iter1, const MultiMapIterator< Tmap_iter2, Tlist_iter2, Tkey, Tvalue2, Tcompare > &iter2)
Compare two MultiMap iterators.
Definition: multimap.hpp:205
void BuildFileList(AbstractFileType abstract_filetype, SaveLoadOperation fop)
Construct a file list with the given kind of files, for the stated purpose.
Definition: fios.cpp:76
const FiosItem * Get(uint index) const
Get a pointer to the indicated file information.
Definition: fios.h:150
FileList & file_list
Destination of the found files.
Definition: fios.cpp:286
Part of the network protocol handling content distribution.
static void GetFileTitle(const char *file, char *title, const char *last, Subdirectory subdir)
Get the title of a file, which (if exists) is stored in a file named the same as the data file but wi...
Definition: fios.cpp:433
File is being loaded.
Definition: fileio_type.h:51
int CDECL CompareFiosItems(const FiosItem *da, const FiosItem *db)
Compare two FiosItem&#39;s.
Definition: fios.cpp:52
uint32 StringID
Numeric value that represents a string, independent of the selected language.
Definition: strings_type.h:18
byte md5sum[16]
The MD5 checksum.
Definition: tcp_content.h:78
const TCHAR * OTTD2FS(const char *name, bool console_cp)
Convert from OpenTTD&#39;s encoding to that of the local environment.
Definition: win32.cpp:631
SaveLoadOperation
Operation performed on the file.
Definition: fileio_type.h:49
void Compact()
Compact the list down to the smallest block size boundary.
Definition: fios.h:185
bool FiosDelete(const char *name)
Delete a file.
Definition: fios.cpp:270
void FiosGetHeightmapList(SaveLoadOperation fop, FileList &file_list)
Get a list of heightmaps.
Definition: fios.cpp:619
static void FiosGetFileList(SaveLoadOperation fop, fios_getlist_callback_proc *callback_proc, Subdirectory subdir, FileList &file_list)
Fill the list of the files in a directory, according to some arbitrary rule.
Definition: fios.cpp:358
void FiosMakeHeightmapName(char *buf, const char *name, const char *last)
Construct a filename for a height map.
Definition: fios.cpp:256
static bool StrEmpty(const char *s)
Check if a string buffer is empty.
Definition: string_func.h:59
Declarations for savegames operations.
List of file information.
Definition: fios.h:106
const char * FiosBrowseTo(const FiosItem *item)
Browse to a new path based on the passed item, starting at #_fios_path.
Definition: fios.cpp:152
static FiosType FiosGetScenarioListCallback(SaveLoadOperation fop, const char *file, const char *ext, char *title, const char *last)
Callback for FiosGetFileList.
Definition: fios.cpp:521
Scanner to scan for a particular type of FIOS file.
Definition: fios.cpp:283
char * strecpy(char *dst, const char *src, const char *last)
Copies characters from one buffer to another.
Definition: depend.cpp:68
const char * GetCurrentScreenshotExtension()
Get filename extension of current screenshot file format.
Definition: screenshot.cpp:581
static DIR * ttd_opendir(const char *path)
A wrapper around opendir() which will convert the string from OPENTTD encoding to that of the filesys...
Definition: fileio_func.h:147
heightmap file
Definition: fileio_type.h:22
SaveLoadOperation fop
The kind of file we are looking for.
Definition: fios.cpp:284
FiosItem * Append()
Construct a new entry in the file list.
Definition: fios.h:114
ScenarioScanner()
Initialise.
Definition: fios.cpp:685
const char * FiosGetScreenshotDir()
Get the directory for screenshots.
Definition: fios.cpp:644
static void QSortT(T *base, uint num, int(CDECL *comparator)(const T *, const T *), bool desc=false)
Type safe qsort()
Definition: sort_func.hpp:28
Container for all important information about a piece of content.
Definition: tcp_content.h:58