(svn r1821) Move generic string handling functions to string.[ch] and introduce stre{cpy,cat}, see string.h for their semantics
authortron
Sun, 06 Feb 2005 13:41:02 +0000
changeset 1317 f382f1b439c7
parent 1316 a7352de83a2b
child 1318 0cfb27523b44
(svn r1821) Move generic string handling functions to string.[ch] and introduce stre{cpy,cat}, see string.h for their semantics
Makefile
console_cmds.c
functions.h
misc_cmd.c
network.c
network_client.c
network_gui.c
network_server.c
network_udp.c
os2.c
players.c
settings.c
settings_gui.c
stdafx.h
string.c
string.h
strings.c
ttd.c
unix.c
w32dm.c
win32.c
--- a/Makefile	Sun Feb 06 11:23:41 2005 +0000
+++ b/Makefile	Sun Feb 06 13:41:02 2005 +0000
@@ -624,6 +624,7 @@
 C_SOURCES += spritecache.c
 C_SOURCES += station_cmd.c
 C_SOURCES += station_gui.c
+C_SOURCES += string.c
 C_SOURCES += strings.c
 C_SOURCES += subsidy_gui.c
 C_SOURCES += terraform_gui.c
--- a/console_cmds.c	Sun Feb 06 11:23:41 2005 +0000
+++ b/console_cmds.c	Sun Feb 06 13:41:02 2005 +0000
@@ -5,6 +5,7 @@
 #include "debug.h"
 #include "engine.h"
 #include "functions.h"
+#include "string.h"
 #include "variables.h"
 #include "network_data.h"
 #include "network_client.h"
--- a/functions.h	Sun Feb 06 11:23:41 2005 +0000
+++ b/functions.h	Sun Feb 06 13:41:02 2005 +0000
@@ -249,9 +249,6 @@
 };
 void ShowSaveLoadDialog(int mode);
 
-void ttd_strlcpy(char *dst, const char *src, size_t size);
-void ttd_strlcat(char *dst, const char *src, size_t size);
-
 // callback from drivers that is called if the game size changes dynamically
 void GameSizeChanged(void);
 bool FileExists(const char *filename);
--- a/misc_cmd.c	Sun Feb 06 11:23:41 2005 +0000
+++ b/misc_cmd.c	Sun Feb 06 13:41:02 2005 +0000
@@ -1,6 +1,7 @@
 
 #include "stdafx.h"
 #include "ttd.h"
+#include "string.h"
 #include "table/strings.h"
 #include "command.h"
 #include "player.h"
--- a/network.c	Sun Feb 06 11:23:41 2005 +0000
+++ b/network.c	Sun Feb 06 13:41:02 2005 +0000
@@ -1,5 +1,6 @@
 #include "stdafx.h"
 #include "debug.h"
+#include "string.h"
 #include "strings.h"
 #include "map.h"
 #include "network_data.h"
--- a/network_client.c	Sun Feb 06 11:23:41 2005 +0000
+++ b/network_client.c	Sun Feb 06 13:41:02 2005 +0000
@@ -1,5 +1,6 @@
 #include "stdafx.h"
 #include "debug.h"
+#include "string.h"
 #include "strings.h"
 #include "network_data.h"
 
--- a/network_gui.c	Sun Feb 06 11:23:41 2005 +0000
+++ b/network_gui.c	Sun Feb 06 13:41:02 2005 +0000
@@ -1,5 +1,6 @@
 #include "stdafx.h"
 #include "ttd.h"
+#include "string.h"
 #include "strings.h"
 #include "network.h"
 #include "saveload.h"
--- a/network_server.c	Sun Feb 06 11:23:41 2005 +0000
+++ b/network_server.c	Sun Feb 06 13:41:02 2005 +0000
@@ -1,5 +1,6 @@
 #include "stdafx.h"
 #include "debug.h"
+#include "string.h"
 #include "strings.h"
 #include "network_data.h"
 
