src/string.cpp
author skidd13
Wed, 29 Oct 2008 16:30:41 +0000
changeset 10303 d3ec9842c33c
parent 10300 e336f1784ba4
child 10312 3c14d0ea0ef5
permissions -rw-r--r--
(svn r14546) -Codechange: Unify string(cpy|cat) functions
-Doc: string(cpy|cat) functions
5165
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
     1
/* $Id$ */
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
     2
9111
48ce04029fe4 (svn r12971) -Documentation: add @file in files that missed them and add something more than whitespace as description of files that don't have a description.
rubidium
parents: 8264
diff changeset
     3
/** @file string.cpp Handling of C-type strings (char*). */
6420
456c275f3313 (svn r9556) -Documentation: doxygen and comment-style changes. 'R', 'S'.. The end of the preliminary work is near
belugas
parents: 6274
diff changeset
     4
5165
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
     5
#include "stdafx.h"
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
     6
#include "openttd.h"
7257
a0c906e11895 (svn r10566) -Fix [FS#1025]: a NewGRF could have a information message that is too long for the internal buffers to handle. We should not crash on such a case even though the message is too big for the window anyway.
rubidium
parents: 6420
diff changeset
     7
#include "debug.h"
8130
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents: 8113
diff changeset
     8
#include "core/alloc_func.hpp"
10299
946c84fdc58e (svn r14540) -Codechange: introduce [v]seprintf which are like [v]snprintf but do return the number of characters written instead of the number of characters that would be written; as size_t is unsigned substraction can cause integer underflows quite quickly.
rubidium
parents: 9997
diff changeset
     9
#include "core/math_func.hpp"
8214
971f861d5543 (svn r11777) -Codechange: split the string header and make do not include it when it's not necessary.
rubidium
parents: 8181
diff changeset
    10
#include "string_func.h"
5165
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
    11
8264
b1e85998c7d3 (svn r11828) -Codechange: include table/* as the last includes and remove an unneeded include from openttd.h.
rubidium
parents: 8214
diff changeset
    12
#include "table/control_codes.h"
b1e85998c7d3 (svn r11828) -Codechange: include table/* as the last includes and remove an unneeded include from openttd.h.
rubidium
parents: 8214
diff changeset
    13
5165
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
    14
#include <stdarg.h>
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
    15
#include <ctype.h> // required for tolower()
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
    16
10300
e336f1784ba4 (svn r14541) -Fix (r14540): mingw didn't like it :(
rubidium
parents: 10299
diff changeset
    17
/**
e336f1784ba4 (svn r14541) -Fix (r14540): mingw didn't like it :(
rubidium
parents: 10299
diff changeset
    18
 * Safer implementation of vsnprintf; same as vsnprintf except:
e336f1784ba4 (svn r14541) -Fix (r14540): mingw didn't like it :(
rubidium
parents: 10299
diff changeset
    19
 * - last instead of size, i.e. replace sizeof with lastof.
e336f1784ba4 (svn r14541) -Fix (r14540): mingw didn't like it :(
rubidium
parents: 10299
diff changeset
    20
 * - return gives the amount of characters added, not what it would add.
e336f1784ba4 (svn r14541) -Fix (r14540): mingw didn't like it :(
rubidium
parents: 10299
diff changeset
    21
 * @param str    buffer to write to up to last
e336f1784ba4 (svn r14541) -Fix (r14540): mingw didn't like it :(
rubidium
parents: 10299
diff changeset
    22
 * @param last   last character we may write to
e336f1784ba4 (svn r14541) -Fix (r14540): mingw didn't like it :(
rubidium
parents: 10299
diff changeset
    23
 * @param format the formatting (see snprintf)
e336f1784ba4 (svn r14541) -Fix (r14540): mingw didn't like it :(
rubidium
parents: 10299
diff changeset
    24
 * @param ap     the list of arguments for the format
e336f1784ba4 (svn r14541) -Fix (r14540): mingw didn't like it :(
rubidium
parents: 10299
diff changeset
    25
 * @return the number of added characters
e336f1784ba4 (svn r14541) -Fix (r14540): mingw didn't like it :(
rubidium
parents: 10299
diff changeset
    26
 */
e336f1784ba4 (svn r14541) -Fix (r14540): mingw didn't like it :(
rubidium
parents: 10299
diff changeset
    27
static int CDECL vseprintf(char *str, const char *last, const char *format, va_list ap)
e336f1784ba4 (svn r14541) -Fix (r14540): mingw didn't like it :(
rubidium
parents: 10299
diff changeset
    28
{
e336f1784ba4 (svn r14541) -Fix (r14540): mingw didn't like it :(
rubidium
parents: 10299
diff changeset
    29
	if (str >= last) return 0;
e336f1784ba4 (svn r14541) -Fix (r14540): mingw didn't like it :(
rubidium
parents: 10299
diff changeset
    30
	size_t size = last - str;
e336f1784ba4 (svn r14541) -Fix (r14540): mingw didn't like it :(
rubidium
parents: 10299
diff changeset
    31
	return min((int)size, vsnprintf(str, size, format, ap));
e336f1784ba4 (svn r14541) -Fix (r14540): mingw didn't like it :(
rubidium
parents: 10299
diff changeset
    32
}
e336f1784ba4 (svn r14541) -Fix (r14540): mingw didn't like it :(
rubidium
parents: 10299
diff changeset
    33
5165
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
    34
void ttd_strlcat(char *dst, const char *src, size_t size)
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
    35
{
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
    36
	assert(size > 0);
10303
d3ec9842c33c (svn r14546) -Codechange: Unify string(cpy|cat) functions
skidd13
parents: 10300
diff changeset
    37
	while (size > 0 && *dst != '\0') {
d3ec9842c33c (svn r14546) -Codechange: Unify string(cpy|cat) functions
skidd13
parents: 10300
diff changeset
    38
		size--;
d3ec9842c33c (svn r14546) -Codechange: Unify string(cpy|cat) functions
skidd13
parents: 10300
diff changeset
    39
		dst++;
d3ec9842c33c (svn r14546) -Codechange: Unify string(cpy|cat) functions
skidd13
parents: 10300
diff changeset
    40
	}
d3ec9842c33c (svn r14546) -Codechange: Unify string(cpy|cat) functions
skidd13
parents: 10300
diff changeset
    41
d3ec9842c33c (svn r14546) -Codechange: Unify string(cpy|cat) functions
skidd13
parents: 10300
diff changeset
    42
	ttd_strlcpy(dst, src, size);
5165
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
    43
}
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
    44
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
    45
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
    46
void ttd_strlcpy(char *dst, const char *src, size_t size)
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
    47
{
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
    48
	assert(size > 0);
10303
d3ec9842c33c (svn r14546) -Codechange: Unify string(cpy|cat) functions
skidd13
parents: 10300
diff changeset
    49
	while (--size > 0 && *src != '\0') {
d3ec9842c33c (svn r14546) -Codechange: Unify string(cpy|cat) functions
skidd13
parents: 10300
diff changeset
    50
		*dst++ = *src++;
d3ec9842c33c (svn r14546) -Codechange: Unify string(cpy|cat) functions
skidd13
parents: 10300
diff changeset
    51
	}
5165
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
    52
	*dst = '\0';
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
    53
}
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
    54
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
    55
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
    56
char* strecat(char* dst, const char* src, const char* last)
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
    57
{
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
    58
	assert(dst <= last);
10303
d3ec9842c33c (svn r14546) -Codechange: Unify string(cpy|cat) functions
skidd13
parents: 10300
diff changeset
    59
	while (*dst != '\0') {
5165
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
    60
		if (dst == last) return dst;
10303
d3ec9842c33c (svn r14546) -Codechange: Unify string(cpy|cat) functions
skidd13
parents: 10300
diff changeset
    61
		dst++;
d3ec9842c33c (svn r14546) -Codechange: Unify string(cpy|cat) functions
skidd13
parents: 10300
diff changeset
    62
	}
d3ec9842c33c (svn r14546) -Codechange: Unify string(cpy|cat) functions
skidd13
parents: 10300
diff changeset
    63
5165
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
    64
	return strecpy(dst, src, last);
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
    65
}
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
    66
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
    67
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
    68
char* strecpy(char* dst, const char* src, const char* last)
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
    69
{
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
    70
	assert(dst <= last);
10303
d3ec9842c33c (svn r14546) -Codechange: Unify string(cpy|cat) functions
skidd13
parents: 10300
diff changeset
    71
	while (dst != last && *src != '\0') {
d3ec9842c33c (svn r14546) -Codechange: Unify string(cpy|cat) functions
skidd13
parents: 10300
diff changeset
    72
		*dst++ = *src++;
d3ec9842c33c (svn r14546) -Codechange: Unify string(cpy|cat) functions
skidd13
parents: 10300
diff changeset
    73
	}
5165
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
    74
	*dst = '\0';
10303
d3ec9842c33c (svn r14546) -Codechange: Unify string(cpy|cat) functions
skidd13
parents: 10300
diff changeset
    75
5165
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
    76
	if (dst == last && *src != '\0') {
7257
a0c906e11895 (svn r10566) -Fix [FS#1025]: a NewGRF could have a information message that is too long for the internal buffers to handle. We should not crash on such a case even though the message is too big for the window anyway.
rubidium
parents: 6420
diff changeset
    77
#ifdef STRGEN
5165
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
    78
		error("String too long for destination buffer");
7257
a0c906e11895 (svn r10566) -Fix [FS#1025]: a NewGRF could have a information message that is too long for the internal buffers to handle. We should not crash on such a case even though the message is too big for the window anyway.
rubidium
parents: 6420
diff changeset
    79
#else /* STRGEN */
a0c906e11895 (svn r10566) -Fix [FS#1025]: a NewGRF could have a information message that is too long for the internal buffers to handle. We should not crash on such a case even though the message is too big for the window anyway.
rubidium
parents: 6420
diff changeset
    80
		DEBUG(misc, 0, "String too long for destination buffer");
a0c906e11895 (svn r10566) -Fix [FS#1025]: a NewGRF could have a information message that is too long for the internal buffers to handle. We should not crash on such a case even though the message is too big for the window anyway.
rubidium
parents: 6420
diff changeset
    81
		*dst = '\0';
a0c906e11895 (svn r10566) -Fix [FS#1025]: a NewGRF could have a information message that is too long for the internal buffers to handle. We should not crash on such a case even though the message is too big for the window anyway.
rubidium
parents: 6420
diff changeset
    82
#endif /* STRGEN */
5165
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
    83
	}
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
    84
	return dst;
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
    85
}
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
    86
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
    87
10299
946c84fdc58e (svn r14540) -Codechange: introduce [v]seprintf which are like [v]snprintf but do return the number of characters written instead of the number of characters that would be written; as size_t is unsigned substraction can cause integer underflows quite quickly.
rubidium
parents: 9997
diff changeset
    88
char *CDECL str_fmt(const char *str, ...)
5165
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
    89
{
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
    90
	char buf[4096];
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
    91
	va_list va;
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
    92
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
    93
	va_start(va, str);
10299
946c84fdc58e (svn r14540) -Codechange: introduce [v]seprintf which are like [v]snprintf but do return the number of characters written instead of the number of characters that would be written; as size_t is unsigned substraction can cause integer underflows quite quickly.
rubidium
parents: 9997
diff changeset
    94
	int len = vseprintf(buf, lastof(buf), str, va);
5165
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
    95
	va_end(va);
10299
946c84fdc58e (svn r14540) -Codechange: introduce [v]seprintf which are like [v]snprintf but do return the number of characters written instead of the number of characters that would be written; as size_t is unsigned substraction can cause integer underflows quite quickly.
rubidium
parents: 9997
diff changeset
    96
	char *p = MallocT<char>(len + 1);
946c84fdc58e (svn r14540) -Codechange: introduce [v]seprintf which are like [v]snprintf but do return the number of characters written instead of the number of characters that would be written; as size_t is unsigned substraction can cause integer underflows quite quickly.
rubidium
parents: 9997
diff changeset
    97
	memcpy(p, buf, len + 1);
5165
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
    98
	return p;
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
    99
}
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   100
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   101
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   102
void str_validate(char *str)
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   103
{
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   104
	char *dst = str;
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   105
	WChar c;
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   106
	size_t len;
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   107
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   108
	for (len = Utf8Decode(&c, str); c != '\0'; len = Utf8Decode(&c, str)) {
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   109
		if (IsPrintable(c) && (c < SCC_SPRITE_START || c > SCC_SPRITE_END ||
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   110
			IsValidChar(c - SCC_SPRITE_START, CS_ALPHANUMERAL))) {
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   111
			/* Copy the character back. Even if dst is current the same as str
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   112
			 * (i.e. no characters have been changed) this is quicker than
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   113
			 * moving the pointers ahead by len */
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   114
			do {
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   115
				*dst++ = *str++;
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   116
			} while (--len != 0);
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   117
		} else {
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   118
			/* Replace the undesirable character with a question mark */
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   119
			str += len;
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   120
			*dst++ = '?';
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   121
		}
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   122
	}
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   123
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   124
	*dst = '\0';
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   125
}
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   126
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   127
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   128
void str_strip_colours(char *str)
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   129
{
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   130
	char *dst = str;
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   131
	WChar c;
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   132
	size_t len;
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   133
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   134
	for (len = Utf8Decode(&c, str); c != '\0'; len = Utf8Decode(&c, str)) {
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   135
		if (c < SCC_BLUE || c > SCC_BLACK) {
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   136
			/* Copy the character back. Even if dst is current the same as str
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   137
			 * (i.e. no characters have been changed) this is quicker than
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   138
			 * moving the pointers ahead by len */
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   139
			do {
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   140
				*dst++ = *str++;
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   141
			} while (--len != 0);
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   142
		} else {
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   143
			/* Just skip (strip) the colour codes */
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   144
			str += len;
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   145
		}
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   146
	}
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   147
	*dst = '\0';
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   148
}
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   149
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   150
/** Convert a given ASCII string to lowercase.
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   151
 * NOTE: only support ASCII characters, no UTF8 fancy. As currently
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   152
 * the function is only used to lowercase data-filenames if they are
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   153
 * not found, this is sufficient. If more, or general functionality is
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   154
 * needed, look to r7271 where it was removed because it was broken when
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   155
 * using certain locales: eg in Turkish the uppercase 'I' was converted to
6420
456c275f3313 (svn r9556) -Documentation: doxygen and comment-style changes. 'R', 'S'.. The end of the preliminary work is near
belugas
parents: 6274
diff changeset
   156
 * '?', so just revert to the old functionality
456c275f3313 (svn r9556) -Documentation: doxygen and comment-style changes. 'R', 'S'.. The end of the preliminary work is near
belugas
parents: 6274
diff changeset
   157
 * @param str string to convert */
5165
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   158
void strtolower(char *str)
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   159
{
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   160
	for (; *str != '\0'; str++) *str = tolower(*str);
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   161
}
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   162
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   163
/**
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   164
 * Only allow certain keys. You can define the filter to be used. This makes
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   165
 *  sure no invalid keys can get into an editbox, like BELL.
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   166
 * @param key character to be checked
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   167
 * @param afilter the filter to use
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   168
 * @return true or false depending if the character is printable/valid or not
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   169
 */
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   170
bool IsValidChar(WChar key, CharSetFilter afilter)
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   171
{
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   172
	switch (afilter) {
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   173
		case CS_ALPHANUMERAL: return IsPrintable(key);
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   174
		case CS_NUMERAL:      return (key >= '0' && key <= '9');
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   175
		case CS_ALPHA:        return IsPrintable(key) && !(key >= '0' && key <= '9');
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   176
	}
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   177
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   178
	return false;
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   179
}
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   180
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   181
#ifdef WIN32
8181
9c6a2d52e136 (svn r11744) -Codechange: don't redefine snprintf when using MinGW Runtime 3.14 or superior, as it now have snprintf() and vsnprintf conform to C99
glx
parents: 8131
diff changeset
   182
/* Since version 3.14, MinGW Runtime has snprintf() and vsnprintf() conform to C99 but it's not the case for older versions */
9c6a2d52e136 (svn r11744) -Codechange: don't redefine snprintf when using MinGW Runtime 3.14 or superior, as it now have snprintf() and vsnprintf conform to C99
glx
parents: 8131
diff changeset
   183
#if (__MINGW32_MAJOR_VERSION < 3) || ((__MINGW32_MAJOR_VERSION == 3) && (__MINGW32_MINOR_VERSION < 14))
5165
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   184
int CDECL snprintf(char *str, size_t size, const char *format, ...)
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   185
{
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   186
	va_list ap;
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   187
	int ret;
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   188
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   189
	va_start(ap, format);
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   190
	ret = vsnprintf(str, size, format, ap);
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   191
	va_end(ap);
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   192
	return ret;
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   193
}
8181
9c6a2d52e136 (svn r11744) -Codechange: don't redefine snprintf when using MinGW Runtime 3.14 or superior, as it now have snprintf() and vsnprintf conform to C99
glx
parents: 8131
diff changeset
   194
#endif /* MinGW Runtime < 3.14 */
5165
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   195
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   196
#ifdef _MSC_VER
5630
2bbfa6ea3545 (svn r8089) -[win32] MS-borkedness: All *nprintf functions are broken, but we didn't test to fix it ourselves when 'len = count'.
Darkvater
parents: 5609
diff changeset
   197
/* *nprintf broken, not POSIX compliant, MSDN description
2bbfa6ea3545 (svn r8089) -[win32] MS-borkedness: All *nprintf functions are broken, but we didn't test to fix it ourselves when 'len = count'.
Darkvater
parents: 5609
diff changeset
   198
 * - If len < count, then len characters are stored in buffer, a null-terminator is appended, and len is returned.
2bbfa6ea3545 (svn r8089) -[win32] MS-borkedness: All *nprintf functions are broken, but we didn't test to fix it ourselves when 'len = count'.
Darkvater
parents: 5609
diff changeset
   199
 * - If len = count, then len characters are stored in buffer, no null-terminator is appended, and len is returned.
2bbfa6ea3545 (svn r8089) -[win32] MS-borkedness: All *nprintf functions are broken, but we didn't test to fix it ourselves when 'len = count'.
Darkvater
parents: 5609
diff changeset
   200
 * - If len > count, then count characters are stored in buffer, no null-terminator is appended, and a negative value is returned
2bbfa6ea3545 (svn r8089) -[win32] MS-borkedness: All *nprintf functions are broken, but we didn't test to fix it ourselves when 'len = count'.
Darkvater
parents: 5609
diff changeset
   201
 */
5165
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   202
int CDECL vsnprintf(char *str, size_t size, const char *format, va_list ap)
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   203
{
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   204
	int ret;
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   205
	ret = _vsnprintf(str, size, format, ap);
5631
fd4e111c7ce2 (svn r8090) -[win32] Fix (r8089): for *nprintf 'ret = count' NOT 'ret = 0'...
Darkvater
parents: 5630
diff changeset
   206
	if (ret < 0 || ret == size) str[size - 1] = '\0';
5165
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   207
	return ret;
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   208
}
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   209
#endif /* _MSC_VER */
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   210
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   211
#endif /* WIN32 */
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   212
10299
946c84fdc58e (svn r14540) -Codechange: introduce [v]seprintf which are like [v]snprintf but do return the number of characters written instead of the number of characters that would be written; as size_t is unsigned substraction can cause integer underflows quite quickly.
rubidium
parents: 9997
diff changeset
   213
/**
946c84fdc58e (svn r14540) -Codechange: introduce [v]seprintf which are like [v]snprintf but do return the number of characters written instead of the number of characters that would be written; as size_t is unsigned substraction can cause integer underflows quite quickly.
rubidium
parents: 9997
diff changeset
   214
 * Safer implementation of snprintf; same as snprintf except:
946c84fdc58e (svn r14540) -Codechange: introduce [v]seprintf which are like [v]snprintf but do return the number of characters written instead of the number of characters that would be written; as size_t is unsigned substraction can cause integer underflows quite quickly.
rubidium
parents: 9997
diff changeset
   215
 * - last instead of size, i.e. replace sizeof with lastof.
946c84fdc58e (svn r14540) -Codechange: introduce [v]seprintf which are like [v]snprintf but do return the number of characters written instead of the number of characters that would be written; as size_t is unsigned substraction can cause integer underflows quite quickly.
rubidium
parents: 9997
diff changeset
   216
 * - return gives the amount of characters added, not what it would add.
946c84fdc58e (svn r14540) -Codechange: introduce [v]seprintf which are like [v]snprintf but do return the number of characters written instead of the number of characters that would be written; as size_t is unsigned substraction can cause integer underflows quite quickly.
rubidium
parents: 9997
diff changeset
   217
 * @param str    buffer to write to up to last
946c84fdc58e (svn r14540) -Codechange: introduce [v]seprintf which are like [v]snprintf but do return the number of characters written instead of the number of characters that would be written; as size_t is unsigned substraction can cause integer underflows quite quickly.
rubidium
parents: 9997
diff changeset
   218
 * @param last   last character we may write to
946c84fdc58e (svn r14540) -Codechange: introduce [v]seprintf which are like [v]snprintf but do return the number of characters written instead of the number of characters that would be written; as size_t is unsigned substraction can cause integer underflows quite quickly.
rubidium
parents: 9997
diff changeset
   219
 * @param format the formatting (see snprintf)
946c84fdc58e (svn r14540) -Codechange: introduce [v]seprintf which are like [v]snprintf but do return the number of characters written instead of the number of characters that would be written; as size_t is unsigned substraction can cause integer underflows quite quickly.
rubidium
parents: 9997
diff changeset
   220
 * @return the number of added characters
946c84fdc58e (svn r14540) -Codechange: introduce [v]seprintf which are like [v]snprintf but do return the number of characters written instead of the number of characters that would be written; as size_t is unsigned substraction can cause integer underflows quite quickly.
rubidium
parents: 9997
diff changeset
   221
 */
946c84fdc58e (svn r14540) -Codechange: introduce [v]seprintf which are like [v]snprintf but do return the number of characters written instead of the number of characters that would be written; as size_t is unsigned substraction can cause integer underflows quite quickly.
rubidium
parents: 9997
diff changeset
   222
int CDECL seprintf(char *str, const char *last, const char *format, ...)
946c84fdc58e (svn r14540) -Codechange: introduce [v]seprintf which are like [v]snprintf but do return the number of characters written instead of the number of characters that would be written; as size_t is unsigned substraction can cause integer underflows quite quickly.
rubidium
parents: 9997
diff changeset
   223
{
946c84fdc58e (svn r14540) -Codechange: introduce [v]seprintf which are like [v]snprintf but do return the number of characters written instead of the number of characters that would be written; as size_t is unsigned substraction can cause integer underflows quite quickly.
rubidium
parents: 9997
diff changeset
   224
	va_list ap;
946c84fdc58e (svn r14540) -Codechange: introduce [v]seprintf which are like [v]snprintf but do return the number of characters written instead of the number of characters that would be written; as size_t is unsigned substraction can cause integer underflows quite quickly.
rubidium
parents: 9997
diff changeset
   225
946c84fdc58e (svn r14540) -Codechange: introduce [v]seprintf which are like [v]snprintf but do return the number of characters written instead of the number of characters that would be written; as size_t is unsigned substraction can cause integer underflows quite quickly.
rubidium
parents: 9997
diff changeset
   226
	va_start(ap, format);
946c84fdc58e (svn r14540) -Codechange: introduce [v]seprintf which are like [v]snprintf but do return the number of characters written instead of the number of characters that would be written; as size_t is unsigned substraction can cause integer underflows quite quickly.
rubidium
parents: 9997
diff changeset
   227
	int ret = vseprintf(str, last, format, ap);
946c84fdc58e (svn r14540) -Codechange: introduce [v]seprintf which are like [v]snprintf but do return the number of characters written instead of the number of characters that would be written; as size_t is unsigned substraction can cause integer underflows quite quickly.
rubidium
parents: 9997
diff changeset
   228
	va_end(ap);
946c84fdc58e (svn r14540) -Codechange: introduce [v]seprintf which are like [v]snprintf but do return the number of characters written instead of the number of characters that would be written; as size_t is unsigned substraction can cause integer underflows quite quickly.
rubidium
parents: 9997
diff changeset
   229
	return ret;
946c84fdc58e (svn r14540) -Codechange: introduce [v]seprintf which are like [v]snprintf but do return the number of characters written instead of the number of characters that would be written; as size_t is unsigned substraction can cause integer underflows quite quickly.
rubidium
parents: 9997
diff changeset
   230
}
946c84fdc58e (svn r14540) -Codechange: introduce [v]seprintf which are like [v]snprintf but do return the number of characters written instead of the number of characters that would be written; as size_t is unsigned substraction can cause integer underflows quite quickly.
rubidium
parents: 9997
diff changeset
   231
5165
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   232
5634
ba6b9ebe197a (svn r8093) -Codechange: Add a function to get a string representation of an MD5SUM and use it.
Darkvater
parents: 5631
diff changeset
   233
/** Convert the md5sum to a hexadecimal string representation
ba6b9ebe197a (svn r8093) -Codechange: Add a function to get a string representation of an MD5SUM and use it.
Darkvater
parents: 5631
diff changeset
   234
 * @param buf buffer to put the md5sum into
ba6b9ebe197a (svn r8093) -Codechange: Add a function to get a string representation of an MD5SUM and use it.
Darkvater
parents: 5631
diff changeset
   235
 * @param last last character of buffer (usually lastof(buf))
ba6b9ebe197a (svn r8093) -Codechange: Add a function to get a string representation of an MD5SUM and use it.
Darkvater
parents: 5631
diff changeset
   236
 * @param md5sum the md5sum itself
ba6b9ebe197a (svn r8093) -Codechange: Add a function to get a string representation of an MD5SUM and use it.
Darkvater
parents: 5631
diff changeset
   237
 * @return a pointer to the next character after the md5sum */
ba6b9ebe197a (svn r8093) -Codechange: Add a function to get a string representation of an MD5SUM and use it.
Darkvater
parents: 5631
diff changeset
   238
char *md5sumToString(char *buf, const char *last, const uint8 md5sum[16])
ba6b9ebe197a (svn r8093) -Codechange: Add a function to get a string representation of an MD5SUM and use it.
Darkvater
parents: 5631
diff changeset
   239
{
ba6b9ebe197a (svn r8093) -Codechange: Add a function to get a string representation of an MD5SUM and use it.
Darkvater
parents: 5631
diff changeset
   240
	char *p = buf;
ba6b9ebe197a (svn r8093) -Codechange: Add a function to get a string representation of an MD5SUM and use it.
Darkvater
parents: 5631
diff changeset
   241
ba6b9ebe197a (svn r8093) -Codechange: Add a function to get a string representation of an MD5SUM and use it.
Darkvater
parents: 5631
diff changeset
   242
	for (uint i = 0; i < 16; i++) {
10299
946c84fdc58e (svn r14540) -Codechange: introduce [v]seprintf which are like [v]snprintf but do return the number of characters written instead of the number of characters that would be written; as size_t is unsigned substraction can cause integer underflows quite quickly.
rubidium
parents: 9997
diff changeset
   243
		p += seprintf(p, last, "%02X", md5sum[i]);
5634
ba6b9ebe197a (svn r8093) -Codechange: Add a function to get a string representation of an MD5SUM and use it.
Darkvater
parents: 5631
diff changeset
   244
	}
ba6b9ebe197a (svn r8093) -Codechange: Add a function to get a string representation of an MD5SUM and use it.
Darkvater
parents: 5631
diff changeset
   245
ba6b9ebe197a (svn r8093) -Codechange: Add a function to get a string representation of an MD5SUM and use it.
Darkvater
parents: 5631
diff changeset
   246
	return p;
ba6b9ebe197a (svn r8093) -Codechange: Add a function to get a string representation of an MD5SUM and use it.
Darkvater
parents: 5631
diff changeset
   247
}
ba6b9ebe197a (svn r8093) -Codechange: Add a function to get a string representation of an MD5SUM and use it.
Darkvater
parents: 5631
diff changeset
   248
ba6b9ebe197a (svn r8093) -Codechange: Add a function to get a string representation of an MD5SUM and use it.
Darkvater
parents: 5631
diff changeset
   249
5165
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   250
/* UTF-8 handling routines */
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   251
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   252
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   253
/* Decode and consume the next UTF-8 encoded character
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   254
 * @param c Buffer to place decoded character.
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   255
 * @param s Character stream to retrieve character from.
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   256
 * @return Number of characters in the sequence.
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   257
 */
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   258
size_t Utf8Decode(WChar *c, const char *s)
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   259
{
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   260
	assert(c != NULL);
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   261
7928
63e18de69e50 (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 7257
diff changeset
   262
	if (!HasBit(s[0], 7)) {
5165
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   263
		/* Single byte character: 0xxxxxxx */
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   264
		*c = s[0];
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   265
		return 1;
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   266
	} else if (GB(s[0], 5, 3) == 6) {
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   267
		if (IsUtf8Part(s[1])) {
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   268
			/* Double byte character: 110xxxxx 10xxxxxx */
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   269
			*c = GB(s[0], 0, 5) << 6 | GB(s[1], 0, 6);
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   270
			if (*c >= 0x80) return 2;
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   271
		}
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   272
	} else if (GB(s[0], 4, 4) == 14) {
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   273
		if (IsUtf8Part(s[1]) && IsUtf8Part(s[2])) {
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   274
			/* Triple byte character: 1110xxxx 10xxxxxx 10xxxxxx */
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   275
			*c = GB(s[0], 0, 4) << 12 | GB(s[1], 0, 6) << 6 | GB(s[2], 0, 6);
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   276
			if (*c >= 0x800) return 3;
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   277
		}
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   278
	} else if (GB(s[0], 3, 5) == 30) {
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   279
		if (IsUtf8Part(s[1]) && IsUtf8Part(s[2]) && IsUtf8Part(s[3])) {
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   280
			/* 4 byte character: 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx */
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   281
			*c = GB(s[0], 0, 3) << 18 | GB(s[1], 0, 6) << 12 | GB(s[2], 0, 6) << 6 | GB(s[3], 0, 6);
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   282
			if (*c >= 0x10000 && *c <= 0x10FFFF) return 4;
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   283
		}
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   284
	}
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   285
5380
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5165
diff changeset
   286
	//DEBUG(misc, 1, "[utf8] invalid UTF-8 sequence");
5165
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   287
	*c = '?';
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   288
	return 1;
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   289
}
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   290
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   291
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   292
/* Encode a unicode character and place it in the buffer
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   293
 * @param buf Buffer to place character.
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   294
 * @param c   Unicode character to encode.
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   295
 * @return Number of characters in the encoded sequence.
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   296
 */
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   297
size_t Utf8Encode(char *buf, WChar c)
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   298
{
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   299
	if (c < 0x80) {
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   300
		*buf = c;
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   301
		return 1;
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   302
	} else if (c < 0x800) {
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   303
		*buf++ = 0xC0 + GB(c,  6, 5);
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   304
		*buf   = 0x80 + GB(c,  0, 6);
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   305
		return 2;
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   306
	} else if (c < 0x10000) {
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   307
		*buf++ = 0xE0 + GB(c, 12, 4);
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   308
		*buf++ = 0x80 + GB(c,  6, 6);
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   309
		*buf   = 0x80 + GB(c,  0, 6);
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   310
		return 3;
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   311
	} else if (c < 0x110000) {
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   312
		*buf++ = 0xF0 + GB(c, 18, 3);
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   313
		*buf++ = 0x80 + GB(c, 12, 6);
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   314
		*buf++ = 0x80 + GB(c,  6, 6);
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   315
		*buf   = 0x80 + GB(c,  0, 6);
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   316
		return 4;
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   317
	}
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   318
5380
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5165
diff changeset
   319
	//DEBUG(misc, 1, "[utf8] can't UTF-8 encode value 0x%X", c);
5165
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   320
	*buf = '?';
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   321
	return 1;
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   322
}
6215
bbd141b026b5 (svn r9012) -Fix/Feature (UTF8): When cutting strings into multiple lines also take into consideration whitespace characters of more than 1 byte length (eg IDEOGRAPHIC SPACE, IsWhitespace() function). When trimming such strings, account for multiple-byte long sequences so use *Utf8PrevChar(v) = '\0'.
Darkvater
parents: 5634
diff changeset
   323
bbd141b026b5 (svn r9012) -Fix/Feature (UTF8): When cutting strings into multiple lines also take into consideration whitespace characters of more than 1 byte length (eg IDEOGRAPHIC SPACE, IsWhitespace() function). When trimming such strings, account for multiple-byte long sequences so use *Utf8PrevChar(v) = '\0'.
Darkvater
parents: 5634
diff changeset
   324
/**
bbd141b026b5 (svn r9012) -Fix/Feature (UTF8): When cutting strings into multiple lines also take into consideration whitespace characters of more than 1 byte length (eg IDEOGRAPHIC SPACE, IsWhitespace() function). When trimming such strings, account for multiple-byte long sequences so use *Utf8PrevChar(v) = '\0'.
Darkvater
parents: 5634
diff changeset
   325
 * Properly terminate an UTF8 string to some maximum length
bbd141b026b5 (svn r9012) -Fix/Feature (UTF8): When cutting strings into multiple lines also take into consideration whitespace characters of more than 1 byte length (eg IDEOGRAPHIC SPACE, IsWhitespace() function). When trimming such strings, account for multiple-byte long sequences so use *Utf8PrevChar(v) = '\0'.
Darkvater
parents: 5634
diff changeset
   326
 * @param s string to check if it needs additional trimming
bbd141b026b5 (svn r9012) -Fix/Feature (UTF8): When cutting strings into multiple lines also take into consideration whitespace characters of more than 1 byte length (eg IDEOGRAPHIC SPACE, IsWhitespace() function). When trimming such strings, account for multiple-byte long sequences so use *Utf8PrevChar(v) = '\0'.
Darkvater
parents: 5634
diff changeset
   327
 * @param maxlen the maximum length the buffer can have.
bbd141b026b5 (svn r9012) -Fix/Feature (UTF8): When cutting strings into multiple lines also take into consideration whitespace characters of more than 1 byte length (eg IDEOGRAPHIC SPACE, IsWhitespace() function). When trimming such strings, account for multiple-byte long sequences so use *Utf8PrevChar(v) = '\0'.
Darkvater
parents: 5634
diff changeset
   328
 * @return the new length in bytes of the string (eg. strlen(new_string))
bbd141b026b5 (svn r9012) -Fix/Feature (UTF8): When cutting strings into multiple lines also take into consideration whitespace characters of more than 1 byte length (eg IDEOGRAPHIC SPACE, IsWhitespace() function). When trimming such strings, account for multiple-byte long sequences so use *Utf8PrevChar(v) = '\0'.
Darkvater
parents: 5634
diff changeset
   329
 * @NOTE maxlen is the string length _INCLUDING_ the terminating '\0'
bbd141b026b5 (svn r9012) -Fix/Feature (UTF8): When cutting strings into multiple lines also take into consideration whitespace characters of more than 1 byte length (eg IDEOGRAPHIC SPACE, IsWhitespace() function). When trimming such strings, account for multiple-byte long sequences so use *Utf8PrevChar(v) = '\0'.
Darkvater
parents: 5634
diff changeset
   330
 */
bbd141b026b5 (svn r9012) -Fix/Feature (UTF8): When cutting strings into multiple lines also take into consideration whitespace characters of more than 1 byte length (eg IDEOGRAPHIC SPACE, IsWhitespace() function). When trimming such strings, account for multiple-byte long sequences so use *Utf8PrevChar(v) = '\0'.
Darkvater
parents: 5634
diff changeset
   331
size_t Utf8TrimString(char *s, size_t maxlen)
bbd141b026b5 (svn r9012) -Fix/Feature (UTF8): When cutting strings into multiple lines also take into consideration whitespace characters of more than 1 byte length (eg IDEOGRAPHIC SPACE, IsWhitespace() function). When trimming such strings, account for multiple-byte long sequences so use *Utf8PrevChar(v) = '\0'.
Darkvater
parents: 5634
diff changeset
   332
{
bbd141b026b5 (svn r9012) -Fix/Feature (UTF8): When cutting strings into multiple lines also take into consideration whitespace characters of more than 1 byte length (eg IDEOGRAPHIC SPACE, IsWhitespace() function). When trimming such strings, account for multiple-byte long sequences so use *Utf8PrevChar(v) = '\0'.
Darkvater
parents: 5634
diff changeset
   333
	size_t length = 0;
bbd141b026b5 (svn r9012) -Fix/Feature (UTF8): When cutting strings into multiple lines also take into consideration whitespace characters of more than 1 byte length (eg IDEOGRAPHIC SPACE, IsWhitespace() function). When trimming such strings, account for multiple-byte long sequences so use *Utf8PrevChar(v) = '\0'.
Darkvater
parents: 5634
diff changeset
   334
bbd141b026b5 (svn r9012) -Fix/Feature (UTF8): When cutting strings into multiple lines also take into consideration whitespace characters of more than 1 byte length (eg IDEOGRAPHIC SPACE, IsWhitespace() function). When trimming such strings, account for multiple-byte long sequences so use *Utf8PrevChar(v) = '\0'.
Darkvater
parents: 5634
diff changeset
   335
	for (const char *ptr = strchr(s, '\0'); *s != '\0';) {
bbd141b026b5 (svn r9012) -Fix/Feature (UTF8): When cutting strings into multiple lines also take into consideration whitespace characters of more than 1 byte length (eg IDEOGRAPHIC SPACE, IsWhitespace() function). When trimming such strings, account for multiple-byte long sequences so use *Utf8PrevChar(v) = '\0'.
Darkvater
parents: 5634
diff changeset
   336
		size_t len = Utf8EncodedCharLen(*s);
6274
40d2a82c6bd6 (svn r9083) -Codechange: Be more lenient when trimming UTF-8 strings and don't terminate the string when an invalid encoding is encountered, but only focus on maximum length.
Darkvater
parents: 6218
diff changeset
   337
		/* Silently ignore invalid UTF8 sequences, our only concern trimming */
40d2a82c6bd6 (svn r9083) -Codechange: Be more lenient when trimming UTF-8 strings and don't terminate the string when an invalid encoding is encountered, but only focus on maximum length.
Darkvater
parents: 6218
diff changeset
   338
		if (len == 0) len = 1;
6215
bbd141b026b5 (svn r9012) -Fix/Feature (UTF8): When cutting strings into multiple lines also take into consideration whitespace characters of more than 1 byte length (eg IDEOGRAPHIC SPACE, IsWhitespace() function). When trimming such strings, account for multiple-byte long sequences so use *Utf8PrevChar(v) = '\0'.
Darkvater
parents: 5634
diff changeset
   339
bbd141b026b5 (svn r9012) -Fix/Feature (UTF8): When cutting strings into multiple lines also take into consideration whitespace characters of more than 1 byte length (eg IDEOGRAPHIC SPACE, IsWhitespace() function). When trimming such strings, account for multiple-byte long sequences so use *Utf8PrevChar(v) = '\0'.
Darkvater
parents: 5634
diff changeset
   340
		/* Take care when a hard cutoff was made for the string and
bbd141b026b5 (svn r9012) -Fix/Feature (UTF8): When cutting strings into multiple lines also take into consideration whitespace characters of more than 1 byte length (eg IDEOGRAPHIC SPACE, IsWhitespace() function). When trimming such strings, account for multiple-byte long sequences so use *Utf8PrevChar(v) = '\0'.
Darkvater
parents: 5634
diff changeset
   341
		 * the last UTF8 sequence is invalid */
bbd141b026b5 (svn r9012) -Fix/Feature (UTF8): When cutting strings into multiple lines also take into consideration whitespace characters of more than 1 byte length (eg IDEOGRAPHIC SPACE, IsWhitespace() function). When trimming such strings, account for multiple-byte long sequences so use *Utf8PrevChar(v) = '\0'.
Darkvater
parents: 5634
diff changeset
   342
		if (length + len >= maxlen || (s + len > ptr)) break;
bbd141b026b5 (svn r9012) -Fix/Feature (UTF8): When cutting strings into multiple lines also take into consideration whitespace characters of more than 1 byte length (eg IDEOGRAPHIC SPACE, IsWhitespace() function). When trimming such strings, account for multiple-byte long sequences so use *Utf8PrevChar(v) = '\0'.
Darkvater
parents: 5634
diff changeset
   343
		s += len;
bbd141b026b5 (svn r9012) -Fix/Feature (UTF8): When cutting strings into multiple lines also take into consideration whitespace characters of more than 1 byte length (eg IDEOGRAPHIC SPACE, IsWhitespace() function). When trimming such strings, account for multiple-byte long sequences so use *Utf8PrevChar(v) = '\0'.
Darkvater
parents: 5634
diff changeset
   344
		length += len;
bbd141b026b5 (svn r9012) -Fix/Feature (UTF8): When cutting strings into multiple lines also take into consideration whitespace characters of more than 1 byte length (eg IDEOGRAPHIC SPACE, IsWhitespace() function). When trimming such strings, account for multiple-byte long sequences so use *Utf8PrevChar(v) = '\0'.
Darkvater
parents: 5634
diff changeset
   345
	}
bbd141b026b5 (svn r9012) -Fix/Feature (UTF8): When cutting strings into multiple lines also take into consideration whitespace characters of more than 1 byte length (eg IDEOGRAPHIC SPACE, IsWhitespace() function). When trimming such strings, account for multiple-byte long sequences so use *Utf8PrevChar(v) = '\0'.
Darkvater
parents: 5634
diff changeset
   346
bbd141b026b5 (svn r9012) -Fix/Feature (UTF8): When cutting strings into multiple lines also take into consideration whitespace characters of more than 1 byte length (eg IDEOGRAPHIC SPACE, IsWhitespace() function). When trimming such strings, account for multiple-byte long sequences so use *Utf8PrevChar(v) = '\0'.
Darkvater
parents: 5634
diff changeset
   347
	*s = '\0';
bbd141b026b5 (svn r9012) -Fix/Feature (UTF8): When cutting strings into multiple lines also take into consideration whitespace characters of more than 1 byte length (eg IDEOGRAPHIC SPACE, IsWhitespace() function). When trimming such strings, account for multiple-byte long sequences so use *Utf8PrevChar(v) = '\0'.
Darkvater
parents: 5634
diff changeset
   348
	return length;
6218
d41f73299be5 (svn r9015) -Fix
tron
parents: 6215
diff changeset
   349
}
9997
d858e88e871a (svn r14154) -Fix (r14153): strndup is a GNU extension, so it doesn't exist on all platforms
glx
parents: 9111
diff changeset
   350
d858e88e871a (svn r14154) -Fix (r14153): strndup is a GNU extension, so it doesn't exist on all platforms
glx
parents: 9111
diff changeset
   351
#ifndef _GNU_SOURCE
d858e88e871a (svn r14154) -Fix (r14153): strndup is a GNU extension, so it doesn't exist on all platforms
glx
parents: 9111
diff changeset
   352
#include "core/math_func.hpp"
d858e88e871a (svn r14154) -Fix (r14153): strndup is a GNU extension, so it doesn't exist on all platforms
glx
parents: 9111
diff changeset
   353
char *strndup(const char *s, size_t len)
d858e88e871a (svn r14154) -Fix (r14153): strndup is a GNU extension, so it doesn't exist on all platforms
glx
parents: 9111
diff changeset
   354
{
d858e88e871a (svn r14154) -Fix (r14153): strndup is a GNU extension, so it doesn't exist on all platforms
glx
parents: 9111
diff changeset
   355
	len = min(strlen(s), len);
d858e88e871a (svn r14154) -Fix (r14153): strndup is a GNU extension, so it doesn't exist on all platforms
glx
parents: 9111
diff changeset
   356
	char *tmp = CallocT<char>(len + 1);
d858e88e871a (svn r14154) -Fix (r14153): strndup is a GNU extension, so it doesn't exist on all platforms
glx
parents: 9111
diff changeset
   357
	memcpy(tmp, s, len);
d858e88e871a (svn r14154) -Fix (r14153): strndup is a GNU extension, so it doesn't exist on all platforms
glx
parents: 9111
diff changeset
   358
	return tmp;
d858e88e871a (svn r14154) -Fix (r14153): strndup is a GNU extension, so it doesn't exist on all platforms
glx
parents: 9111
diff changeset
   359
}
d858e88e871a (svn r14154) -Fix (r14153): strndup is a GNU extension, so it doesn't exist on all platforms
glx
parents: 9111
diff changeset
   360
#endif /* !_GNU_SOURCE */