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
|
/* Part of SWI-Prolog
Author: Jan Wielemaker
E-mail: J.Wielemaker@vu.nl
WWW: http://www.swi-prolog.org
Copyright (c) 1999-2015, University of 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.
*/
#include <config.h>
#include <SWI-Prolog.h>
#ifdef HAVE_MALLOC_H
#include <malloc.h>
#endif
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "order.h"
#include "error.h"
#ifndef TRUE
#define TRUE 1
#define FALSE 0
#endif
static void exact_table(OrdTable ot);
static void case_insensitive_table(OrdTable ot);
static void iso_latin_1_table(OrdTable ot);
static void iso_latin_1_case_table(OrdTable ot);
static int register_table(OrdTable ot);
static int get_order_table(term_t handle, OrdTable *t);
/*******************************
* PROLOG CONSTANTS *
*******************************/
static atom_t ATOM_lt;
static atom_t ATOM_eq;
static atom_t ATOM_gt;
static atom_t ATOM_ignore;
static atom_t ATOM_tag;
static atom_t ATOM_break;
static atom_t ATOM_case_insensitive;
static atom_t ATOM_iso_latin_1;
static atom_t ATOM_iso_latin_1_case_insensitive;
static atom_t ATOM_exact;
static atom_t ATOM_copy;
static void
standard_table(atom_t name, void (*func)(OrdTable))
{ OrdTable t = malloc(sizeof(ordtable));
if ( t )
{ exact_table(t);
t->name = name;
if ( func )
(*func)(t);
register_table(t);
} else
PL_warning("Could not allocate table");
}
static void
init_constants()
{ ATOM_lt = PL_new_atom("<");
ATOM_eq = PL_new_atom("=");
ATOM_gt = PL_new_atom(">");
ATOM_ignore = PL_new_atom("ignore");
ATOM_tag = PL_new_atom("tag");
ATOM_break = PL_new_atom("break");
ATOM_copy = PL_new_atom("copy");
ATOM_iso_latin_1 = PL_new_atom("iso_latin_1");
ATOM_iso_latin_1_case_insensitive
= PL_new_atom("iso_latin_1_case_insensitive");
ATOM_break = PL_new_atom("break");
ATOM_case_insensitive = PL_new_atom("case_insensitive");
ATOM_exact = PL_new_atom("exact");
standard_table(ATOM_exact, NULL);
standard_table(ATOM_case_insensitive, case_insensitive_table);
standard_table(ATOM_iso_latin_1, iso_latin_1_table);
standard_table(ATOM_iso_latin_1_case_insensitive, iso_latin_1_case_table);
}
/*******************************
* REGISTRATION *
*******************************/
#define MAXTABLES 100
static OrdTable tables[MAXTABLES];
static int
register_table(OrdTable ot)
{ int i;
int slot = -1;
for(i=0; i<MAXTABLES; i++)
{ if ( tables[i] && tables[i]->name == ot->name )
{ free(tables[i]);
tables[i] = ot;
return TRUE;
} else if ( slot < 0 && !tables[i] )
slot = i;
}
if ( slot >= 0 )
{ tables[slot] = ot;
return TRUE;
}
return FALSE;
}
OrdTable
findOrdTable(atom_t name)
{ int i;
for(i=0; i<MAXTABLES; i++)
{ if ( tables[i] && tables[i]->name == name )
return tables[i];
}
return NULL;
}
/*******************************
* ORDER TABLE *
*******************************/
static int
get_char(term_t t, int *chr)
{ int i;
if ( PL_get_integer(t, &i) && i >= 0 && i <= 255 )
{ *chr = i;
return TRUE;
}
return FALSE;
}
static int
parse_set(OrdTable ot, atom_t name, term_t set)
{ int type;
size_t len;
char *s;
if ( name == ATOM_break )
type = ORD_BREAK;
else if ( name == ATOM_ignore )
type = ORD_IGNORE;
else if ( name == ATOM_tag )
type = ORD_TAG;
else
return FALSE;
if ( PL_get_nchars(set, &len, &s, CVT_STRING|CVT_LIST|CVT_EXCEPTION) )
{ size_t i;
for(i=0; i<len; i++)
ORD(ot, s[i]&0xff) = type;
return TRUE;
}
return FALSE;
}
static void
copy_table(OrdTable to, OrdTable from)
{ int i;
to->magic = ORD_MAGIC;
for(i=0; i<256; i++)
ORD(to, i) = ORD(from, i);
}
static void
exact_table(OrdTable ot)
{ int i;
ot->magic = ORD_MAGIC;
for(i=0; i<256; i++)
ORD(ot, i) = i;
}
static void
case_insensitive_table(OrdTable ot)
{ int i;
ot->magic = ORD_MAGIC;
for(i='A'; i<='Z'; i++)
ORD(ot, i) = i + ('a' - 'A');
}
static void
iso_latin_1_table(OrdTable ot)
{ int i;
exact_table(ot);
ORD(ot, 192) = 'A';
ORD(ot, 193) = 'A';
ORD(ot, 194) = 'A';
ORD(ot, 195) = 'A';
ORD(ot, 196) = 'A';
ORD(ot, 197) = 'A';
ORD(ot, 199) = 'C';
ORD(ot, 200) = 'E';
ORD(ot, 201) = 'E';
ORD(ot, 202) = 'E';
ORD(ot, 203) = 'E';
ORD(ot, 204) = 'I';
ORD(ot, 205) = 'I';
ORD(ot, 206) = 'I';
ORD(ot, 207) = 'I';
ORD(ot, 208) = 'D';
ORD(ot, 209) = 'N';
ORD(ot, 210) = 'O';
ORD(ot, 211) = 'O';
ORD(ot, 212) = 'O';
ORD(ot, 213) = 'O';
ORD(ot, 214) = 'O';
ORD(ot, 216) = 'O';
ORD(ot, 217) = 'U';
ORD(ot, 218) = 'U';
ORD(ot, 219) = 'U';
ORD(ot, 220) = 'U';
ORD(ot, 221) = 'Y';
ORD(ot, 223) = 'S'; /* german SS */
for(i=224; i<=253; i++)
{ if ( i == 230 || /* ae */
i == 247 ) /* x and / */
continue;
ORD(ot, i) = ORD(ot, i-32) + 'a' - 'A';
}
}
static void
iso_latin_1_case_table(OrdTable ot)
{ int i;
iso_latin_1_table(ot);
for(i=0; i<=255; i++)
{ int o = ORD(ot, i);
if ( o >= 'A' && o <= 'Z' )
ORD(ot, i) = o + ('a' - 'A');
}
}
static foreign_t
pl_new_order_table(term_t name, term_t options)
{ OrdTable t = malloc(sizeof(ordtable));
term_t tail = PL_copy_term_ref(options);
term_t head = PL_new_term_ref();
exact_table(t);
if ( !PL_get_atom(name, &t->name) )
{ free(t);
return error(ERR_INSTANTIATION, "new_order_table/2", 1, name);
}
while(PL_get_list(tail, head, tail))
{ atom_t name;
size_t arity;
if ( PL_get_name_arity(head, &name, &arity) )
{ if ( name == ATOM_case_insensitive )
{ case_insensitive_table(t);
} else if ( name == ATOM_iso_latin_1 )
{ iso_latin_1_table(t);
} else if ( name == ATOM_iso_latin_1_case_insensitive )
{ iso_latin_1_case_table(t);
} else if ( name == ATOM_copy && arity == 1 )
{ term_t a = PL_new_term_ref();
OrdTable from;
_PL_get_arg(1, head, a);
if ( get_order_table(a, &from) )
{ copy_table(t, from);
} else
{ free(t);
return FALSE;
}
} else if ( arity == 1 )
{ fid_t fid = PL_open_foreign_frame();
term_t a = PL_new_term_ref();
_PL_get_arg(1, head, a);
if ( !parse_set(t, name, a) )
goto err1;
PL_close_foreign_frame(fid);
} else if ( name == ATOM_eq && arity == 2 )
{ fid_t fid = PL_open_foreign_frame();
term_t c = PL_new_term_ref();
int from, to;
if ( !PL_get_arg(1, head, c) || !get_char(c, &from) ||
!PL_get_arg(2, head, c) || !get_char(c, &to) )
{ free(t);
return FALSE;
}
ORD(t, from) = to;
PL_close_foreign_frame(fid);
} else
goto err1;
} else
{ err1:
free(t);
return error(ERR_INSTANTIATION, "new_order_table/2", 2, options);
}
}
if ( !PL_get_nil(tail) )
goto err1;
register_table(t);
PL_succeed;
}
static int
unify_mapped_code(term_t to, int ti)
{ switch(ti)
{ case ORD_BREAK:
return PL_unify_atom(to, ATOM_break);
case ORD_IGNORE:
return PL_unify_atom(to, ATOM_ignore);
case ORD_TAG:
return PL_unify_atom(to, ATOM_tag);
default:
return PL_unify_integer(to, ti);
}
}
static foreign_t
pl_order_table_mapping(term_t handle, term_t from, term_t to, control_t ctrl)
{ OrdTable t;
int f;
if ( !get_order_table(handle, &t) )
return FALSE;
if ( PL_get_integer(from, &f) && f >= 0 && f <= 255 )
return unify_mapped_code(to, ORD(t, f));
if ( PL_is_variable(from) )
{ switch(PL_foreign_control(ctrl))
{ case PL_FIRST_CALL:
f = 0;
break;
case PL_REDO:
f = (int)PL_foreign_context(ctrl);
break;
case PL_PRUNED:
return TRUE;
}
while( f <= 255 && !unify_mapped_code(to, ORD(t, f)) )
f++;
if ( f <= 255 )
{ if ( !PL_unify_integer(from, f) )
return FALSE;
PL_retry(f+1);
}
return FALSE;
}
return FALSE;
}
/*******************************
* DEFAULT TABLES *
*******************************/
static int
get_order_table(term_t handle, OrdTable *t)
{ atom_t name;
OrdTable ot;
if ( PL_get_atom(handle, &name) &&
(ot = findOrdTable(name)) )
{ *t = ot;
return TRUE;
}
return FALSE;
}
static int
compare_strings_(const char *s1, const char **S2, size_t n, OrdTable ot)
{ const char *e1 = s1 + n;
const char *s2 = *S2;
for(;;)
{ int o1, o2;
if ( s1 == e1 )
{ *S2 = s2;
return 0; /* equal */
}
o1 = ORD(ot, *s1);
o2 = ORD(ot, *s2);
if ( o1 == o2 )
{ if ( o1 == ORD_END )
{ *S2 = s2;
return 0; /* equal */
}
if ( o1 == ORD_BREAK ) /* a break, loop on both */
{ while(ORD(ot, *s1) == ORD_BREAK)
s1++;
while(ORD(ot, *s2) == ORD_BREAK)
s2++;
continue;
}
s1++;
s2++;
continue;
}
/* ignore stuff */
if ( o1 == ORD_IGNORE )
{ s1++;
continue;
}
if ( o2 == ORD_IGNORE )
{ s2++;
continue;
}
return o1 < o2 ? -1 : 1;
}
}
int
compare_strings(const char *s1, const char *s2, size_t n, OrdTable ot)
{ return compare_strings_(s1, &s2, n, ot);
}
static foreign_t
pl_compare_strings(term_t ord, term_t t1, term_t t2, term_t result)
{ OrdTable ot;
char *s1, *s2;
size_t len;
int rval;
unsigned int flags = (CVT_ATOM|CVT_STRING|CVT_LIST|BUF_RING|CVT_EXCEPTION);
if ( !get_order_table(ord, &ot) )
return error(ERR_INSTANTIATION, "compare_strings/4", 1, ord);
if ( !PL_get_nchars(t1, &len, &s1, flags) )
return FALSE;
if ( !PL_get_nchars(t2, &len, &s2, flags) )
return FALSE;
rval = compare_strings(s1, s2, len, ot);
return PL_unify_atom(result,
rval == 0 ? ATOM_eq :
rval < 0 ? ATOM_lt :
ATOM_gt);
}
static foreign_t
pl_prefix_string(term_t ord, term_t pre, term_t t2)
{ OrdTable ot;
char *s1, *s2;
size_t l1, l2;
unsigned int flags = (CVT_ATOM|CVT_STRING|CVT_LIST|BUF_RING|CVT_EXCEPTION);
if ( !get_order_table(ord, &ot) )
return error(ERR_INSTANTIATION, "prefix_string/3", 1, ord);
if ( !PL_get_nchars(pre, &l1, &s1, flags) )
return FALSE;
if ( !PL_get_nchars(t2, &l2, &s2, flags) )
return FALSE;
if ( l1 <= l2 &&
compare_strings(s1, s2, l1, ot) == 0 )
return TRUE;
return FALSE;
}
static foreign_t
pl_prefix_string4(term_t ord, term_t pre, term_t post, term_t t2)
{ OrdTable ot;
char *s1;
const char *s2;
size_t l1, l2;
unsigned int flags = (CVT_ATOM|CVT_STRING|CVT_LIST|BUF_RING|CVT_EXCEPTION);
if ( !get_order_table(ord, &ot) )
return error(ERR_INSTANTIATION, "prefix_string/4", 1, ord);
if ( !PL_get_nchars(pre, &l1, &s1, flags) )
return FALSE;
if ( !PL_get_nchars(t2, &l2, (char**)&s2, flags) )
return FALSE;
if ( l1 <= l2 &&
compare_strings_(s1, &s2, l1, ot) == 0 )
return PL_unify_atom_chars(post, s2);
return FALSE;
}
static foreign_t
pl_sub_string(term_t ord, term_t pre, term_t t2)
{ OrdTable ot;
char *s1, *s2;
size_t l1, l2;
unsigned int offset = 0;
unsigned int flags = (CVT_ATOM|CVT_STRING|CVT_LIST|BUF_RING|CVT_EXCEPTION);
if ( !get_order_table(ord, &ot) )
return error(ERR_INSTANTIATION, "sub_string/3", 1, ord);
if ( !PL_get_nchars(pre, &l1, &s1, flags) )
return FALSE;
if ( !PL_get_nchars(t2, &l2, &s2, flags) )
return FALSE;
for( ; l1+offset <= l2; offset++ )
{ if ( compare_strings(s1, s2+offset, l1, ot) == 0 )
return TRUE;
}
return FALSE;
}
install_t
install_order()
{ init_constants();
PL_register_foreign("new_order_table", 2, pl_new_order_table, 0);
PL_register_foreign("order_table_mapping", 3, pl_order_table_mapping,
PL_FA_NONDETERMINISTIC);
PL_register_foreign("compare_strings", 4, pl_compare_strings, 0);
PL_register_foreign("prefix_string", 3, pl_prefix_string, 0);
PL_register_foreign("prefix_string", 4, pl_prefix_string4, 0);
PL_register_foreign("sub_string", 3, pl_sub_string, 0);
}
|