string.c
author Darkvater
Sat, 18 Nov 2006 00:14:43 +0000
changeset 5120 e12dfc67761f
parent 5119 7e7fc659218a
child 5164 a4fb0ede4ce5
permissions -rw-r--r--
(svn r7200) -Codechange: remove unneeded redraw (console.c), coding style, use FindWindowById
instead of _windows loop (viewport.c), remove dump-code (mixer.c), MSVC6 borkdness
in stdafx.h, constness (viewport.c), variable localization (win32.c), comments (window.c)
2186
461a2aff3486 (svn r2701) Insert Id tags into all source files
tron
parents: 1317
diff changeset
     1
/* $Id$ */
461a2aff3486 (svn r2701) Insert Id tags into all source files
tron
parents: 1317
diff changeset
     2
1317
f382f1b439c7 (svn r1821) Move generic string handling functions to string.[ch] and introduce stre{cpy,cat}, see string.h for their semantics
tron
parents:
diff changeset
     3
#include "stdafx.h"
4912
d04b3f2bca70 (svn r6884) -Codechange: Add strict bounds checking in string formatting system.
Darkvater
parents: 4370
diff changeset
     4
#include "openttd.h"
d04b3f2bca70 (svn r6884) -Codechange: Add strict bounds checking in string formatting system.
Darkvater
parents: 4370
diff changeset
     5
#include "functions.h"
1317
f382f1b439c7 (svn r1821) Move generic string handling functions to string.[ch] and introduce stre{cpy,cat}, see string.h for their semantics
tron
parents:
diff changeset
     6
#include "string.h"
5108
dc67d70b5a45 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents: 5101
diff changeset
     7
#include "macros.h"
dc67d70b5a45 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents: 5101
diff changeset
     8
#include "table/control_codes.h"
1317
f382f1b439c7 (svn r1821) Move generic string handling functions to string.[ch] and introduce stre{cpy,cat}, see string.h for their semantics
tron
parents:
diff changeset
     9
2234
d44294cfea36 (svn r2754) Move str_fmt into string.[ch]
tron
parents: 2186
diff changeset
    10
#include <stdarg.h>
5119
7e7fc659218a (svn r7199) -Codechange: [utf8] Make strtolower and str_strip_colours UTF8 aware.
Darkvater
parents: 5108
diff changeset
    11
#include <wctype.h> // required for towlower()
7e7fc659218a (svn r7199) -Codechange: [utf8] Make strtolower and str_strip_colours UTF8 aware.
Darkvater
parents: 5108
diff changeset
    12
#include <locale.h> // required for setlocale()
2234
d44294cfea36 (svn r2754) Move str_fmt into string.[ch]
tron
parents: 2186
diff changeset
    13
1317
f382f1b439c7 (svn r1821) Move generic string handling functions to string.[ch] and introduce stre{cpy,cat}, see string.h for their semantics
tron
parents:
diff changeset
    14
void ttd_strlcat(char *dst, const char *src, size_t size)
f382f1b439c7 (svn r1821) Move generic string handling functions to string.[ch] and introduce stre{cpy,cat}, see string.h for their semantics
tron
parents:
diff changeset
    15
{
f382f1b439c7 (svn r1821) Move generic string handling functions to string.[ch] and introduce stre{cpy,cat}, see string.h for their semantics
tron
parents:
diff changeset
    16
	assert(size > 0);
f382f1b439c7 (svn r1821) Move generic string handling functions to string.[ch] and introduce stre{cpy,cat}, see string.h for their semantics
tron
parents:
diff changeset
    17
	for (; size > 0 && *dst != '\0'; --size, ++dst) {}
f382f1b439c7 (svn r1821) Move generic string handling functions to string.[ch] and introduce stre{cpy,cat}, see string.h for their semantics
tron
parents:
diff changeset
    18
	assert(size > 0);
f382f1b439c7 (svn r1821) Move generic string handling functions to string.[ch] and introduce stre{cpy,cat}, see string.h for their semantics
tron
parents:
diff changeset
    19
	while (--size > 0 && *src != '\0') *dst++ = *src++;
f382f1b439c7 (svn r1821) Move generic string handling functions to string.[ch] and introduce stre{cpy,cat}, see string.h for their semantics
tron
parents:
diff changeset
    20
	*dst = '\0';
f382f1b439c7 (svn r1821) Move generic string handling functions to string.[ch] and introduce stre{cpy,cat}, see string.h for their semantics
tron
parents:
diff changeset
    21
}
f382f1b439c7 (svn r1821) Move generic string handling functions to string.[ch] and introduce stre{cpy,cat}, see string.h for their semantics
tron
parents:
diff changeset
    22
f382f1b439c7 (svn r1821) Move generic string handling functions to string.[ch] and introduce stre{cpy,cat}, see string.h for their semantics
tron
parents:
diff changeset
    23
f382f1b439c7 (svn r1821) Move generic string handling functions to string.[ch] and introduce stre{cpy,cat}, see string.h for their semantics
tron
parents:
diff changeset
    24
