]>
code.delx.au - refind/blob - filesystems/mk_fsw_strfunc.py
4 # Copyright (c) 2006 Christoph Pfisterer
7 # Redistribution and use in source and binary forms, with or without
8 # modification, are permitted provided that the following conditions are
11 # * Redistributions of source code must retain the above copyright
12 # notice, this list of conditions and the following disclaimer.
14 # * Redistributions in binary form must reproduce the above copyright
15 # notice, this list of conditions and the following disclaimer in the
16 # documentation and/or other materials provided with the
19 # * Neither the name of Christoph Pfisterer nor the names of the
20 # contributors may be used to endorse or promote products derived
21 # from this software without specific prior written permission.
23 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
46 'UTF16_SWAPPED': 'fsw_u16',
49 'ISO88591': 'VARC = *VARP++;',
50 'UTF8': """VARC = *VARP++;
51 if ((VARC & 0xe0) == 0xc0) {
52 VARC = ((VARC & 0x1f) << 6) | (*VARP++ & 0x3f);
53 } else if ((VARC & 0xf0) == 0xe0) {
54 VARC = ((VARC & 0x0f) << 12) | ((*VARP++ & 0x3f) << 6);
55 VARC |= (*VARP++ & 0x3f);
56 } else if ((VARC & 0xf8) == 0xf0) {
57 VARC = ((VARC & 0x07) << 18) | ((*VARP++ & 0x3f) << 12);
58 VARC |= ((*VARP++ & 0x3f) << 6);
59 VARC |= (*VARP++ & 0x3f);
61 'UTF16': 'VARC = *VARP++;',
62 'UTF16_SWAPPED': 'VARC = *VARP++; VARC = FSW_SWAPVALUE_U16(VARC);',
65 combos
= ( ('ISO88591', 'UTF8'), ('ISO88591', 'UTF16'), ('ISO88591', 'UTF16_SWAPPED'),
66 ('UTF8', 'UTF16'), ('UTF8', 'UTF16_SWAPPED'),
67 ('UTF16', 'UTF16_SWAPPED') )
71 coerce_combos
.setdefault(combo
[0], []).append(combo
[1])
72 coerce_combos
.setdefault(combo
[1], []).append(combo
[0])
76 output
= """/* fsw_strfunc.h generated by mk_fsw_strfunc.py */
79 # generate streq functions (symmetric)
85 getnext1
= getnext
[enc1
].replace('VARC', 'c1').replace('VARP', 'p1').replace("\n", "\n ")
86 getnext2
= getnext
[enc2
].replace('VARC', 'c2').replace('VARP', 'p2').replace("\n", "\n ")
89 static int fsw_streq_%(enc1)s_%(enc2)s(void *s1data, void *s2data, int len)
92 %(type1)s *p1 = (%(type1)s *)s1data;
93 %(type2)s *p2 = (%(type2)s *)s2data;
96 for (i = 0; i < len; i++) {
106 # generate strcoerce functions (asymmetric, destination-specific)
108 for enc2
in ('ISO88591', 'UTF16'):
109 for enc1
in coerce_combos
[enc2
]:
112 getnext1
= getnext
[enc1
].replace('VARC', 'c').replace('VARP', 'sp').replace("\n", "\n ")
114 static fsw_status_t fsw_strcoerce_%(enc1)s_%(enc2)s(void *srcdata, int srclen, struct fsw_string *dest)
122 dest->type = FSW_STRING_TYPE_%(enc2)s;
124 dest->size = srclen * sizeof(%(type2)s);
125 status = fsw_alloc(dest->size, &dest->data);
129 sp = (%(type1)s *)srcdata;
130 dp = (%(type2)s *)dest->data;
131 for (i = 0; i < srclen; i++) {
139 for enc2
in ('UTF8',):
140 for enc1
in coerce_combos
[enc2
]:
143 getnext1
= getnext
[enc1
].replace('VARC', 'c').replace('VARP', 'sp').replace("\n", "\n ")
145 static fsw_status_t fsw_strcoerce_%(enc1)s_%(enc2)s(void *srcdata, int srclen, struct fsw_string *dest)
153 sp = (%(type1)s *)srcdata;
155 for (i = 0; i < srclen; i++) {
160 else if (c < 0x000800)
162 else if (c < 0x010000)
168 dest->type = FSW_STRING_TYPE_%(enc2)s;
170 dest->size = destsize;
171 status = fsw_alloc(dest->size, &dest->data);
175 sp = (%(type1)s *)srcdata;
176 dp = (%(type2)s *)dest->data;
177 for (i = 0; i < srclen; i++) {
182 } else if (c < 0x000800) {
183 *dp++ = 0xc0 | ((c >> 6) & 0x1f);
184 *dp++ = 0x80 | (c & 0x3f);
185 } else if (c < 0x010000) {
186 *dp++ = 0xe0 | ((c >> 12) & 0x0f);
187 *dp++ = 0x80 | ((c >> 6) & 0x3f);
188 *dp++ = 0x80 | (c & 0x3f);
190 *dp++ = 0xf0 | ((c >> 18) & 0x07);
191 *dp++ = 0x80 | ((c >> 12) & 0x3f);
192 *dp++ = 0x80 | ((c >> 6) & 0x3f);
193 *dp++ = 0x80 | (c & 0x3f);
200 # coerce functions with destination UFT16_SWAPPED missing by design
204 f
= file("fsw_strfunc.h", "w")