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 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861
|
/* Part of SWI-Prolog
Author: Jan Wielemaker
E-mail: J.Wielemaker@vu.nl
WWW: http://www.swi-prolog.org
Copyright (c) 2000-2014, University of Amsterdam
VU University Amsterdam
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
#define _ISOC99_SOURCE 1 /* fwprintf(), etc prototypes */
#include <config.h>
#define UTIL_H_IMPLEMENTATION
#include "util.h"
#include <ctype.h>
#include <wctype.h>
#include <stdlib.h>
#ifdef HAVE_MALLOC_H
#include <malloc.h>
#endif
#include <stdio.h>
#include <string.h>
#include <errno.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_IO_H
#include <io.h>
#endif
#include <sys/stat.h>
#include <fcntl.h>
#include <assert.h>
#include "utf8.h"
size_t
istrlen(const ichar *s)
{ size_t len =0;
while(*s++)
len++;
return len;
}
ichar *
istrdup(const ichar *s)
{ if ( s )
{ ichar *dup = sgml_malloc((istrlen(s)+1)*sizeof(ichar));
ichar *d = dup;
while(*s)
*d++ = *s++;
*d = 0;
return dup;
} else
{ return NULL;
}
}
ichar *
istrndup(const ichar *s, int len)
{ ichar *dup = sgml_malloc((len+1)*sizeof(ichar));
ichar *d = dup;
while(--len >= 0)
*d++ = *s++;
*d = 0;
return dup;
}
ichar *
istrcpy(ichar *d, const ichar *s)
{ ichar *r = d;
while(*s)
*d++ = *s++;
*d = 0;
return r;
}
ichar *
istrcat(ichar *d, const ichar *s)
{ ichar *r = d;
d += istrlen(d);
istrcpy(d, s);
return r;
}
ichar *
istrncpy(ichar *d, const ichar *s, size_t len)
{ ichar *r = d;
while(*s && len-- > 0)
*d++ = *s++;
return r;
}
int
istrcaseeq(const ichar *s1, const ichar *s2)
{ ichar c;
while ((c = *s1++) != '\0')
{ if (towlower(*s2++) != towlower(c))
return FALSE;
}
return *s2 == '\0';
}
int
istreq(const ichar *s1, const ichar *s2)
{ while(*s1 && *s1 == *s2)
s1++, s2++;
if ( *s1 == 0 && *s2 == 0 )
return TRUE;
return FALSE;
}
int
istrncaseeq(const ichar *s1, const ichar *s2, int len)
{ while(--len >= 0 && towlower(*s1) == towlower(*s2))
s1++, s2++;
if ( len < 0 )
return TRUE;
return FALSE;
}
int
istrprefix(const ichar *pref, const ichar *s)
{ while(*pref && *pref == *s)
pref++, s++;
if ( *pref == 0 )
return TRUE;
return FALSE;
}
ichar *
istrchr(const ichar *s, int c)
{ for( ; *s; s++ )
{ if ( c == *s )
return (ichar *)s;
}
return NULL;
}
ichar *
istrupper(ichar *s)
{ ichar *r = s;
for( ; *s; s++)
*s = toupper(*s);
return r;
}
ichar *
istrlower(ichar *s)
{ ichar *r = s;
for( ; *s; s++)
*s = towlower(*s);
return r;
}
int
istrhash(const ichar *t, int tsize)
{ unsigned int value = 0;
unsigned int shift = 5;
while(*t)
{ unsigned int c = *t++;
c -= 'a';
value ^= c << (shift & 0xf);
shift ^= c;
}
value = value ^ (value >> 16);
return value % tsize;
}
int
istrcasehash(const ichar *t, int tsize)
{ unsigned int value = 0;
unsigned int shift = 5;
while(*t)
{ unsigned int c = towlower(*t++); /* case insensitive */
c -= 'a';
value ^= c << (shift & 0xf);
shift ^= c;
}
value = value ^ (value >> 16);
return value % tsize;
}
int
istrtol(const ichar *s, long *val)
{ long v;
ichar *e;
if ( *s )
{ v = wcstol(s, &e, 10);
if ( !e[0] && errno != ERANGE )
{ *val = v;
return TRUE;
}
}
return FALSE;
}
/*******************************
* INPUT CHARACTER BUFFER *
*******************************/
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Input character buffer is used to collect data between SGML markup, such
as <...>
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
icharbuf *
new_icharbuf(size_t limit)
{ icharbuf *buf = sgml_malloc(sizeof(*buf));
buf->allocated = 0;
buf->size = 0;
buf->limit = limit;
buf->limit_reached = FALSE;
buf->data = NULL;
return buf;
}
void
free_icharbuf(icharbuf *buf)
{ if ( buf->data )
sgml_free(buf->data);
sgml_free(buf);
}
void
__add_icharbuf(icharbuf *buf, int chr)
{ if ( buf->size == buf->allocated )
{ size_t sz = (buf->allocated ? buf->allocated*2 : 128);
if ( buf->limit && sz*sizeof(ichar) > buf->limit )
{ buf->limit_reached = 1;
return;
}
buf->allocated = sz;
if ( buf->data )
buf->data = sgml_realloc(buf->data, buf->allocated*sizeof(ichar));
else
buf->data = sgml_malloc(buf->allocated*sizeof(ichar));
}
buf->data[buf->size++] = chr;
}
void
del_icharbuf(icharbuf *buf)
{ if ( buf->size > 0 )
buf->size--;
}
void
terminate_icharbuf(icharbuf *buf)
{ add_icharbuf(buf, '\0');
buf->size--;
}
void
empty_icharbuf(icharbuf *buf)
{ buf->size = 0;
}
/*******************************
* OUTPUT CHARACTER BUFFER *
*******************************/
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Output character buffer deals with two representations: ISO Latin-1 and
UCS. It starts life as ISO Latin-1 and is upgraded to UCS as the first
character that doesn't fit ISO Latin-1 is added to the buffer.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
ocharbuf *
init_ocharbuf(ocharbuf *buf, size_t limit)
{ buf->size = 0;
buf->allocated = sizeof(buf->localbuf)/sizeof(wchar_t);
buf->limit = limit;
buf->limit_reached = FALSE;
buf->data.w = buf->localbuf;
return buf;
}
ocharbuf *
new_ocharbuf(size_t limit)
{ ocharbuf *buf = sgml_malloc(sizeof(*buf));
return init_ocharbuf(buf, limit);
}
void
discard_ocharbuf(ocharbuf *buf)
{ if ( buf->data.w && buf->data.w != buf->localbuf )
sgml_free(buf->data.w);
}
void
free_ocharbuf(ocharbuf *buf)
{ discard_ocharbuf(buf);
sgml_free(buf);
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Make sure the data of the buffer is malloc'ed and nul-terminated.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
ocharbuf *
malloc_ocharbuf(ocharbuf *buf)
{ if ( buf->data.w == buf->localbuf )
{ int bytes = (buf->size+1) * sizeof(wchar_t);
buf->data.w = sgml_malloc(bytes);
memcpy(buf->data.w, buf->localbuf, bytes);
buf->data.w[buf->size] = 0;
} else
terminate_ocharbuf(buf);
return buf;
}
void
add_ocharbuf(ocharbuf *buf, int chr)
{ size_t needed = 1;
#if SIZEOF_WCHAR_T == 2
if ( chr > 0xffff )
needed = 2;
#endif
if ( buf->size+needed > buf->allocated )
{ size_t sz = buf->allocated * 2;
if ( buf->limit && sz*sizeof(wchar_t) > buf->limit )
{ buf->limit_reached = TRUE;
return;
}
buf->allocated = sz;
if ( buf->data.w != (wchar_t*)buf->localbuf )
{ buf->data.w = sgml_realloc(buf->data.w, buf->allocated*sizeof(wchar_t));
} else
{ buf->data.w = sgml_malloc(buf->allocated*sizeof(wchar_t));
memcpy(buf->data.w, buf->localbuf, sizeof(buf->localbuf));
}
}
put_wchar(&buf->data.w[buf->size], chr);
buf->size += needed;
}
void
del_ocharbuf(ocharbuf *buf)
{ if ( buf->size > 0 )
{ int c;
const wchar_t *s = get_wchar_r(&buf->data.w[buf->size], &c);
(void) c;
buf->size = s - buf->data.w;
}
}
void
terminate_ocharbuf(ocharbuf *buf)
{ add_ocharbuf(buf, '\0');
buf->size--;
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
empty_ocharbuf() frees the associated buffer after a big lump has been
in it. Otherwise it simply sets the size to 0.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
void
empty_ocharbuf(ocharbuf *buf)
{ buf->size = 0;
if ( buf->allocated > 8192 )
{ assert(buf->data.w != buf->localbuf);
sgml_free(buf->data.w);
buf->allocated = sizeof(buf->localbuf)/sizeof(wchar_t);
buf->data.w = buf->localbuf;
}
}
/*******************************
* BUFFER RING *
*******************************/
#define RINGSIZE 16
typedef struct ring
{ void *ring[RINGSIZE];
int ringp;
} ring;
#ifdef _REENTRANT
#include <pthread.h>
static pthread_key_t ring_key;
static void
free_ring(void *ptr)
{ ring *r = ptr;
int i;
void **bp;
for(i=0, bp=r->ring; i<RINGSIZE; i++, bp++)
{ if ( *bp )
{ sgml_free(*bp);
*bp = NULL;
}
}
sgml_free(r);
}
static ring *
my_ring(void)
{ ring *r;
if ( (r=pthread_getspecific(ring_key)) )
return r;
if ( (r = sgml_calloc(1, sizeof(*r))) )
pthread_setspecific(ring_key, r);
return r;
}
void
init_ring(void)
{ pthread_key_create(&ring_key, free_ring);
}
void
stop_ring(void)
{ pthread_key_delete(ring_key);
}
#else
static ring ring_store;
#define my_ring() (&ring_store)
void init_ring(void) {}
void stop_ring(void) {}
#endif
wchar_t *
str2ring(const wchar_t *in)
{ ring *r;
wchar_t *copy;
if ( !(r=my_ring()) ||
!(copy = sgml_malloc((wcslen(in)+1)*sizeof(wchar_t))) )
{ sgml_nomem();
return NULL;
}
wcscpy(copy, in);
if ( r->ring[r->ringp] )
sgml_free(r->ring[r->ringp]);
r->ring[r->ringp++] = copy;
if ( r->ringp == RINGSIZE )
r->ringp = 0;
return copy;
}
void *
ringallo(size_t size)
{ ring *r;
char *result;
if ( !(r=my_ring()) || !(result = sgml_malloc(size)) )
{ sgml_nomem();
return NULL;
}
if ( r->ring[r->ringp] )
sgml_free(r->ring[r->ringp]);
r->ring[r->ringp++] = result;
if ( r->ringp == RINGSIZE )
r->ringp = 0;
return result;
}
/*******************************
* MISC *
*******************************/
wchar_t const *
str_summary(wchar_t const *s, int len)
{ wchar_t *buf;
size_t l = wcslen(s);
if ( l < (size_t)len )
return s;
buf = ringallo((len + 10)*sizeof(wchar_t));
wcsncpy(buf, s, len-5);
wcscpy(&buf[len-5], L" ... ");
wcscpy(&buf[len], &s[l-5]);
return buf;
}
wchar_t *
utf8towcs(const char *in)
{ size_t sl = strlen(in);
size_t len = utf8_utf16strlen(in, sl);
wchar_t *buf = sgml_malloc((len + 1)*sizeof(wchar_t));
const char *e = in+sl;
wchar_t *o = buf;
while( in < e )
{ int chr;
in = utf8_get_char(in, &chr);
o = put_wchar(o, chr);
}
*o = 0;
return buf;
}
char *
wcstoutf8(const wchar_t *in)
{ size_t size = 0;
const wchar_t *s;
char *rc, *o;
for(s=in; *s; )
{ char buf[6];
int c;
s = get_wchar(s, &c);
if ( c >= 0x80 )
{ char *o2 = utf8_put_char(buf, c);
size += o2-buf;
} else
{ size++;
}
}
rc = sgml_malloc(size+1);
for(o=rc, s=in; *s; )
{ int c;
s = get_wchar(s, &c);
o = utf8_put_char(o, c);
}
*o = '\0';
return rc;
}
/*******************************
* FILES *
*******************************/
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Load a file into memory. This would be so easy if we didn't had to deal
with &#RE/&#RS handling that forces us to create the proper record start
and end.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
#ifndef O_BINARY
#define O_BINARY 0
#endif
FILE *
wfopen(const wchar_t *name, const char *mode)
{ size_t mbl = wcstombs(NULL, name, 0);
if ( mbl > 0 )
{ char *mbs = sgml_malloc(mbl+1);
FILE *f;
wcstombs(mbs, name, mbl+1);
f = fopen(mbs, mode);
sgml_free(mbs);
return f;
}
return NULL;
}
static int
wopen(const wchar_t *name, int flags)
{ size_t mbl = wcstombs(NULL, name, 0);
if ( mbl > 0 )
{ char *mbs = sgml_malloc(mbl+1);
int fd;
wcstombs(mbs, name, mbl+1);
fd = open(mbs, flags);
sgml_free(mbs);
return fd;
}
return -1;
}
ichar *
load_sgml_file_to_charp(const ichar *file, int normalise_rsre, size_t *length)
{ int fd;
if ( (fd = wopen(file, O_RDONLY|O_BINARY)) >= 0 )
{ struct stat buf;
if ( fstat(fd, &buf) == 0 )
{ size_t len = buf.st_size;
char *r = sgml_malloc(len+1);
if ( r )
{ char *s = r;
while(len>0)
{ int n;
if ( (n=(int)read(fd, s, (unsigned int)len)) < 0 )
{ close(fd); /* I/O error */
sgml_free(r);
return NULL;
} else if ( n == 0 )
break;
len -= n;
s += n;
}
len = s-r;
*s = '\0'; /* ensure closing EOS */
close(fd);
{ int nl;
int last_is_lf;
ichar *r2, *t;
if ( normalise_rsre )
{ last_is_lf = (len > 0 && s[-1] == '\n');
for(s=r, nl=0; *s; s++)
{ if ( *s == '\n' && s>r && s[-1] != '\r' )
nl++;
}
} else
{ nl = 0;
last_is_lf = 0;
}
r2 = sgml_malloc((len+nl+1)*sizeof(ichar));
for(s=r, t=r2; *s; s++)
{ if ( *s == '\n' )
{ if ( s>r && s[-1] != '\r' )
*t++ = CR;
*t++ = LF;
} else
*t++ = *s;
}
len = t-r2;
*t = '\0';
if ( last_is_lf )
r2[--len] = '\0'; /* delete last LF */
if ( length )
*length = len;
sgml_free(r);
return r2;
}
}
}
}
return NULL;
}
/*******************************
* ALLOCATION *
*******************************/
#ifdef _WINDOWS
#include <windows.h>
#endif
void
sgml_nomem(void)
{ fprintf(stderr, "SGML: Fatal: out of memory\n");
#ifdef _WINDOWS
MessageBox(NULL, "SGML: Fatal: out of memory", "SGML", MB_OK|MB_TASKMODAL);
#endif
exit(1);
}
void *
sgml_malloc(size_t size)
{ void *mem;
if ( size == 0 )
return NULL;
if ( (mem = malloc(size)) )
return mem;
sgml_nomem();
return NULL;
}
void *
sgml_realloc(void *old, size_t size)
{ void *mem;
if ( old )
{ if ( (mem = realloc(old, size)) )
return mem;
} else
{ if ( (mem = malloc(size)) )
return mem;
}
sgml_nomem();
return NULL;
}
void *
sgml_calloc(size_t n, size_t size)
{ void *mem;
if ( (mem=calloc(n, size)) )
return mem;
sgml_nomem();
return NULL;
}
void
sgml_free(void *mem)
{ if ( mem )
free(mem);
}
/*******************************
* DEBUG *
*******************************/
void
wputs(ichar *s)
{ fwprintf(stderr, L"%ls", s);
}
|