void ttd_strlcpy(char *dst, const char *src, size_t size)
f382f1b439c7 (svn r1821) Move generic string handling functions to string.[ch] and introduce stre{cpy,cat}, see string.h for their semantics
tron
parents:
diff changeset
    25
{
f382f1b439c7 (svn r1821) Move generic string handling functions to string.[ch] and introduce stre{cpy,cat}, see string.h for their semantics
tron
parents:
diff changeset
    26
	assert(size > 0);
f382f1b439c7 (svn r1821) Move generic string handling functions to string.[ch] and introduce stre{cpy,cat}, see string.h for their semantics
tron
parents:
diff changeset
    27
	while (--size > 0 && *src != '\0') *dst++ = *src++;
f382f1b439c7 (svn r1821) Move generic string handling functions to string.[ch] and introduce stre{cpy,cat}, see string.h for their semantics
tron
parents:
diff changeset
    28
	*dst = '\0';
f382f1b439c7 (svn r1821) Move generic string handling functions to string.[ch] and introduce stre{cpy,cat}, see string.h for their semantics
tron
parents:
diff changeset
    29
}
f382f1b439c7 (svn r1821) Move generic string handling functions to string.[ch] and introduce stre{cpy,cat}, see string.h for their semantics
tron
parents:
diff changeset
    30
f382f1b439c7 (svn r1821) Move generic string handling functions to string.[ch] and introduce stre{cpy,cat}, see string.h for their semantics
tron
parents:
diff changeset
    31
f382f1b439c7 (svn r1821) Move generic string handling functions to string.[ch] and introduce stre{cpy,cat}, see string.h for their semantics
tron
parents:
diff changeset
    32
char* strecat(char* dst, const char* src, const char* last)
f382f1b439c7 (svn r1821) Move generic string handling functions to string.[ch] and introduce stre{cpy,cat}, see string.h for their semantics
tron
parents:
diff changeset
    33
{
4912
d04b3f2bca70 (svn r6884) -Codechange: Add strict bounds checking in string formatting system.
Darkvater
parents: 4370
diff changeset
    34
	assert(dst <= last);
1317
f382f1b439c7 (svn r1821) Move generic string handling functions to string.[ch] and introduce stre{cpy,cat}, see string.h for their semantics
tron
parents:
diff changeset
    35
	for (; *dst != '\0'; ++dst)
f382f1b439c7 (svn r1821) Move generic string handling functions to string.[ch] and introduce stre{cpy,cat}, see string.h for their semantics
tron
parents:
diff changeset
    36
		if (dst == last) return dst;
f382f1b439c7 (svn r1821) Move generic string handling functions to string.[ch] and introduce stre{cpy,cat}, see string.h for their semantics
tron
parents:
diff changeset
    37
	for (; *src != '\0' && dst != last; ++dst, ++src) *dst = *src;
f382f1b439c7 (svn r1821) Move generic string handling functions to string.[ch] and introduce stre{cpy,cat}, see string.h for their semantics
tron
parents:
diff changeset
    38
	*dst = '\0';
f382f1b439c7 (svn r1821) Move generic string handling functions to string.[ch] and introduce stre{cpy,cat}, see string.h for their semantics
tron
parents:
diff changeset
    39
	return strecpy(dst, src, last);
f382f1b439c7 (svn r1821) Move generic string handling functions to string.[ch] and introduce stre{cpy,cat}, see string.h for their semantics
tron
parents:
diff changeset
    40
}
f382f1b439c7 (svn r1821) Move generic string handling functions to string.[ch] and introduce stre{cpy,cat}, see string.h for their semantics
tron
parents:
diff changeset
    41
f382f1b439c7 (svn r1821) Move generic string handling functions to string.[ch] and introduce stre{cpy,cat}, see string.h for their semantics
tron
parents:
diff changeset
    42
f382f1b439c7 (svn r1821) Move generic string handling functions to string.[ch] and introduce stre{cpy,cat}, see string.h for their semantics
tron
parents:
diff changeset
    43
char* strecpy(char* dst, const char* src, const char* last)
f382f1b439c7 (svn r1821) Move generic string handling functions to string.[ch] and introduce stre{cpy,cat}, see string.h for their semantics
tron
parents:
diff changeset
    44
{
4912
d04b3f2bca70 (svn r6884) -Codechange: Add strict bounds checking in string formatting system.
Darkvater
parents: 4370
diff changeset
    45
	assert(dst <= last);
1317
f382f1b439c7 (svn r1821) Move generic string handling functions to string.[ch] and introduce stre{cpy,cat}, see string.h for their semantics
tron
parents:
diff changeset
    46
	for (; *src != '\0' && dst != last; ++dst, ++src) *dst = *src;
f382f1b439c7 (svn r1821) Move generic string handling functions to string.[ch] and introduce stre{cpy,cat}, see string.h for their semantics
tron
parents:
diff changeset
    47
	*dst = '\0';
4914
5e36af0cfb1d (svn r6886) -Be anal for the time being about string-wannabe-buffer-overflows
Darkvater
parents: 4912
diff changeset
    48
#if 1
4912
d04b3f2bca70 (svn r6884) -Codechange: Add strict bounds checking in string formatting system.
Darkvater
parents: 4370
diff changeset
    49
	if (dst == last && *src != '\0') {
d04b3f2bca70 (svn r6884) -Codechange: Add strict bounds checking in string formatting system.
Darkvater
parents: 4370
diff changeset
    50
		error("String too long for destination buffer");
d04b3f2bca70 (svn r6884) -Codechange: Add strict bounds checking in string formatting system.
Darkvater
parents: 4370
diff changeset
    51
	}
d04b3f2bca70 (svn r6884) -Codechange: Add strict bounds checking in string formatting system.
Darkvater
parents: 4370
diff changeset
    52
#endif
1317
f382f1b439c7 (svn r1821) Move generic string handling functions to string.[ch] and introduce stre{cpy,cat}, see string.h for their semantics
tron
parents:
diff changeset
    53
	return dst;
f382f1b439c7 (svn r1821) Move generic string handling functions to string.[ch] and introduce stre{cpy,cat}, see string.h for their semantics
tron
parents:
diff changeset
    54
}
2234
d44294cfea36 (svn r2754) Move str_fmt into string.[ch]
tron
parents: 2186
diff changeset
    55