--- a/network_udp.c	Sun Feb 06 11:23:41 2005 +0000
+++ b/network_udp.c	Sun Feb 06 13:41:02 2005 +0000
@@ -1,5 +1,6 @@
 #include "stdafx.h"
 #include "debug.h"
+#include "string.h"
 #include "network_data.h"
 
 #ifdef ENABLE_NETWORK
--- a/os2.c	Sun Feb 06 11:23:41 2005 +0000
+++ b/os2.c	Sun Feb 06 13:41:02 2005 +0000
@@ -1,5 +1,6 @@
 #include "stdafx.h"
 #include "ttd.h"
+#include "string.h"
 #include "table/strings.h"
 #include "hal.h"
 
--- a/players.c	Sun Feb 06 11:23:41 2005 +0000
+++ b/players.c	Sun Feb 06 13:41:02 2005 +0000
@@ -1,5 +1,6 @@
 #include "stdafx.h"
 #include "ttd.h"
+#include "string.h"
 #include "strings.h"
 #include "table/strings.h"
 #include "map.h"
--- a/settings.c	Sun Feb 06 11:23:41 2005 +0000
+++ b/settings.c	Sun Feb 06 13:41:02 2005 +0000
@@ -1,6 +1,7 @@
 #include "stdafx.h"
 #include "ttd.h"
 #include "sound.h"
+#include "string.h"
 #include "table/currency.h"
 #include "network.h"
 #include "settings.h"
--- a/settings_gui.c	Sun Feb 06 11:23:41 2005 +0000
+++ b/settings_gui.c	Sun Feb 06 13:41:02 2005 +0000
@@ -1,5 +1,6 @@
 #include "stdafx.h"
 #include "ttd.h"
+#include "string.h"
 #include "strings.h" // XXX GetCurrentCurrencyRate()
 #include "table/strings.h"
 #include "window.h"
--- a/stdafx.h	Sun Feb 06 11:23:41 2005 +0000
+++ b/stdafx.h	Sun Feb 06 13:41:02 2005 +0000
@@ -214,6 +214,7 @@
 
 #define lengthof(x) (sizeof(x)/sizeof(x[0]))
 #define endof(x) (&x[lengthof(x)])
+#define lastof(x) (&x[lengthof(x) - 1])
 #ifndef offsetof
 #define offsetof(s,m)   (size_t)&(((s *)0)->m)
 #endif
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/string.c	Sun Feb 06 13:41:02 2005 +0000
@@ -0,0 +1,39 @@
+#include "stdafx.h"
+#include "string.h"
+
+void ttd_strlcat(char *dst, const char *src, size_t size)
+{
+	assert(size > 0);
+	for (; size > 0 && *dst != '\0'; --size, ++dst) {}
+	assert(size > 0);
+	while (--size > 0 && *src != '\0') *dst++ = *src++;
+	*dst = '\0';
+}
+
+
+void ttd_strlcpy(char *dst, const char *src, size_t size)
+{
+	assert(size > 0);
+	while (--size > 0 && *src != '\0') *dst++ = *src++;
+	*dst = '\0';
+}
+
+
+char* strecat(char* dst, const char* src, const char* last)
+{
+	assert(last == NULL || dst <= last);
+	for (; *dst != '\0'; ++dst)
+		if (dst == last) return dst;
+	for (; *src != '\0' && dst != last; ++dst, ++src) *dst = *src;
+	*dst = '\0';
+	return strecpy(dst, src, last);
+}
+
+
+char* strecpy(char* dst, const char* src, const char* last)
+{
+	assert(last == NULL || dst <= last);
+	for (; *src != '\0' && dst != last; ++dst, ++src) *dst = *src;
+	*dst = '\0';
+	return dst;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/string.h	Sun Feb 06 13:41:02 2005 +0000
@@ -0,0 +1,24 @@
+#ifndef STRING_H
+#define STRING_H
+
+/*
+ * dst: destination buffer
+ * src: string to copy/concatenate
+ * size: size of the destination buffer
+ * usage: ttd_strlcpy(dst, src, lengthof(dst));
+ */
+void ttd_strlcat(char *dst, const char *src, size_t size);
+void ttd_strlcpy(char *dst, const char *src, size_t size);
+
+/*
+ * dst: destination buffer
+ * src: string to copy
+ * last: pointer to the last element in the dst array
+ *       if NULL no boundary check is performed
+ * returns a pointer to the terminating \0 in the destination buffer
+ * usage: strecpy(dst, src, lastof(dst));
+ */
+char* strecat(char* dst, const char* src, const char* last);
+char* strecpy(char* dst, const char* src, const char* last);
+
+#endif
--- a/strings.c	Sun Feb 06 11:23:41 2005 +0000
+++ b/strings.c	Sun Feb 06 13:41:02 2005 +0000
@@ -1,5 +1,6 @@
 #include "stdafx.h"
 #include "ttd.h"
+#include "string.h"
 #include "strings.h"
 #include "table/strings.h"
 #include "namegen.h"
--- a/ttd.c	Sun Feb 06 11:23:41 2005 +0000
+++ b/ttd.c	Sun Feb 06 13:41:02 2005 +0000
@@ -1,4 +1,5 @@
 #include "stdafx.h"
+#include "string.h"
 #include "table/strings.h"
 #include "debug.h"
 #include "strings.h"
@@ -197,27 +198,6 @@
 	return best;
 }
 
