string.c
author celestar
Wed, 05 Apr 2006 19:08:09 +0000
changeset 3455 fc1fb215e35b
parent 2775 a18db0ab5e51
child 4200 e6ba94085b81
permissions -rw-r--r--
(svn r4290) -Fix: fixed power computation for mixed consists on level crossings (michi_cc)
2186
db48cf29b983 (svn r2701) Insert Id tags into all source files
tron
parents: 1317
diff changeset
     1
/* $Id$ */
db48cf29b983 (svn r2701) Insert Id tags into all source files
tron
parents: 1317
diff changeset
     2
1317
3c90086ff34f (svn r1821) Move generic string handling functions to string.[ch] and introduce stre{cpy,cat}, see string.h for their semantics
tron
parents:
diff changeset
     3
#include "stdafx.h"
3c90086ff34f (svn r1821) Move generic string handling functions to string.[ch] and introduce stre{cpy,cat}, see string.h for their semantics
tron
parents:
diff changeset
     4
#include "string.h"
3c90086ff34f (svn r1821) Move generic string handling functions to string.[ch] and introduce stre{cpy,cat}, see string.h for their semantics
tron
parents:
diff changeset
     5
2234
8a9b4c6d9c2a (svn r2754) Move str_fmt into string.[ch]
tron
parents: 2186
diff changeset
     6
#include <stdarg.h>
8a9b4c6d9c2a (svn r2754) Move str_fmt into string.[ch]
tron
parents: 2186
diff changeset
     7
1317
3c90086ff34f (svn r1821) Move generic string handling functions to string.[ch] and introduce stre{cpy,cat}, see string.h for their semantics
tron
parents:
diff changeset
     8
void ttd_strlcat(char *dst, const char *src, size_t size)
3c90086ff34f (svn r1821) Move generic string handling functions to string.[ch] and introduce stre{cpy,cat}, see string.h for their semantics
tron
parents:
diff changeset
     9
{
3c90086ff34f (svn r1821) Move generic string handling functions to string.[ch] and introduce stre{cpy,cat}, see string.h for their semantics
tron
parents:
diff changeset
    10
	assert(size > 0);
3c90086ff34f (svn r1821) Move generic string handling functions to string.[ch] and introduce stre{cpy,cat}, see string.h for their semantics
tron
parents:
diff changeset
    11
	for (; size > 0 && *dst != '\0'; --size, ++dst) {}
3c90086ff34f (svn r1821) Move generic string handling functions to string.[ch] and introduce stre{cpy,cat}, see string.h for their semantics
tron
parents:
diff changeset
    12
	assert(size > 0);
3c90086ff34f (svn r1821) Move generic string handling functions to string.[ch] and introduce stre{cpy,cat}, see string.h for their semantics
tron
parents:
diff changeset
    13
	while (--size > 0 && *src != '\0') *dst++ = *src++;
3c90086ff34f (svn r1821) Move generic string handling functions to string.[ch] and introduce stre{cpy,cat}, see string.h for their semantics
tron
parents:
diff changeset
    14
	*dst = '\0';
3c90086ff34f (svn r1821) Move generic string handling functions to string.[ch] and introduce stre{cpy,cat}, see string.h for their semantics
tron
parents:
diff changeset
    15
}
3c90086ff34f (svn r1821) Move generic string handling functions to string.[ch] and introduce stre{cpy,cat}, see string.h for their semantics
tron
parents:
diff changeset
    16
3c90086ff34f (svn r1821) Move generic string handling functions to string.[ch] and introduce stre{cpy,cat}, see string.h for their semantics
tron
parents:
diff changeset
    17
3c90086ff34f (svn r1821) Move generic string handling functions to string.[ch] and introduce stre{cpy,cat}, see string.h for their semantics
tron
parents:
diff changeset
    18