d44294cfea36 (svn r2754) Move str_fmt into string.[ch]
tron
parents: 2186
diff changeset
    56
d44294cfea36 (svn r2754) Move str_fmt into string.[ch]
tron
parents: 2186
diff changeset
    57
char* CDECL str_fmt(const char* str, ...)
d44294cfea36 (svn r2754) Move str_fmt into string.[ch]
tron
parents: 2186
diff changeset
    58
{
d44294cfea36 (svn r2754) Move str_fmt into string.[ch]
tron
parents: 2186
diff changeset
    59
	char buf[4096];
d44294cfea36 (svn r2754) Move str_fmt into string.[ch]
tron
parents: 2186
diff changeset
    60
	va_list va;
d44294cfea36 (svn r2754) Move str_fmt into string.[ch]
tron
parents: 2186
diff changeset
    61
	int len;
d44294cfea36 (svn r2754) Move str_fmt into string.[ch]
tron
parents: 2186
diff changeset
    62
	char* p;
d44294cfea36 (svn r2754) Move str_fmt into string.[ch]
tron
parents: 2186
diff changeset
    63
d44294cfea36 (svn r2754) Move str_fmt into string.[ch]
tron
parents: 2186
diff changeset
    64
	va_start(va, str);
4370
c7bd128b1670 (svn r6089) -Backport r6088: added -s (source) and -d (destination) to strgen (Darkvater)
truelight
parents: 4300
diff changeset
    65
	len = vsnprintf(buf, lengthof(buf), str, va);
2234
d44294cfea36 (svn r2754) Move str_fmt into string.[ch]
tron
parents: 2186
diff changeset
    66
	va_end(va);
d44294cfea36 (svn r2754) Move str_fmt into string.[ch]
tron
parents: 2186
diff changeset
    67
	p = malloc(len + 1);
d44294cfea36 (svn r2754) Move str_fmt into string.[ch]
tron
parents: 2186
diff changeset
    68
	if (p != NULL) memcpy(p, buf, len + 1);
d44294cfea36 (svn r2754) Move str_fmt into string.[ch]
tron
parents: 2186
diff changeset
    69
	return p;
d44294cfea36 (svn r2754) Move str_fmt into string.[ch]
tron
parents: 2186
diff changeset
    70
}
2775
d3ed38a97250 (svn r3322) - Fix: Network window crash when it receives invalid information for example from the integrated nightly, so validate the network-input when it is received
Darkvater
parents: 2234
diff changeset
    71
5119
7e7fc659218a (svn r7199) -Codechange: [utf8] Make strtolower and str_strip_colours UTF8 aware.
Darkvater
parents: 5108
diff changeset
    72
2775
d3ed38a97250 (svn r3322) - Fix: Network window crash when it receives invalid information for example from the integrated nightly, so validate the network-input when it is received
Darkvater
parents: 2234
diff changeset
    73
