Toevoegen van de sources die ik vorige keer vergeten was, en nog wat
[projects/chimara/chimara.git] / src / case.c
1 #include <gtk/gtk.h>
2 #include "glk.h"
3
4 /**
5  * glk_char_to_lower:
6  * @ch: A Latin-1 character.
7  *
8  * If @ch is an uppercase character in the Latin-1 character set, converts it
9  * to lowercase. Otherwise, leaves it unchanged.
10  *
11  * Returns: A lowercase or non-letter Latin-1 character.
12  */
13 unsigned char
14 glk_char_to_lower(unsigned char ch)
15 {
16         if( (ch >= 0x41 && ch <= 0x5A) || (ch >= 0xC0 && ch <= 0xD6) || (ch >= 0xD8 && ch <= 0xDE) )
17                 return ch + 0x20;
18         return ch;
19 }
20
21 /**
22  * glk_char_to_upper:
23  * @ch: A Latin-1 character.
24  *
25  * If @ch is a lowercase character in the Latin-1 character set, converts it to
26  * uppercase. Otherwise, leaves it unchanged.
27  *
28  * Returns: An uppercase or non-letter Latin-1 character.
29  */
30 unsigned char
31 glk_char_to_upper(unsigned char ch)
32 {
33         if( (ch >= 0x61 && ch <= 0x7A) || (ch >= 0xE0 && ch <= 0xF6) || (ch >= 0xF8 && ch <= 0xFE) )
34                 return ch - 0x20;
35         return ch;
36 }
37
38 #ifdef GLK_MODULE_UNICODE
39
40 /**
41  * glk_buffer_to_lower_case_uni:
42  * @buf: A character array in UCS-4.
43  * @len: Available length of @buf.
44  * @numchars: Number of characters in @buf.
45  *
46  * Converts the first @numchars characters of @buf to their lowercase 
47  * equivalents, if there is such a thing. These functions provide two length
48  * arguments because a string of Unicode characters may expand when its case
49  * changes. The @len argument is the available length of the buffer; @numchars
50  * is the number of characters in the buffer initially. (So @numchars must be
51  * less than or equal to @len. The contents of the buffer after @numchars do
52  * not affect the operation.)
53  *
54  * Returns: The number of characters after conversion. If this is greater than
55  * @len, the characters in the array will be safely truncated at len, but the
56  * true count will be returned. (The contents of the buffer after the returned
57  * count are undefined.)
58  */
59 glui32
60 glk_buffer_to_lower_case_uni(glui32 *buf, glui32 len, glui32 numchars)
61 {
62         g_return_val_if_fail(buf != NULL && (len > 0 || numchars > 0), 0);
63         g_return_val_if_fail(numchars <= len, 0);
64         
65         /* GLib has a function that converts _one_ UCS-4 character to _one_
66         lowercase UCS-4 character; so apparently we don't have to worry about the
67         string length changing... */
68         glui32 *ptr;
69         for(ptr = buf; ptr < buf + numchars; ptr++)
70                 *ptr = g_unichar_tolower(*ptr);
71         
72         return numchars;
73 }
74
75 /**
76  * glk_buffer_to_upper_case_uni:
77  * @buf: A character array in UCS-4.
78  * @len: Available length of @buf.
79  * @numchars: Number of characters in @buf.
80  *
81  * Converts the first @numchars characters of @buf to their uppercase 
82  * equivalents, if there is such a thing. See glk_buffer_to_lower_case_uni().
83  *
84  * Returns: The number of characters after conversion.
85  */
86 glui32
87 glk_buffer_to_upper_case_uni(glui32 *buf, glui32 len, glui32 numchars)
88 {
89         g_return_val_if_fail(buf != NULL && (len > 0 || numchars > 0), 0);
90         g_return_val_if_fail(numchars <= len, 0);
91         
92         /* GLib has a function that converts _one_ UCS-4 character to _one_
93         uppercase UCS-4 character; so apparently we don't have to worry about the
94         string length changing... */
95         glui32 *ptr;
96         for(ptr = buf; ptr < buf + numchars; ptr++)
97                 *ptr = g_unichar_toupper(*ptr);
98         
99         return numchars;
100 }
101
102 /**
103  * glk_buffer_to_title_case_uni:
104  * @buf: A character array in UCS-4.
105  * @len: Available length of @buf.
106  * @numchars: Number of characters in @buf.
107  * @lowerrest: #TRUE if the rest of @buf should be lowercased, #FALSE 
108  * otherwise.
109  *
110  * Converts the first character of @buf to uppercase, if there is such a thing.
111  * See glk_buffer_to_lower_case_uni(). If @lowerrest is #TRUE, then the
112  * remainder of @buf is lowercased.
113  *
114  * Returns: The number of characters after conversion.
115  */
116 glui32
117 glk_buffer_to_title_case_uni(glui32 *buf, glui32 len, glui32 numchars, 
118                              glui32 lowerrest)
119 {
120         g_return_val_if_fail(buf != NULL && (len > 0 || numchars > 0), 0);
121         g_return_val_if_fail(numchars <= len, 0);
122         
123         /* GLib has a function that converts _one_ UCS-4 character to _one_
124         uppercase UCS-4 character; so apparently we don't have to worry about the
125         string length changing... */
126         *buf = g_unichar_totitle(*buf);
127         /* Call lowercase on the rest of the string */
128         if(lowerrest)
129                 return glk_buffer_to_lower_case_uni(buf + 1, len - 1, numchars - 1) +1;
130         return numchars;
131 }
132
133 #endif /* GLK_MODULE_UNICODE */