void ttd_strlcpy(char *dst, const char *src, size_t size)
3c90086ff34f (svn r1821) Move generic string handling functions to string.[ch] and introduce stre{cpy,cat}, see string.h for their semantics
tron
parents:
diff changeset
    19
{
3c90086ff34f (svn r1821) Move generic string handling functions to string.[ch] and introduce stre{cpy,cat}, see string.h for their semantics
tron
parents:
diff changeset
    20
	assert(size > 0);
3c90086ff34f (svn r1821) Move generic string handling functions to string.[ch] and introduce stre{cpy,cat}, see string.h for their semantics
tron
parents:
diff changeset
    21
	while (--size > 0 && *src != '\0') *dst++ = *src++;
3c90086ff34f (svn r1821) Move generic string handling functions to string.[ch] and introduce stre{cpy,cat}, see string.h for their semantics
tron
parents:
diff changeset
    22
	*dst = '\0';
3c90086ff34f (svn r1821) Move generic string handling functions to string.[ch] and introduce stre{cpy,cat}, see string.h for their semantics
tron
parents:
diff changeset
    23
}
3c90086ff34f (svn r1821) Move generic string handling functions to string.[ch] and introduce stre{cpy,cat}, see string.h for their semantics
tron
parents:
diff changeset
    24
3c90086ff34f (svn r1821) Move generic string handling functions to string.[ch] and introduce stre{cpy,cat}, see string.h for their semantics
tron
parents:
diff changeset
    25
3c90086ff34f (svn r1821) Move generic string handling functions to string.[ch] and introduce stre{cpy,cat}, see string.h for their semantics
tron
parents:
diff changeset
    26
char* strecat(char* dst, const char* src, const char* last)
3c90086ff34f (svn r1821) Move generic string handling functions to string.[ch] and introduce stre{cpy,cat}, see string.h for their semantics
tron
parents:
diff changeset
    27
{
3c90086ff34f (svn r1821) Move generic string handling functions to string.[ch] and introduce stre{cpy,cat}, see string.h for their semantics
tron
parents:
diff changeset
    28
	assert(last == NULL || dst <= last);
3c90086ff34f (svn r1821) Move generic string handling functions to string.[ch] and introduce stre{cpy,cat}, see string.h for their semantics
tron
parents:
diff changeset
    29
	for (; *dst != '\0'; ++dst)
3c90086ff34f (svn r1821) Move generic string handling functions to string.[ch] and introduce stre{cpy,cat}, see string.h for their semantics
tron
parents:
diff changeset
    30
		if (dst == last) return dst;
3c90086ff34f (svn r1821) Move generic string handling functions to string.[ch] and introduce stre{cpy,cat}, see string.h for their semantics
tron
parents:
diff changeset
    31
	for (; *src != '\0' && dst != last; ++dst, ++src) *dst = *src;
3c90086ff34f (svn r1821) Move generic string handling functions to string.[ch] and introduce stre{cpy,cat}, see string.h for their semantics
tron
parents:
diff changeset
    32
	*dst = '\0';
3c90086ff34f (svn r1821) Move generic string handling functions to string.[ch] and introduce stre{cpy,cat}, see string.h for their semantics
tron
parents:
diff changeset
    33
	return strecpy(dst, src, last);
3c90086ff34f (svn r1821) Move generic string handling functions to string.[ch] and introduce stre{cpy,cat}, see string.h for their semantics
tron
parents:
diff changeset
    34
}
3c90086ff34f (svn r1821) Move generic string handling functions to string.[ch] and introduce stre{cpy,cat}, see string.h for their semantics
tron
parents:
diff changeset
    35
3c90086ff34f (svn r1821) Move generic string handling functions to string.[ch] and introduce stre{cpy,cat}, see string.h for their semantics
tron
parents:
diff changeset
    36
3c90086ff34f (svn r1821) Move generic string handling functions to string.[ch] and introduce stre{cpy,cat}, see string.h for their semantics
tron
parents:
diff changeset
    37
char* strecpy(char* dst, const char* src, const char* last)
3c90086ff34f (svn r1821) Move generic string handling functions to string.[ch] and introduce stre{cpy,cat}, see string.h for their semantics
tron
parents:
diff changeset
    38
{
3c90086ff34f (svn r1821) Move generic string handling functions to string.[ch] and introduce stre{cpy,cat}, see string.h for their semantics
tron
parents:
diff changeset
    39
	assert(last == NULL || dst <= last);
3c90086ff34f (svn r1821) Move generic string handling functions to string.[ch] and introduce stre{cpy,cat}, see string.h for their semantics
tron
parents:
diff changeset
    40
	for (; *src != '\0' && dst != last; ++dst, ++src) *dst = *src;
3c90086ff34f (svn r1821) Move generic string handling functions to string.[ch] and introduce stre{cpy,cat}, see string.h for their semantics
tron
parents:
diff changeset
    41
	*dst = '\0';
3c90086ff34f (svn r1821) Move generic string handling functions to string.[ch] and introduce stre{cpy,cat}, see string.h for their semantics
tron
parents:
diff changeset
    42
	return dst;
3c90086ff34f (svn r1821) Move generic string handling functions to string.[ch] and introduce stre{cpy,cat}, see string.h for their semantics
tron
parents:
diff changeset
    43
}
2234
8a9b4c6d9c2a (svn r2754) Move str_fmt into string.[ch]
tron
parents: 2186
diff changeset
    44