void str_validate(char *str)
d3ed38a97250 (svn r3322) - Fix: Network window crash when it receives invalid information for example from the integrated nightly, so validate the network-input when it is received
Darkvater
parents: 2234
diff changeset
    74
{
5108
dc67d70b5a45 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents: 5101
diff changeset
    75
	char *dst = str;
dc67d70b5a45 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents: 5101
diff changeset
    76
	WChar c;
5119
7e7fc659218a (svn r7199) -Codechange: [utf8] Make strtolower and str_strip_colours UTF8 aware.
Darkvater
parents: 5108
diff changeset
    77
	size_t len;
5108
dc67d70b5a45 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents: 5101
diff changeset
    78
5119
7e7fc659218a (svn r7199) -Codechange: [utf8] Make strtolower and str_strip_colours UTF8 aware.
Darkvater
parents: 5108
diff changeset
    79
	for (len = Utf8Decode(&c, str); c != '\0'; len = Utf8Decode(&c, str)) {
5108
dc67d70b5a45 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents: 5101
diff changeset
    80
		if (IsPrintable(c) && (c < SCC_SPRITE_START || c > SCC_SPRITE_END ||
dc67d70b5a45 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents: 5101
diff changeset
    81
			IsValidChar(c - SCC_SPRITE_START, CS_ALPHANUMERAL))) {
dc67d70b5a45 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents: 5101
diff changeset
    82
			/* Copy the character back. Even if dst is current the same as str
dc67d70b5a45 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents: 5101
diff changeset
    83
			 * (i.e. no characters have been changed) this is quicker than
dc67d70b5a45 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents: 5101
diff changeset
    84
			 * moving the pointers ahead by len */
dc67d70b5a45 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents: 5101
diff changeset
    85
			do {
dc67d70b5a45 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents: 5101
diff changeset
    86
				*dst++ = *str++;
5119
7e7fc659218a (svn r7199) -Codechange: [utf8] Make strtolower and str_strip_colours UTF8 aware.
Darkvater
parents: 5108
diff changeset
    87
			} while (--len != 0);
5108
dc67d70b5a45 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents: 5101
diff changeset
    88
		} else {
dc67d70b5a45 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents: 5101
diff changeset
    89
			/* Replace the undesirable character with a question mark */
dc67d70b5a45 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents: 5101
diff changeset
    90
			str += len;
dc67d70b5a45 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents: 5101
diff changeset
    91
			*dst++ = '?';
dc67d70b5a45 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents: 5101
diff changeset
    92
		}
dc67d70b5a45 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents: 5101
diff changeset
    93
	}
dc67d70b5a45 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents: 5101
diff changeset
    94
dc67d70b5a45 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents: 5101
diff changeset
    95
	*dst = '\0';
2775
d3ed38a97250 (svn r3322) - Fix: Network window crash when it receives invalid information for example from the integrated nightly, so validate the network-input when it is received
Darkvater
parents: 2234
diff changeset
    96
}
4200
a45420ba0c23 (svn r5684) - Codechange: create an strtolower() function that uses tolower() on a whole string and apply it in the places this was used.
Darkvater
parents: 2775
diff changeset
    97
5119
7e7fc659218a (svn r7199) -Codechange: [utf8] Make strtolower and str_strip_colours UTF8 aware.
Darkvater
parents: 5108
diff changeset
    98
5101
797a070e5b22 (svn r7172) -Fix [r6931]: The console showed '?' characters instead of colours. Now strip all
Darkvater
parents: 4914
diff changeset
    99
void str_strip_colours(char *str)
797a070e5b22 (svn r7172) -Fix [r6931]: The console showed '?' characters instead of colours. Now strip all
Darkvater
parents: 4914
diff changeset
   100
{
797a070e5b22 (svn r7172) -Fix [r6931]: The console showed '?' characters instead of colours. Now strip all
Darkvater
parents: 4914
diff changeset
   101
	char *dst = str;
5119
7e7fc659218a (svn r7199) -Codechange: [utf8] Make strtolower and str_strip_colours UTF8 aware.
Darkvater
parents: 5108
diff changeset
   102
	WChar c;
7e7fc659218a (svn r7199) -Codechange: [utf8] Make strtolower and str_strip_colours UTF8 aware.
Darkvater
parents: 5108
diff changeset
   103
	size_t len;
7e7fc659218a (svn r7199) -Codechange: [utf8] Make strtolower and str_strip_colours UTF8 aware.
Darkvater
parents: 5108
diff changeset
   104
7e7fc659218a (svn r7199) -Codechange: [utf8] Make strtolower and str_strip_colours UTF8 aware.
Darkvater
parents: 5108
diff changeset
   105
	strtolower(str);
7e7fc659218a (svn r7199) -Codechange: [utf8] Make strtolower and str_strip_colours UTF8 aware.
Darkvater
parents: 5108
diff changeset
   106
	for (len = Utf8Decode(&c, str); c != '\0'; len = Utf8Decode(&c, str)) {
7e7fc659218a (svn r7199) -Codechange: [utf8] Make strtolower and str_strip_colours UTF8 aware.
Darkvater
parents: 5108
diff changeset
   107
		if (c < SCC_BLUE || c > SCC_BLACK) {
7e7fc659218a (svn r7199) -Codechange: [utf8] Make strtolower and str_strip_colours UTF8 aware.
Darkvater
parents: 5108
diff changeset
   108
			/* Copy the character back. Even if dst is current the same as str
7e7fc659218a (svn r7199) -Codechange: [utf8] Make strtolower and str_strip_colours UTF8 aware.
Darkvater
parents: 5108
diff changeset
   109
			 * (i.e. no characters have been changed) this is quicker than
7e7fc659218a (svn r7199) -Codechange: [utf8] Make strtolower and str_strip_colours UTF8 aware.
Darkvater
parents: 5108
diff changeset
   110
			 * moving the pointers ahead by len */
7e7fc659218a (svn r7199) -Codechange: [utf8] Make strtolower and str_strip_colours UTF8 aware.
Darkvater
parents: 5108
diff changeset
   111
			do {
7e7fc659218a (svn r7199) -Codechange: [utf8] Make strtolower and str_strip_colours UTF8 aware.
Darkvater
parents: 5108
diff changeset
   112
				*dst++ = *str++;
7e7fc659218a (svn r7199) -Codechange: [utf8] Make strtolower and str_strip_colours UTF8 aware.
Darkvater
parents: 5108
diff changeset
   113
			} while (--len != 0);
5101
797a070e5b22 (svn r7172) -Fix [r6931]: The console showed '?' characters instead of colours. Now strip all
Darkvater
parents: 4914
diff changeset
   114
		} else {
5119
7e7fc659218a (svn r7199) -Codechange: [utf8] Make strtolower and str_strip_colours UTF8 aware.
Darkvater
parents: 5108
diff changeset
   115
			/* Just skip (strip) the colour codes */
7e7fc659218a (svn r7199) -Codechange: [utf8] Make strtolower and str_strip_colours UTF8 aware.
Darkvater
parents: 5108
diff changeset
   116
			str += len;
5101
797a070e5b22 (svn r7172) -Fix [r6931]: The console showed '?' characters instead of colours. Now strip all
Darkvater
parents: 4914
diff changeset
   117
		}
797a070e5b22 (svn r7172) -Fix [r6931]: The console showed '?' characters instead of colours. Now strip all
Darkvater
parents: 4914
diff changeset
   118
	}
797a070e5b22 (svn r7172) -Fix [r6931]: The console showed '?' characters instead of colours. Now strip all
Darkvater
parents: 4914
diff changeset
   119
	*dst = '\0';
797a070e5b22 (svn r7172) -Fix [r6931]: The console showed '?' characters instead of colours. Now strip all
Darkvater
parents: 4914
diff changeset
   120
}
797a070e5b22 (svn r7172) -Fix [r6931]: The console showed '?' characters instead of colours. Now strip all
Darkvater
parents: 4914
diff changeset
   121
