diff options
author | tron <tron@openttd.org> | 2005-02-06 13:41:02 +0000 |
---|---|---|
committer | tron <tron@openttd.org> | 2005-02-06 13:41:02 +0000 |
commit | 59fa465dce1b9a09514ae2d6255a611ec2fea9d9 (patch) | |
tree | 0aab391e034d9f14ce3c721ca9f25bfc6395c9fc | |
parent | bcf4b3b913158c077ba505f5617daa55769416a9 (diff) | |
download | openttd-59fa465dce1b9a09514ae2d6255a611ec2fea9d9.tar.xz |
(svn r1821) Move generic string handling functions to string.[ch] and introduce stre{cpy,cat}, see string.h for their semantics
-rw-r--r-- | Makefile | 1 | ||||
-rw-r--r-- | console_cmds.c | 1 | ||||
-rw-r--r-- | functions.h | 3 | ||||
-rw-r--r-- | misc_cmd.c | 1 | ||||
-rw-r--r-- | network.c | 1 | ||||
-rw-r--r-- | network_client.c | 1 | ||||
-rw-r--r-- | network_gui.c | 1 | ||||
-rw-r--r-- | network_server.c | 1 | ||||
-rw-r--r-- | network_udp.c | 1 | ||||
-rw-r--r-- | os2.c | 1 | ||||
-rw-r--r-- | players.c | 1 | ||||
-rw-r--r-- | settings.c | 1 | ||||
-rw-r--r-- | settings_gui.c | 1 | ||||
-rw-r--r-- | stdafx.h | 1 | ||||
-rw-r--r-- | string.c | 39 | ||||
-rw-r--r-- | string.h | 24 | ||||
-rw-r--r-- | strings.c | 1 | ||||
-rw-r--r-- | ttd.c | 25 | ||||
-rw-r--r-- | unix.c | 1 | ||||
-rw-r--r-- | w32dm.c | 1 | ||||
-rw-r--r-- | win32.c | 1 |
21 files changed, 83 insertions, 25 deletions
@@ -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" @@ -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 @@ -1,5 +1,6 @@ #include "stdafx.h" #include "ttd.h" +#include "string.h" #include "table/strings.h" #include "hal.h" @@ -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" @@ -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 @@ -1,5 +1,6 @@ #include "stdafx.h" #include "ttd.h" +#include "string.h" #include "strings.h" #include "table/strings.h" #include "namegen.h" @@ -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++) { @@ -1,5 +1,6 @@ #include "stdafx.h" #include "ttd.h" +#include "string.h" #include "table/strings.h" #include "hal.h" @@ -29,6 +29,7 @@ #ifdef WIN32_ENABLE_DIRECTMUSIC_SUPPORT #include "ttd.h" +#include "string.h" #include "sound.h" #include "hal.h" @@ -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" |