1 /* vim:expandtab:ts=2 sw=2:
2 */
3 /*  Grafx2 - The Ultimate 256-color bitmap paint program
4 
5 	Copyright owned by various GrafX2 authors, see COPYRIGHT.txt for details.
6 
7     Grafx2 is free software; you can redistribute it and/or
8     modify it under the terms of the GNU General Public License
9     as published by the Free Software Foundation; version 2
10     of the License.
11 
12     Grafx2 is distributed in the hope that it will be useful,
13     but WITHOUT ANY WARRANTY; without even the implied warranty of
14     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15     GNU General Public License for more details.
16 
17     You should have received a copy of the GNU General Public License
18     along with Grafx2; if not, see <http://www.gnu.org/licenses/>
19 */
20 
21 #include <stdlib.h>
22 #include <string.h>
23 
24 #include "gfx2mem.h"
25 #include "unicode.h"
26 
Unicode_strlen(const word * str)27 size_t Unicode_strlen(const word * str)
28 {
29   size_t len;
30 
31   len = 0;
32   while(str[len] != 0)
33     len++;
34   return len;
35 }
36 
37 /// equivalent of strdup() for our Unicode strings
Unicode_strdup(const word * str)38 word * Unicode_strdup(const word * str)
39 {
40   size_t byte_size;
41   word * new_str;
42 
43   if (str == NULL)
44     return NULL;
45   byte_size = Unicode_strlen(str) * 2 + 2;
46   new_str = GFX2_malloc(byte_size);
47   if (new_str != NULL)
48     memcpy(new_str, str, byte_size);
49   return new_str;
50 }
51 
52 /// Copy unicode string
Unicode_strlcpy(word * dst,const word * src,size_t len)53 void Unicode_strlcpy(word * dst, const word * src, size_t len)
54 {
55   if (len == 0)
56     return;
57   while (len > 1)
58   {
59     *dst = *src;
60     if (*src == '\0')
61       return;
62     dst++;
63     src++;
64     len--;
65   }
66   *dst = 0;
67 }
68 
69 /// concatenate unicode string
Unicode_strlcat(word * dst,const word * src,size_t len)70 void Unicode_strlcat(word * dst, const word * src, size_t len)
71 {
72   size_t dst_len = Unicode_strlen(dst);
73   if (dst_len >= len)
74     return;
75   Unicode_strlcpy(dst + dst_len, src, len - dst_len);
76 }
77 
78 /// Compare two unicode strings
Unicode_strcmp(const word * s1,const word * s2)79 int Unicode_strcmp(const word * s1, const word * s2)
80 {
81   while (*s1 == *s2)
82   {
83     if (*s1 == 0) return 0;
84     s1++;
85     s2++;
86   }
87   return (*s1 > *s2) ? 1 : -1;
88 }
89 
90 /// Compare an unicode string with a regular Latin1 string
Unicode_char_strcmp(const word * s1,const char * s2)91 int Unicode_char_strcmp(const word * s1, const char * s2)
92 {
93   const byte * str2 = (const byte *)s2;
94 
95   while (*s1 == *str2)
96   {
97     if (*s1 == 0) return 0;
98     s1++;
99     str2++;
100   }
101   return (*s1 > *str2) ? 1 : -1;
102 }
103 
104 /// Compare an unicode string with a regular Latin1 string. Ignoring case
Unicode_char_strcasecmp(const word * s1,const char * s2)105 int Unicode_char_strcasecmp(const word * s1, const char * s2)
106 {
107   const byte * str2 = (const byte *)s2;
108   unsigned int c1, c2;
109 
110   for (;;)
111   {
112     c1 = *s1++;
113     c2 = *str2++;
114     // first convert to lower case
115     if ('a' <= c1 && c1 <= 'z')
116         c1 -= ('a'-'A');
117     if ('a' <= c2 && c2 <= 'z')
118         c2 -= ('a'-'A');
119     if (c1 != c2)
120       return (c1 > c2) ? 1 : -1;
121     if (c1 == 0)
122       return 0;
123   }
124 }
125 
126 /// Copy a regular Latin1 string to an unicode string
Unicode_char_strlcpy(word * dst,const char * src,size_t len)127 void Unicode_char_strlcpy(word * dst, const char * src, size_t len)
128 {
129   const byte * s = (const byte *)src;
130 
131   if (len == 0)
132     return;
133   while (len > 1)
134   {
135     *dst = *s;
136     if (*s == '\0')
137       return;
138     dst++;
139     s++;
140     len--;
141   }
142   *dst = 0;
143 }
144 
145 /// Append a regular Latin1 string to an unicode string
Unicode_char_strlcat(word * dst,const char * src,size_t len)146 void Unicode_char_strlcat(word * dst, const char * src, size_t len)
147 {
148   size_t dst_len = Unicode_strlen(dst);
149   if (dst_len >= len)
150     return;
151   Unicode_char_strlcpy(dst + dst_len, src, len - dst_len);
152 }
153