5119
7e7fc659218a (svn r7199) -Codechange: [utf8] Make strtolower and str_strip_colours UTF8 aware.
Darkvater
parents: 5108
diff changeset
   122
7e7fc659218a (svn r7199) -Codechange: [utf8] Make strtolower and str_strip_colours UTF8 aware.
Darkvater
parents: 5108
diff changeset
   123
void strtolower(char *str)
7e7fc659218a (svn r7199) -Codechange: [utf8] Make strtolower and str_strip_colours UTF8 aware.
Darkvater
parents: 5108
diff changeset
   124
{
7e7fc659218a (svn r7199) -Codechange: [utf8] Make strtolower and str_strip_colours UTF8 aware.
Darkvater
parents: 5108
diff changeset
   125
	WChar c;
7e7fc659218a (svn r7199) -Codechange: [utf8] Make strtolower and str_strip_colours UTF8 aware.
Darkvater
parents: 5108
diff changeset
   126
	/* Convert according to native locale, needed for unicode characters
7e7fc659218a (svn r7199) -Codechange: [utf8] Make strtolower and str_strip_colours UTF8 aware.
Darkvater
parents: 5108
diff changeset
   127
	 * We backup the current locale, then set it to native "", the set back */
7e7fc659218a (svn r7199) -Codechange: [utf8] Make strtolower and str_strip_colours UTF8 aware.
Darkvater
parents: 5108
diff changeset
   128
	char *locale = strdup(setlocale(LC_CTYPE, NULL));
7e7fc659218a (svn r7199) -Codechange: [utf8] Make strtolower and str_strip_colours UTF8 aware.
Darkvater
parents: 5108
diff changeset
   129
7e7fc659218a (svn r7199) -Codechange: [utf8] Make strtolower and str_strip_colours UTF8 aware.
Darkvater
parents: 5108
diff changeset
   130
	setlocale(LC_CTYPE, "");
7e7fc659218a (svn r7199) -Codechange: [utf8] Make strtolower and str_strip_colours UTF8 aware.
Darkvater
parents: 5108
diff changeset
   131
	for (Utf8Decode(&c, str); c != '\0'; Utf8Decode(&c, str)) {
7e7fc659218a (svn r7199) -Codechange: [utf8] Make strtolower and str_strip_colours UTF8 aware.
Darkvater
parents: 5108
diff changeset
   132
		/* XXX - assume lowercase version does not use more bytes */
7e7fc659218a (svn r7199) -Codechange: [utf8] Make strtolower and str_strip_colours UTF8 aware.
Darkvater
parents: 5108
diff changeset
   133
		c = towlower(c);
7e7fc659218a (svn r7199) -Codechange: [utf8] Make strtolower and str_strip_colours UTF8 aware.
Darkvater
parents: 5108
diff changeset
   134
		str += Utf8Encode(str, c);
7e7fc659218a (svn r7199) -Codechange: [utf8] Make strtolower and str_strip_colours UTF8 aware.
Darkvater
parents: 5108
diff changeset
   135
	}
7e7fc659218a (svn r7199) -Codechange: [utf8] Make strtolower and str_strip_colours UTF8 aware.
Darkvater
parents: 5108
diff changeset
   136
	setlocale(LC_CTYPE, locale);
7e7fc659218a (svn r7199) -Codechange: [utf8] Make strtolower and str_strip_colours UTF8 aware.
Darkvater
parents: 5108
diff changeset
   137
	free(locale);
7e7fc659218a (svn r7199) -Codechange: [utf8] Make strtolower and str_strip_colours UTF8 aware.
Darkvater
parents: 5108
diff changeset
   138
}
7e7fc659218a (svn r7199) -Codechange: [utf8] Make strtolower and str_strip_colours UTF8 aware.
Darkvater
parents: 5108
diff changeset
   139
