Changeset - r6088:dd09788d020f
[Not reviewed]
master
0 4 0
Darkvater - 17 years ago 2007-02-20 09:46:10
darkvater@openttd.org
(svn r8823) -Fix (r8821): wrapping opendir to ottd-specific function failed on non-windows because this wrapper was only active for win32 (thx Rubidium)
4 files changed with 4 insertions and 10 deletions:
0 comments (0 inline, 0 general)
src/fios.cpp
Show inline comments
 
/* $Id$ */
 

	
 
/** @file fios.c
 
 * This file contains functions for building file lists for the save/load dialogs.
 
 */
 

	
 
#include "stdafx.h"
 
#include "openttd.h"
 
#include "hal.h"
 
#include "string.h"
 
#include "variables.h"
 
#include "functions.h"
 
#include "heightmap.h"
 
#include "helpers.hpp"
 
#include "table/strings.h"
 
#include "fios.h"
 
#include <sys/types.h>
 
#include <sys/stat.h>
 

	
 
#ifdef WIN32
 
# include <io.h>
 
#else
 
# include <unistd.h>
 
# include <dirent.h>
 
#endif /* WIN32 */
 

	
 
/* Variables to display file lists */
 
int _fios_num;
 

	
 
static char *_fios_path;
 
static FiosItem *_fios_items;
 
static int _fios_count, _fios_alloc;
 

	
 
/* OS-specific functions are taken from their respective files (win32/unix/os2 .c) */
 
extern bool FiosIsRoot(const char *path);
 
extern bool FiosIsValidFile(const char *path, const struct dirent *ent, struct stat *sb);
 
extern void FiosGetDrives(void);
 
extern bool FiosGetDiskFreeSpace(const char *path, uint32 *tot);
 

	
 
/* get the name of an oldstyle savegame */
 
extern void GetOldSaveGameName(char *title, const char *path, const char *file);
 

	
 
/**
 
 * Allocate a new FiosItem.
 
 * @return A pointer to the newly allocated FiosItem.
 
 */
 
FiosItem *FiosAlloc(void)
 
{
 
	if (_fios_count == _fios_alloc) {
 
		_fios_alloc += 256;
 
		_fios_items = ReallocT(_fios_items, _fios_alloc);
 
	}
 
	return &_fios_items[_fios_count++];
 
}
 

	
 
/**
 
 * Compare two FiosItem's. Used with qsort when sorting the file list.
 
 * @param a A pointer to the first FiosItem to compare.
 
 * @param a A pointer to the second FiosItem to compare.
 
 * @return -1, 0 or 1, depending on how the two items should be sorted.
 
 */
 
int CDECL compare_FiosItems(const void *a, const void *b)
 
{
 
	const FiosItem *da = (const FiosItem *)a;
 
	const FiosItem *db = (const FiosItem *)b;
 
	int r;
 

	
 
	if (_savegame_sort_order & SORT_BY_NAME) {
 
		r = strcasecmp(da->title, db->title);
 
	} else {
 
		r = da->mtime < db->mtime ? -1 : 1;
 
	}
 

	
 
	if (_savegame_sort_order & SORT_DESCENDING) r = -r;
 
	return r;
 
}
 

	
 
/**
 
 * Free the list of savegames
 
 */
 
void FiosFreeSavegameList(void)
 
{
 
	free(_fios_items);
 
	_fios_items = NULL;
 
	_fios_alloc = _fios_count = 0;
 
}
 

	
 
/**
 
 * Get descriptive texts. Returns the path and free space
 
 * left on the device
 
 * @param path string describing the path
 
 * @param total_free total free space in megabytes, optional (can be NULL)
 
 * @return StringID describing the path (free space or failure)
 
 */
 
StringID FiosGetDescText(const char **path, uint32 *total_free)
 
{
 
	*path = _fios_path;
 
	return FiosGetDiskFreeSpace(*path, total_free) ? STR_4005_BYTES_FREE : STR_4006_UNABLE_TO_READ_DRIVE;
 
}
 

	
 
/* Browse to a new path based on the passed FiosItem struct
 
 * @param *item FiosItem object telling us what to do
 
 * @return a string if we have given a file as a target, otherwise NULL */
 
char *FiosBrowseTo(const FiosItem *item)
 
