src/string.cpp
author translators
Thu, 18 Dec 2008 18:47:39 +0000
changeset 10437 6d64230b9fb9
parent 10312 3c14d0ea0ef5
permissions -rw-r--r--
(svn r14691) -Update: WebTranslator2 update to 2008-12-18 18:47:25
arabic_egypt - 132 fixed, 1 changed by khaloofah (133)
czech - 10 fixed, 25 changed by Hadez (35)
esperanto - 15 fixed by Athaba (15)
greek - 7 fixed by ouranogrammi (7)
indonesian - 88 changed by fanioz (88)
latvian - 117 fixed, 16 changed by peerer (83), marismols (50)
lithuanian - 15 fixed by Zogg (15)
malay - 38 fixed by tombakemas (5), Syed (33)
serbian - 39 fixed, 1 changed by Jenraux (40)
thai - 105 fixed by vetbook (105)
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
#endif /* STRGEN */
5165
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
    82
	}
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
    83
	return dst;
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
    84
}
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
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
    87
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
    88
{
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
    89
	char buf[4096];
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
    90
	va_list va;
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
    91
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
    92
	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
    93
	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
    94
	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
    95
	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
    96
	memcpy(p, buf, len + 1);
5165
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
    97
	return p;
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
    98
}
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
void str_validate(char *str)
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   102
{
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   103
	char *dst = str;
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   104
	WChar c;
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   105
	size_t len;
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   106
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   107
	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
   108
		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
   109
			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
   110
			/* 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
   111
			 * (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
   112
			 * moving the pointers ahead by len */
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   113
			do {
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   114
				*dst++ = *str++;
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   115
			} while (--len != 0);
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   116
		} else {
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   117
			/* 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
   118
			str += len;
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   119
			*dst++ = '?';
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   120
		}
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
	*dst = '\0';
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   124
}
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
void str_strip_colours(char *str)
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   128
{
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   129
	char *dst = str;
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   130
	WChar c;
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   131
	size_t len;
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   132
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   133
	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
   134
		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
   135
			/* 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
   136
			 * (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
   137
			 * moving the pointers ahead by len */
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   138
			do {
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   139
				*dst++ = *str++;
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   140
			} while (--len != 0);
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   141
		} else {
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   142
			/* Just skip (strip) the colour codes */
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   143
			str += len;
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   144
		}
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
	*dst = '\0';
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   147
}
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
/** 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
   150
 * 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
   151
 * 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
   152
 * 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
   153
 * 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
   154
 * 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
   155
 * '?', 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
   156
 * @param str string to convert */
5165
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   157
void strtolower(char *str)
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   158
{
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   159
	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
   160
}
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
 * 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
   164
 *  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
   165
 * @param key character to be checked
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   166
 * @param afilter the filter to use
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   167
 * @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
   168
 */
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   169
bool IsValidChar(WChar key, CharSetFilter afilter)
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   170
{
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   171
	switch (afilter) {
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   172
		case CS_ALPHANUMERAL: return IsPrintable(key);
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   173
		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
   174
		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
   175
	}
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
	return false;
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   178
}
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
#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
   181
/* 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
   182
#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
   183
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
   184
{
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   185
	va_list ap;
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   186
	int ret;
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   187
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   188
	va_start(ap, format);
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   189
	ret = vsnprintf(str, size, format, ap);
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   190
	va_end(ap);
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   191
	return ret;
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   192
}
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
   193
#endif /* MinGW Runtime < 3.14 */
5165
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   194
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   195
#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
   196
/* *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
   197
 * - 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
   198
 * - 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
   199
 * - 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
   200
 */
5165
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   201
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
   202
{
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   203
	int ret;
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   204
	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
   205
	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
   206
	return ret;
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   207
}
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   208
#endif /* _MSC_VER */
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   209
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   210
#endif /* WIN32 */
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   211
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
   212
/**
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
 * 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
   214
 * - 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
   215
 * - 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
   216
 * @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
   217
 * @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
   218
 * @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
   219
 * @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
   220
 */
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
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
   222
{
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
	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
   224
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
	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
   226
	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
   227
	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
   228
	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
   229
}
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
5165
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   231
5634
ba6b9ebe197a (svn r8093) -Codechange: Add a function to get a string representation of an MD5SUM and use it.
Darkvater
parents: 5631
diff changeset
   232