7e7fc659218a (svn r7199) -Codechange: [utf8] Make strtolower and str_strip_colours UTF8 aware.
Darkvater
parents: 5108
diff changeset
   140
4300
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents: 4299
diff changeset
   141
/**
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents: 4299
diff changeset
   142
 * Only allow certain keys. You can define the filter to be used. This makes
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents: 4299
diff changeset
   143
 *  sure no invalid keys can get into an editbox, like BELL.
4299
b86602eaaff1 (svn r5944) -Merge TGP (r5578, r5579, r5724, r5726): -Feature: filter for textboxes to only
truelight
parents: 4209
diff changeset
   144
 * @param key character to be checked
4300
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents: 4299
diff changeset
   145
 * @param afilter the filter to use
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents: 4299
diff changeset
   146
 * @return true or false depending if the character is printable/valid or not
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents: 4299
diff changeset
   147
 */
5108
dc67d70b5a45 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents: 5101
diff changeset
   148
bool IsValidChar(WChar key, CharSetFilter afilter)
4299
b86602eaaff1 (svn r5944) -Merge TGP (r5578, r5579, r5724, r5726): -Feature: filter for textboxes to only
truelight
parents: 4209
diff changeset
   149
{
b86602eaaff1 (svn r5944) -Merge TGP (r5578, r5579, r5724, r5726): -Feature: filter for textboxes to only
truelight
parents: 4209
diff changeset
   150
	switch (afilter) {
5108
dc67d70b5a45 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents: 5101
diff changeset
   151
		case CS_ALPHANUMERAL: return IsPrintable(key);
dc67d70b5a45 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents: 5101
diff changeset
   152
		case CS_NUMERAL:      return (key >= '0' && key <= '9');
dc67d70b5a45 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents: 5101
diff changeset
   153
		case CS_ALPHA:        return IsPrintable(key) && !(key >= '0' && key <= '9');
4299
b86602eaaff1 (svn r5944) -Merge TGP (r5578, r5579, r5724, r5726): -Feature: filter for textboxes to only
truelight
parents: 4209
diff changeset
   154
	}
b86602eaaff1 (svn r5944) -Merge TGP (r5578, r5579, r5724, r5726): -Feature: filter for textboxes to only
truelight
parents: 4209
diff changeset
   155
5108
dc67d70b5a45 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents: 5101
diff changeset
   156
	return false;
4299
b86602eaaff1 (svn r5944) -Merge TGP (r5578, r5579, r5724, r5726): -Feature: filter for textboxes to only
truelight
parents: 4209
diff changeset
   157
}
4300
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents: 4299
diff changeset
   158
4370
c7bd128b1670 (svn r6089) -Backport r6088: added -s (source) and -d (destination) to strgen (Darkvater)
truelight
parents: 4300
diff changeset
   159
#ifdef WIN32
c7bd128b1670 (svn r6089) -Backport r6088: added -s (source) and -d (destination) to strgen (Darkvater)
truelight
parents: 4300
diff changeset
   160
int CDECL snprintf(char *str, size_t size, const char *format, ...)
c7bd128b1670 (svn r6089) -Backport r6088: added -s (source) and -d (destination) to strgen (Darkvater)
truelight
parents: 4300
diff changeset
   161
{
c7bd128b1670 (svn r6089) -Backport r6088: added -s (source) and -d (destination) to strgen (Darkvater)
truelight
parents: 4300
diff changeset
   162
	va_list ap;
c7bd128b1670 (svn r6089) -Backport r6088: added -s (source) and -d (destination) to strgen (Darkvater)
truelight
parents: 4300
diff changeset
   163
	int ret;
c7bd128b1670 (svn r6089) -Backport r6088: added -s (source) and -d (destination) to strgen (Darkvater)
truelight
parents: 4300
diff changeset
   164
c7bd128b1670 (svn r6089) -Backport r6088: added -s (source) and -d (destination) to strgen (Darkvater)
truelight
parents: 4300
diff changeset
   165
	va_start(ap, format);
c7bd128b1670 (svn r6089) -Backport r6088: added -s (source) and -d (destination) to strgen (Darkvater)
truelight
parents: 4300
diff changeset
   166
	ret = vsnprintf(str, size, format, ap);
c7bd128b1670 (svn r6089) -Backport r6088: added -s (source) and -d (destination) to strgen (Darkvater)
truelight
parents: 4300
diff changeset
   167
	va_end(ap);
c7bd128b1670 (svn r6089) -Backport r6088: added -s (source) and -d (destination) to strgen (Darkvater)
truelight
parents: 4300
diff changeset
   168
	return ret;
c7bd128b1670 (svn r6089) -Backport r6088: added -s (source) and -d (destination) to strgen (Darkvater)
truelight
parents: 4300
diff changeset
   169
}
c7bd128b1670 (svn r6089) -Backport r6088: added -s (source) and -d (destination) to strgen (Darkvater)
truelight
parents: 4300
diff changeset
   170
