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 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251
|
/*
* Committee for Graphics Arts Technologies Standards
* CGATS.5 and IT8.7 family file I/O class
* Version 2.05
*/
/*
* Author: Graeme W. Gill
* Date: 20/12/95
*
* Copyright 1995, 1996, 2002, Graeme W. Gill
* All rights reserved.
*
* This material is licensed with an "MIT" free use license:-
* see the License4.txt file in this directory for licensing details.
*/
/*
Should add a function to promote a field type, ie.
promote integer to float, to avoid misrecognition
problems.
To make this more portable for independent use,
should save/set/restore LC_NUMERIC locale before
printf/scanf from file. e.g.
include <locale.h>
char *old_locale, *saved_locale;
old_locale = setlocale (LC_NUMERIC, NULL);
saved_locale = strdup (old_locale);
if (saved_locale == NULL)
error ("Out of memory");
setlocale (LC_NUMERIC, "C");
.... read or write ...
setlocale (LC_NUMERIC, saved_locale);
free (saved_locale);
Also apply to pars.c
*/
#define _CGATS_C_ /* Turn on implimentation code */
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#undef DEBUG /* Debug only in slected places */
#ifdef DEBUG
# define DBGA stderr
# define DBGF(xx) fprintf xx
#else
# define DBGF(xx)
#endif
#ifdef STANDALONE_TEST
extern void error(const char *fmt, ...), warning(const char *fmt, ...);
#endif
#include <sys/types.h>
#include <time.h>
#include <string.h>
#include <math.h>
#include "pars.h"
#include "cgats.h"
#undef EMIT_KEYWORDS /* [und] Emit unknown keywords by default */
#define REAL_SIGDIG 6 /* [6] Number of significant digits in real representation */
static int cgats_read(cgats *p, cgatsFile *fp);
static int find_kword(cgats *p, int table, const char *ksym);
static int find_field(cgats *p, int table, const char *fsym);
static int add_table(cgats *p, table_type tt, int oi);
static int set_table_type(cgats *p, int table, table_type tt, int oi);
static int set_table_flags(cgats *p, int table, int sup_id, int sup_kwords, int sup_fields);
static int set_cgats_type(cgats *p, const char *osym);
static int add_other(cgats *p, const char *osym);
static int get_oi(cgats *p, const char *osym);
static int add_kword(cgats *p, int table, const char *ksym, const char *kdata, const char *kcom);
static int add_field(cgats *p, int table, const char *fsym, data_type ftype);
static int add_set(cgats *p, int table, ...);
static int add_setarr(cgats *p, int table, cgats_set_elem *args);
static int get_setarr(cgats *p, int table, int set_index, cgats_set_elem *args);
static int cgats_write(cgats *p, cgatsFile *fp);
static int cgats_error(cgats *p, char **mes);
static void cgats_del(cgats *p);
static void cgats_table_free(cgats_table *t);
static void *alloc_copy_data_type(cgatsAlloc *al, data_type ktype, void *dpoint);
static int reserved_kword(const char *ksym);
static int standard_kword(const char *ksym);
static data_type standard_field(const char *fsym);
static int cs_has_ws(const char *cs);
static char *quote_cs(cgatsAlloc *al, const char *cs);
static int clear_fields(cgats *p, int table);
static int add_kword_at(cgats *p, int table, int pos, const char *ksym, const char *kdatak, const char *kcom);
static int add_data_item(cgats *p, int table, void *data);
static void unquote_cs(char *cs);
static data_type guess_type(const char *cs);
static void real_format(double value, int nsd, char *fmt);
#ifdef COMBINED_STD
static int cgats_read_name(cgats *p, const char *filename);
static int cgats_write_name(cgats *p, const char *filename);
#endif
static const char *data_type_desc[] =
{ "real", "integer", "char string", "non-quoted char string", "no type" };
/* Create an empty cgats object */
/* Return NULL on error */
cgats *new_cgats_al(
cgatsAlloc *al /* memory allocator */
) {
cgats *p;
if ((p = (cgats *) al->calloc(al, sizeof(cgats), 1)) == NULL) {
return NULL;
}
p->al = al; /* Heap allocator */
/* Initialize the methods */
p->find_kword = find_kword;
p->find_field = find_field;
p->read = cgats_read;
p->add_table = add_table;
p->set_table_type = set_table_type;
p->set_table_flags = set_table_flags;
p->set_cgats_type = set_cgats_type;
p->add_other = add_other;
p->get_oi = get_oi;
p->add_kword = add_kword;
p->add_kword_at = add_kword_at;
p->add_field = add_field;
p->add_set = add_set;
p->add_setarr = add_setarr;
p->get_setarr = get_setarr;
p->write = cgats_write;
p->error = cgats_error;
p->del = cgats_del;
#ifndef SEPARATE_STD
p->read_name = cgats_read_name;
p->write_name = cgats_write_name;
#else
p->read_name = NULL;
p->write_name = NULL;
#endif
#ifdef EMIT_KEYWORDS
p->emit_keywords = 1;
#endif
return p;
}
static int err(cgats *p, int errc, const char *fmt, ...);
/* new_cgats() with default malloc allocator */
#ifndef SEPARATE_STD
#define COMBINED_STD
#include "cgatsstd.c"
#undef COMBINED_STD
#endif /* SEPARATE_STD */
/* ------------------------------------------- */
/* Implimentation function - register an error */
/* Return the error number */
static int
err(cgats *p, int errc, const char *fmt, ...) {
va_list args;
p->errc = errc;
va_start(args, fmt);
vsprintf(p->err, fmt, args);
va_end(args);
/* If this is the first registered error */
if (p->ferrc != 0) {
p->ferrc = p->errc;
strcpy(p->ferr, p->err);
}
return errc;
}
/* Define methods */
/* Return error code and message */
/* for the first error, if any error */
/* has occured since object creation. */
static int cgats_error(
cgats *p,
char **mes
) {
if (p->ferrc != 0) {
if (mes != NULL)
*mes = p->ferr;
return p->ferrc;
}
return 0;
}
/* ------------------------------------------- */
/* Free the cgats object */
static void
cgats_del(cgats *p) {
int i;
cgatsAlloc *al = p->al;
int del_al = p->del_al;
/* Free all the user defined file identifiers */
if (p->cgats_type != NULL) {
al->free(al, p->cgats_type);
}
if (p->others != NULL) {
for (i = 0; i < p->nothers; i++)
if(p->others[i] != NULL)
al->free(al, p->others[i]);
al->free(al, p->others);
}
/* Free contents of all the tables */
for (i = 0; i < p->ntables; i++)
cgats_table_free(&p->t[i]);
/* Free the table structures */
if (p->t != NULL)
al->free(al, p->t);
al->free(al, p);
if (del_al) /* We are responsible for deleting allocator */
al->del(al);
}
/* Free up the contents of a cgats_table struct */
static void
cgats_table_free(cgats_table *t) {
cgatsAlloc *al = t->al;
int i,j;
/* Free all the keyword symbols */
if (t->ksym != NULL) {
for (i = 0; i < t->nkwords; i++)
if(t->ksym[i] != NULL)
al->free(al, t->ksym[i]);
al->free(al, t->ksym);
}
/* Free all the keyword values */
if (t->kdata != NULL) {
for (i = 0; i < t->nkwords; i++)
if(t->kdata[i] != NULL)
al->free(al, t->kdata[i]);
al->free(al, t->kdata);
}
/* Free all the keyword comments */
if (t->kcom != NULL) {
for (i = 0; i < t->nkwords; i++)
if(t->kcom[i] != NULL)
al->free(al, t->kcom[i]);
al->free(al, t->kcom);
}
/* Free all the field symbols */
if (t->fsym != NULL) {
for (i = 0; i < t->nfields; i++)
if(t->fsym[i] != NULL)
al->free(al, t->fsym[i]);
al->free(al, t->fsym);
}
/* Free array of field types */
if (t->ftype != NULL)
al->free(al, t->ftype);
/* Free all the original fields text values */
if (t->rfdata != NULL) {
for (j = 0; j < t->nsets; j++)
if (t->rfdata[j] != NULL) {
for (i = 0; i < t->nfields; i++)
if(t->rfdata[j][i] != NULL)
al->free(al, t->rfdata[j][i]);
al->free(al, t->rfdata[j]);
}
al->free(al, t->rfdata);
}
/* Free all the fields values */
if (t->fdata != NULL) {
for (j = 0; j < t->nsets; j++)
if (t->fdata[j] != NULL) {
for (i = 0; i < t->nfields; i++)
if(t->fdata[j][i] != NULL)
al->free(al, t->fdata[j][i]);
al->free(al, t->fdata[j]);
}
al->free(al, t->fdata);
}
}
/* Return index of the keyword, -1 on fail */
/* -2 on illegal table index, message in err & errc */
static int
find_kword(cgats *p, int table, const char *ksym) {
int i;
cgats_table *t;
p->errc = 0;
p->err[0] = '\000';
if (table < 0 || table >= p->ntables)
return err(p, -2, "cgats.find_kword(), table number '%d' is out of range",table);
t = &p->t[table];
if (ksym == NULL || ksym[0] == '\000')
return -1;
for (i = 0; i < t->nkwords; i ++) {
if (t->ksym[i] != NULL && t->kdata[i] != NULL
&& strcmp(t->ksym[i],ksym) == 0)
return i;
}
return -1;
}
/* Return index of the field, -1 on fail */
/* -2 on illegal table index, message in err & errc */
static int
find_field(cgats *p, int table, const char *fsym) {
int i;
cgats_table *t;
p->errc = 0;
p->err[0] = '\000';
if (table < 0 || table >= p->ntables)
return err(p, -2, "cgats.find_field(), table number '%d' is out of range",table);
t = &p->t[table];
if (fsym == NULL || fsym[0] == '\000')
return -1;
for (i = 0; i < t->nfields; i ++)
if (strcmp(t->fsym[i],fsym) == 0)
return i;
return -1;
}
/* Read a cgats file into structure */
/* returns 0 normally, -ve if there was an error, */
/* and p->errc and p->err will be valid */
static int
cgats_read(cgats *p, cgatsFile *fp) {
parse *pp;
/* Read states */
#define R_IDENT 0 /* Reading file identifier */
#define R_KWORDS 1 /* Reading keywords */
#define R_KWORD_VALUE 2 /* Reading keywords values */
#define R_FIELDS 3 /* Reading field declarations */
#define R_DATA 4 /* Reading data in set */
int rstate = R_IDENT;
int tablef = 0; /* Current table we should be filling */
int expsets = 0; /* Expected number of sets */
char *kw = NULL; /* keyword symbol */
p->errc = 0;
p->err[0] = '\000';
if ((pp = new_parse_al(p->al, fp)) == NULL) {
DBGF((DBGA,"Failed to open parser for file\n"));
return err(p, -1, "Unable to create file parser for file '%s'",fp->fname(fp));
}
/* Setup our token parsing charaters */
/* Terminators, Not Read, Comment start, Quote characters */
pp->add_del(pp, " \t"," \t", "#", "\"");
/* Read in the file */
for (;;) {
char *tp; /* Token string */
/* Fetch the next token */
while ((tp = pp->get_token(pp)) == NULL) {
int rc;
if (pp->errc != 0) { /* get_token got an error */
err(p, -1, "%s", pp->err);
pp->del(pp);
DBGF((DBGA,"Get token got error '%s'\n",pp->err));
return p->errc;
}
if ((rc = pp->read_line(pp)) == 0)
break; /* End of file */
else if (rc == -1) { /* read_line got an error */
err(p, -1, "%s", pp->err);
pp->del(pp);
DBGF((DBGA,"Read line got error '%s'\n",pp->err));
return p->errc;
}
}
if (tp == NULL)
break; /* EOF */
/* This seems unlikely and will cause err() to barf */
if (strlen(tp) > CGATS_ERRM_LENGTH/2) {
tp[CGATS_ERRM_LENGTH/2] = '\000';
err(p,-1,"Read line got symbol '%s' that's too long\n",tp);
pp->del(pp);
return p->errc;
}
switch(rstate) {
case R_IDENT: /* Expecting file identifier */
case R_KWORDS: { /* Expecting keyword, field def or data */
table_type tt = tt_none;
int oi = 0; /* Index if tt_other */
DBGF((DBGA,"Got kword '%s'\n",tp));
if (rstate == R_IDENT) {
DBGF((DBGA,"Expecting file identifier\n"));
}
/* The standard says that keywords have to be at the start of a line */
if (pp->token != 1) /* Be robust and ignore any problems */
break;
/* See if we have a file identifier */
if(strcmp(tp,"IT8.7/1") == 0)
tt = it8_7_1;
else if(strcmp(tp,"IT8.7/2") == 0)
tt = it8_7_2;
else if(strcmp(tp,"IT8.7/3") == 0)
tt = it8_7_3;
else if(strcmp(tp,"IT8.7/4") == 0)
tt = it8_7_4;
else if(strcmp(tp,"CGATS.5") == 0)
tt = cgats_5;
else if(strncmp(tp,"CGATS.",6) == 0) { /* Variable CGATS type */
tt = cgats_X;
if (p->cgats_type != NULL)
p->al->free(p->al, p->cgats_type);
if ((p->cgats_type = (char *)p->al->malloc(p->al,
(strlen(tp)+1) * sizeof(char))) == NULL) {
err(p,-1,"Failed to malloc space for CGATS.X keyword");
pp->del(pp);
return p->errc;
}
strcpy(p->cgats_type,tp);
DBGF((DBGA,"Found CGATS file identifier\n"));
rstate = R_KWORDS;
} else { /* See if it is an 'other' file identifier */
int iswild = 0;
DBGF((DBGA,"Checking for 'other' identifier\n"));
/* Check for non-wildcard "other" */
for (oi = 0; oi < p->nothers; oi++) {
if (p->others[oi][0] == '\000') { /* Wild card */
iswild = 1;
continue;
}
/* If "other" is a specific string */
if(strcmp(tp,p->others[oi]) == 0) {
DBGF((DBGA,"Matches 'other' %s\n",p->others[oi]));
tt = tt_other;
rstate = R_KWORDS;
break;
}
}
if (tt == tt_none
&& iswild
&& rstate == R_IDENT /* First token after a table */
&& standard_kword(tp) == 0 /* And not an obvious kword */
&& reserved_kword(tp) == 0) {
DBGF((DBGA,"Matches 'other' wildcard\n"));
if ((oi = add_other(p, tp)) == -2) {
pp->del(pp);
DBGF((DBGA,"add_other for wilidcard failed\n"));
return p->errc;
}
tt = tt_other;
rstate = R_KWORDS;
}
}
/* First ever token must be file identifier */
if (tt == tt_none && p->ntables == 0) {
err(p,-1,"Error at line %d of file '%s': No CGATS file identifier found",pp->line,fp->fname(fp));
pp->del(pp);
DBGF((DBGA,"Failed to match file identifier\n"));
return p->errc;
}
/* Any token after previous table has data finished */
/* causes a new table to be created. */
if (p->ntables == tablef) {
if (tt != tt_none) { /* Current token is a file identifier */
DBGF((DBGA,"Got file identifier, adding plain table\n"));
if (add_table(p, tt, oi) < 0) {
pp->del(pp);
DBGF((DBGA,"Add table failed\n"));
return p->errc;
}
} else { /* Carry everything over from previous table the table type */
int i;
cgats_table *pt;
int ct;
DBGF((DBGA,"No file identifier, adding table copy of previous\n"));
if (add_table(p, p->t[p->ntables-1].tt, p->t[p->ntables-1].oi) < 0) {
pp->del(pp);
DBGF((DBGA,"Add table failed\n"));
return p->errc;
}
pt = &p->t[p->ntables-2];
ct = p->ntables-1;
for (i = 0; i < pt->nkwords; i++) {
if (p->add_kword(p, ct, pt->ksym[i], pt->kdata[i], pt->kcom[i]) < 0) {
pp->del(pp);
DBGF((DBGA,"Add keyword failed\n"));
return p->errc;
}
}
for (i = 0; i < pt->nfields; i++)
if (p->add_field(p, ct, pt->fsym[i], none_t) < 0) {
pp->del(pp);
DBGF((DBGA,"Add field failed\n"));
return p->errc;
}
}
}
/* If not a file identifier */
if (tt == tt_none) {
/* See if we're starting the field declarations */
if(strcmp(tp,"BEGIN_DATA_FORMAT") == 0) {
rstate = R_FIELDS;
if (clear_fields(p, p->ntables-1) < 0) {
pp->del(pp);
DBGF((DBGA,"Clear field failed\n"));
return p->errc;
}
break;
}
if(strcmp(tp,"SAMPLE_ID") == 0) { /* Faulty table - cope gracefully */
rstate = R_FIELDS;
if (clear_fields(p, p->ntables-1) < 0) {
pp->del(pp);
DBGF((DBGA,"Clear field failed\n"));
return p->errc;
}
goto first_field;
}
if(strcmp(tp,"BEGIN_DATA") == 0) {
rstate = R_DATA;
break;
}
/* Else must be a keyword */
if ((kw = (char *)alloc_copy_data_type(p->al, cs_t, (void *)tp)) == NULL) {
err(p, -2, "cgats.alloc_copy_data_type() malloc fail");
pp->del(pp);
DBGF((DBGA,"Alloc data type failed\n"));
return p->errc;
}
rstate = R_KWORD_VALUE;
}
break;
}
case R_KWORD_VALUE: {
/* Add a keyword and its value */
DBGF((DBGA,"Got keyword value '%s'\n",kw));
/* Special case for read() use */
if(strcmp(kw,"NUMBER_OF_SETS") == 0)
expsets = atoi(tp);
if (!reserved_kword(kw)) { /* Don't add reserved keywords */
int ix;
/* Replace keyword if it already exists */
unquote_cs(tp);
if ((ix = find_kword(p, p->ntables-1, kw)) < -1) {
pp->del(pp);
DBGF((DBGA,"Failed to find keyword\n"));
return p->errc;
}
if (add_kword_at(p, p->ntables-1, ix, kw, tp, NULL) < 0) {
pp->del(pp);
DBGF((DBGA,"Failed to add keyword '%s'\n",kw));
return p->errc;
}
}
p->al->free(p->al, kw);
rstate = R_KWORDS;
break;
}
case R_FIELDS: {
DBGF((DBGA,"Got fields value '%s'\n",tp));
/* Add a list of field name declarations */
if(strcmp(tp,"END_DATA_FORMAT") == 0) {
rstate = R_KWORDS;
break;
}
if(strcmp(tp,"BEGIN_DATA") == 0) { /* Faulty table - cope gracefully */
rstate = R_DATA;
break;
}
if(strcmp(tp,"DEVICE_NAME") == 0) { /* Faulty CB table - cope gracefully */
/* It's unlikely anyone will use DEVICE_NAME as a field name */
/* Assume this is a keyword */
if ((kw = (char *)alloc_copy_data_type(p->al, cs_t, (void *)tp)) == NULL) {
err(p, -2, "cgats.alloc_copy_data_type() malloc fail");
pp->del(pp);
DBGF((DBGA,"Alloc data type failed\n"));
return p->errc;
}
rstate = R_KWORD_VALUE;
break;
}
first_field:; /* Direct leap - cope with faulty table */
if (p->add_field(p, p->ntables-1, tp, none_t) < 0) /* none == cs untill figure type */ {
pp->del(pp);
DBGF((DBGA,"Add field failed\n"));
return p->errc;
}
break;
}
case R_DATA: {
cgats_table *ct = &p->t[p->ntables-1];
DBGF((DBGA,"Got data value '%s'\n",tp));
if(strcmp(tp,"END_DATA") == 0) {
int i,j;
#ifdef NEVER
if (ct->nsets == 0) {
err(p,-1,"Error at line %d of file '%s': End of data without any data being read",pp->line,fp->fname(fp));
pp->del(pp);
DBGF((DBGA,"End of data without any data being read\n"));
return p->errc;
}
#endif // NEVER
if (expsets != 0 && ct->nsets != expsets) {
err(p,-1,"Error at line %d of file '%s': Read %d sets, expected %d sets",pp->line,fp->fname(fp),ct->nsets,expsets);
pp->del(pp);
DBGF((DBGA,"End of mimatch in number of sets\n"));
return p->errc;
}
if (ct->ndf != 0) {
err(p,-1,"Error at line %d of file '%s': Data was not an integer multiple of fields (remainder %d out of %d)",pp->line,fp->fname(fp),ct->ndf,ct->nfields);
pp->del(pp);
DBGF((DBGA,"Not an interger multiple of fields\n"));
return p->errc;
}
/* We now need to determine the data types */
/* and convert them appropriately */
for (i = 0; i < ct->nfields; i++) {
data_type bt = i_t, st;
for (j = 0; j < ct->nsets; j++) {
data_type ty;
ty = guess_type(((char *)ct->rfdata[j][i]));
if (ty == cs_t) {
bt = cs_t;
break; /* Early out */
} else if (ty == nqcs_t) {
if (bt == i_t || bt == r_t)
bt = ty;
} else if (ty == r_t) {
if (bt == i_t)
bt = ty;
} else { /* ty == i_t */
/* This is the default */
}
}
/* Got guessed type bt. Sanity check against known field types */
/* and promote if that seems reasonable */
st = standard_field(ct->fsym[i]);
if ((st == r_t && bt == i_t) /* If ambiguous, use standard field */
|| ((st == cs_t || st == nqcs_t) && bt == i_t) /* Promote any to string */
|| ((st == cs_t || st == nqcs_t) && bt == r_t) /* Promote any to string */
|| (st == nqcs_t && bt == cs_t)
|| (st == cs_t && bt == nqcs_t))
bt = st;
/* If standard type doesn't match what it should, throw an error */
if (st != none_t && st != bt) {
err(p, -1,"Error in file '%s': Field '%s' has unexpected type, should be '%s', is '%s'",fp->fname(fp),ct->fsym[i],data_type_desc[st],data_type_desc[bt]);
pp->del(pp);
DBGF((DBGA,"Standard field has unexpected data type\n"));
return p->errc;
}
/* Set field type, and then convert the fields to correct type. */
ct->ftype[i] = bt;
for (j = 0; j < ct->nsets; j++) {
switch(bt) {
case r_t: {
double dv;
dv = atof((char *)ct->rfdata[j][i]);
if ((ct->fdata[j][i] = alloc_copy_data_type(p->al, bt, (void *)&dv)) == NULL) {
err(p, -2, "cgats.alloc_copy_data_type() malloc fail");
pp->del(pp);
DBGF((DBGA,"Alloc copy data type failed\n"));
return p->errc;
}
break;
}
case i_t: {
int iv;
iv = atoi((char *)ct->rfdata[j][i]);
if ((ct->fdata[j][i] = alloc_copy_data_type(p->al, bt, (void *)&iv)) == NULL) {
err(p, -2, "cgats.alloc_copy_data_type() malloc fail");
pp->del(pp);
DBGF((DBGA,"Alloc copy data type failed\n"));
return p->errc = -2;
}
break;
}
case cs_t:
case nqcs_t: {
char *cv;
cv = ct->rfdata[j][i];
if ((ct->fdata[j][i]
= alloc_copy_data_type(p->al, bt, (void *)cv)) == NULL) {
err(p, -2, "cgats.alloc_copy_data_type() malloc fail");
pp->del(pp);
DBGF((DBGA,"Alloc copy data type failed\n"));
return p->errc = -2;
}
unquote_cs((char *)ct->fdata[j][i]);
break;
}
case none_t:
break;
}
}
}
tablef = p->ntables; /* Finished data for current table */
rstate = R_IDENT;
break;
}
/* Make sure fields have been decalared */
if (ct->nfields == 0) {
err(p, -1,"Error at line %d of file '%s': Found data without field definitions",pp->line,fp->fname(fp));
pp->del(pp);
DBGF((DBGA,"Found data without field definition\n"));
return p->errc;
}
/* Add the data item */
if (add_data_item(p, p->ntables-1, tp) < 0) {
pp->del(pp);
DBGF((DBGA,"Adding data item failed\n"));
return p->errc;
}
break;
}
}
}
pp->del(pp); /* Clean up the parse file */
if (p->ntables == 0)
return -1; /* Failed to load any table */
return 0;
}
/* Define the (one) variable CGATS type */
/* Return -2 & set errc and err on system error */
static int
set_cgats_type(cgats *p, const char *osym) {
cgatsAlloc *al = p->al;
p->errc = 0;
p->err[0] = '\000';
if (p->cgats_type != NULL)
al->free(al, p->cgats_type);
if ((p->cgats_type = (char *)al->malloc(al, (strlen(osym)+1) * sizeof(char))) == NULL)
return err(p,-2,"cgats.add_cgats_type(), malloc failed!");
strcpy(p->cgats_type,osym);
return 0;
}
/* Add an 'other' file identifier string, and return the oi. */
/* Use a zero length string to indicate a wildcard. */
/* Return -2 & set errc and err on system error */
static int
add_other(cgats *p, const char *osym) {
cgatsAlloc *al = p->al;
p->errc = 0;
p->err[0] = '\000';
p->nothers++;
if ((p->others = (char **)al->realloc(al, p->others, p->nothers * sizeof(char *))) == NULL)
return err(p,-2, "cgats.add_other(), realloc failed!");
if ((p->others[p->nothers-1] =
(char *)al->malloc(al, (strlen(osym)+1) * sizeof(char))) == NULL)
return err(p,-2,"cgats.add_other(), malloc failed!");
strcpy(p->others[p->nothers-1],osym);
return p->nothers-1;
}
/* Return the oi of the given other type */
/* return -ve and errc and err set on error */
static int get_oi(cgats *p, const char *osym) {
int oi;
p->errc = 0;
p->err[0] = '\000';
for (oi = 0; oi < p->nothers; oi++) {
if (strcmp(p->others[oi], osym) == 0)
return oi;
}
return err(p,-1,"cgats.get_oi(), failed to find '%s'!",osym);
}
/* Add a new (empty) table to the structure */
/* Return the index of the table. */
/* tt defines the table type, and oi is used if tt = tt_other */
/* Return -2 & set errc and err on system error */
static int
add_table(cgats *p, table_type tt, int oi) {
cgatsAlloc *al = p->al;
cgats_table *t;
p->errc = 0;
p->err[0] = '\000';
p->ntables++;
if ((p->t = (cgats_table *) al->realloc(al, p->t, p->ntables * sizeof(cgats_table))) == NULL)
return err(p,-2, "cgats.add_table(), realloc failed!");
memset(&p->t[p->ntables-1],0,sizeof(cgats_table));
t = &p->t[p->ntables-1];
t->al = al; /* Pointer to allocator */
t->tt = tt;
t->oi = oi;
return p->ntables-1;
}
/* Override the table type */
static int
set_table_type(cgats *p, int table, table_type tt, int oi) {
cgats_table *t = &p->t[table];
t->tt = tt;
t->oi = oi;
return 0;
}
/* set or reset table flags */
/* The sup_id flag suppreses the writing of the file identifier string for the table */
/* The sup_kwords flag suppreses the writing of the standard keyword definitions for the table */
/* The sup_fields flag suppreses the writing of the field definitions for the table */
/* The assumption is that the previous tables id and/or fields will be correct for */
/* the table that have these flags set. */
/* Return -1 & set errc and err on error */
static int set_table_flags(cgats *p, int table, int sup_id, int sup_kwords, int sup_fields) {
cgats_table *t;
p->errc = 0;
p->err[0] = '\000';
if (table < 0 || table >= p->ntables)
return err(p,-1, "cgats.set_table_flags(), table number '%d' is out of range",table);
t = &p->t[table];
if (sup_id == 0 && (sup_kwords != 0 || sup_fields != 0))
return err(p,-1, "cgats.set_table_flags(), Can't suppress kwords or fields if ID is not suppressed");
t->sup_id = sup_id;
t->sup_kwords = sup_kwords;
t->sup_fields = sup_fields;
return 0;
}
/* Append a new keyword/value + optional comment pair to the table */
/* If no comment is provided, kcom should be set to NULL */
/* A comment only line can be inserted amongst the keywords by providing */
/* NULL values for ksym and kdata, and the comment in kcom */
/* Return the index of the new keyword, or -1, err & errc on error */
static int
add_kword(cgats *p, int table, const char *ksym, const char *kdata, const char *kcom) {
cgats_table *t;
p->errc = 0;
p->err[0] = '\000';
if (table < 0 || table >= p->ntables)
return err(p,-1, "cgats.add_kword(), table number '%d' is out of range",table);
t = &p->t[table];
return add_kword_at(p, table, t->nkwords, ksym, kdata, kcom);
}
/* Replace or append a new keyword/value pair + optional comment */
/* to the table in the given position. The keyword will be appended */
/* if it is < 0. */
/* If no comment is provided, kcom should be set to NULL */
/* A comment only line can be inserted amongst the keywords by providing */
/* NULL values for ksym and kdata, and the comment in kcom */
/* Return the index of the keyword, or -1, err & errc on error */
static int
add_kword_at(cgats *p, int table, int pos, const char *ksym, const char *kdata, const char *kcom) {
cgatsAlloc *al = p->al;
cgats_table *t;
p->errc = 0;
p->err[0] = '\000';
if (table < 0 || table >= p->ntables) {
DBGF((DBGA,"add_kword_at: table is invalid\n"));
return err(p,-1, "cgats.add_kword(), table number '%d' is out of range",table);
}
t = &p->t[table];
if (ksym != NULL && cs_has_ws(ksym)) { /* oops */
DBGF((DBGA,"add_kword_at: keyword '%s' is illegal (embedded white space, quote or comment character)\n",ksym));
return err(p,-1, "cgats.add_kword(), keyword '%s'is illegal",ksym);
}
if (ksym != NULL && reserved_kword(ksym)) { /* oops */
DBGF((DBGA,"add_kword_at: keyword '%s' is illegal (reserved)\n",ksym));
return err(p,-1, "cgats.add_kword(), keyword '%s'is generated automatically",ksym);
}
if (pos < 0 || pos >= t->nkwords) { /* This is an append */
t->nkwords++;
if (t->nkwords > t->nkwordsa) { /* Allocate keyword pointers in groups of 8 */
t->nkwordsa += 8;
if ((t->ksym = (char **)al->realloc(al, t->ksym, t->nkwordsa * sizeof(char *))) == NULL)
return err(p,-2, "cgats.add_kword(), realloc failed!");
if ((t->kdata = (char **)al->realloc(al, t->kdata, t->nkwordsa * sizeof(char *)))
== NULL)
return err(p,-2, "cgats.add_kword(), realloc failed!");
if ((t->kcom = (char **)al->realloc(al, t->kcom, t->nkwordsa * sizeof(char *)))
== NULL)
return err(p,-2, "cgats.add_kword(), realloc failed!");
}
pos = t->nkwords-1;
} else { /* This is a replacement */
if (t->ksym[pos] != NULL)
al->free(al, t->ksym[pos]);
if (t->kdata[pos] != NULL)
al->free(al, t->kdata[pos]);
if (t->kcom[pos] != NULL)
al->free(al, t->kcom[pos]);
}
if (ksym != NULL) {
if ((t->ksym[pos] = (char *)alloc_copy_data_type(al, cs_t, (void *)ksym)) == NULL)
return err(p,-2,"cgats.alloc_copy_data_type() malloc fail");
} else
t->ksym[pos] = NULL;
if (kdata != NULL) {
if ((t->kdata[pos] = (char *)alloc_copy_data_type(al, cs_t, (void *)kdata)) == NULL)
return err(p,-2,"cgats.alloc_copy_data_type() malloc fail");
} else
t->kdata[pos] = NULL;
if (kcom != NULL) {
if ((t->kcom[pos] = (char *)alloc_copy_data_type(al, cs_t, (void *)kcom)) == NULL)
return err(p,-2,"cgats.alloc_copy_data_type() malloc fail");
} else
t->kcom[pos] = NULL;
return pos;
}
/* Add a new field to the table */
/* Return the index of the field */
/* return -1 or -2, errc & err on error */
static int
add_field(cgats *p, int table, const char *fsym, data_type ftype) {
cgatsAlloc *al = p->al;
cgats_table *t;
data_type st;
p->errc = 0;
p->err[0] = '\000';
if (table < 0 || table >= p->ntables) {
return err(p,-1,"cgats.add_field(), table parameter out of range");
}
t = &p->t[table];
if (t->nsets != 0) {
return err(p,-1,"cgats.add_field(), attempt to add field to non-empty table");
}
/* Check the field name is reasonable */
if (cs_has_ws(fsym)) {
return err(p,-1,"cgats.add_kword(), field name '%s'is illegal",fsym);
}
if (ftype == none_t)
ftype = cs_t; /* Fudge - unknown type yet, used for reads */
else {
/* Check that the data type is reasonable */
st = standard_field(fsym);
if (st == nqcs_t && ftype == cs_t) /* Fudge - standard type to non-quoted if normal */
ftype = nqcs_t;
if (st != none_t && st != ftype) {
return err(p,-1,"cgats.add_field(): unexpected data type for standard field name");
}
}
t->nfields++;
if (t->nfields > t->nfieldsa) {
/* Allocate fields in groups of 32 */
t->nfieldsa += 32;
if ((t->fsym = (char **)al->realloc(al, t->fsym, t->nfieldsa * sizeof(char *))) == NULL) {
return err(p,-2,"cgats.add_field(), realloc failed!");
}
if ((t->ftype = (data_type *)al->realloc(al, t->ftype, t->nfieldsa * sizeof(data_type)))
== NULL) {
return err(p,-2,"cgats.add_field(), realloc failed!");
}
}
if ((t->fsym[t->nfields-1] = (char *)alloc_copy_data_type(al, cs_t, (void *)fsym)) == NULL) {
return err(p,-2,"cgats.alloc_copy_data_type() malloc fail");
}
t->ftype[t->nfields-1] = ftype;
return t->nfields-1;
}
/* Clear all fields in the table */
/* return 0 if OK */
/* return -1 or -2, errc & err on error */
static int
clear_fields(cgats *p, int table) {
cgatsAlloc *al = p->al;
int i;
cgats_table *t;
p->errc = 0;
p->err[0] = '\000';
if (table < 0 || table >= p->ntables)
return err(p,-1,"cgats.clear_field(), table parameter out of range");
t = &p->t[table];
if (t->nsets != 0)
return err(p,-1,"cgats.clear_field(), attempt to clear fields in a non-empty table");
/* Free all the field symbols */
if (t->fsym != NULL) {
for (i = 0; i < t->nfields; i++)
if(t->fsym[i] != NULL)
al->free(al, t->fsym[i]);
al->free(al, t->fsym);
t->fsym = NULL;
}
/* Free array of field types */
if (t->ftype != NULL)
al->free(al, t->ftype);
t->ftype = NULL;
/* Zero all the field counters */
t->nfields = 0;
t->nfieldsa = 0;
return 0;
}
/* Add a set of data */
/* return 0 normally. */
/* return -2, -1, errc & err on error */
static int
add_set(cgats *p, int table, ...) {
cgatsAlloc *al = p->al;
va_list args;
int i;
cgats_table *t;
va_start(args, table);
p->errc = 0;
p->err[0] = '\000';
if (table < 0 || table >= p->ntables)
return err(p,-1,"cgats.add_kword(), table parameter out of range");
t = &p->t[table];
if (t->nfields == 0)
return err(p,-1,"cgats.add_set(), attempt to add set when no fields are defined");
t->nsets++;
if (t->nsets > t->nsetsa) /* Allocate space for more sets */ {
/* Allocate set pointers in groups of 100 */
t->nsetsa += 100;
if ((t->fdata = (void ***)al->realloc(al, t->fdata, t->nsetsa * sizeof(void **))) == NULL)
return err(p,-2,"cgats.add_set(), realloc failed!");
}
/* Allocate set pointer to data element values */
if ((t->fdata[t->nsets-1] = (void **)al->malloc(al, t->nfields * sizeof(void *))) == NULL)
return err(p,-2,"cgats.add_set(), malloc failed!");
/* Allocate and copy data to new set */
for (i = 0; i < t->nfields; i++) {
switch(t->ftype[i]) {
case r_t: {
double dv;
dv = va_arg(args, double);
if ((t->fdata[t->nsets-1][i] = alloc_copy_data_type(al, t->ftype[i], (void *)&dv)) == NULL)
return err(p,-2,"cgats.alloc_copy_data_type() malloc fail");
break;
}
case i_t: {
int iv;
iv = va_arg(args, int);
if ((t->fdata[t->nsets-1][i] = alloc_copy_data_type(al, t->ftype[i], (void *)&iv)) == NULL)
return err(p,-2,"cgats.alloc_copy_data_type() malloc fail");
break;
}
case cs_t:
case nqcs_t: {
char *sv;
sv = va_arg(args, char *);
if ((t->fdata[t->nsets-1][i] = alloc_copy_data_type(al, t->ftype[i], (void *)sv)) == NULL)
return err(p,-2,"cgats.alloc_copy_data_type() malloc fail");
break;
}
default:
return err(p,-1,"cgats.add_set(), field has unknown data type");
}
}
va_end(args);
return 0;
}
/* Add a set of data from void array */
/* (Courtesy of Neil Okamoto) */
/* return 0 normally. */
/* return -2, -1, errc & err on error */
static int
add_setarr(cgats *p, int table, cgats_set_elem *args) {
cgatsAlloc *al = p->al;
int i;
cgats_table *t;
p->errc = 0;
p->err[0] = '\000';
if (table < 0 || table >= p->ntables)
return err(p,-1,"cgats.add_setarr(), table parameter out of range");
t = &p->t[table];
if (t->nfields == 0)
return err(p,-1,"cgats.add_setarr(), attempt to add set when no fields are defined");
t->nsets++;
if (t->nsets > t->nsetsa) /* Allocate space for more sets */ {
/* Allocate set pointers in groups of 100 */
t->nsetsa += 100;
if ((t->fdata = (void ***)al->realloc(al,t->fdata, t->nsetsa * sizeof(void **))) == NULL)
return err(p,-2,"cgats.add_set(), realloc failed!");
}
/* Allocate set pointer to data element values */
if ((t->fdata[t->nsets-1] = (void **)al->malloc(al,t->nfields * sizeof(void *))) == NULL)
return err(p,-2,"cgats.add_set(), malloc failed!");
/* Allocate and copy data to new set */
for (i = 0; i < t->nfields; i++) {
switch(t->ftype[i]) {
case r_t: {
double dv;
dv = args[i].d;
if ((t->fdata[t->nsets-1][i] = alloc_copy_data_type(al, t->ftype[i], (void *)&dv)) == NULL)
return err(p,-2,"cgats.alloc_copy_data_type() malloc fail");
break;
}
case i_t: {
int iv;
iv = args[i].i;
if ((t->fdata[t->nsets-1][i] = alloc_copy_data_type(al, t->ftype[i], (void *)&iv)) == NULL)
return err(p,-2,"cgats.alloc_copy_data_type() malloc fail");
break;
}
case cs_t:
case nqcs_t: {
char *sv;
sv = args[i].c;
if ((t->fdata[t->nsets-1][i] = alloc_copy_data_type(al, t->ftype[i], (void *)sv)) == NULL)
return err(p,-2,"cgats.alloc_copy_data_type() malloc fail");
break;
}
default:
return err(p,-1,"cgats.add_set(), field has unknown data type");
}
}
return 0;
}
/* Fill a suitable set_element with a set of data. */
/* Note a returned char pointer is to a string in *p */
/* return 0 normally. */
/* return -2, -1, errc & err on error */
static int
get_setarr(cgats *p, int table, int set_index, cgats_set_elem *args) {
int i;
cgats_table *t;
p->errc = 0;
p->err[0] = '\000';
if (table < 0 || table >= p->ntables)
return err(p,-1,"cgats.get_setarr(), table parameter out of range");
t = &p->t[table];
if (set_index < 0 || set_index >= t->nsets)
return err(p,-1,"cgats.get_setarr(), set parameter out of range");
for (i = 0; i < t->nfields; i++) {
switch(t->ftype[i]) {
case r_t:
args[i].d = *((double *)t->fdata[set_index][i]);
break;
case i_t:
args[i].i = *((int *)t->fdata[set_index][i]);
break;
case cs_t:
case nqcs_t:
args[i].c = ((char *)t->fdata[set_index][i]);
break;
default:
return err(p,-1,"cgats.get_setarr(), field has unknown data type");
}
}
return 0;
}
/* Add an item of data to rgdata[][] from the read file. */
/* return 0 normally. */
/* return -2, -1, errc & err on error */
static int
add_data_item(cgats *p, int table, void *data) {
cgatsAlloc *al = p->al;
cgats_table *t;
p->errc = 0;
p->err[0] = '\000';
if (table < 0 || table >= p->ntables)
return err(p,-1,"cgats.add_kword(), table parameter out of range");
t = &p->t[table];
if (t->nfields == 0)
return err(p,-1,"cgats.add_item(), attempt to add data when no fields are defined");
if (t->ndf == 0) { /* We're about to do the first element of a new set */
t->nsets++;
if (t->nsets > t->nsetsa) { /* Allocate space for more sets */
/* Allocate set pointers in groups of 100 */
t->nsetsa += 100;
if ((t->rfdata = (char ***)al->realloc(al, t->rfdata, t->nsetsa * sizeof(void **)))
== NULL)
return err(p,-2,"cgats.add_item(), realloc failed!");
if ((t->fdata = (void ***)al->realloc(al, t->fdata, t->nsetsa * sizeof(void **)))
== NULL)
return err(p,-2,"cgats.add_item(), realloc failed!");
}
/* Allocate set pointer to data element values */
if ((t->rfdata[t->nsets-1] = (char **)al->malloc(al, t->nfields * sizeof(void *))) == NULL)
return err(p,-2,"cgats.add_item(), malloc failed!");
if ((t->fdata[t->nsets-1] = (void **)al->malloc(al, t->nfields * sizeof(void *))) == NULL)
return err(p,-2,"cgats.add_item(), malloc failed!");
}
/* Data type is always cs_t at this point, because we haven't decided the type */
if ((t->rfdata[t->nsets-1][t->ndf] = alloc_copy_data_type(al, cs_t, data)) == NULL)
return err(p,-2,"cgats.alloc_copy_data_type() malloc fail");
if (++t->ndf >= t->nfields)
t->ndf = 0;
return 0;
}
/* Write structure into cgats file */
/* Return -ve, errc & err if there was an error */
static int
cgats_write(cgats *p, cgatsFile *fp) {
cgatsAlloc *al = p->al;
int i;
int table,set,field;
int *sfield = NULL; /* Standard field flag */
p->errc = 0;
p->err[0] = '\000';
DBGF((DBGA,"CGATS write called, ntables = %d\n",p->ntables));
for (table = 0; table < p->ntables; table++) {
cgats_table *t = &p->t[table];
DBGF((DBGA,"CGATS writing table %d\n",table));
/* Figure out the standard and non-standard fields */
if (t->nfields > 0)
if ((sfield = (int *)al->malloc(al, t->nfields * sizeof(int))) == NULL)
return err(p,-2,"cgats.write(), malloc failed!");
for (field = 0; field < t->nfields; field++) {
if (standard_field(t->fsym[field]) != none_t)
sfield[field] = 1; /* Is standard */
else
sfield[field] = 0;
}
if (!t->sup_kwords) /* If not suppressed */ {
/* Make sure table has basic keywords */
if ((i = p->find_kword(p,table,"ORIGINATOR")) < 0) /* Create it */
if (p->add_kword(p,table,"ORIGINATOR","Not specified", NULL) < 0) {
al->free(al, sfield);
return p->errc;
}
if ((i = p->find_kword(p,table,"DESCRIPTOR")) < 0) /* Create it */
if (p->add_kword(p,table,"DESCRIPTOR","Not specified", NULL) < 0) {
al->free(al, sfield);
return p->errc;
}
if ((i = p->find_kword(p,table,"CREATED")) < 0) { /* Create it */
static char *amonths[] = {"January","February","March","April",
"May","June","July","August","September",
"October","November","December"};
time_t ctime;
struct tm *ptm;
char tcs[100];
ctime = time(NULL);
ptm = localtime(&ctime);
sprintf(tcs,"%s %d, %d",amonths[ptm->tm_mon],ptm->tm_mday,1900+ptm->tm_year);
if (p->add_kword(p,table,"CREATED",tcs, NULL) < 0) {
al->free(al, sfield);
return p->errc;
}
}
/* And table type specific keywords */
/* (Not sure this is correct - CGATS.5 appendix J is not specific enough) */
switch(t->tt) {
case it8_7_1:
case it8_7_2: /* Physical target reference files */
if ((i = p->find_kword(p,table,"MANUFACTURER")) < 0) /* Create it */
if (p->add_kword(p,table,"MANUFACTURER","Not specified", NULL) < 0) {
al->free(al, sfield);
return p->errc;
}
if ((i = p->find_kword(p,table,"PROD_DATE")) < 0) /* Create it */
if (p->add_kword(p,table,"PROD_DATE","Not specified", NULL) < 0) {
al->free(al, sfield);
return p->errc;
}
if ((i = p->find_kword(p,table,"SERIAL")) < 0) /* Create it */
if (p->add_kword(p,table,"SERIAL","Not specified", NULL) < 0) {
al->free(al, sfield);
return p->errc;
}
if ((i = p->find_kword(p,table,"MATERIAL")) < 0) /* Create it */
if (p->add_kword(p,table,"MATERIAL","Not specified", NULL) < 0) {
al->free(al, sfield);
return p->errc;
}
break;
case it8_7_3: /* Target measurement files */
case it8_7_4:
case cgats_5:
case cgats_X:
if ((i = p->find_kword(p,table,"INSTRUMENTATION")) < 0) /* Create it */
if (p->add_kword(p,table,"INSTRUMENTATION","Not specified", NULL) < 0) {
al->free(al, sfield);
return p->errc;
}
if ((i = p->find_kword(p,table,"MEASUREMENT_SOURCE")) < 0) /* Create it */
if (p->add_kword(p,table,"MEASUREMENT_SOURCE","Not specified", NULL) < 0) {
al->free(al, sfield);
return p->errc;
}
if ((i = p->find_kword(p,table,"PRINT_CONDITIONS")) < 0) /* Create it */
if (p->add_kword(p,table,"PRINT_CONDITIONS","Not specified", NULL) < 0) {
al->free(al, sfield);
return p->errc;
}
break;
case tt_other:
/* We enforce no pre-defined keywords for user defined file types */
break;
default:
break;
}
}
/* Output the table */
/* First the table identifier */
if (!t->sup_id) /* If not suppressed */ {
switch(t->tt) {
case it8_7_1:
if (fp->gprintf(fp,"IT8.7/1\n\n") < 0)
goto write_error;
break;
case it8_7_2:
if (fp->gprintf(fp,"IT8.7/2\n\n") < 0)
goto write_error;
break;
case it8_7_3:
if (fp->gprintf(fp,"IT8.7/3\n\n") < 0)
goto write_error;
break;
case it8_7_4:
if (fp->gprintf(fp,"IT8.7/4\n\n") < 0)
goto write_error;
break;
case cgats_5:
if (fp->gprintf(fp,"CGATS.5\n\n") < 0)
goto write_error;
break;
case cgats_X: /* variable CGATS type */
if (p->cgats_type == NULL)
goto write_error;
if (fp->gprintf(fp,"%-7s\n\n", p->cgats_type) < 0)
goto write_error;
break;
case tt_other: /* User defined file identifier */
if (fp->gprintf(fp,"%-7s\n\n",p->others[t->oi]) < 0)
goto write_error;
break;
case tt_none:
break;
}
} else { /* At least space the next table out a bit */
if (table == 0) {
al->free(al, sfield);
return err(p,-1,"cgats_write(), ID should not be suppressed on first table");
}
if (t->tt != p->t[table-1].tt || (t->tt == tt_other && t->oi != p->t[table-1].oi)) {
al->free(al, sfield);
return err(p,-1,"cgats_write(), ID should not be suppressed when table %d type is not the same as previous table",table);
}
if (fp->gprintf(fp,"\n\n") < 0)
goto write_error;
}
/* Then all the keywords */
for (i = 0; i < t->nkwords; i++) {
char *qs = NULL;
DBGF((DBGA,"CGATS writing keyword %d\n",i));
/* Keyword and data if it is present */
if (t->ksym[i] != NULL && t->kdata[i] != NULL) {
if (p->emit_keywords && !standard_kword(t->ksym[i])) { /* Do the right thing */
if ((qs = quote_cs(al, t->ksym[i])) == NULL) {
al->free(al, sfield);
return err(p,-2,"quote_cs() malloc failed!");
}
if (fp->gprintf(fp,"KEYWORD %s\n",qs) < 0) {
al->free(al, qs);
goto write_error;
}
al->free(al, qs);
}
if ((qs = quote_cs(al, t->kdata[i])) == NULL) {
al->free(al, sfield);
return err(p,-2,"quote_cs() malloc failed!");
}
if (fp->gprintf(fp,"%s %s%s",t->ksym[i],qs,
t->kcom[i] == NULL ? "\n":"\t") < 0) {
al->free(al, qs);
goto write_error;
}
al->free(al, qs);
}
/* Comment if its present */
if (t->kcom[i] != NULL) {
if (fp->gprintf(fp,"# %s\n",t->kcom[i]) < 0) {
al->free(al, qs);
goto write_error;
}
}
}
/* Then the field specification */
if (!t->sup_fields) { /* If not suppressed */
if (fp->gprintf(fp,"\n") < 0)
goto write_error;
/* Declare any non-standard fields */
for (field = 0; field < t->nfields; field++) {
if (p->emit_keywords && !sfield[field]) /* Non-standard */ {
char *qs;
if ((qs = quote_cs(al, t->fsym[field])) == NULL) {
al->free(al, sfield);
return err(p,-2,"quote_cs() malloc failed!");
}
if (fp->gprintf(fp,"KEYWORD %s\n",qs) < 0) {
al->free(al, qs);
goto write_error;
}
al->free(al, qs);
}
}
if (fp->gprintf(fp,"NUMBER_OF_FIELDS %d\n",t->nfields) < 0)
goto write_error;
if (fp->gprintf(fp,"BEGIN_DATA_FORMAT\n") < 0)
goto write_error;
for (field = 0; field < t->nfields; field ++) {
DBGF((DBGA,"CGATS writing field %d\n",field));
if (fp->gprintf(fp,"%s ",t->fsym[field]) < 0)
goto write_error;
}
if (fp->gprintf(fp,"\nEND_DATA_FORMAT\n") < 0)
goto write_error;
} else { /* Check that it is safe to suppress fields */
cgats_table *pt = &p->t[table-1];
if (table == 0) {
al->free(al, sfield);
return err(p,-1,"cgats_write(), Fields should not be suppressed on first table");
}
if (t->nfields != pt->nfields) {
al->free(al, sfield);
return err(p,-1,"cgats_write(), Fields should not be suppressed when table %d different number than previous table",table);
}
for (field = 0; field < t->nfields; field ++)
if (strcmp(t->fsym[field],pt->fsym[field]) != 0
|| t->ftype[field] != pt->ftype[field]) {
al->free(al, sfield);
return err(p,-1,"cgats_write(), Fields should not be suppressed when table %d types is not the same as previous table",table);
}
}
/* Then the actual data */
if (fp->gprintf(fp,"\nNUMBER_OF_SETS %d\n",t->nsets) < 0)
goto write_error;
if (fp->gprintf(fp,"BEGIN_DATA\n") < 0)
goto write_error;
for (set = 0; set < t->nsets; set++) {
DBGF((DBGA,"CGATS writing set %d\n",set));
for (field = 0; field < t->nfields; field++) {
data_type tt;
if (t->ftype[field] == r_t) {
char fmt[30];
double val = *((double *)t->fdata[set][field]);
real_format(val, REAL_SIGDIG, fmt);
strcat(fmt," ");
if (fp->gprintf(fp,fmt,val) < 0)
goto write_error;
} else if (t->ftype[field] == i_t) {
if (fp->gprintf(fp,"%d ",*((int *)t->fdata[set][field])) < 0)
goto write_error;
} else if (t->ftype[field] == nqcs_t
&& !cs_has_ws((char *)t->fdata[set][field])
&& (sfield[field] || (tt = guess_type((char *)t->fdata[set][field]),
tt != i_t && tt != r_t))) {
/* We can only print a non-quote string if it doesn't contain white space, */
/* quote or comment characters, and if it is a standard field or */
/* can't be mistaken for a number. */
if (fp->gprintf(fp,"%s ",(char *)t->fdata[set][field]) < 0)
goto write_error;
} else if (t->ftype[field] == nqcs_t
|| t->ftype[field] == cs_t) {
char *qs;
if ((qs = quote_cs(al, (char *)t->fdata[set][field])) == NULL) {
al->free(al, sfield);
return err(p,-2,"quote_cs() malloc failed!");
}
if (fp->gprintf(fp,"%s ",qs) < 0) {
al->free(al, qs);
goto write_error;
}
al->free(al, qs);
} else {
al->free(al, sfield);
return err(p,-1,"cgats_write(), illegal data type found");
}
}
if (fp->gprintf(fp,"\n") < 0)
goto write_error;
}
if (fp->gprintf(fp,"END_DATA\n") < 0)
goto write_error;
if (sfield != NULL)
al->free(al, sfield);
sfield = NULL;
}
return 0;
write_error:
err(p,-1,"Write error to file '%s'",fp->fname(fp));
if (sfield != NULL)
al->free(al, sfield);
return p->errc;
}
/* Allocate space for data with given type, and copy it from source */
/* Return NULL if alloc failed, or unknown data type */
static void *
alloc_copy_data_type(cgatsAlloc *al, data_type dtype, void *dpoint) {
switch(dtype) {
case r_t: { /* Real value */
double *p;
if ((p = (double *)al->malloc(al, sizeof(double))) == NULL)
return NULL;
*p = *((double *)dpoint);
return (void *)p;
}
case i_t: { /* Integer value */
int *p;
if ((p = (int *)al->malloc(al, sizeof(int))) == NULL)
return NULL;
*p = *((int *)dpoint);
return (void *)p;
}
case cs_t: /* Character string */
case nqcs_t: { /* Character string */
char *p;
if ((p = (char *)al->malloc(al, (strlen(((char *)dpoint))+1) * sizeof(char))) == NULL)
return NULL;
strcpy(p, (char *)dpoint);
return (void *)p;
}
case none_t:
default:
return NULL;
}
return NULL; /* Shut the compiler up */
}
/* See if the keyword name is a standard one */
/* Return non-zero if it is standard */
static int
standard_kword(const char *ksym) {
if (ksym == NULL)
return 0;
if (strcmp(ksym,"ORIGINATOR") == 0)
return 1;
if (strcmp(ksym,"DESCRIPTOR") == 0)
return 1;
if (strcmp(ksym,"CREATED") == 0)
return 1;
if (strcmp(ksym,"MANUFACTURER") == 0)
return 1;
if (strcmp(ksym,"PROD_DATE") == 0)
return 1;
if (strcmp(ksym,"SERIAL") == 0)
return 1;
if (strcmp(ksym,"MATERIAL") == 0)
return 1;
if (strcmp(ksym,"INSTRUMENTATION") == 0)
return 1;
if (strcmp(ksym,"MEASUREMENT_SOURCE") == 0)
return 1;
if (strcmp(ksym,"PRINT_CONDITIONS") == 0)
return 1;
return 0;
}
/* See if the keyword name is reserved. */
/* (code generates it automatically) */
/* Return non-zero if it is reserved */
static int
reserved_kword(const char *ksym) {
if (ksym == NULL)
return 0;
if (strcmp(ksym,"NUMBER_OF_FIELDS") == 0)
return 1;
if (strcmp(ksym,"BEGIN_DATA_FORMAT") == 0)
return 1;
if (strcmp(ksym,"END_DATA_FORMAT") == 0)
return 1;
if (strcmp(ksym,"NUMBER_OF_SETS") == 0)
return 1;
if (strcmp(ksym,"BEGIN_DATA") == 0)
return 1;
if (strcmp(ksym,"END_DATA") == 0)
return 1;
if (strcmp(ksym,"KEYWORD") == 0)
return 1;
return 0;
}
/* See if the field name is a standard one */
/* with an expected data type */
static data_type
standard_field(const char *fsym) {
if (strcmp(fsym,"SAMPLE_ID") == 0)
return nqcs_t;
if (strcmp(fsym,"STRING") == 0)
return cs_t;
if (strncmp(fsym,"CMYK_",5) == 0) {
if (fsym[5] == 'C')
return r_t;
if (fsym[5] == 'M')
return r_t;
if (fsym[5] == 'Y')
return r_t;
if (fsym[5] == 'K')
return r_t;
return none_t;
}
/* Non standard, but logical */
if (strncmp(fsym,"CMY_",4) == 0) {
if (fsym[4] == 'C')
return r_t;
if (fsym[4] == 'M')
return r_t;
if (fsym[4] == 'Y')
return r_t;
return none_t;
}
if (strncmp(fsym,"D_",2) == 0) {
if (strcmp(fsym + 2, "RED") == 0)
return r_t;
if (strcmp(fsym + 2, "GREEN") == 0)
return r_t;
if (strcmp(fsym + 2, "BLUE") == 0)
return r_t;
if (strcmp(fsym + 2, "VIS") == 0)
return r_t;
return none_t;
}
if (strncmp(fsym,"RGB_",4) == 0) {
if (fsym[4] == 'R')
return r_t;
if (fsym[4] == 'G')
return r_t;
if (fsym[4] == 'B')
return r_t;
return none_t;
}
if (strncmp(fsym,"SPECTRAL_",9) == 0) {
if (strcmp(fsym + 9, "NM") == 0)
return r_t;
if (strcmp(fsym + 9, "PCT") == 0)
return r_t;
return none_t;
}
if (strncmp(fsym,"XYZ_",4) == 0) {
if (fsym[4] == 'X')
return r_t;
if (fsym[4] == 'Y')
return r_t;
if (fsym[4] == 'Z')
return r_t;
return none_t;
}
if (strncmp(fsym,"XYY_",4) == 0) {
if (fsym[4] == 'X')
return r_t;
if (fsym[4] == 'Y')
return r_t;
if (strcmp(fsym + 4, "CAPY") == 0)
return r_t;
return none_t;
}
if (strncmp(fsym,"LAB_",4) == 0) {
if (fsym[4] == 'L')
return r_t;
if (fsym[4] == 'A')
return r_t;
if (fsym[4] == 'B')
return r_t;
if (fsym[4] == 'C')
return r_t;
if (fsym[4] == 'H')
return r_t;
if (strcmp(fsym + 4, "DE") == 0)
return r_t;
return none_t;
}
if (strncmp(fsym,"STDEV_",6) == 0) {
if (fsym[6] == 'X')
return r_t;
if (fsym[6] == 'Y')
return r_t;
if (fsym[6] == 'Z')
return r_t;
if (fsym[6] == 'L')
return r_t;
if (fsym[6] == 'A')
return r_t;
if (fsym[6] == 'B')
return r_t;
if (strcmp(fsym + 6, "DE") == 0)
return r_t;
return none_t;
}
return none_t;
}
/* Return non-zero if char string has an embedded */
/* white space, quote or comment character. */
static int
cs_has_ws(const char *cs)
{
int i;
for (i = 0; cs[i] != '\000'; i++)
{
switch (cs[i])
{
case ' ':
case '\r':
case '\n':
case '\t':
case '"':
case '#':
return 1;
}
}
return 0;
}
/* Return a string with quotes added */
/* Return NULL if malloc failed */
/* (Returned string should be free()'d after use) */
static char *
quote_cs(cgatsAlloc *al, const char *cs) {
int i,j;
char *rs;
/* see how much space we need for returned string */
for (i = 0, j = 3; cs[i] != '\000'; i++, j++)
if (cs[i] == '"')
j++;
if ((rs = (char *)al->malloc(al, j * sizeof(char))) == NULL) {
return NULL;
}
j = 0;
rs[j++] = '"';
for (i = 0; cs[i] != '\000'; i++, j++) {
if (cs[i] == '"')
rs[j++] = '"';
rs[j] = cs[i];
}
rs[j++] = '"';
rs[j] = '\000';
return rs;
}
/* Remove quotes from the string */
static void
unquote_cs(char *cs) {
int sl,i,j;
int s; /* flag - set if skipped last character */
sl = strlen(cs);
if (sl < 2 || cs[0] != '"' || cs[sl-1] != '"')
return;
for (i = 1, j = 0, s = 1; i < (sl-1); i++) {
if (s == 1 || cs[i-1] != '"' || cs[i] != '"') {
cs[j++] = cs[i]; /* skip second " in a row */
s = 0;
} else
s = 1;
}
cs[j] = '\000';
return;
}
/* Guess the data type from the string */
static data_type
guess_type(const char *cs)
{
int i;
int rf = 0;
/* See if its a quoted string */
if (cs[0] == '"')
{
for (i = 1;cs[i] != '\000';i++);
if (i >= 2 && cs[i-1] == '"')
return cs_t;
return nqcs_t;
}
/* See if its an integer or real */
for (i = 0;;i++)
{
char c = cs[i];
if (c == '\000')
break;
if (c >= '0' && c <= '9') /* Strictly numeric */
continue;
if (c == '-' && /* Allow appropriate '-' */
( i == 0 ||
( i > 0 && (cs[i-1] == 'e' || cs[i-1] == 'E'))))
continue;
if (c == '+' && /* Allow appropriate '+' */
( i == 0 ||
( i > 0 && (cs[i-1] == 'e' || cs[i-1] == 'E'))))
continue;
if (!(rf & 3) && c == '.') /* Allow one '.' before 'e' in real */
{
rf |= 1;
continue;
}
if (!(rf & 2) && (c == 'e' || c == 'E')) /* Allow one 'e' in real */
{
rf |= 2;
continue;
}
/* Not numeric or incorrect 'e' or '.' or '-' or '+' */
return nqcs_t;
}
if (rf)
return r_t;
return i_t;
}
/* Set the character format to the appropriate printf() */
/* format given the real value and the desired number of significant digits. */
/* We try to do this while not using the %e format for normal values. */
/* The fmt string space is assumed to be big enough to contain the format */
static void
real_format(double value, int nsd, char *fmt) {
int ndigs;
int tot = nsd + 1;
int xtot = tot;
if (value == 0.0) {
sprintf(fmt,"%%%d.%df",tot,tot-2);
return;
}
if (value != value) { /* Hmm. A nan */
sprintf(fmt,"%%f");
return;
}
if (value < 0.0) {
value = -value;
xtot++;
}
if (value < 1.0) {
int thr = -5;
ndigs = (int)(log10(value));
if (ndigs > 310 || ndigs < 310) { /* Protect against silliness.. */
strcpy(fmt,"%g");
return;
}
if (ndigs <= thr) {
sprintf(fmt,"%%%d.%de",xtot,tot-2);
return;
}
sprintf(fmt,"%%%d.%df",xtot-ndigs,nsd-ndigs);
return;
} else {
int thr = -0;
ndigs = (int)(log10(value));
if (ndigs > 310 || ndigs < 310) { /* Protect against silliness.. */
strcpy(fmt,"%g");
return;
}
if (ndigs >= (nsd + thr)) {
sprintf(fmt,"%%%d.%de",xtot,tot-2);
return;
}
sprintf(fmt,"%%%d.%df",xtot,(nsd + thr)-ndigs);
return;
}
}
/* ---------------------------------------------------------- */
/* Debug and test code */
/* ---------------------------------------------------------- */
#ifdef STANDALONE_TEST
/* Dump the contents of a cgats structure to the given output */
static void
cgats_dump(cgats *p, cgatsFile *fp) {
int tn;
fp->gprintf(fp,"Number of tables = %d\n",p->ntables);
for (tn = 0; tn < p->ntables; tn++) {
cgats_table *t;
int i,j;
t = &p->t[tn];
fp->gprintf(fp,"\nTable %d:\n",tn);
switch(t->tt) /* Table identifier */
{
case it8_7_1:
fp->gprintf(fp,"Identifier = 'IT8.7/1'\n");
break;
case it8_7_2:
fp->gprintf(fp,"Identifier = 'IT8.7/2'\n");
break;
case it8_7_3:
fp->gprintf(fp,"Identifier = 'IT8.7/3'\n");
break;
case it8_7_4:
fp->gprintf(fp,"Identifier = 'IT8.7/4'\n");
break;
case cgats_5:
fp->gprintf(fp,"Identifier = 'CGATS.5'\n");
break;
case cgats_X:
fp->gprintf(fp,"Identifier = '%s'\n",p->cgats_type);
break;
case tt_other: /* User defined file identifier */
fp->gprintf(fp,"Identifier = '%s'\n",p->others[t->oi]);
break;
default:
fp->gprintf(fp,"**ILLEGAL**\n");
break;
}
fp->gprintf(fp,"\nNumber of keywords = %d\n",t->nkwords);
/* Dump all the keyword symbols and values */
for (i = 0; i < t->nkwords; i++) {
if (t->ksym[i] != NULL && t->kdata[i] != NULL)
{
if (t->kcom[i] != NULL)
fp->gprintf(fp,"Keyword '%s' has value '%s' and comment '%s'\n",
t->ksym[i],t->kdata[i],t->kcom[i]);
else
fp->gprintf(fp,"Keyword '%s' has value '%s'\n",t->ksym[i],t->kdata[i]);
}
if (t->kcom[i] != NULL)
fp->gprintf(fp,"Comment '%s'\n",t->kcom[i]);
}
fp->gprintf(fp,"\nNumber of field defs = %d\n",t->nfields);
/* Dump all the field symbols */
for (i = 0; i < t->nfields; i++) {
char *fname;
switch(t->ftype[i]) {
case r_t:
fname = "real";
break;
case i_t:
fname = "integer";
break;
case cs_t:
fname = "character string";
break;
case nqcs_t:
fname = "non-quoted char string";
break;
default:
fname = "illegal";
break;
}
fp->gprintf(fp,"Field '%s' has type '%s'\n",t->fsym[i],fname);
}
fp->gprintf(fp,"\nNumber of sets = %d\n",t->nsets);
/* Dump all the set values */
for (j = 0; j < t->nsets; j++) {
for (i = 0; i < t->nfields; i++) {
switch(t->ftype[i]) {
case r_t: {
char fmt[30];
double val = *((double *)t->fdata[j][i]);
fmt[0] = ' ';
real_format(val, REAL_SIGDIG, fmt+1);
fp->gprintf(fp,fmt,*((double *)t->fdata[j][i]));
break;
}
case i_t:
fp->gprintf(fp," %d",*((int *)t->fdata[j][i]));
break;
case cs_t:
case nqcs_t:
fp->gprintf(fp," %s",((char *)t->fdata[j][i]));
break;
default:
fp->gprintf(fp," illegal");
break;
}
}
fp->gprintf(fp,"\n");
}
}
}
int
main(int argc, char *argv[]) {
char *fn;
cgatsFile *fp;
cgats *pp;
if (argc == 1) {
/* Write test */
pp = new_cgats(); /* Create a CGATS structure */
if (pp->add_table(pp, cgats_5, 0) < 0 /* Start the first table */
|| pp->add_kword(pp, 0, NULL, NULL, "Comment only test comment") < 0
|| pp->add_kword(pp, 0, "TEST_KEY_WORD", "try this out", "Keyword comment") < 0
|| pp->add_kword(pp, 0, "TEST_KEY_WORD2", "try this\" out \"\" huh !", NULL) < 0
|| pp->add_field(pp, 0, "SAMPLE_ID", cs_t) < 0
|| pp->add_field(pp, 0, "SAMPLE_LOC", nqcs_t) < 0
|| pp->add_field(pp, 0, "XYZ_X", r_t) < 0)
error("Initial error: '%s'",pp->err);
if (pp->add_set(pp, 0, "1", "A1", 0.000000012345678) < 0
|| pp->add_set(pp, 0, "2", "A1", 0.00000012345678) < 0
|| pp->add_set(pp, 0, "3", "A1", 0.0000012345678) < 0
|| pp->add_set(pp, 0, "4", "A1", 0.000012345678) < 0
|| pp->add_set(pp, 0, "5", "A1", 0.000012345678) < 0
|| pp->add_set(pp, 0, "6", "A2", 0.00012345678) < 0
|| pp->add_set(pp, 0, "7", "A5",0.0012345678) < 0
|| pp->add_set(pp, 0, "8", "A5", 0.012345678) < 0
|| pp->add_set(pp, 0, "9", "A5", 0.12345678) < 0
|| pp->add_set(pp, 0, "10", "A5", 0.00000000) < 0
|| pp->add_set(pp, 0, "11", "A5", 1.2345678) < 0
|| pp->add_set(pp, 0, "12", "A5", 12.345678) < 0
|| pp->add_set(pp, 0, "13", "A5", 123.45678) < 0
|| pp->add_set(pp, 0, "14", "A5", 1234.5678) < 0
|| pp->add_set(pp, 0, "15", "A5", 12345.678) < 0
|| pp->add_set(pp, 0, "16", "A5", 123456.78) < 0
|| pp->add_set(pp, 0, "17", "A5", 1234567.8) < 0
|| pp->add_set(pp, 0, "18", "A5", 12345678.0) < 0
|| pp->add_set(pp, 0, "19", "A5", 123456780.0) < 0
|| pp->add_set(pp, 0, "20", "A5", 1234567800.0) < 0
|| pp->add_set(pp, 0, "21", "A5", 12345678000.0) < 0
|| pp->add_set(pp, 0, "22", "A5", 123456780000.0) < 0
|| pp->add_set(pp, 0, "23", "A5", 1234567800000.0) < 0)
error("Adding set error '%s'",pp->err);
if (pp->add_table(pp, cgats_5, 0) < 0 /* Start the second table */
|| pp->set_table_flags(pp, 1, 1, 1, 1) < 0 /* Suppress id, kwords and fields */
|| pp->add_kword(pp, 1, NULL, NULL, "Second Comment only test comment") < 0)
error("Adding table error '%s'",pp->err);
if (pp->add_field(pp, 1, "SAMPLE_ID", cs_t) < 0 /* Need to define fields same as table 0 */
|| pp->add_field(pp, 1, "SAMPLE_LOC", nqcs_t) < 0
|| pp->add_field(pp, 1, "XYZ_X", r_t) < 0)
error("Adding field error '%s'",pp->err);
if (pp->add_set(pp, 1, "4", "A4", -0.000012345678) < 0
|| pp->add_set(pp, 1, "5", "A5", -0.00012345678) < 0
|| pp->add_set(pp, 1, "6", "A5", -0.0012345678) < 0
|| pp->add_set(pp, 1, "7", "A5", -0.012345678) < 0
|| pp->add_set(pp, 1, "8", "A5", -0.12345678) < 0
|| pp->add_set(pp, 1, "9", "A5", -1.2345678) < 0
|| pp->add_set(pp, 1, "10", "A5", -12.345678) < 0
|| pp->add_set(pp, 1, "11", "A5", -123.45678) < 0
|| pp->add_set(pp, 1, "12", "A5", -1234.5678) < 0
|| pp->add_set(pp, 1, "13", "A5", -12345.678) < 0
|| pp->add_set(pp, 1, "14", "A5", -123456.78) < 0
|| pp->add_set(pp, 1, "15", "A5", -1234567.8) < 0
|| pp->add_set(pp, 1, "16", "A5", -12345678.0) < 0
|| pp->add_set(pp, 1, "17", "A5", -123456780.0) < 0
|| pp->add_set(pp, 1, "18", "A5", -1234567800.0) < 0
|| pp->add_set(pp, 1, "19", "A5", -12345678000.0) < 0
|| pp->add_set(pp, 1, "20", "A5", -123456780000.0) < 0)
error("Adding set 2 error '%s'",pp->err);
if ((fp = new_cgatsFileStd_name("fred.it8", "w")) == NULL)
error("Error opening '%s' for writing","fred.it8");
if (pp->write(pp, fp))
error("Write error : %s",pp->err);
fp->del(fp); /* Close file */
pp->del(pp); /* Clean up */
}
/* Read test */
pp = new_cgats(); /* Create a CGATS structure */
/* Setup to cope with Argyll files */
if (pp->add_other(pp, "CTI1") == -2
|| pp->add_other(pp, "CTI2") == -2
|| pp->add_other(pp, "CTI3") == -2)
error("Adding other error '%s'",pp->err);
/* Setup to cope with colorblind files */
if (pp->add_other(pp, "CBSC") == -2
|| pp->add_other(pp, "CBTA") == -2
|| pp->add_other(pp, "CBPR") == -2)
error("Adding other 2 error '%s'",pp->err);
if (argc == 2)
fn = argv[1];
else
fn = "fred.it8";
if ((fp = new_cgatsFileStd_name(fn, "r")) == NULL)
error("Error opening '%s' for reading",fn);
if (pp->read(pp, fp))
error("Read error : %s",pp->err);
fp->del(fp); /* Close file */
if ((fp = new_cgatsFileStd_fp(stdout)) == NULL)
error("Error opening stdout");
cgats_dump(pp, fp);
fp->del(fp); /* delete file object */
pp->del(pp); /* Clean up */
return 0;
}
/* Basic printf type error() and warning() routines */
void
error(const char *fmt, ...) {
va_list args;
fprintf(stderr,"cgats: Error - ");
va_start(args, fmt);
vfprintf(stderr, fmt, args);
va_end(args);
fprintf(stderr, "\n");
exit (-1);
}
void
warning(const char *fmt, ...) {
va_list args;
fprintf(stderr,"cgats: Warning - ");
va_start(args, fmt);
vfprintf(stderr, fmt, args);
va_end(args);
fprintf(stderr, "\n");
}
#endif /* STANDALONE_TEST */
/* ---------------------------------------------------------- */
|