-void ttd_strlcpy(char *dst, const char *src, size_t size)
-{
-	assert(size > 0);
-	while (--size > 0 && *src != '\0') *dst++ = *src++;
-	*dst = '\0';
-}
-
-void ttd_strlcat(char *dst, const char *src, size_t size)
-{
-	assert(size > 0);
-	for (; size > 0 && *dst != '\0'; --size, ++dst) {}
-	assert(size > 0);
-	while (--size > 0 && *src != '\0') *dst++ = *src++;
-	*dst = '\0';
-}
-
-static char *strecpy(char *dst, const char *src)
-{
-	while ( (*dst++ = *src++) != 0) {}
-	return dst - 1;
-}
 
 void *ReadFileToMem(const char *filename, size_t *lenp, size_t maxsize)
 {
@@ -318,7 +298,8 @@
 		"  -f                  = Fork into the background (dedicated only)\n"
 		#endif
 		"  -i                  = Force to use the DOS palette (use this if you see a lot of pink)\n"
-		"  -p #player          = Player as #player (deprecated) (network only)\n"
+		"  -p #player          = Player as #player (deprecated) (network only)\n",
+		lastof(buf)
 	);
 
 	for(i=0; i!=lengthof(_driver_classes); i++,dc++) {
--- a/unix.c	Sun Feb 06 11:23:41 2005 +0000
+++ b/unix.c	Sun Feb 06 13:41:02 2005 +0000
@@ -1,5 +1,6 @@
 #include "stdafx.h"
 #include "ttd.h"
+#include "string.h"
 #include "table/strings.h"
 #include "hal.h"
 
--- a/w32dm.c	Sun Feb 06 11:23:41 2005 +0000
+++ b/w32dm.c	Sun Feb 06 13:41:02 2005 +0000
@@ -29,6 +29,7 @@
 #ifdef WIN32_ENABLE_DIRECTMUSIC_SUPPORT
 
 #include "ttd.h"
+#include "string.h"
 #include "sound.h"
 #include "hal.h"
 
--- a/win32.c	Sun Feb 06 11:23:41 2005 +0000
+++ b/win32.c	Sun Feb 06 13:41:02 2005 +0000
@@ -1,6 +1,7 @@
 #include "stdafx.h"
 #include "ttd.h"
 #include "debug.h"
+#include "string.h"
 #include "table/strings.h"
 #include "gfx.h"
 #include "sound.h"