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
|
/*
* Copyright 2008, 2009 University Corporation for Atmospheric Research
*
* This file is part of the UDUNITS-2 package. See the file LICENSE
* in the top-level source-directory of the package for copying and
* redistribution conditions.
*/
/*
* Unit-to-identifier map.
*/
/*LINTLIBRARY*/
#ifndef _XOPEN_SOURCE
# define _XOPEN_SOURCE 500
#endif
#include <assert.h>
#include <errno.h>
#include <search.h>
#include <stdlib.h>
#include <string.h>
#include "udunits2.h"
#include "unitAndId.h"
#include "unitToIdMap.h" /* this module's API */
#include "systemMap.h"
typedef struct {
void* ascii;
void* latin1;
void* utf8;
} UnitToIdMap;
static SystemMap* systemToUnitToName = NULL;
static SystemMap* systemToUnitToSymbol = NULL;
/******************************************************************************
* Miscellaneous Functions:
******************************************************************************/
/*
* Unconditionally converts an ISO Latin-1 string into a UTF-8 string.
*
* Arguments:
* latin1String Pointer to the ISO Latin-1 string.
* Returns:
* NULL Failure. See errno.
* else Pointer to the equivalent UTF-8 string. Should be freed
* when no longer needed.
*/
static char*
latin1ToUtf8(
const char* const latin1String)
{
int nchar;
const char* inp;
char* outp;
char* utf8String;
for (nchar = 0, inp = latin1String; *inp; ++inp, ++nchar)
if ((*inp & 0x80U) != 0)
nchar++;
utf8String = malloc(nchar+1);
if (utf8String != NULL) {
for (inp = latin1String, outp = utf8String; *inp; ++inp, ++outp) {
if ((*inp & 0x80U) == 0) {
*outp = *inp;
}
else {
*outp++ = (char)(0xC0U | ((unsigned)*inp >> 6));
*outp = (char)(0x80U | (*inp & 0x3FU));
}
}
*outp = 0;
}
return utf8String;
}
/*
* Adjust a given encoding according to a string. Because ASCII is a subset of
* ISO Latin-1 and because a UTF-8 encoded string must follow certain rules,
* it's possible for strings to be mis-encoded and for an encoding specification
* to be too restrictive or over-generous. If the encoding specification is
* ASCII and the given string contains a character with the high-order bit
* set, then the encoding will be changed to ISO Latin-1. If the encoding
* specification is ISO Latin-1 but the string doesn't contain a character with
* the high-order bit set, then the encoding will be changed to ASCII. If the
* encoding specification is UTF-8 but the string doesn't follow the UTF-8
* encoding rules, then the function will error-return.
*
* Arguments:
* encoding Pointer to the presumptive encoding. Might be modified
* on return to reflect the actual, most restrictive,
* encoding of "string".
* string The string to be checked.
* Returns:
* 0 Success. "*encoding" might be modified.
* -1 Failure. "string" doesn't conform to "encoding".
*/
static int
adjustEncoding(
ut_encoding* const encoding,
const char* string)
{
int status = 0; /* success */
if (*encoding == UT_ASCII) {
for (; *string && ((*string & 0x80U) == 0); string++)
;
if (*string != 0)
*encoding = UT_LATIN1;
}
else if (*encoding == UT_LATIN1) {
for (; *string && ((*string & 0x80U) == 0); string++)
;
if (*string == 0)
*encoding = UT_ASCII;
}
else if (*encoding == UT_UTF8) {
for (; *string; string++) {
if (*string & 0x80U) {
if ((*string & 0xE0U) == 0xC0U) {
if ((*++string & 0xC0U) != 0x80U)
break;
}
else if ((*string & 0xF0U) == 0xE0U) {
if ((*++string & 0xC0U) != 0x80U)
break;
if ((*++string & 0xC0U) != 0x80U)
break;
}
else if ((*string & 0xF8U) == 0xF0U) {
if ((*++string & 0xC0U) != 0x80U)
break;
if ((*++string & 0xC0U) != 0x80U)
break;
if ((*++string & 0xC0U) != 0x80U)
break;
}
}
}
if (*string)
status = -1;
}
return status;
}
/******************************************************************************
* Internal Map Functions:
******************************************************************************/
static int
compareUnits(
const void* const entry1,
const void* const entry2)
{
return ut_compare(((const UnitAndId*)entry1)->unit,
((const UnitAndId*)entry2)->unit);
}
/*
* Selects a unit-and-identifier tree corresponding to a given encoding.
*
* Arguments:
* map The unit-to-id map.
* encoding The encoding.
* Returns:
* Pointer to the root of the unit-and-identifier tree in "map" that
* corresponds to "encoding".
*/
static void**
selectTree(
UnitToIdMap* const unitToIdMap,
const ut_encoding encoding)
{
return
encoding == UT_ASCII
? &unitToIdMap->ascii
: encoding == UT_LATIN1
? &unitToIdMap->latin1
: &unitToIdMap->utf8;
}
/*
* Returns a new instance of a unit-to-identifier map.
*
* Returns:
* NULL Failure. See "errno".
* else Pointer to a new unit-to-identifier map.
*/
static UnitToIdMap*
utimNew(void)
{
UnitToIdMap* const map = malloc(sizeof(UnitToIdMap));
if (map != NULL) {
map->ascii = NULL;
map->latin1 = NULL;
map->utf8 = NULL;
}
return map;
}
/*
* Frees a unit-to-identifier map. All entries in all encodings are freed.
*
* Arguments:
* map Pointer to the map to be freed.
*/
static void
utimFree(
UnitToIdMap* map)
{
if (map != NULL) {
ut_encoding encodings[] = {UT_ASCII, UT_LATIN1, UT_UTF8};
int i;
for (i = 0; i < sizeof(encodings)/sizeof(encodings[0]); ++i) {
void** rootp = selectTree(map, encodings[i]);
while (*rootp != NULL) {
UnitAndId* uai = **(UnitAndId***)rootp;
(void)tdelete(uai, rootp, compareUnits);
uaiFree(uai);
}
}
free(map);
}
}
/*
* Adds an entry to a unit-to-identifier map.
*
* Arguments:
* map Pointer to unit-to-identifier map.
* unit The unit. May be freed upon return.
* id The identifier. May be freed upon return.
* encoding The ostensible encoding of "id".
* Returns:
* UT_BAD_ARG "id" is inconsistent with "encoding".
* UT_OS Operating-system error. See "errno".
* UT_EXISTS "unit" already maps to a different identifier.
* UT_SUCCESS Success.
*/
static ut_status
utimAdd(
UnitToIdMap* const map,
const ut_unit* unit,
const char* const id,
ut_encoding encoding)
{
ut_status status;
assert(map != NULL);
assert(unit != NULL);
assert(id != NULL);
if (adjustEncoding(&encoding, id)) {
status = UT_BAD_ARG;
ut_set_status(status);
ut_handle_error_message("Identifier not in given encoding");
}
else {
UnitAndId* targetEntry = uaiNew(unit, id);
if (targetEntry != NULL) {
void** rootp = selectTree(map, encoding);
UnitAndId** treeEntry = tsearch(targetEntry, rootp, compareUnits);
if (treeEntry == NULL) {
status = UT_OS;
ut_set_status(status);
ut_handle_error_message(strerror(errno));
ut_handle_error_message("Couldn't add search-tree entry");
uaiFree(targetEntry);
}
else {
if (strcmp((*treeEntry)->id, id) != 0) {
status = UT_EXISTS;
ut_set_status(status);
ut_handle_error_message("Unit already maps to \"%s\"",
(*treeEntry)->id);
}
else {
status = UT_SUCCESS;
}
if (targetEntry != *treeEntry)
uaiFree(targetEntry);
}
} /* "targetEntry" allocated */
} /* valid arguments */
return status;
}
/*
* Removes an entry from a unit-to-identifier map.
*
* Arguments:
* map Pointer to the unit-to-identifier map.
* unit The unit. May be freed upon return.
* encoding The encoding to be removed.
* Returns:
* UT_SUCCESS Success.
*/
static ut_status
utimRemove(
UnitToIdMap* const map,
const ut_unit* unit,
ut_encoding encoding)
{
ut_status status;
UnitAndId targetEntry;
UnitAndId** treeEntry;
assert(map != NULL);
assert(unit != NULL);
targetEntry.unit = (ut_unit*)unit;
treeEntry = tfind(&targetEntry, selectTree(map, encoding), compareUnits);
if (treeEntry == NULL || *treeEntry == NULL) {
status = UT_SUCCESS;
}
else {
UnitAndId* uai = *treeEntry;
(void)tdelete(uai, selectTree(map, encoding), compareUnits);
uaiFree(uai);
}
return status;
}
/*
* Returns the unit-and-identifier whose ASCII identifier corresponding to a
* unit.
*
* Arguments:
* map The unit-to-identifier map.
* unit The unit to be used as the key in the search.
* Returns:
* NULL The map doesn't contain an entry corresponding to "unit" whose
* identifier is in ASCII.
* else Pointer to the entry corresponding to "unit" whose identifier is
* in ASCII.
*/
static UnitAndId*
utimFindAsciiByUnit(
UnitToIdMap* const map,
const ut_unit* const unit)
{
UnitAndId targetEntry;
UnitAndId** treeEntry;
targetEntry.unit = (ut_unit*)unit;
treeEntry = tfind(&targetEntry, &map->ascii, compareUnits);
return treeEntry == NULL ? NULL : *treeEntry;
}
/*
* Finds a unit-search-entry with a Latin-1 identifier correcponding to a unit.
*
* Arguments:
* map The unit-to-identifier map.
* unit The unit to be used as the key in the search.
* Returns:
* NULL The map doesn't contain an entry corresponding to "unit" whose
* identifier is in Latin-1.
* else Pointer to the entry corresponding to "unit" whose identifier is
* in Latin-1 (and might, actually, be in ASCII).
*/
static UnitAndId*
utimFindLatin1ByUnit(
UnitToIdMap* const map,
const ut_unit* const unit)
{
UnitAndId targetEntry;
UnitAndId** treeEntry;
targetEntry.unit = (ut_unit*)unit;
treeEntry = tfind(&targetEntry, &map->latin1, compareUnits);
if (treeEntry == NULL)
treeEntry = tfind(&targetEntry, &map->ascii, compareUnits);
return treeEntry == NULL ? NULL : *treeEntry;
}
/*
* Finds an entry with a UTF-8 identifier corresponding to a unit.
*
* Arguments:
* map The unit-to-identifier map.
* unit The unit to be used as the key in the search.
* Returns:
* NULL The map doesn't contain an entry corresponding to "unit" whose
* identifier is in UTF-8.
* else Pointer to the entry corresponding to "unit" whose identifier is
* in UTF-8 (and might, actually, be in ASCII).
*/
static UnitAndId*
utimFindUtf8ByUnit(
UnitToIdMap* const map,
const ut_unit* const unit)
{
UnitAndId targetEntry;
UnitAndId** treeEntry = NULL; /* failure */
targetEntry.unit = (ut_unit*)unit;
treeEntry = tfind(&targetEntry, &map->utf8, compareUnits);
if (treeEntry == NULL) {
treeEntry = tfind(&targetEntry, &map->latin1, compareUnits);
if (treeEntry == NULL) {
treeEntry = tfind(&targetEntry, &map->ascii, compareUnits);
}
else {
/*
* Create the UTF-8 version of the Latin-1 identifier and add it to
* the UTF-8 unit-to-id map so that it will be found next time.
*/
char* const id = latin1ToUtf8((*treeEntry)->id);
if (id == NULL) {
ut_set_status(UT_OS);
ut_handle_error_message(strerror(errno));
ut_handle_error_message(
"Couldn't convert identifier from ISO-8859-1 to UTF-8");
treeEntry = NULL;
}
else {
UnitAndId* newEntry = uaiNew(unit, id);
if (newEntry != NULL) {
treeEntry = tsearch(newEntry, &map->utf8, compareUnits);
if (treeEntry == NULL) {
uaiFree(newEntry);
ut_set_status(UT_OS);
ut_handle_error_message(strerror(errno));
ut_handle_error_message(
"Couldn't add unit-and-identifier to search-tree");
}
}
free(id);
} /* UTF-8 identifier created */
} /* found Latin-1 identifier */
} /* no UTF-8 identifier */
return treeEntry == NULL ? NULL : *treeEntry;
}
/*
* Adds an entry to the unit-to-identifier map associated with a unit-system.
*
* Arguments:
* sytemMap Address of the pointer to the
* system-to-unit-to-identifier map.
* unit The unit. May be freed upon return.
* id The identifier. May be freed upon return.
* encoding The ostensible encoding of "id".
* Returns:
* UT_BAD_ARG "unit" or "id" is NULL, or "id" is inconsistent with
* "encoding".
* UT_OS Operating-system error. See "errno".
* UT_EXISTS "unit" already maps to a different identifier.
* UT_SUCCESS Success.
*/
static ut_status
mapUnitToId(
SystemMap** const systemMap,
const ut_unit* const unit,
const char* const id,
ut_encoding encoding)
{
ut_status status;
assert(systemMap != NULL);
if (unit == NULL || id == NULL) {
status = UT_BAD_ARG;
}
else {
if (*systemMap == NULL) {
*systemMap = smNew();
if (*systemMap == NULL)
status = UT_OS;
}
if (*systemMap != NULL) {
UnitToIdMap** const unitToIdMap =
(UnitToIdMap**)smSearch(*systemMap, ut_get_system(unit));
if (unitToIdMap == NULL) {
status = UT_OS;
}
else {
if (*unitToIdMap == NULL) {
*unitToIdMap = utimNew();
if (*unitToIdMap == NULL)
status = UT_OS;
}
if (*unitToIdMap != NULL)
status = utimAdd(*unitToIdMap, unit, id, encoding);
}
}
}
return status;
}
/*
* Removes an entry from the unit-to-identifier map associated with a
* unit-system.
*
* Arguments:
* sytemMap Pointer to the system-to-unit-to-identifier map.
* unit The unit. May be freed upon return.
* encoding The ostensible encoding of "id".
* Returns:
* UT_BAD_ARG "systemMap" is NULL.
* UT_BAD_ARG "unit" is NULL.
* UT_SUCCESS Success.
*/
static ut_status
unmapUnitToId(
SystemMap* const systemMap,
const ut_unit* const unit,
ut_encoding encoding)
{
ut_status status;
if (systemMap == NULL || unit == NULL) {
status = UT_BAD_ARG;
}
else {
UnitToIdMap** const unitToIdMap =
(UnitToIdMap**)smFind(systemMap, ut_get_system(unit));
status =
(unitToIdMap == NULL || *unitToIdMap == NULL)
? UT_SUCCESS
: utimRemove(*unitToIdMap, unit, encoding);
}
return status;
}
/*
* Returns the identifier in a given encoding to which a unit associated with
* a unit-system maps.
*
* Arguments:
* systemMap Pointer to the system-to-unit-to-id map.
* unit Pointer to the unit whose identifier should be returned.
* encoding The desired encoding of the identifier.
* Returns:
* NULL Failure. "ut_get_status()" will be
* UT_BAD_ARG "unit" was NULL.
* else Pointer to the identifier in the given encoding
* associated with "unit".
*/
static const char*
getId(
SystemMap* const systemMap,
const ut_unit* const unit,
const ut_encoding encoding)
{
const char* id = NULL; /* failure */
if (unit == NULL) {
ut_set_status(UT_BAD_ARG);
ut_handle_error_message("NULL unit argument");
}
else {
UnitToIdMap** const unitToId =
(UnitToIdMap**)smFind(systemMap, ut_get_system(unit));
if (unitToId != NULL) {
UnitAndId* mapEntry =
encoding == UT_LATIN1
? utimFindLatin1ByUnit(*unitToId, unit)
: encoding == UT_UTF8
? utimFindUtf8ByUnit(*unitToId, unit)
: utimFindAsciiByUnit(*unitToId, unit);
if (mapEntry != NULL)
id = mapEntry->id;
}
}
return id;
}
/******************************************************************************
* Public API:
******************************************************************************/
/*
* Adds a mapping from a unit to a name.
*
* Arguments:
* unit Pointer to the unit to be mapped to "name". May be
* freed upon return.
* name Pointer to the name to be mapped-to by "unit". May be
* freed upon return.
* encoding The encoding of "name".
* Returns:
* UT_SUCCESS Success.
* UT_BAD_ARG "unit" or "name" is NULL, or "name" is not in the
* specified encoding.
* UT_OS Operating-system error. See "errno".
* UT_EXISTS "unit" already maps to a name.
*/
ut_status
ut_map_unit_to_name(
const ut_unit* const unit,
const char* const name,
ut_encoding encoding)
{
ut_set_status(mapUnitToId(&systemToUnitToName, unit, name, encoding));
return ut_get_status();
}
/*
* Removes a mapping from a unit to a name.
*
* Arguments:
* unit Pointer to the unit. May be freed upon return.
* encoding The encoding to be removed. No other encodings will be
* removed.
* Returns:
* UT_BAD_ARG "unit" is NULL.
* UT_SUCCESS Success.
*/
ut_status
ut_unmap_unit_to_name(
const ut_unit* const unit,
ut_encoding encoding)
{
ut_set_status(unmapUnitToId(systemToUnitToName, unit, encoding));
return ut_get_status();
}
/*
* Adds a mapping from a unit to a symbol.
*
* Arguments:
* unit Pointer to the unit to be mapped to "symbol". May be
* freed upon return.
* symbol Pointer to the symbol to be mapped-to by "unit". May
* be freed upon return.
* encoding The encoding of "symbol".
* Returns:
* UT_SUCCESS Success.
* UT_BAD_ARG "unit" or "symbol" is NULL.
* UT_OS Operating-system error. See "errno".
* UT_EXISTS "unit" already maps to a symbol.
*/
ut_status
ut_map_unit_to_symbol(
const ut_unit* unit,
const char* const symbol,
ut_encoding encoding)
{
ut_set_status(mapUnitToId(&systemToUnitToSymbol, unit, symbol, encoding));
return ut_get_status();
}
/*
* Removes a mapping from a unit to a symbol.
*
* Arguments:
* unit Pointer to the unit to be unmapped to a symbol. May be
* freed upon return.
* encoding The encoding to be removed. The mappings for "unit" in
* other encodings will not be removed.
* Returns:
* UT_SUCCESS Success.
* UT_BAD_ARG "unit" is NULL.
*/
ut_status
ut_unmap_unit_to_symbol(
const ut_unit* const unit,
ut_encoding encoding)
{
ut_set_status(unmapUnitToId(systemToUnitToSymbol, unit, encoding));
return ut_get_status();
}
/*
* Returns the name in a given encoding to which a unit maps.
*
* Arguments:
* unit Pointer to the unit whose name should be returned.
* encoding The desired encoding of the name.
* Returns:
* NULL Failure. "ut_get_status()" will be
* UT_BAD_ARG "unit" is NULL.
* UT_SUCCESS "unit" doesn't map to a name in
* in the given encoding.
* else Pointer to the name in the given encoding to which
* "unit" maps.
*/
const char*
ut_get_name(
const ut_unit* const unit,
const ut_encoding encoding)
{
ut_set_status(UT_SUCCESS);
return getId(systemToUnitToName, unit, encoding);
}
/*
* Returns the symbol in a given encoding to which a unit maps.
*
* Arguments:
* unit Pointer to the unit whose symbol should be returned.
* encoding The desired encoding of the symbol.
* Returns:
* NULL Failure. "ut_get_status()" will be
* UT_BAD_ARG "unit" is NULL.
* UT_SUCCESS "unit" doesn't map to a symbol
* in the given encoding.
* else Pointer to the symbol in the given encoding to which
* "unit" maps.
*/
const char*
ut_get_symbol(
const ut_unit* const unit,
const ut_encoding encoding)
{
ut_set_status(UT_SUCCESS);
return getId(systemToUnitToSymbol, unit, encoding);
}
/*
* Frees resources associated with a unit-system.
*
* Arguments:
* system Pointer to the unit-system to have its associated
* resources freed.
*/
void
utimFreeSystem(
ut_system* system)
{
if (system != NULL) {
SystemMap* systemMaps[2];
int i;
systemMaps[0] = systemToUnitToName;
systemMaps[1] = systemToUnitToSymbol;
for (i = 0; i < 2; i++) {
if (systemMaps[i] != NULL) {
UnitToIdMap** const unitToId =
(UnitToIdMap**)smFind(systemMaps[i], system);
if (unitToId != NULL)
utimFree(*unitToId);
smRemove(systemMaps[i], system);
}
}
}
}
|