From b2ae5b197ef3c9753e044f98d7973829d4848dba Mon Sep 17 00:00:00 2001 From: tron Date: Sun, 6 Feb 2005 13:41:02 +0000 Subject: (svn r1821) Move generic string handling functions to string.[ch] and introduce stre{cpy,cat}, see string.h for their semantics --- Makefile | 1 + console_cmds.c | 1 + functions.h | 3 --- misc_cmd.c | 1 + network.c | 1 + network_client.c | 1 + network_gui.c | 1 + network_server.c | 1 + network_udp.c | 1 + os2.c | 1 + players.c | 1 + settings.c | 1 + settings_gui.c | 1 + stdafx.h | 1 + string.c | 39 +++++++++++++++++++++++++++++++++++++++ string.h | 24 ++++++++++++++++++++++++ strings.c | 1 + ttd.c | 25 +++---------------------- unix.c | 1 + w32dm.c | 1 + win32.c | 1 + 21 files changed, 83 insertions(+), 25 deletions(-) create mode 100644 string.c create mode 100644 string.h diff --git a/Makefile b/Makefile index 2ae440659..69a1af572 100644 --- a/Makefile +++ b/Makefile @@ -624,6 +624,7 @@ C_SOURCES += sprite.c 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 diff --git a/console_cmds.c b/console_cmds.c index e73896b21..8cb03151a 100644 --- a/console_cmds.c +++ b/console_cmds.c @@ -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" diff --git a/functions.h b/functions.h index 04d255f29..164c5f634 100644 --- a/functions.h +++ b/functions.h @@ -249,9 +249,6 @@ enum { }; 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); diff --git a/misc_cmd.c b/misc_cmd.c index 67d4d958f..2c83d1f70 100644 --- a/misc_cmd.c +++ b/misc_cmd.c @@ -1,6 +1,7 @@ #include "stdafx.h" #include "ttd.h" +#include "string.h" #include "table/strings.h" #include "command.h" #include "player.h" diff --git a/network.c b/network.c index 2a72de771..48a46a3e0 100644 --- a/network.c +++ b/network.c @@ -1,5 +1,6 @@ #include "stdafx.h" #include "debug.h" +#include "string.h" #include "strings.h" #include "map.h" #include "network_data.h" diff --git a/network_client.c b/network_client.c index 6a42b75d0..d2aa72e72 100644 --- a/network_client.c +++ b/network_client.c @@ -1,5 +1,6 @@ #include "stdafx.h" #include "debug.h" +#include "string.h" #include "strings.h" #include "network_data.h" diff --git a/network_gui.c b/network_gui.c index 95b4707e0..afa515b44 100644 --- a/network_gui.c +++ b/network_gui.c @@ -1,5 +1,6 @@ #include "stdafx.h" #include "ttd.h" +#include "string.h" #include "strings.h" #include "network.h" #include "saveload.h" diff --git a/network_server.c b/network_server.c index 594f7856a..1d5393d41 100644 --- a/network_server.c +++ b/network_server.c @@ -1,5 +1,6 @@ #include "stdafx.h" #include "debug.h" +#include "string.h" #include "strings.h" #include "network_data.h" diff --git a/network_udp.c b/network_udp.c index aa0af4521..4fd033d41 100644 --- a/network_udp.c +++ b/network_udp.c @@ -1,5 +1,6 @@ #include "stdafx.h" #include "debug.h" +#include "string.h" #include "network_data.h" #ifdef ENABLE_NETWORK diff --git a/os2.c b/os2.c index 858213866..e6a1b86d1 100644 --- a/os2.c +++ b/os2.c @@ -1,5 +1,6 @@ #include "stdafx.h" #include "ttd.h" +#include "string.h" #include "table/strings.h" #include "hal.h" diff --git a/players.c b/players.c index a48b4ac7c..040f52a02 100644 --- a/players.c +++ b/players.c @@ -1,5 +1,6 @@ #include "stdafx.h" #include "ttd.h" +#include "string.h" #include "strings.h" #include "table/strings.h" #include "map.h" diff --git a/settings.c b/settings.c index 61847f464..99d254c30 100644 --- a/settings.c +++ b/settings.c @@ -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" diff --git a/settings_gui.c b/settings_gui.c index e1e0512a6..73690d579 100644 --- a/settings_gui.c +++ b/settings_gui.c @@ -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" diff --git a/stdafx.h b/stdafx.h index 8fbc51b1d..982c71cf4 100644 --- a/stdafx.h +++ b/stdafx.h @@ -214,6 +214,7 @@ assert_compile(sizeof(uint8) == 1); #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 diff --git a/string.c b/string.c new file mode 100644 index 000000000..5022e7524 --- /dev/null +++ b/string.c @@ -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; +} diff --git a/string.h b/string.h new file mode 100644 index 000000000..a868a8774 --- /dev/null +++ b/string.h @@ -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 diff --git a/strings.c b/strings.c index e5e462f1a..c30ef1a84 100644 --- a/strings.c +++ b/strings.c @@ -1,5 +1,6 @@ #include "stdafx.h" #include "ttd.h" +#include "string.h" #include "strings.h" #include "table/strings.h" #include "namegen.h" diff --git a/ttd.c b/ttd.c index f4d98a5b1..86886c956 100644 --- a/ttd.c +++ b/ttd.c @@ -1,4 +1,5 @@ #include "stdafx.h" +#include "string.h" #include "table/strings.h" #include "debug.h" #include "strings.h" @@ -197,27 +198,6 @@ static const DriverDesc *ChooseDefaultDriver(const DriverDesc *dd) 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 @@ static void showhelp(void) " -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++) { diff --git a/unix.c b/unix.c index ab2c8b948..096bca1e1 100644 --- a/unix.c +++ b/unix.c @@ -1,5 +1,6 @@ #include "stdafx.h" #include "ttd.h" +#include "string.h" #include "table/strings.h" #include "hal.h" diff --git a/w32dm.c b/w32dm.c index 89617c2c6..9bd1ce3f5 100644 --- a/w32dm.c +++ b/w32dm.c @@ -29,6 +29,7 @@ #ifdef WIN32_ENABLE_DIRECTMUSIC_SUPPORT #include "ttd.h" +#include "string.h" #include "sound.h" #include "hal.h" diff --git a/win32.c b/win32.c index 8066575bd..6b1a2e2e1 100644 --- a/win32.c +++ b/win32.c @@ -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" -- cgit v1.2.3-70-g09d2