c7bd128b1670 (svn r6089) -Backport r6088: added -s (source) and -d (destination) to strgen (Darkvater)
truelight
parents: 4300
diff changeset
   171
#ifdef _MSC_VER
c7bd128b1670 (svn r6089) -Backport r6088: added -s (source) and -d (destination) to strgen (Darkvater)
truelight
parents: 4300
diff changeset
   172
int CDECL vsnprintf(char *str, size_t size, const char *format, va_list ap)
c7bd128b1670 (svn r6089) -Backport r6088: added -s (source) and -d (destination) to strgen (Darkvater)
truelight
parents: 4300
diff changeset
   173
{
c7bd128b1670 (svn r6089) -Backport r6088: added -s (source) and -d (destination) to strgen (Darkvater)
truelight
parents: 4300
diff changeset
   174
	int ret;
c7bd128b1670 (svn r6089) -Backport r6088: added -s (source) and -d (destination) to strgen (Darkvater)
truelight
parents: 4300
diff changeset
   175
	ret = _vsnprintf(str, size, format, ap);
c7bd128b1670 (svn r6089) -Backport r6088: added -s (source) and -d (destination) to strgen (Darkvater)
truelight
parents: 4300
diff changeset
   176
	if (ret < 0) str[size - 1] = '\0';
c7bd128b1670 (svn r6089) -Backport r6088: added -s (source) and -d (destination) to strgen (Darkvater)
truelight
parents: 4300
diff changeset
   177
	return ret;
c7bd128b1670 (svn r6089) -Backport r6088: added -s (source) and -d (destination) to strgen (Darkvater)
truelight
parents: 4300
diff changeset
   178
}
c7bd128b1670 (svn r6089) -Backport r6088: added -s (source) and -d (destination) to strgen (Darkvater)
truelight
parents: 4300
diff changeset
   179
#endif /* _MSC_VER */
c7bd128b1670 (svn r6089) -Backport r6088: added -s (source) and -d (destination) to strgen (Darkvater)
truelight
parents: 4300
diff changeset
   180
c7bd128b1670 (svn r6089) -Backport r6088: added -s (source) and -d (destination) to strgen (Darkvater)
truelight
parents: 4300
diff changeset
   181
#endif /* WIN32 */
5108
dc67d70b5a45 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents: 5101
diff changeset
   182
dc67d70b5a45 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents: 5101
diff changeset
   183
dc67d70b5a45 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents: 5101
diff changeset
   184
/* UTF-8 handling routines */
dc67d70b5a45 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents: 5101
diff changeset
   185
dc67d70b5a45 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents: 5101
diff changeset
   186
dc67d70b5a45 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents: 5101
diff changeset
   187
/* Decode and consume the next UTF-8 encoded character
dc67d70b5a45 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents: 5101
diff changeset
   188
 * @param c Buffer to place decoded character.
dc67d70b5a45 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents: 5101
diff changeset
   189
 * @param s Character stream to retrieve character from.
dc67d70b5a45 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents: 5101
diff changeset
   190
 * @return Number of characters in the sequence.
dc67d70b5a45 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents: 5101
diff changeset
   191
 */
dc67d70b5a45 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents: 5101
diff changeset
   192
size_t Utf8Decode(WChar *c, const char *s)
dc67d70b5a45 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents: 5101
diff changeset
   193
{
dc67d70b5a45 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents: 5101
diff changeset
   194
	assert(c != NULL);
dc67d70b5a45 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents: 5101
diff changeset
   195
dc67d70b5a45 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents: 5101
diff changeset
   196
	if (!HASBIT(s[0], 7)) {
dc67d70b5a45 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents: 5101
diff changeset
   197
		/* Single byte character: 0xxxxxxx */
dc67d70b5a45 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents: 5101
diff changeset
   198
		*c = s[0];
dc67d70b5a45 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents: 5101
diff changeset
   199
		return 1;
dc67d70b5a45 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents: 5101
diff changeset
   200
	} else if (GB(s[0], 5, 3) == 6) {
dc67d70b5a45 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents: 5101
diff changeset
   201
		if (IsUtf8Part(s[1])) {
dc67d70b5a45 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents: 5101
diff changeset
   202
			/* Double byte character: 110xxxxx 10xxxxxx */
dc67d70b5a45 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents: 5101
diff changeset
   203
			*c = GB(s[0], 0, 5) << 6 | GB(s[1], 0, 6);
dc67d70b5a45 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents: 5101
diff changeset
   204
			if (*c >= 0x80) return 2;
dc67d70b5a45 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents: 5101
diff changeset
   205
		}
dc67d70b5a45 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents: 5101
diff changeset
   206
	} else if (GB(s[0], 4, 4) == 14) {
dc67d70b5a45 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents: 5101
diff changeset
   207
		if (IsUtf8Part(s[1]) && IsUtf8Part(s[2])) {
dc67d70b5a45 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents: 5101
diff changeset
   208
			/* Triple byte character: 1110xxxx 10xxxxxx 10xxxxxx */
dc67d70b5a45 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents: 5101
diff changeset
   209
			*c = GB(s[0], 0, 4) << 12 | GB(s[1], 0, 6) << 6 | GB(s[2], 0, 6);
dc67d70b5a45 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents: 5101
diff changeset
   210
			if (*c >= 0x800) return 3;
dc67d70b5a45 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents: 5101
diff changeset
   211
		}
dc67d70b5a45 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents: 5101
diff changeset
   212
	} else if (GB(s[0], 3, 5) == 30) {
dc67d70b5a45 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents: 5101
diff changeset
   213
		if (IsUtf8Part(s[1]) && IsUtf8Part(s[2]) && IsUtf8Part(s[3])) {
dc67d70b5a45 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents: 5101
diff changeset
   214
			/* 4 byte character: 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx */
dc67d70b5a45 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents: 5101
diff changeset
   215
			*c = GB(s[0], 0, 3) << 18 | GB(s[1], 0, 6) << 12 | GB(s[2], 0, 6) << 6 | GB(s[3], 0, 6);
dc67d70b5a45 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents: 5101
diff changeset
   216
			if (*c >= 0x10000 && *c <= 0x10FFFF) return 4;
dc67d70b5a45 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents: 5101
diff changeset
   217
		}
dc67d70b5a45 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents: 5101
diff changeset
   218
	}
dc67d70b5a45 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents: 5101
diff changeset
   219
dc67d70b5a45 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents: 5101
diff changeset
   220
	//DEBUG(misc, 1) ("Invalid UTF-8 sequence");
dc67d70b5a45 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents: 5101
diff changeset
   221
	*c = '?';
dc67d70b5a45 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents: 5101
diff changeset
   222
	return 1;
dc67d70b5a45 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents: 5101
diff changeset
   223
}
dc67d70b5a45 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents: 5101
diff changeset
   224
