1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
|
/*
* Copyright 1998, Gisle Aas.
*
* This library is free software; you can redistribute it and/or
* modify it under the same terms as Perl itself.
*/
#ifdef __cplusplus
extern "C" {
#endif
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
#ifdef __cplusplus
}
#endif
#include "patchlevel.h"
#if PATCHLEVEL <= 4 && !defined(PL_dowarn)
#define PL_dowarn dowarn
#endif
#include "map8.h"
/* Some renaming that helps avoiding name class with the Perl versions
* of the constructors
*/
#define map8__new map8_new
#define map8__new_txtfile map8_new_txtfile
#define map8__new_binfile map8_new_binfile
/* Callbacks are always on and will invoke methods on the
* Unicode::Map8 object.
*/
static U16*
to16_cb(U8 u, Map8* m, STRLEN *len)
{
dSP;
int n;
SV* sv;
U16* buf;
STRLEN buflen;
PUSHMARK(sp);
XPUSHs(sv_2mortal(newRV_inc(m->obj)));
XPUSHs(sv_2mortal(newSViv(u)));
PUTBACK;
n = perl_call_method("unmapped_to16", G_SCALAR);
assert(n == 1);
SPAGAIN;
sv = POPs;
PUTBACK;
buf = (U16*)SvPV(sv, buflen);
*len = buflen / sizeof(U16);
return buf;
}
static U8*
to8_cb(U16 u, Map8* m, STRLEN *len)
{
dSP;
int n;
SV* sv;
PUSHMARK(sp);
XPUSHs(sv_2mortal(newRV_inc(m->obj)));
XPUSHs(sv_2mortal(newSViv(u)));
PUTBACK;
n = perl_call_method("unmapped_to8", G_SCALAR);
assert(n == 1);
SPAGAIN;
sv = POPs;
PUTBACK;
return SvPV(sv, *len);
}
/* We use '~' magic to attach the Map8* objects to Unicode::Map8
* objects. The pointer to the attached Map8* object is stored in
* the mg_obj fields of struct magic. The attached Map8* object
* is also automatically freed when the magic is freed.
*/
static int
map8_magic_free(pTHX_ SV* sv, MAGIC* mg)
{
map8_free((Map8*)mg->mg_obj);
return 1;
}
static MGVTBL magic_cleanup = { 0, 0, 0, 0, map8_magic_free };
static Map8*
find_map8(SV* obj)
{
MAGIC *m;
if (!sv_derived_from(obj, "Unicode::Map8"))
croak("Not an Unicode::Map8 object");
m = mg_find(SvRV(obj), '~');
if (!m) croak("No magic attached");
if (m->mg_len != 666) croak("Bad magic in ~-magic");
return (Map8*) m->mg_obj;
}
static void
attach_map8(SV* obj, Map8* map8)
{
SV* hv = SvRV(obj);
MAGIC *m;
sv_magic(hv, NULL, '~', 0, 666);
m = mg_find(hv, '~');
if (!m) croak("Can't find back ~ magic");
m->mg_virtual = &magic_cleanup;
m->mg_obj = (SV*)map8;
/* register callbacks */
map8->cb_to8 = to8_cb;
map8->cb_to16 = to16_cb;
map8->obj = (void*)hv; /* so callbacks can find the object again */
}
MODULE = Unicode::Map8 PACKAGE = Unicode::Map8 PREFIX=map8_
PROTOTYPES: DISABLE
Map8*
map8__new()
Map8*
map8__new_txtfile(filename)
char*filename
Map8*
map8__new_binfile(filename)
char*filename
void
map8_addpair(map, u8, u16)
Map8* map
U8 u8
U16 u16
U16
map8_default_to8(map,...)
Map8* map
ALIAS:
default_to16 = 1
CODE:
RETVAL = ix ? map8_get_def_to16(map) : map8_get_def_to8(map);
if (items > 1) {
if (ix)
map8_set_def_to16(map, SvIV(ST(1)));
else
map8_set_def_to8(map, SvIV(ST(1)));
}
OUTPUT:
RETVAL
void
map8_nostrict(map)
Map8* map
U16
MAP8_BINFILE_MAGIC_HI()
CODE:
RETVAL = MAP8_BINFILE_MAGIC_HI;
OUTPUT:
RETVAL
U16
MAP8_BINFILE_MAGIC_LO()
CODE:
RETVAL = MAP8_BINFILE_MAGIC_LO;
OUTPUT:
RETVAL
U16
NOCHAR()
CODE:
RETVAL = NOCHAR;
OUTPUT:
RETVAL
SV*
_empty_block(map, block)
Map8* map
U8 block
CODE:
if (block > 0xFF)
croak("Only 256 blocks exists");
RETVAL = boolSV(map8_empty_block(map, block));
OUTPUT:
RETVAL
U16
map8_to_char16(map, c)
Map8* map
U8 c
CODE:
RETVAL = ntohs(map8_to_char16(map, c));
OUTPUT:
RETVAL
U16
map8_to_char8(map, uc)
Map8* map
U16 uc
SV*
to8(map, str16)
Map8* map
PREINIT:
STRLEN len;
STRLEN origlen;
char* str;
char* cur;
INPUT:
U16* str16 = (U16*)SvPV(ST(1), len);
CODE:
if (PL_dowarn && (len % 2) != 0)
warn("Uneven length of wide string");
len /= 2;
origlen = len;
RETVAL = newSV(len + 1);
SvPOK_on(RETVAL);
str = SvPVX(RETVAL);
for (cur = str; len--; str16++) {
U16 c16 = ntohs(*str16);
U16 c = map8_to_char8(map, c16);
if (c != NOCHAR) {
*cur++ = (U8)c;
} else if (map->def_to8 != NOCHAR) {
*cur++ = (U8)map->def_to8;
} else if (map->cb_to8) {
U8* buf;
STRLEN blen;
buf = map->cb_to8(c16, map, &blen);
if (buf && blen > 0) {
if (blen == 1) {
*cur++ = *buf;
} else {
/* we might need to grow the string buffer.
* Find out the minimum requirement and a
* guess that avoids growing each time if
* several char map longer strings
*/
STRLEN curlen = cur - str;
STRLEN guess = origlen * (curlen + blen) /
(origlen - len);
STRLEN min = curlen + blen + len + 1;
if (guess < min)
guess = min;
else if (curlen <= 1 && guess > min*4)
guess = min*4;
str = SvGROW(RETVAL, guess);
cur = str + curlen;
while (blen--)
*cur++ = *buf++;
}
}
}
}
SvCUR_set(RETVAL, cur - str);
*cur = '\0';
OUTPUT:
RETVAL
SV*
to16(map, str8)
Map8* map
PREINIT:
STRLEN len;
STRLEN origlen;
U16* str;
U16* cur;
INPUT:
U8* str8 = SvPV(ST(1), len);
CODE:
origlen = len;
RETVAL = newSV(sizeof(U16)*len + 1);
SvPOK_on(RETVAL);
str = (U16*)SvPVX(RETVAL);
for (cur = str; len--; str8++) {
U16 c = map8_to_char16(map, *str8);
if (c != NOCHAR) {
*cur++ = c;
} else if (map->def_to16 != NOCHAR) {
*cur++ = map->def_to16;
} else if (map->cb_to16) {
U16* buf;
STRLEN blen;
buf = map->cb_to16(*str8, map, &blen);
if (buf && blen > 0) {
if (blen == 1) {
*cur++ = *buf;
} else {
/* we might need to grow the string buffer.
* Find out the minimum requirement and a
* guess that avoids growing each time if
* several char map longer strings
*/
STRLEN curlen = cur - str;
STRLEN guess = origlen * (curlen + blen) /
(origlen - len);
STRLEN min = curlen + blen + len + 1;
if (guess < min)
guess = min;
else if (curlen <= 1 && guess > min*4)
guess = min*4;
str = (U16*)SvGROW(RETVAL, sizeof(U16)*guess);
cur = str + curlen;
while (blen--)
*cur++ = *buf++;
}
}
}
}
SvCUR_set(RETVAL, (cur - str)*sizeof(U16));
*cur = '\0';
OUTPUT:
RETVAL
SV*
recode8(m1, m2, str)
Map8* m1
Map8* m2
PREINIT:
STRLEN len;
STRLEN rlen;
char* res;
INPUT:
char* str = SvPV(ST(2), len);
CODE:
RETVAL = newSV(len + 1);
SvPOK_on(RETVAL);
res = SvPVX(RETVAL);
map8_recode8(m1, m2, str, res, len, &rlen);
res[rlen] = '\0';
SvCUR_set(RETVAL, rlen);
OUTPUT:
RETVAL
|