/** 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
   233
 * @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
   234
 * @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
   235
 * @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
   236
 * @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
   237
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
   238
{
ba6b9ebe197a (svn r8093) -Codechange: Add a function to get a string representation of an MD5SUM and use it.
Darkvater
parents: 5631
diff changeset
   239
	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
   240
ba6b9ebe197a (svn r8093) -Codechange: Add a function to get a string representation of an MD5SUM and use it.
Darkvater
parents: 5631
diff changeset
   241
	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
   242
		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
   243
	}
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
	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
   246
}
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
5165
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   249
/* UTF-8 handling routines */
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   250
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
/* 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
   253
 * @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
   254
 * @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
   255
 * @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
   256
 */
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   257
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
   258
{
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   259
	assert(c != NULL);
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   260
7928
63e18de69e50 (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 7257
diff changeset
   261
	if (!HasBit(s[0], 7)) {
5165
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   262
		/* Single byte character: 0xxxxxxx */
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   263
		*c = s[0];
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   264
		return 1;
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   265
	} 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
   266
		if (IsUtf8Part(s[1])) {
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   267
			/* Double byte character: 110xxxxx 10xxxxxx */
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   268
			*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
   269
			if (*c >= 0x80) return 2;
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   270
		}
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   271
	} 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
   272
		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
   273
			/* Triple byte character: 1110xxxx 10xxxxxx 10xxxxxx */
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   274
			*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
   275
			if (*c >= 0x800) return 3;
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   276
		}
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   277
	} 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
   278
		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
   279
			/* 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
   280
			*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
   281
			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
   282
		}
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
5380
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5165
diff changeset
   285
	//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
   286
	*c = '?';
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   287
	return 1;
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   288
}
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
/* 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
   292
 * @param buf Buffer to place character.
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   293
 * @param c   Unicode character to encode.
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   294
 * @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
   295
 */
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   296
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
   297
{
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   298
	if (c < 0x80) {
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   299
		*buf = c;
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   300
		return 1;
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   301
	} else if (c < 0x800) {
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   302
		*buf++ = 0xC0 + GB(c,  6, 5);
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   303
		*buf   = 0x80 + GB(c,  0, 6);
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   304
		return 2;
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   305
	} else if (c < 0x10000) {
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   306
		*buf++ = 0xE0 + GB(c, 12, 4);
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   307
		*buf++ = 0x80 + GB(c,  6, 6);
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   308
		*buf   = 0x80 + GB(c,  0, 6);
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   309
		return 3;
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   310
	} else if (c < 0x110000) {
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   311
		*buf++ = 0xF0 + GB(c, 18, 3);
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   312
		*buf++ = 0x80 + GB(c, 12, 6);
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   313
		*buf++ = 0x80 + GB(c,  6, 6);
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   314
		*buf   = 0x80 + GB(c,  0, 6);
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   315
		return 4;
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   316
	}
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   317
5380
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5165
diff changeset
   318
	//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
   319
	*buf = '?';
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   320
	return 1;
028df092bebd (svn r7272) -Ok, let's hope this one's correct...stupid msvc
Darkvater
parents: 5164
diff changeset
   321
}
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
   322
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
 * 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
   325
 * @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
   326
 * @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
   327
 * @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
   328
 * @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
   329
 */
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
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
   331
{
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
	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
   333
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
	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
   335
		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
   336
		/* 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
   337
		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
   338
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
		/* 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
   340
		 * 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
   341
		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
   342
		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
   343
		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
   344
	}
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
	*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
   347
	return length;
6218
d41f73299be5 (svn r9015) -Fix
tron
parents: 6215
diff changeset
   348
}
9997
d858e88e871a (svn r14154) -Fix (r14153): strndup is a GNU extension, so it doesn't exist on all platforms
glx
parents: 9111
diff changeset
   349
d858e88e871a (svn r14154) -Fix (r14153): strndup is a GNU extension, so it doesn't exist on all platforms
glx
parents: 9111
diff changeset
   350
#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
   351
#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
   352
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
   353
{
d858e88e871a (svn r14154) -Fix (r14153): strndup is a GNU extension, so it doesn't exist on all platforms
glx
parents: 9111
diff changeset
   354
	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
   355
	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
   356
	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
   357
	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
   358
}
d858e88e871a (svn r14154) -Fix (r14153): strndup is a GNU extension, so it doesn't exist on all platforms
glx
parents: 9111
diff changeset
   359
#endif /* !_GNU_SOURCE */