(svn r5871) -Feature: Add a possibility to handle pointer strings without a buffer from the configuration file. Handy for variables that will never be changed during runtime

This commit is contained in:
Darkvater 2006-08-12 22:56:45 +00:00
parent df4f4253da
commit 00aaf8b8f1
3 changed files with 24 additions and 7 deletions

View File

@ -510,11 +510,13 @@ static inline size_t SlCalcStringLen(const char *ptr, uint length)
* Save/Load a string.
* @param ptr the string being manipulated
* @param the length of the string (full length)
* @param conv must be SLE_FILE_STRING */
* @param conv must be SLE_FILE_STRING
* XXX - only works with global strings of a pre-allocated buffer */
static void SlString(void *ptr, uint length, VarType conv)
{
uint len;
assert(GetVarFileType(conv) == SLE_FILE_STRING);
assert(GetVarMemType(conv) == SLE_VAR_STRB || GetVarMemType(conv) == SLE_VAR_STRQ);
if (_sl.save) {
len = SlCalcNetStringLen(ptr, length);

View File

@ -98,8 +98,10 @@ enum VarTypes {
SLE_VAR_U64 = 8 << 4,
SLE_VAR_NULL = 9 << 4, ///< useful to write zeros in savegame.
SLE_VAR_STRB = 10 << 4, ///< normal string (with pre-allocated buffer)
SLE_VAR_STRQ = 11 << 4, ///< string enclosed in parentheses
/* 4 more possible memory-primitives */
SLE_VAR_STRBQ= 11 << 4, ///< string enclosed in parentheses (with pre-allocated buffer)
SLE_VAR_STR = 12 << 4, ///< string pointer
SLE_VAR_STRQ = 13 << 4, ///< string enclosed in parentheses
/* 2 more possible memory-primitives */
/* Shortcut values */
SLE_VAR_CHAR = SLE_VAR_I8,
@ -119,12 +121,16 @@ enum VarTypes {
SLE_CHAR = SLE_FILE_I8 | SLE_VAR_CHAR,
SLE_STRINGID = SLE_FILE_STRINGID | SLE_VAR_U16,
SLE_STRINGBUF = SLE_FILE_STRING | SLE_VAR_STRB,
SLE_STRINGBQUOTE= SLE_FILE_STRING | SLE_VAR_STRBQ,
SLE_STRING = SLE_FILE_STRING | SLE_VAR_STR,
SLE_STRINGQUOTE = SLE_FILE_STRING | SLE_VAR_STRQ,
/* Shortcut values */
SLE_UINT = SLE_UINT32,
SLE_INT = SLE_INT32,
SLE_STRB = SLE_STRINGBUF,
SLE_STRBQ= SLE_STRINGBQUOTE,
SLE_STR = SLE_STRING,
SLE_STRQ = SLE_STRINGQUOTE,
/* 8 bytes allocated for a maximum of 8 flags

View File

@ -695,9 +695,16 @@ static void ini_load_settings(IniFile *ini, const SettingDesc *sd, const char *g
case SDT_STRING:
switch (GetVarMemType(sld->conv)) {
case SLE_VAR_STRB:
case SLE_VAR_STRQ:
case SLE_VAR_STRBQ:
if (p != NULL) ttd_strlcpy((char*)ptr, p, sld->length);
break;
case SLE_VAR_STR:
case SLE_VAR_STRQ:
if (p != NULL) {
free(*(char**)ptr);
*(char**)ptr = strdup((const char*)p);
}
break;
case SLE_VAR_CHAR: *(char*)ptr = *(char*)p; break;
default: NOT_REACHED(); break;
}
@ -806,7 +813,9 @@ static void ini_save_settings(IniFile *ini, const SettingDesc *sd, const char *g
case SDT_STRING:
switch (GetVarMemType(sld->conv)) {
case SLE_VAR_STRB: strcpy(buf, (char*)ptr); break;
case SLE_VAR_STRQ: sprintf(buf, "\"%s\"", (char*)ptr); break;
case SLE_VAR_STRBQ:sprintf(buf, "\"%s\"", (char*)ptr); break;
case SLE_VAR_STR: strcpy(buf, *(char**)ptr); break;
case SLE_VAR_STRQ: sprintf(buf, "\"%s\"", *(char**)ptr); break;
case SLE_VAR_CHAR: sprintf(buf, "\"%c\"", *(char*)ptr); break;
default: NOT_REACHED();
}
@ -1431,8 +1440,8 @@ static const SettingDesc _currency_settings[] = {
SDT_VAR(CurrencySpec, rate, SLE_UINT16, S, 0, 1, 0, 100, STR_NULL, NULL),
SDT_CHR(CurrencySpec, separator, S, 0, ".", STR_NULL, NULL),
SDT_VAR(CurrencySpec, to_euro, SLE_UINT16, S, 0, 0, 0,1000, STR_NULL, NULL),
SDT_STR(CurrencySpec, prefix, SLE_STRQ, S, 0, NULL, STR_NULL, NULL),
SDT_STR(CurrencySpec, suffix, SLE_STRQ, S, 0, " credits", STR_NULL, NULL),
SDT_STR(CurrencySpec, prefix, SLE_STRBQ, S, 0, NULL, STR_NULL, NULL),
SDT_STR(CurrencySpec, suffix, SLE_STRBQ, S, 0, " credits", STR_NULL, NULL),
SDT_END()
};