summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDarkvater <darkvater@openttd.org>2006-04-11 17:05:50 +0000
committerDarkvater <darkvater@openttd.org>2006-04-11 17:05:50 +0000
commit57bc71993cb4a46990fff451b6c20e22076e635d (patch)
treef7faa0b4e619458e72b7d5a6d3507802b501b20d
parentaa4f6605ad0549aaaffc96c88b2755b122dd3f7d (diff)
downloadopenttd-57bc71993cb4a46990fff451b6c20e22076e635d.tar.xz
(svn r4361) - Fix: Write and Read the true length of a string-buffer into the savegame instead of the whole buffer.
-rw-r--r--saveload.c49
1 files changed, 41 insertions, 8 deletions
diff --git a/saveload.c b/saveload.c
index f3a4c77fe..475a7675c 100644
--- a/saveload.c
+++ b/saveload.c
@@ -489,25 +489,58 @@ static void SlSaveLoadConv(void *ptr, VarType conv)
}
}
+/** Calculate the net length of a string. This is in almost all cases
+ * just strlen(), but if the string is not properly terminated, we'll
+ * resort to the maximum length of the buffer.
+ * @param ptr pointer to the stringbuffer
+ * @param length maximum length of the string (buffer)
+ * @return return the net length of the string */
+static inline size_t SlCalcNetStringLen(const char *ptr, uint length)
+{
+ return minu(strlen(ptr), length - 1);
+}
+
+/** Calculate the gross length of the string that it
+ * will occupy in the savegame. This includes the real length, returned
+ * by SlCalcNetStringLen and the length that the index will occupy.
+ * @param ptr pointer to the stringbuffer
+ * @param length maximum length of the string (buffer size, etc.)
+ * @return return the gross length of the string */
static inline size_t SlCalcStringLen(const char *ptr, uint length)
{
- DEBUG(misc, 1) ("[Sl] TODO: save/load real length");
- return length;//(_sl.save) ? min(strlen(ptr), length - 1) : SlReadArrayLength();
+ uint len = SlCalcNetStringLen(ptr, length);
+ return len + SlGetArrayLength(len); // also include the length of the index
}
/**
* 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
- * @todo the full length of the string is saved, even when the buffer
- * is 512 bytes and only 10 of those are used */
+ * @param conv must be SLE_FILE_STRING */
static void SlString(void *ptr, uint length, VarType conv)
{
- uint len = SlCalcStringLen(ptr, length);
- assert((conv & 0xF) == SLE_FILE_STRING);
+ uint len;
+ assert(GetVarFileType(conv) == SLE_FILE_STRING);
+
+ if (_sl.save) {
+ len = SlCalcNetStringLen(ptr, length);
+ SlWriteArrayLength(len);
+ SlCopyBytes(ptr, len);
+ return;
+ }
+
+ len = SlReadArrayLength();
+
+ if (len >= length) {
+ DEBUG(misc, 0) ("[Sl] String length in savegame is bigger than buffer, truncating");
+ SlCopyBytes(ptr, length);
+ SlSkipBytes(len - length);
+ len = length - 1;
+ } else {
+ SlCopyBytes(ptr, len);
+ }
- SlCopyBytes(ptr, len);
+ ((char*)ptr)[len] = '\0'; // properly terminate the string
}
/**