{
 
	char *s;
 
	char *path = _fios_path;
 

	
 
	switch (item->type) {
 
#if defined(WIN32) || defined(__OS2__)
 
	case FIOS_TYPE_DRIVE: sprintf(path, "%c:" PATHSEP, item->title[0]); break;
 
#endif
 

	
 
	case FIOS_TYPE_PARENT:
 
		/* Check for possible NULL ptr (not required for UNIXes, but AmigaOS-alikes) */
 
		if ((s = strrchr(path, PATHSEPCHAR)) != NULL) {
 
			s[1] = '\0'; // go up a directory
 
			if (!FiosIsRoot(path)) s[0] = '\0';
 
		}
 
#if defined(__MORPHOS__) || defined(__AMIGAOS__)
src/fios.h
Show inline comments
 
/* $Id$ */
 

	
 
#ifndef FIOS_H
 
#define FIOS_H
 

	
 
/* Deals with finding savegames */
 
typedef struct {
 
	byte type;
 
	uint64 mtime;
 
	char title[64];
 
	char name[256 - 12 - 64];
 
} FiosItem;
 

	
 
enum {
 
	FIOS_TYPE_DRIVE        =   0,
 
	FIOS_TYPE_PARENT       =   1,
 
	FIOS_TYPE_DIR          =   2,
 
	FIOS_TYPE_FILE         =   3,
 
	FIOS_TYPE_OLDFILE      =   4,
 
	FIOS_TYPE_SCENARIO     =   5,
 
	FIOS_TYPE_OLD_SCENARIO =   6,
 
	FIOS_TYPE_DIRECT       =   7,
 
	FIOS_TYPE_PNG          =   8,
 
	FIOS_TYPE_BMP          =   9,
 
	FIOS_TYPE_INVALID      = 255,
 
};
 

	
 
/* Variables to display file lists */
 
extern FiosItem *_fios_list; // defined in misc_gui.c
 
extern int _fios_num;        // defined in fios.c, read_only version of _fios_count
 
extern int _saveload_mode;   // defined in misc_gui.c
 

	
 
// Get a list of savegames
 
FiosItem *FiosGetSavegameList(int mode);
 
// Get a list of scenarios
 
FiosItem *FiosGetScenarioList(int mode);
 
// Get a list of Heightmaps
 
FiosItem *FiosGetHeightmapList(int mode);
 
// Free the list of savegames
 
void FiosFreeSavegameList(void);
 
// Browse to. Returns a filename w/path if we reached a file.
 
char *FiosBrowseTo(const FiosItem *item);
 
// Return path, free space and stringID
 
StringID FiosGetDescText(const char **path, uint32 *total_free);
 
// Delete a name
 
bool FiosDelete(const char *name);
 
// Make a filename from a name
 
void FiosMakeSavegameName(char *buf, const char *name, size_t size);
 
// Allocate a new FiosItem
 
FiosItem *FiosAlloc(void);
 

	
 
int CDECL compare_FiosItems(const void *a, const void *b);
 

	
 
/* Implementation of opendir/readdir/closedir for Windows */
 
#if defined(WIN32)
 
#include <windows.h>
 
typedef struct DIR DIR;
 

	
 
typedef struct dirent { // XXX - only d_name implemented
 
	wchar_t *d_name; /* name of found file */
 
	/* little hack which will point to parent DIR struct which will
 
	 * save us a call to GetFileAttributes if we want information
 
	 * about the file (for example in function fio_bla */
 
	DIR *dir;
 
} dirent;
 

	
 
struct DIR {
 
	HANDLE hFind;
 
	/* the dirent returned by readdir.
 
	 * note: having only one global instance is not possible because
 
	 * multiple independent opendir/readdir sequences must be supported. */
 
	dirent ent;
 
	WIN32_FIND_DATAW fd;
 
	/* since opendir calls FindFirstFile, we need a means of telling the
 
	 * first call to readdir that we already have a file.
 
	 * that's the case iff this is true */
 
	bool at_first_entry;
 
};
 

	
 
DIR *opendir(const wchar_t *path);
 
struct dirent *readdir(DIR *d);
 
int closedir(DIR *d);
 
#else
 
/* Use system-supplied opendir/readdir/closedir functions */
 
# include <sys/types.h>
 
# include <dirent.h>
 
#endif /* defined(WIN32) */
 

	
 
/**
 
 * A wrapper around opendir() which will convert the string from
 
 * OPENTTD encoding to that of the filesystem. For all purposes this
 
 * function behaves the same as the original opendir function
 
 * @param path string to open directory of
 
 * @return DIR pointer
 
 */
 
static inline DIR *ttd_opendir(const char *path)
 
{
 
	return opendir(OTTD2FS(path));
 
}
 

	
 
#endif /* FIOS_H */
src/newgrf_config.cpp
Show inline comments
 
/* $Id$ */
 

	
 
#include "stdafx.h"
 
#include "openttd.h"
 
#include "functions.h"
 
#include "macros.h"
 
#include "debug.h"
 
#include "variables.h"
 
#include "string.h"
 
#include "saveload.h"
 
#include "md5.h"
 
#include "network/network_data.h"
 
#include "newgrf.h"
 
#include "newgrf_config.h"
 
#include "helpers.hpp"
 

	
 
#include "fileio.h"
 
#include "fios.h"
 
#include <sys/types.h>
 
#include <sys/stat.h>
 

	
 
#ifdef WIN32
 
# include <io.h>
 
#else
 
# include <unistd.h>
 
# include <dirent.h>
 
#endif /* WIN32 */
 

	
 

	
 
GRFConfig *_all_grfs;
 
GRFConfig *_grfconfig;
 
GRFConfig *_grfconfig_newgame;
 
GRFConfig *_grfconfig_static;
 

	
 

	
 
/* Calculate the MD5 Sum for a GRF */
 
static bool CalcGRFMD5Sum(GRFConfig *config)
 
{
 
	FILE *f;
 
	char filename[MAX_PATH];
 
	md5_state_t md5state;
 
	md5_byte_t buffer[1024];
 
	size_t len;
 

	
 
	/* open the file */
 
	snprintf(filename, lengthof(filename), "%s%s", _paths.data_dir, config->filename);
 
	f = fopen(filename, "rb");
 
	if (f == NULL) return false;
 

	
 
	/* calculate md5sum */
 
	md5_init(&md5state);
 
	while ((len = fread(buffer, 1, sizeof(buffer), f)) != 0) {
 
		md5_append(&md5state, buffer, len);
 
	}
 
	md5_finish(&md5state, config->md5sum);
 

	
 
	fclose(f);
 

	
 
	return true;
 
}
 

	
 

	
 
/* Find the GRFID and calculate the md5sum */
 
bool FillGRFDetails(GRFConfig *config, bool is_static)
 
{
 
	if (!FioCheckFileExists(config->filename)) {
 
		SETBIT(config->flags, GCF_NOT_FOUND);
 
		return false;
 
	}
 

	
 
	/* Find and load the Action 8 information */
 
	/* 62 is the last file slot before sample.cat.
 
	 * Should perhaps be some "don't care" value */
 
	LoadNewGRFFile(config, 62, GLS_FILESCAN);
 

	
 
	/* Skip if the grfid is 0 (not read) or 0xFFFFFFFF (ttdp system grf) */
 
	if (config->grfid == 0 || config->grfid == 0xFFFFFFFF) return false;
 

	
 
	if (is_static) {
 
		/* Perform a 'safety scan' for static GRFs */
 
		LoadNewGRFFile(config, 62, GLS_SAFETYSCAN);
 

	
 
		/* GCF_UNSAFE is set if GLS_SAFETYSCAN finds unsafe actions */
 
		if (HASBIT(config->flags, GCF_UNSAFE)) return false;
 
	}
 

	
 
	return CalcGRFMD5Sum(config);
 
}
 

	
 

	
 
void ClearGRFConfig(GRFConfig **config)
 
{
 
	/* GCF_COPY as in NOT strdupped/alloced the filename, name and info */
 
	if (!HASBIT((*config)->flags, GCF_COPY)) {
 
		free((*config)->filename);
 
		free((*config)->name);
 
		free((*config)->info);
 
	}
 
	free(*config);
 
	*config = NULL;
 
}
 

	
 

	
 
/* Clear a GRF Config list */
 
void ClearGRFConfigList(GRFConfig **config)
 
{
 
	GRFConfig *c, *next;
 
	for (c = *config; c != NULL; c = next) {
 
		next = c->next;
 
		ClearGRFConfig(&c);
 
	}
 
	*config = NULL;
 
}
 

	
 

	
 
/** Copy a GRF Config list
 
 * @param dst pointer to destination list
 
 * @param src pointer to source list values
 
 * @return pointer to the last value added to the destination list */
 
GRFConfig **CopyGRFConfigList(GRFConfig **dst, const GRFConfig *src)
 
{
 
	/* Clear destination as it will be overwritten */
src/strings.cpp
Show inline comments
 
/* $Id$ */
 

	
 
#include "stdafx.h"
 
#include "openttd.h"
 
#include "currency.h"
 
#include "functions.h"
 
#include "string.h"
 
#include "strings.h"
 
#include "table/strings.h"
 
#include "namegen.h"
 
#include "station.h"
 
#include "town.h"
 
#include "vehicle.h"
 
#include "news.h"
 
#include "screenshot.h"
 
#include "waypoint.h"
 
#include "industry.h"
 
#include "variables.h"
 
#include "newgrf_text.h"
 
#include "table/landscape_const.h"
 
#include "table/control_codes.h"
 
#include "music.h"
 
#include "date.h"
 
#include "industry.h"
 
#include "helpers.hpp"
 

	
 
#ifdef WIN32
 
/* for opendir/readdir/closedir */
 
# include "fios.h"
 
#else
 
# include <sys/types.h>
 
# include <dirent.h>
 
#endif /* WIN32 */
 

	
 
char _userstring[128];
 

	
 
static char *StationGetSpecialString(char *buff, int x, const char* last);
 
static char *GetSpecialTownNameString(char *buff, int ind, uint32 seed, const char* last);
 
static char *GetSpecialPlayerNameString(char *buff, int ind, const int32 *argv, const char* last);
 

	
 
static char *FormatString(char *buff, const char *str, const int32 *argv, uint casei, const char* last);
 

	
 
typedef struct LanguagePack {
 
	uint32 ident;
 
	uint32 version;     // 32-bits of auto generated version info which is basically a hash of strings.h
 
	char name[32];      // the international name of this language
 
	char own_name[32];  // the localized name of this language
 
	char isocode[16];   // the ISO code for the language (not country code)
 
	uint16 offsets[32]; // the offsets
 
	byte plural_form;   // how to compute plural forms
 
	byte pad[3];        // pad header to be a multiple of 4
 
	char data[VARARRAY_SIZE];
 
} LanguagePack;
 

	
 
static char **_langpack_offs;
 
static LanguagePack *_langpack;
 
static uint _langtab_num[32]; // Offset into langpack offs
 
static uint _langtab_start[32]; // Offset into langpack offs
 

	
 

	
 
// Read an int64 from the argv array.
 
static inline int64 GetInt64(const int32 **argv)
 
{
 
	int64 result;
 

	
 
	assert(argv);
 
	result = (uint32)(*argv)[0] + ((uint64)(uint32)(*argv)[1] << 32);
 
	(*argv)+=2;
 
	return result;
 
}
 

	
 
// Read an int32 from the argv array.
 
static inline int32 GetInt32(const int32 **argv)
 
{
 
	assert(argv);
 
	return *(*argv)++;
 
}
 

	
 
// Read an array from the argv array.
 
static inline const int32 *GetArgvPtr(const int32 **argv, int n)
 
{
 
	const int32 *result;
 
	assert(*argv);
 
	result = *argv;
 
	(*argv) += n;
 
	return result;
 
}
 

	
 

	
 
#define NUM_BOUND_STRINGS 8
 

	
 
// Array to hold the bound strings.
 
static const char *_bound_strings[NUM_BOUND_STRINGS];
 

	
 
// This index is used to implement a "round-robin" allocating of
 
// slots for BindCString. NUM_BOUND_STRINGS slots are reserved.
 
// Which means that after NUM_BOUND_STRINGS calls to BindCString,
 
// the indices will be reused.
 
static int _bind_index;
 

	
 
static const char *GetStringPtr(StringID string)
 
{
 
	return _langpack_offs[_langtab_start[string >> 11] + (string & 0x7FF)];
 
}
 

	
 
// The highest 8 bits of string contain the "case index".
 
// These 8 bits will only be set when FormatString wants to print
 
// the string in a different case. No one else except FormatString
 
// should set those bits, therefore string CANNOT be StringID, but uint32.
 
static char *GetStringWithArgs(char *buffr, uint string, const int32 *argv, const char* last)
 
{
 
	uint index = GB(string,  0, 11);
 
	uint tab   = GB(string, 11,  5);
 
	char buff[512];
 

	
 
	if (GB(string, 0, 16) == 0) error("!invalid string id 0 in GetString");
 

	
 
	switch (tab) {
 
		case 4:
 
			if (index >= 0xC0)
 
				return GetSpecialTownNameString(buffr, index - 0xC0, GetInt32(&argv), last);
 
			break;
 

	
 
		case 14:
 
			if (index >= 0xE4)
 
				return GetSpecialPlayerNameString(buffr, index - 0xE4, argv, last);
 
			break;
 

	
 
		// User defined name
0 comments (0 inline, 0 general)