Add the ability to print unicode characters in addition to "Latin1" ones
This commit is contained in:
81
src/unicode.c
Normal file
81
src/unicode.c
Normal file
@@ -0,0 +1,81 @@
|
||||
/* vim:expandtab:ts=2 sw=2:
|
||||
*/
|
||||
/* Grafx2 - The Ultimate 256-color bitmap paint program
|
||||
|
||||
Copyright 2018 Thomas Bernard
|
||||
Copyright 1996-2001 Sunset Design (Guillaume Dorme & Karl Maritaud)
|
||||
|
||||
Grafx2 is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU General Public License
|
||||
as published by the Free Software Foundation; version 2
|
||||
of the License.
|
||||
|
||||
Grafx2 is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with Grafx2; if not, see <http://www.gnu.org/licenses/>
|
||||
*/
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "unicode.h"
|
||||
|
||||
size_t Unicode_strlen(const word * str)
|
||||
{
|
||||
size_t len;
|
||||
|
||||
len = 0;
|
||||
while(str[len] != 0)
|
||||
len++;
|
||||
return len;
|
||||
}
|
||||
|
||||
/// equivalent of strdup() for our Unicode strings
|
||||
word * Unicode_strdup(const word * str)
|
||||
{
|
||||
size_t byte_size;
|
||||
word * new_str;
|
||||
|
||||
byte_size = Unicode_strlen(str) * 2 + 2;
|
||||
new_str = malloc(byte_size);
|
||||
if (new_str != NULL)
|
||||
memcpy(new_str, str, byte_size);
|
||||
return new_str;
|
||||
}
|
||||
|
||||
/// Compare an unicode string with a regular Latin1 string
|
||||
int Unicode_char_strcmp(const word * s1, const char * s2)
|
||||
{
|
||||
const byte * str2 = (const byte *)s2;
|
||||
|
||||
while (*s1 == *str2)
|
||||
{
|
||||
if (*s1 == 0) return 0;
|
||||
s1++;
|
||||
str2++;
|
||||
}
|
||||
return (*s1 > *str2) ? 1 : -1;
|
||||
}
|
||||
|
||||
/// Copy a regular Latin1 string to an unicode string
|
||||
void Unicode_char_strlcpy(word * dst, const char * src, size_t len)
|
||||
{
|
||||
const byte * s = (const byte *)src;
|
||||
|
||||
if (len == 0)
|
||||
return;
|
||||
while (len > 1)
|
||||
{
|
||||
*dst = *s;
|
||||
if (*s == '\0')
|
||||
return;
|
||||
dst++;
|
||||
s++;
|
||||
len--;
|
||||
}
|
||||
*dst = 0;
|
||||
}
|
||||
Reference in New Issue
Block a user