8a9b4c6d9c2a (svn r2754) Move str_fmt into string.[ch]
tron
parents: 2186
diff changeset
    45
8a9b4c6d9c2a (svn r2754) Move str_fmt into string.[ch]
tron
parents: 2186
diff changeset
    46
char* CDECL str_fmt(const char* str, ...)
8a9b4c6d9c2a (svn r2754) Move str_fmt into string.[ch]
tron
parents: 2186
diff changeset
    47
{
8a9b4c6d9c2a (svn r2754) Move str_fmt into string.[ch]
tron
parents: 2186
diff changeset
    48
	char buf[4096];
8a9b4c6d9c2a (svn r2754) Move str_fmt into string.[ch]
tron
parents: 2186
diff changeset
    49
	va_list va;
8a9b4c6d9c2a (svn r2754) Move str_fmt into string.[ch]
tron
parents: 2186
diff changeset
    50
	int len;
8a9b4c6d9c2a (svn r2754) Move str_fmt into string.[ch]
tron
parents: 2186
diff changeset
    51
	char* p;
8a9b4c6d9c2a (svn r2754) Move str_fmt into string.[ch]
tron
parents: 2186
diff changeset
    52
8a9b4c6d9c2a (svn r2754) Move str_fmt into string.[ch]
tron
parents: 2186
diff changeset
    53
	va_start(va, str);
8a9b4c6d9c2a (svn r2754) Move str_fmt into string.[ch]
tron
parents: 2186
diff changeset
    54
	len = vsprintf(buf, str, va);
8a9b4c6d9c2a (svn r2754) Move str_fmt into string.[ch]
tron
parents: 2186
diff changeset
    55
	va_end(va);
8a9b4c6d9c2a (svn r2754) Move str_fmt into string.[ch]
tron
parents: 2186
diff changeset
    56
	p = malloc(len + 1);
8a9b4c6d9c2a (svn r2754) Move str_fmt into string.[ch]
tron
parents: 2186
diff changeset
    57
	if (p != NULL) memcpy(p, buf, len + 1);
8a9b4c6d9c2a (svn r2754) Move str_fmt into string.[ch]
tron
parents: 2186
diff changeset
    58
	return p;
8a9b4c6d9c2a (svn r2754) Move str_fmt into string.[ch]
tron
parents: 2186
diff changeset
    59
}
2775
a18db0ab5e51 (svn r3322) - Fix: Network window crash when it receives invalid information for example from the integrated nightly, so validate the network-input when it is received
Darkvater
parents: 2234
diff changeset
    60
a18db0ab5e51 (svn r3322) - Fix: Network window crash when it receives invalid information for example from the integrated nightly, so validate the network-input when it is received
Darkvater
parents: 2234
diff changeset
    61
void str_validate(char *str)
a18db0ab5e51 (svn r3322) - Fix: Network window crash when it receives invalid information for example from the integrated nightly, so validate the network-input when it is received
Darkvater
parents: 2234
diff changeset
    62
{
a18db0ab5e51 (svn r3322) - Fix: Network window crash when it receives invalid information for example from the integrated nightly, so validate the network-input when it is received
Darkvater
parents: 2234
diff changeset
    63
	for (; *str != '\0'; str++)
a18db0ab5e51 (svn r3322) - Fix: Network window crash when it receives invalid information for example from the integrated nightly, so validate the network-input when it is received
Darkvater
parents: 2234
diff changeset
    64
		if (!IsValidAsciiChar(*str)) *str = '?';
a18db0ab5e51 (svn r3322) - Fix: Network window crash when it receives invalid information for example from the integrated nightly, so validate the network-input when it is received
Darkvater
parents: 2234
diff changeset
    65
}