Files @ r5312:ffd375effb01
Branch filter:

Location: cpp/openttd-patchpack/source/string.h - annotation

Darkvater
(svn r7468) -Codechange: [win32] Add some comments to MB/WIDE_TO_WIDE/MB_[BUFFER] macros and
use them some more in win32 code. Also for the clipboard use the convert_from_fs
function instead of calling Win32 API directly. Make the static buffers in OTTD2FS
and FS2OTTD the same size (character-length wise)
r2186:5ee653b1b5e1
r2186:5ee653b1b5e1
r1317:3783af524858
r1317:3783af524858
r1317:3783af524858
r5108:28640689af54
r5108:28640689af54
r1317:3783af524858
r1317:3783af524858
r1317:3783af524858
r1317:3783af524858
r1317:3783af524858
r1317:3783af524858
r1317:3783af524858
r1317:3783af524858
r1317:3783af524858
r1317:3783af524858
r1317:3783af524858
r1317:3783af524858
r1317:3783af524858
r1317:3783af524858
r1317:3783af524858
r1317:3783af524858
r1317:3783af524858
r1317:3783af524858
r1317:3783af524858
r1317:3783af524858
r2234:6f59925a7314
r2234:6f59925a7314
r2775:3e6efcedc84d
r2775:3e6efcedc84d
r2775:3e6efcedc84d
r2775:3e6efcedc84d
r5101:0f0f27a2e4d3
r5101:0f0f27a2e4d3
r5101:0f0f27a2e4d3
r4300:642b2431578b
r5108:28640689af54
r4300:642b2431578b
r4300:642b2431578b
r4300:642b2431578b
r4300:642b2431578b
r4300:642b2431578b
r4300:642b2431578b
r4300:642b2431578b
r5164:61c960588665
r5108:28640689af54
r5108:28640689af54
r5108:28640689af54
r5108:28640689af54
r4300:642b2431578b
r4300:642b2431578b
r4300:642b2431578b
r4300:642b2431578b
r4300:642b2431578b
r4300:642b2431578b
r4300:642b2431578b
r5108:28640689af54
r5108:28640689af54
r5108:28640689af54
r5108:28640689af54
r5108:28640689af54
r5108:28640689af54
r5108:28640689af54
r5108:28640689af54
r5108:28640689af54
r5108:28640689af54
r5108:28640689af54
r5108:28640689af54
r5108:28640689af54
r4300:642b2431578b
r5108:28640689af54
r5108:28640689af54
r5108:28640689af54
r5108:28640689af54
r5108:28640689af54
r5108:28640689af54
r5108:28640689af54
r5108:28640689af54
r5108:28640689af54
r5108:28640689af54
r5108:28640689af54
r5108:28640689af54
r5108:28640689af54
r5108:28640689af54
r5108:28640689af54
r5108:28640689af54
r5108:28640689af54
r5108:28640689af54
r5108:28640689af54
r5108:28640689af54
r5108:28640689af54
r5108:28640689af54
r5108:28640689af54
r5108:28640689af54
r5108:28640689af54
r5108:28640689af54
r5108:28640689af54
r5108:28640689af54
r5108:28640689af54
r5108:28640689af54
r5108:28640689af54
r4200:4e5f339514ce
r2436:963efe8b84cc
/* $Id$ */

#ifndef STRING_H
#define STRING_H

#include "macros.h"

/*
 * dst: destination buffer
 * src: string to copy/concatenate
 * size: size of the destination buffer
 * usage: ttd_strlcpy(dst, src, lengthof(dst));
 */
void ttd_strlcat(char *dst, const char *src, size_t size);
void ttd_strlcpy(char *dst, const char *src, size_t size);

/*
 * dst: destination buffer
 * src: string to copy
 * last: pointer to the last element in the dst array
 *       if NULL no boundary check is performed
 * returns a pointer to the terminating \0 in the destination buffer
 * usage: strecpy(dst, src, lastof(dst));
 */
char* strecat(char* dst, const char* src, const char* last);
char* strecpy(char* dst, const char* src, const char* last);

char* CDECL str_fmt(const char* str, ...);

/** Scans the string for valid characters and if it finds invalid ones,
 * replaces them with a question mark '?' */
void str_validate(char *str);

/** Scans the string for colour codes and strips them */
void str_strip_colours(char *str);

/**
 * Valid filter types for IsValidChar.
 */
typedef enum CharSetFilter {
	CS_ALPHANUMERAL,      //! Both numeric and alphabetic and spaces and stuff
	CS_NUMERAL,           //! Only numeric ones
	CS_ALPHA,             //! Only alphabetic values
} CharSetFilter;

/** Convert the given string to lowercase, only works with ASCII! */
void strtolower(char *str);

typedef uint32 WChar;

/**
 * Only allow certain keys. You can define the filter to be used. This makes
 *  sure no invalid keys can get into an editbox, like BELL.
 * @param key character to be checked
 * @param afilter the filter to use
 * @return true or false depending if the character is printable/valid or not
 */
bool IsValidChar(WChar key, CharSetFilter afilter);

size_t Utf8Decode(WChar *c, const char *s);
size_t Utf8Encode(char *buf, WChar c);


static inline WChar Utf8Consume(const char **s)
{
	WChar c;
	*s += Utf8Decode(&c, *s);
	return c;
}


/** Return the length of a UTF-8 encoded character.
 * @param c Unicode character.
 * @return Length of UTF-8 encoding for character.
 */
static inline size_t Utf8CharLen(WChar c)
{
	if (c < 0x80)       return 1;
	if (c < 0x800)      return 2;
	if (c < 0x10000)    return 3;
	if (c < 0x110000)   return 4;

	/* Invalid valid, we encode as a '?' */
	return 1;
}


/* Check if the given character is part of a UTF8 sequence */
static inline bool IsUtf8Part(char c)
{
	return GB(c, 6, 2) == 2;
}


static inline bool IsPrintable(WChar c)
{
	if (c < 0x20)   return false;
	if (c < 0xE000) return true;
	if (c < 0xE200) return false;
	return true;
}


#endif /* STRING_H */