2005-07-24 14:12:37 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
2007-04-04 01:35:16 +00:00
|
|
|
/** @file string.h */
|
|
|
|
|
2005-02-06 13:41:02 +00:00
|
|
|
#ifndef STRING_H
|
|
|
|
#define STRING_H
|
|
|
|
|
2006-11-16 22:05:33 +00:00
|
|
|
#include "macros.h"
|
|
|
|
|
2007-04-04 01:35:16 +00:00
|
|
|
/**
|
|
|
|
* usage ttd_strlcpy(dst, src, lengthof(dst));
|
|
|
|
* @param dst destination buffer
|
|
|
|
* @param src string to copy/concatenate
|
|
|
|
* @param size size of the destination buffer
|
2005-02-06 13:41:02 +00:00
|
|
|
*/
|
|
|
|
void ttd_strlcat(char *dst, const char *src, size_t size);
|
|
|
|
void ttd_strlcpy(char *dst, const char *src, size_t size);
|
|
|
|
|
2007-04-04 01:35:16 +00:00
|
|
|
/**
|
2005-02-06 13:41:02 +00:00
|
|
|
* usage: strecpy(dst, src, lastof(dst));
|
2007-04-04 01:35:16 +00:00
|
|
|
* @param dst destination buffer
|
|
|
|
* @param src string to copy
|
|
|
|
* @param last pointer to the last element in the dst array
|
|
|
|
* if NULL no boundary check is performed
|
|
|
|
* @return a pointer to the terminating \0 in the destination buffer
|
2005-02-06 13:41:02 +00:00
|
|
|
*/
|
2007-07-24 17:01:23 +00:00
|
|
|
char *strecat(char *dst, const char *src, const char *last);
|
|
|
|
char *strecpy(char *dst, const char *src, const char *last);
|
2005-02-06 13:41:02 +00:00
|
|
|
|
2007-07-24 17:01:23 +00:00
|
|
|
char *CDECL str_fmt(const char *str, ...);
|
2005-07-29 21:52:20 +00:00
|
|
|
|
2005-12-20 20:52:05 +00:00
|
|
|
/** Scans the string for valid characters and if it finds invalid ones,
|
|
|
|
* replaces them with a question mark '?' */
|
|
|
|
void str_validate(char *str);
|
|
|
|
|
2006-11-16 17:41:24 +00:00
|
|
|
/** Scans the string for colour codes and strips them */
|
|
|
|
void str_strip_colours(char *str);
|
|
|
|
|
(svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
- New optional landscape generator (TerraGenesis Perlin)
- Load heightmaps (either BMP or PNG)
- Progress dialog while generating worlds (no longer a 'hanging' screen)
- New dialogs for NewGame, Create Scenario and Play Heightmap
- Easier to configure your landscape
- More things to configure (tree-placer, ..)
- Speedup of world generation
- New console command 'restart': restart the map EXACTLY as it was when you
first started it (needs a game made after or with this commit)
- New console command 'getseed': get the seed of your map and share it with
others (of course only works with generated maps)
- Many new, world generation related, things
- Many internal cleanups and rewrites
Many tnx to those people who helped making this:
Belugas, DaleStan, glx, KUDr, RichK67, Rubidium, and TrueLight (alfabetic)
Many tnx to those who helped testing:
Arnau, Bjarni, and tokai (alfabetic)
And to all other people who helped testing and sending comments / bugs
Stats: 673 lines changed, 3534 new lines, 79 new strings
2006-08-19 10:00:30 +00:00
|
|
|
/**
|
2006-11-16 22:05:33 +00:00
|
|
|
* Valid filter types for IsValidChar.
|
(svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
- New optional landscape generator (TerraGenesis Perlin)
- Load heightmaps (either BMP or PNG)
- Progress dialog while generating worlds (no longer a 'hanging' screen)
- New dialogs for NewGame, Create Scenario and Play Heightmap
- Easier to configure your landscape
- More things to configure (tree-placer, ..)
- Speedup of world generation
- New console command 'restart': restart the map EXACTLY as it was when you
first started it (needs a game made after or with this commit)
- New console command 'getseed': get the seed of your map and share it with
others (of course only works with generated maps)
- Many new, world generation related, things
- Many internal cleanups and rewrites
Many tnx to those people who helped making this:
Belugas, DaleStan, glx, KUDr, RichK67, Rubidium, and TrueLight (alfabetic)
Many tnx to those who helped testing:
Arnau, Bjarni, and tokai (alfabetic)
And to all other people who helped testing and sending comments / bugs
Stats: 673 lines changed, 3534 new lines, 79 new strings
2006-08-19 10:00:30 +00:00
|
|
|
*/
|
2007-03-07 12:11:48 +00:00
|
|
|
enum CharSetFilter {
|
2007-04-04 01:35:16 +00:00
|
|
|
CS_ALPHANUMERAL, ///< Both numeric and alphabetic and spaces and stuff
|
|
|
|
CS_NUMERAL, ///< Only numeric ones
|
|
|
|
CS_ALPHA, ///< Only alphabetic values
|
2007-03-07 12:11:48 +00:00
|
|
|
};
|
2006-08-19 09:31:22 +00:00
|
|
|
|
2006-11-28 14:12:09 +00:00
|
|
|
/** Convert the given string to lowercase, only works with ASCII! */
|
2006-11-16 22:05:33 +00:00
|
|
|
void strtolower(char *str);
|
|
|
|
|
2006-12-10 21:39:38 +00:00
|
|
|
|
2007-08-31 20:50:18 +00:00
|
|
|
static inline bool StrEmpty(const char *s) { return s == NULL || s[0] == '\0'; }
|
2007-01-13 15:00:16 +00:00
|
|
|
|
|
|
|
|
2006-12-10 21:39:38 +00:00
|
|
|
/** Get the length of a string, within a limited buffer */
|
|
|
|
static inline int ttd_strnlen(const char *str, int maxlen)
|
|
|
|
{
|
|
|
|
const char *t;
|
|
|
|
for (t = str; *t != '\0' && t - str < maxlen; t++);
|
|
|
|
return t - str;
|
|
|
|
}
|
|
|
|
|
2007-01-13 13:47:57 +00:00
|
|
|
/** Convert the md5sum number to a 'hexadecimal' string, return next pos in buffer */
|
|
|
|
char *md5sumToString(char *buf, const char *last, const uint8 md5sum[16]);
|
2006-12-10 21:39:38 +00:00
|
|
|
|
2006-11-16 22:05:33 +00:00
|
|
|
typedef uint32 WChar;
|
|
|
|
|
(svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
- New optional landscape generator (TerraGenesis Perlin)
- Load heightmaps (either BMP or PNG)
- Progress dialog while generating worlds (no longer a 'hanging' screen)
- New dialogs for NewGame, Create Scenario and Play Heightmap
- Easier to configure your landscape
- More things to configure (tree-placer, ..)
- Speedup of world generation
- New console command 'restart': restart the map EXACTLY as it was when you
first started it (needs a game made after or with this commit)
- New console command 'getseed': get the seed of your map and share it with
others (of course only works with generated maps)
- Many new, world generation related, things
- Many internal cleanups and rewrites
Many tnx to those people who helped making this:
Belugas, DaleStan, glx, KUDr, RichK67, Rubidium, and TrueLight (alfabetic)
Many tnx to those who helped testing:
Arnau, Bjarni, and tokai (alfabetic)
And to all other people who helped testing and sending comments / bugs
Stats: 673 lines changed, 3534 new lines, 79 new strings
2006-08-19 10:00:30 +00:00
|
|
|
/**
|
|
|
|
* 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.
|
2005-12-20 20:52:05 +00:00
|
|
|
* @param key character to be checked
|
(svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
- New optional landscape generator (TerraGenesis Perlin)
- Load heightmaps (either BMP or PNG)
- Progress dialog while generating worlds (no longer a 'hanging' screen)
- New dialogs for NewGame, Create Scenario and Play Heightmap
- Easier to configure your landscape
- More things to configure (tree-placer, ..)
- Speedup of world generation
- New console command 'restart': restart the map EXACTLY as it was when you
first started it (needs a game made after or with this commit)
- New console command 'getseed': get the seed of your map and share it with
others (of course only works with generated maps)
- Many new, world generation related, things
- Many internal cleanups and rewrites
Many tnx to those people who helped making this:
Belugas, DaleStan, glx, KUDr, RichK67, Rubidium, and TrueLight (alfabetic)
Many tnx to those who helped testing:
Arnau, Bjarni, and tokai (alfabetic)
And to all other people who helped testing and sending comments / bugs
Stats: 673 lines changed, 3534 new lines, 79 new strings
2006-08-19 10:00:30 +00:00
|
|
|
* @param afilter the filter to use
|
|
|
|
* @return true or false depending if the character is printable/valid or not
|
|
|
|
*/
|
2006-11-16 22:05:33 +00:00
|
|
|
bool IsValidChar(WChar key, CharSetFilter afilter);
|
|
|
|
|
|
|
|
size_t Utf8Decode(WChar *c, const char *s);
|
|
|
|
size_t Utf8Encode(char *buf, WChar c);
|
2007-03-05 00:45:56 +00:00
|
|
|
size_t Utf8TrimString(char *s, size_t maxlen);
|
2006-11-16 22:05:33 +00:00
|
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-03-05 00:45:56 +00:00
|
|
|
/**
|
|
|
|
* Return the length of an UTF-8 encoded value based on a single char. This
|
|
|
|
* char should be the first byte of the UTF-8 encoding. If not, or encoding
|
|
|
|
* is invalid, return value is 0
|
2007-04-04 01:35:16 +00:00
|
|
|
* @param c char to query length of
|
|
|
|
* @return requested size
|
2007-03-05 00:45:56 +00:00
|
|
|
*/
|
|
|
|
static inline size_t Utf8EncodedCharLen(char c)
|
|
|
|
{
|
|
|
|
if (GB(c, 3, 5) == 0x1E) return 4;
|
|
|
|
if (GB(c, 4, 4) == 0x0E) return 3;
|
|
|
|
if (GB(c, 5, 3) == 0x06) return 2;
|
|
|
|
if (GB(c, 7, 1) == 0x00) return 1;
|
|
|
|
|
|
|
|
/* Invalid UTF8 start encoding */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-11-16 22:05:33 +00:00
|
|
|
/* Check if the given character is part of a UTF8 sequence */
|
|
|
|
static inline bool IsUtf8Part(char c)
|
|
|
|
{
|
|
|
|
return GB(c, 6, 2) == 2;
|
|
|
|
}
|
|
|
|
|
2007-03-04 00:49:40 +00:00
|
|
|
/**
|
2007-03-05 00:34:48 +00:00
|
|
|
* Retrieve the previous UNICODE character in an UTF-8 encoded string.
|
|
|
|
* @param s char pointer pointing to (the first char of) the next character
|
2007-04-04 01:35:16 +00:00
|
|
|
* @return a pointer in 's' to the previous UNICODE character's first byte
|
2007-03-05 00:34:48 +00:00
|
|
|
* @note The function should not be used to determine the length of the previous
|
|
|
|
* encoded char because it might be an invalid/corrupt start-sequence
|
2007-03-04 00:49:40 +00:00
|
|
|
*/
|
2007-03-05 00:34:48 +00:00
|
|
|
static inline char *Utf8PrevChar(const char *s)
|
2007-03-04 00:49:40 +00:00
|
|
|
{
|
2007-03-05 00:34:48 +00:00
|
|
|
const char *ret = s;
|
|
|
|
while (IsUtf8Part(*--ret));
|
|
|
|
return (char*)ret;
|
2007-03-04 00:49:40 +00:00
|
|
|
}
|
|
|
|
|
2006-11-16 22:05:33 +00:00
|
|
|
|
|
|
|
static inline bool IsPrintable(WChar c)
|
|
|
|
{
|
|
|
|
if (c < 0x20) return false;
|
|
|
|
if (c < 0xE000) return true;
|
|
|
|
if (c < 0xE200) return false;
|
|
|
|
return true;
|
|
|
|
}
|
2005-12-20 20:52:05 +00:00
|
|
|
|
2007-03-05 00:45:56 +00:00
|
|
|
/**
|
|
|
|
* Check whether UNICODE character is whitespace or not
|
|
|
|
* @param c UNICODE character to check
|
|
|
|
* @return a boolean value whether 'c' is a whitespace character or not
|
|
|
|
* @see http://www.fileformat.info/info/unicode/category/Zs/list.htm
|
|
|
|
*/
|
|
|
|
static inline bool IsWhitespace(WChar c)
|
|
|
|
{
|
|
|
|
return
|
|
|
|
c == 0x0020 /* SPACE */ ||
|
|
|
|
c == 0x00A0 /* NO-BREAK SPACE */ ||
|
|
|
|
c == 0x3000 /* IDEOGRAPHIC SPACE */
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
(svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
- New optional landscape generator (TerraGenesis Perlin)
- Load heightmaps (either BMP or PNG)
- Progress dialog while generating worlds (no longer a 'hanging' screen)
- New dialogs for NewGame, Create Scenario and Play Heightmap
- Easier to configure your landscape
- More things to configure (tree-placer, ..)
- Speedup of world generation
- New console command 'restart': restart the map EXACTLY as it was when you
first started it (needs a game made after or with this commit)
- New console command 'getseed': get the seed of your map and share it with
others (of course only works with generated maps)
- Many new, world generation related, things
- Many internal cleanups and rewrites
Many tnx to those people who helped making this:
Belugas, DaleStan, glx, KUDr, RichK67, Rubidium, and TrueLight (alfabetic)
Many tnx to those who helped testing:
Arnau, Bjarni, and tokai (alfabetic)
And to all other people who helped testing and sending comments / bugs
Stats: 673 lines changed, 3534 new lines, 79 new strings
2006-08-19 10:00:30 +00:00
|
|
|
|
2005-09-18 20:56:44 +00:00
|
|
|
#endif /* STRING_H */
|