dc67d70b5a45 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents: 5101
diff changeset
   225
dc67d70b5a45 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents: 5101
diff changeset
   226
/* Encode a unicode character and place it in the buffer
dc67d70b5a45 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents: 5101
diff changeset
   227
 * @param buf Buffer to place character.
dc67d70b5a45 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents: 5101
diff changeset
   228
 * @param c   Unicode character to encode.
dc67d70b5a45 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents: 5101
diff changeset
   229
 * @return Number of characters in the encoded sequence.
dc67d70b5a45 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents: 5101
diff changeset
   230
 */
dc67d70b5a45 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents: 5101
diff changeset
   231
size_t Utf8Encode(char *buf, WChar c)
dc67d70b5a45 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents: 5101
diff changeset
   232
{
dc67d70b5a45 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents: 5101
diff changeset
   233
	if (c < 0x80) {
dc67d70b5a45 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents: 5101
diff changeset
   234
		*buf = c;
dc67d70b5a45 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents: 5101
diff changeset
   235
		return 1;
dc67d70b5a45 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents: 5101
diff changeset
   236
	} else if (c < 0x800) {
dc67d70b5a45 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents: 5101
diff changeset
   237
		*buf++ = 0xC0 + GB(c,  6, 5);
dc67d70b5a45 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents: 5101
diff changeset
   238
		*buf   = 0x80 + GB(c,  0, 6);
dc67d70b5a45 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents: 5101
diff changeset
   239
		return 2;
dc67d70b5a45 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents: 5101
diff changeset
   240
	} else if (c < 0x10000) {
dc67d70b5a45 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents: 5101
diff changeset
   241
		*buf++ = 0xE0 + GB(c, 12, 4);
dc67d70b5a45 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents: 5101
diff changeset
   242
		*buf++ = 0x80 + GB(c,  6, 6);
dc67d70b5a45 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents: 5101
diff changeset
   243
		*buf   = 0x80 + GB(c,  0, 6);
dc67d70b5a45 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents: 5101
diff changeset
   244
		return 3;
dc67d70b5a45 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents: 5101
diff changeset
   245
	} else if (c < 0x110000) {
dc67d70b5a45 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents: 5101
diff changeset
   246
		*buf++ = 0xF0 + GB(c, 18, 3);
dc67d70b5a45 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents: 5101
diff changeset
   247
		*buf++ = 0x80 + GB(c, 12, 6);
dc67d70b5a45 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents: 5101
diff changeset
   248
		*buf++ = 0x80 + GB(c,  6, 6);
dc67d70b5a45 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents: 5101
diff changeset
   249
		*buf   = 0x80 + GB(c,  0, 6);
dc67d70b5a45 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents: 5101
diff changeset
   250
		return 4;
dc67d70b5a45 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents: 5101
diff changeset
   251
	}
dc67d70b5a45 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents: 5101
diff changeset
   252
dc67d70b5a45 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents: 5101
diff changeset
   253
	//DEBUG(misc, 1) ("Can't UTF-8 encode value 0x%X", c);
dc67d70b5a45 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents: 5101
diff changeset
   254
	*buf = '?';
dc67d70b5a45 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents: 5101
diff changeset
   255
	return 1;
dc67d70b5a45 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents: 5101
diff changeset
   256
}