(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
--- a/saveload.c Sat Aug 12 17:33:05 2006 +0000
+++ b/saveload.c Sat Aug 12 22:56:45 2006 +0000
@@ -510,11 +510,13 @@
* 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);
--- a/saveload.h Sat Aug 12 17:33:05 2006 +0000
+++ b/saveload.h Sat Aug 12 22:56:45 2006 +0000
@@ -98,8 +98,10 @@
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 @@
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
--- a/settings.c Sat Aug 12 17:33:05 2006 +0000
+++ b/settings.c Sat Aug 12 22:56:45 2006 +0000
@@ -695,8 +695,15 @@
case SDT_STRING:
switch (GetVarMemType(sld->conv)) {
case SLE_VAR_STRB:
+ 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) ttd_strlcpy((char*)ptr, p, sld->length);
+ 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 @@
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 @@
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()
};