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 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745
|
/*
Copyright (C) 1993, 1994, RSNA and Washington University
The software and supporting documentation for the Radiological
Society of North America (RSNA) 1993, 1994 Digital Imaging and
Communications in Medicine (DICOM) Demonstration were developed
at the
Electronic Radiology Laboratory
Mallinckrodt Institute of Radiology
Washington University School of Medicine
510 S. Kingshighway Blvd.
St. Louis, MO 63110
as part of the 1993, 1994 DICOM Central Test Node project for, and
under contract with, the Radiological Society of North America.
THIS SOFTWARE IS MADE AVAILABLE, AS IS, AND NEITHER RSNA NOR
WASHINGTON UNIVERSITY MAKE ANY WARRANTY ABOUT THE SOFTWARE, ITS
PERFORMANCE, ITS MERCHANTABILITY OR FITNESS FOR ANY PARTICULAR
USE, FREEDOM FROM ANY COMPUTER DISEASES OR ITS CONFORMITY TO ANY
SPECIFICATION. THE ENTIRE RISK AS TO QUALITY AND PERFORMANCE OF
THE SOFTWARE IS WITH THE USER.
Copyright of the software and supporting documentation is
jointly owned by RSNA and Washington University, and free access
is hereby granted as a license to use this software, copy this
software and prepare derivative works based upon this software.
However, any distribution of this software source code or
supporting documentation or derivative works (source code and
supporting documentation) must include the three paragraphs of
the copyright notice.
*/
/* Copyright marker. Copyright will be inserted above. Do not remove */
/*
** @$=@$=@$=
*/
/*
** DICOM 93
** Electronic Radiology Laboratory
** Mallinckrodt Institute of Radiology
** Washington University School of Medicine
**
** Module Name(s): DB_CreateDB, DB_DeleteDB, DB_Init, DB_Open, DB_Close,
** DB_AddPatient, DB_AddStudy, DB_AddSeries, DB_AddImage,
** DB_DelPatient, DB_DelStudy, DB_DelSeries, DB_DelImage,
** DB_GetPatient, DB_GetNextPatient, DB_GetStudy,
** DB_GetNextStudy, DB_GetSeries, DB_GetNextSeries,
** DB_GetImage, DB_GetNextImage, DB_Addid, DB_Removeid,
** DB_Findid, DB_CompareImageUID, DB_CompareSeriesUID,
** DB_CompareStudyUID, DB_ComparePatID, DB_DumpDB,
** DB_PrintPatient, DB_PrintStudy, DB_PrintSeries,
** DB_PrintImage, DB_WriteStudyNode, DB_ReadStudyNode,
** DB_ReadSeriesNode, DB_WriteSeriesNode, DB_DelBelowStudy,
** DB_DelBelowSeries, DB_UpdatePatientContext,
** DB_UpdateStudyContext, DB_UpdateSeriesContext,
** DB_UpdateImageContext, DB_GetPatientContext,
** DB_GetStudyContext, DB_GetSeriesContext, DB_GetImageContext
** DB_GetNumberofStudies, DB_ComparePat, DB_CompareStudy,
** DB_CompareSeries, DB_CompareImage, DB_Query, DB_NextQuery,
** DB_ResetQueryContext,DB_SetChangeFlag, DB_CompareChangeFlag
**
** Author, Date: David E. Beecher, 3-June-93
**
** Intent: These routines provide access to the databases for the DICOM
** project. They rely heavily on the hunk file facility for
** low-level file handling functions. All routines in this
** file are not meant for public use, but
** rather just the routines which have prototypes in the file
** dbquery.h. dbprivate.h contains the prototypes for all other
** functions to be used only by the database implementor.
**
** Last Update: $Author: smm $, $Date: 1997-08-10 19:21:59 $
** Source File: $RCSfile: database.c,v $
** Revision: $Revision: 1.42 $
** Status: $State: Exp $
*/
#include <stdio.h>
#ifndef MACOS
#include <stdlib.h>
#endif
#include <string.h>
/*lint -e652 */
#include <ctype.h>
/*lint +e652 */
#include <sys/types.h>
#include <sys/time.h>
#include <time.h>
#include <unistd.h>
#include "dicom.h"
#include "condition.h"
#include "hunk_man.h"
#include "utility.h"
#include "dbquery.h"
#include "dbprivate.h"
static char rcsid[] = "$Revision: 1.42 $ $RCSfile: database.c,v $";
static Root
GS_root;
static DBidstruct
*GS_context = (DBidstruct *) NULL;
static short
GS_dbid = 0;
/* DB_CreateDB
**
** Purpose:
** This routine creates a new database for use by the system.
**
** Parameter Dictionary:
** char *dbkey:
** a null terminated string indicating the file name to use for the db.
**
**
** Return Values:
**
** DB_NORMAL: The creation command succeeded
** DB_EXISTS: This database exists and must be removed first
** DB_?: Any valid return from the routine DB_Init
**
** Algorithm:
** A database file can only be created if it represents a filename which is
** located in a directory to which the caller has write access. The file name
** specified by dbkey must not already exist, or the routine returns DB_EXISTS
** and the existing database is retained. The user may use DB_DeleteDB to first
** remove this database and it may then be created with DB_CreateDB. More
** information concerning database creation will be explained in the DB_Init call.
*/
CONDITION
DB_CreateDB(char *dbkey)
{
short
dbid;
if (DB_Open(dbkey, &dbid) == DB_NORMAL) {
(void) DB_Close(dbid);
(void) COND_PushCondition(DB_EXISTS, "DB_CreateDB: Database %s is already open", dbkey);
return (DB_EXISTS);
}
return (DB_Init(dbkey));
}
/* DB_DeleteDB
**
** Purpose:
** This routine deletes an existing database.
**
** Parameter Dictionary:
** char *dbkey:
** a null terminated string indicating the file name to use for the db.
**
**
** Return Values:
**
** DB_NORMAL: The deletion command succeeded
**
** Algorithm:
** The deletion algorithm always assumes it succeeds. It just deletes the file
** name passed in dbkey. This routine may be smartened up if the need arises.
*/
CONDITION
DB_DeleteDB(char *dbkey)
{
char
*temp;
if ((temp = (char *) malloc(2048)) == NULL) {
(void) COND_PushCondition(DB_NOMEMORY, "DB_DeleteDB: No memory available");
return (DB_NOMEMORY);
}
strcpy(temp, "rm -f ");
strcat(temp, dbkey);
(void) system(temp);
free(temp);
return (DB_NORMAL);
}
/* DB_Init
**
** Purpose:
** This routine initializes a new database..
**
** Parameter Dictionary:
** char *dbkey:
** a null terminated string indicating the file name to use for the db.
**
**
** Return Values:
**
** DB_NORMAL: The initialization command succeeded
** DB_CREATERROR: The database file could not be created
** DB_OPENERROR: The newly created database could not be opened
** DB_ALLOCATERROR:The root node record could not be allocated
** DB_WRITERROR: The root node record could not be written
**
** Algorithm:
** The initialization relies on the hunk_man facility to perform most of the
** work. Default parameters are used to set up the hunk file to be used
** by this database, namely, DB_HUNKINITIALALLOCATION for the number of hunks
** to initially allocate to this file, DB_HUNKRECORDSIZE, for the hunk record
** size, and DB_NUMRECSPERHUNK, for the number of records per hunk. These
** parameters can be tuned for optimal performance. The root node is then
** initialized by nulling out all patient records and written to the hunk.
*/
CONDITION
DB_Init(char *dbkey)
{
int i;
if (HF_Create(dbkey, DB_HUNKINITIALALLOCATION,
DB_HUNKRECORDSIZE, DB_NUMRECSPERHUNK) != HF_NORMAL) {
(void) COND_PushCondition(DB_CREATERROR, "DB_Init: HF_Create error");
return (DB_CREATERROR);
}
if (HF_Open(dbkey) != HF_NORMAL) {
(void) COND_PushCondition(DB_OPENERROR, "DB_Init: HF_Open error");
return (DB_OPENERROR);
}
strcpy(GS_root.dbkey, dbkey);
GS_root.num_patients = 0;
for (i = 0; i < DB_MAXPATIENTS; i++) {
GS_root.patient_loc[i].hunk_number = HUNK_PTR_NULL;
GS_root.patient_loc[i].node_number = HUNK_PTR_NULL;
}
if (HF_AllocateStaticRecord(0, sizeof(GS_root)) != HF_NORMAL) {
(void) COND_PushCondition(DB_ALLOCATERROR, "DB_Init: HF_AllocateStaticRecord error");
return (DB_ALLOCATERROR);
}
if (HF_WriteStaticRecord(0, sizeof(GS_root), (void *) &GS_root)
!= HF_NORMAL) {
(void) COND_PushCondition(DB_WRITERROR, "DB_Init: HF_WriteStaticRecord error");
return (DB_WRITERROR);
}
(void) HF_Close(dbkey);
return (DB_NORMAL);
}
/* DB_Close
**
** Purpose:
** This routine closes a previously opened database.
**
** Parameter Dictionary:
** short dbid:
** the database identifier received upon opening the database.
**
**
** Return Values:
**
** DB_NORMAL: The close command succeeded
** DB_CLOSERROR: The database file could not be closed
**
** Algorithm:
** The close simply performs any writes needed at the hunk_file level and
** removes this database id from the list of active dbids.
*/
CONDITION
DB_Close(short dbid)
{
CONDITION
retval;
char
ts[DB_MAXKEYLENGTH];
if ((retval = DB_Removeid(dbid, ts)) != DB_NORMAL) {
(void) COND_PushCondition(retval, "DB_Close: dbid handling error");
return (retval);
}
if (HF_Close(ts) != HF_NORMAL) {
(void) COND_PushCondition(DB_CLOSERROR, "DB_Close: HF_Close error");
return (DB_CLOSERROR);
}
return (DB_NORMAL);
}
/* DB_Addid
**
** Purpose:
** This routine adds a new database access id to an internal list which
** also stores contextual information for sequential database searches.
**
** Parameter Dictionary:
** short dbid:
** the database identifier to be generated by this routine.
**
**
** Return Values:
**
** DB_NORMAL: The command succeeded and generated a new dbid.
** DB_NOMEMORY: No memory is available for list allocation
**
** Algorithm:
** The routine simply increments a static global variable, GS_dbid, to
** create the next dbid. This dbid will be used in all subsequent access
** calls to this database. Then, another DBidstruct is allocated and added
** to the list of existing structs which may be opened by other users.
** Currently, each process may only have a single database open at one time.
** This can easily be modified if the need arises.
*/
CONDITION
DB_Addid(short *dbid)
{
DBidstruct
*temp;
*dbid = ++GS_dbid;
if (GS_context == (DBidstruct *) NULL) {
if ((GS_context = (DBidstruct *) malloc(sizeof(DBidstruct))) ==
(DBidstruct *) NULL) {
return (DB_NOMEMORY);
}
GS_context->next = (DBidstruct *) NULL;
} else {
if ((temp = (DBidstruct *) malloc(sizeof(DBidstruct))) ==
(DBidstruct *) NULL) {
return (DB_NOMEMORY);
}
temp->next = GS_context;
GS_context = temp;
}
return (DB_NORMAL);
}
/* DB_Removeid
**
** Purpose:
** This routine removes the DBidstruct from the existing list if a match
** is found with the dbid passed.
**
** Parameter Dictionary:
** short dbid:
** the database identifier to be removed by this routine.
**
**
** Return Values:
**
** DB_NORMAL: The command succeeded and generated a new dbid.
** DB_IDREMERROR: The DBidstruct list is null, there are no entries.
** Or, the dbid could not be found in the list.
**
** Algorithm:
** The routine searches through the DBidstruct list pointed to by the
** global variable GS_context, and deletes the entry which contains the
** the dbid passed as input to the routine.
*/
CONDITION
DB_Removeid(short dbid, char *dbkey)
{
DBidstruct
*pt,
*temp;
if (GS_context == (DBidstruct *) NULL)
return (DB_IDREMERROR);
pt = temp = GS_context;
while (temp != (DBidstruct *) NULL) {
if (temp->dbid == dbid) {
strcpy(dbkey, temp->dbkey);
if (temp == GS_context) {
GS_context = temp->next;
free(temp);
} else {
pt->next = temp->next;
free(temp);
}
return (DB_NORMAL);
}
pt = temp;
temp = temp->next;
}
return (DB_IDREMERROR);
}
/* DB_Open
**
** Purpose:
** This routine attempts to open for access the database pointed to by
** the input string dbkey.
**
** Parameter Dictionary:
** char *dbkey:
** the name of the database to open
** short dbid:
** will contain the newly created dbid upon successful return
**
**
** Return Values:
**
** DB_NORMAL: The open succeeded and a new database has been opened.
** DB_OPENERROR: The corresponding hunk_file could not be opened.
** DB_?: Any error return from DB_Addid.
** DB_READERROR: The root record of the database could not be read.
**
** Algorithm:
** DB_Open first attempts to open the corresponding hunk file for access. It
** then attempts to generate a new dbid and context structure. Failure for
** either of these two functions results in an open failure.
** If successful, the context structure is effectively set to null, and the
** root record of the database is read.
*/
CONDITION
DB_Open(char *dbkey, short *dbid)
{
CONDITION retval;
HunkBufAdd temp;
if (HF_Open(dbkey) != HF_NORMAL) {
(void) COND_PushCondition(DB_OPENERROR, "DB_Open: HF_Open error");
return (DB_OPENERROR);
}
if ((retval = DB_Addid(dbid)) != DB_NORMAL) {
(void) COND_PushCondition(retval, "DB_Open: id handling error");
(void) HF_Close(dbkey);
return (retval);
}
strcpy(GS_context->dbkey, dbkey);
GS_context->dbid = *dbid;
temp.hunk_number = HUNK_PTR_NULL;
temp.node_number = HUNK_PTR_NULL;
DB_UpdatePatientContext(*dbid, &temp, -1, DB_PATIENTCONTEXT);
DB_UpdatePatientContext(*dbid, &temp, -1, DB_STUDYCONTEXT);
DB_UpdatePatientContext(*dbid, &temp, -1, DB_SERIESCONTEXT);
DB_UpdatePatientContext(*dbid, &temp, -1, DB_IMAGECONTEXT);
DB_UpdateStudyContext(*dbid, &temp, -1, DB_PATIENTCONTEXT);
DB_UpdateStudyContext(*dbid, &temp, -1, DB_STUDYCONTEXT);
DB_UpdateStudyContext(*dbid, &temp, -1, DB_SERIESCONTEXT);
DB_UpdateStudyContext(*dbid, &temp, -1, DB_IMAGECONTEXT);
DB_UpdateSeriesContext(*dbid, &temp, -1, DB_PATIENTCONTEXT);
DB_UpdateSeriesContext(*dbid, &temp, -1, DB_STUDYCONTEXT);
DB_UpdateSeriesContext(*dbid, &temp, -1, DB_SERIESCONTEXT);
DB_UpdateSeriesContext(*dbid, &temp, -1, DB_IMAGECONTEXT);
DB_UpdateImageContext(*dbid, &temp, -1, DB_PATIENTCONTEXT);
DB_UpdateImageContext(*dbid, &temp, -1, DB_STUDYCONTEXT);
DB_UpdateImageContext(*dbid, &temp, -1, DB_SERIESCONTEXT);
DB_UpdateImageContext(*dbid, &temp, -1, DB_IMAGECONTEXT);
DB_UpdateQueryContext(*dbid, &temp, -1, DB_PATIENTCONTEXT);
DB_UpdateQueryContext(*dbid, &temp, -1, DB_STUDYCONTEXT);
DB_UpdateQueryContext(*dbid, &temp, -1, DB_SERIESCONTEXT);
DB_UpdateQueryContext(*dbid, &temp, -1, DB_IMAGECONTEXT);
if (HF_ReadStaticRecord(0, sizeof(GS_root), (void *) &GS_root)
!= HF_NORMAL) {
(void) COND_PushCondition(DB_READERROR, "DB_Open: HF_ReadStaticRecord error");
return (DB_READERROR);
}
return (DB_NORMAL);
}
/* DB_AddPatient
**
** Purpose:
** This routine adds a new patient to the database.
**
** Parameter Dictionary:
** short dbid:
** Contains the database identifier.
** PatientLevel *pat:
** Contains the patient information to add to the database.
**
** Return Values:
**
** DB_NORMAL: The patient add succeeded.
** DB_LOCKERROR: Could not acquire a requested lock
** DB_TOOMANYPATS: Only DB_MAXPATIENTS are allowed, this add would result
** in one more (DB_MAXPATIENTS is currently 20).
** DB_DUPATIENT: The patien with this PatID is already in the database.
** DB_ALLOCATERROR:The new record in the hunk file could not be allocated.
** DB_WRITERROR: The new record could not be written to the hunk file.
**
** Algorithm:
** The dbid must be found in the struct list. Then, the check is made for the
** maximum number of patients and the pre-existing patient. If all this checks
** out, the new records are allocated, the root node is updated, and all new
** information is written out to the database file.
*/
CONDITION
DB_AddPatient(short dbid, PatientLevel *pat)
{
int i;
HunkBufAdd hadd;
PatientNode pnode;
if (DB_Findid(dbid) != DB_NORMAL) {
(void) COND_PushCondition(DB_NOTOPENED, "DB_AddPatient: DB not opened");
return (DB_NOTOPENED);
}
if (GS_root.num_patients >= DB_MAXPATIENTS) {
(void) COND_PushCondition(DB_TOOMANYPATS, "DB_AddPatient: Too many patients");
return (DB_TOOMANYPATS);
}
if (HF_ExclusiveLock() != HF_NORMAL) {
(void) COND_PushCondition(DB_LOCKERROR, "DB_AddPatient: Lock Error");
return (DB_LOCKERROR);
}
for (i = 0; i < GS_root.num_patients; i++) {
if (DB_ComparePatID(pat->PatID, &GS_root.patient_loc[i]) == DB_MATCH) {
(void) COND_PushCondition(DB_DUPATIENT, "DB_AddPatient: Duplicate patient");
return (DB_UnLock(DB_DUPATIENT));
}
}
if (HF_AllocateRecord(sizeof(PatientNode), &hadd) != HF_NORMAL) {
(void) COND_PushCondition(DB_ALLOCATERROR, "DB_AddPatient: HF_AllocateRecord error");
return (DB_UnLock(DB_ALLOCATERROR));
}
GS_root.patient_loc[GS_root.num_patients].hunk_number = hadd.hunk_number;
GS_root.patient_loc[GS_root.num_patients].node_number = hadd.node_number;
pnode.time_stamp = time((time_t *) NULL);
pnode.pat = *pat;
pnode.num_studies = 0;
for (i = 0; i < DB_MAXSTUDIES; i++) {
pnode.study_loc[i].hunk_number = HUNK_PTR_NULL;
pnode.study_loc[i].node_number = HUNK_PTR_NULL;
}
if (HF_WriteRecord(&hadd, sizeof(PatientNode), (void *) &pnode)
!= HF_NORMAL) {
(void) COND_PushCondition(DB_WRITERROR, "DB_AddPatient: HF_WriteRecord error");
return (DB_UnLock(DB_WRITERROR));
}
GS_root.num_patients++;
if (HF_WriteStaticRecord(0, sizeof(GS_root), (void *) &GS_root)
!= HF_NORMAL) {
(void) COND_PushCondition(DB_WRITERROR, "DB_AddPatient: HF_WriteStaticRecord error");
return (DB_UnLock(DB_WRITERROR));
}
return (DB_UnLock(DB_NORMAL));
}
/* DB_FindID
**
** Purpose:
** This routine attempts to locate the dbid in the maintained list.
**
** Parameter Dictionary:
** short dbid:
** Contains the database identifier.
**
** Return Values:
**
** DB_NORMAL: The dbid was found.
** DB_IDNOTHERE: The dbid was not found.
**
** Algorithm:
** The dbid is search for and if found, DB_NORMAL is returned, otherwise,
** DB_IDNOTHERE is returned.
*/
CONDITION
DB_Findid(short dbid)
{
DBidstruct
*temp;
if (GS_context == (DBidstruct *) NULL)
return (DB_IDNOTHERE);
temp = GS_context;
while (temp != (DBidstruct *) NULL) {
if (temp->dbid == dbid) {
return (DB_NORMAL);
}
temp = temp->next;
}
return (DB_IDNOTHERE);
}
/* DB_CompareImageUID
**
** Purpose:
** This Checks an image record to see if the image uid matches the one
** passed to it.
**
** Parameter Dictionary:
** char *imageuid:
** The image uid to check
** HunkBufAdd *image_loc:
** The hunk buffer address of the ImageNode struct to check.
**
** Return Values:
**
** DB_MATCH: The uid's match.
** DB_NOMATCH: The uid's don't match.
**
** Algorithm:
** The image UID is compared to the image UID int the ImageNode struct
** in the hunk file. If a match is found, the routine returns DB_MATCH,
** otherwise, DB_NOMATCH is returned.
*/
CONDITION
DB_CompareImageUID(char *imageuid, HunkBufAdd *image_loc)
{
ImageNode inode;
if (HF_ReadRecord(image_loc, sizeof(inode), (void *) &inode) != HF_NORMAL)
return (DB_NOMATCH);
if (strcmp(inode.image.ImageUID, imageuid) == 0)
return (DB_MATCH);
else
return (DB_NOMATCH);
}
/* DB_CompareSeriesImageUID
**
** Purpose:
** This Checks a series record to see if the series uid matches the one
** passed to it.
**
** Parameter Dictionary:
** char *seriesuid:
** The series uid to check.
** HunkBufAdd *series_loc:
** The hunk buffer address of the SeriesNode struct to check.
**
** Return Values:
**
** DB_MATCH: The uid's match.
** DB_NOMATCH: The uid's don't match.
**
** Algorithm:
** The series UID is compared to the series UID int the SeriesNode struct
** in the hunk file. If a match is found, the routine returns DB_MATCH,
** otherwise, DB_NOMATCH is returned.
*/
CONDITION
DB_CompareSeriesUID(char *seriesuid, HunkBufAdd *series_loc)
{
SeriesNode snode;
if (HF_ReadRecord(series_loc, sizeof(snode), (void *) &snode) != HF_NORMAL)
return (DB_NOMATCH);
if (strcmp(snode.series.SeriesUID, seriesuid) == 0)
return (DB_MATCH);
else
return (DB_NOMATCH);
}
/* DB_CompareStudyUID
**
** Purpose:
** This Checks a study record to see if the study uid matches the one
** passed to it.
**
** Parameter Dictionary:
** char *studyuid:
** The series uid to check.
** HunkBufAdd *study_loc:
** The hunk buffer address of the SeriesNode struct to check.
**
** Return Values:
**
** DB_MATCH: The uid's match.
** DB_NOMATCH: The uid's don't match.
**
** Algorithm:
** The study UID is compared to the series UID int the StudyNode struct
** in the hunk file. If a match is found, the routine returns DB_MATCH,
** otherwise, DB_NOMATCH is returned.
*/
CONDITION
DB_CompareStudyUID(char *studyuid, HunkBufAdd *study_loc)
{
StudyNode snode;
if (HF_ReadRecord(study_loc, sizeof(snode), (void *) &snode) != HF_NORMAL)
return (DB_NOMATCH);
if (strcmp(snode.study.StudyUID, studyuid) == 0)
return (DB_MATCH);
else
return (DB_NOMATCH);
}
/* DB_ComparePatUID
**
** Purpose:
** This checks a PatientNode to see if the patient id matches the one
** passed to it.
**
** Parameter Dictionary:
** char *patid:
** The patient id to check.
** HunkBufAdd *study_loc:
** The hunk buffer address of the PatientNode struct to check.
**
** Return Values:
**
** DB_MATCH: The uid's match.
** DB_NOMATCH: The uid's don't match.
**
** Algorithm:
** The patient UID is compared to the patient UID int the PatinetNode struct
** in the hunk file. If a match is found, the routine returns DB_MATCH,
** otherwise, DB_NOMATCH is returned.
*/
CONDITION
DB_ComparePatID(char *patid, HunkBufAdd *pat_loc)
{
PatientNode pnode;
if (HF_ReadRecord(pat_loc, sizeof(pnode), (void *) &pnode) != HF_NORMAL)
return (DB_NOMATCH);
if (strcmp(pnode.pat.PatID, patid) == 0)
return (DB_MATCH);
else
return (DB_NOMATCH);
}
/* DB_AddStudy
**
** Purpose:
** DB_AddStudy adds a study node to an existing patient
**
** Parameter Dictionary:
** short dbid:
** The database identifer.
** char *patid:
** The patient id to add the study to.
** StudyLevel *study:
** The study level struct to add to the specified patient.
**
** Return Values:
**
** DB_NORMAL: The add study succeeded.
** DB_LOCKERROR: Could not acquire a requested lock
** DB_NOTOPENED: The database has not been opened.
** DB_BADPATIENT: The patient specified counld not be found.
** DB_READERROR: The hunk record could not be read.
** DB_TOOMANYSTUDS:Adding this study would exceed the maximum allowed which
** is set at DB_MAXSTUDIES. This value is currently 20.
** DB_DUPSTUDY: The study being added already exists for this patient.
** DB_ALLOCATERROR:The new record could not be allocated.
** DB_WRITERROR: The new record could not be written.
**
** Algorithm:
** We do the normal checks for database open. Then we try and find the patient
** specified by patid in the database. It is an error not to be able to locate
** this patient. Once the patient is found, we check to see if the study about
** to be added already exists for this patient, if so, this is an error and we
** return. Otherwise, we attempt to allocate the new hunk records, fill them
** with the new data, and write them back to the file.
**
*/
CONDITION
DB_AddStudy(short dbid, char *patid, StudyLevel *study)
{
int i,
found;
HunkBufAdd
patadd,
studadd;
PatientNode pnode;
StudyNode snode;
if (DB_Findid(dbid) != DB_NORMAL) {
(void) COND_PushCondition(DB_NOTOPENED, "DB_AddStudy: DB_Findid failed");
return (DB_NOTOPENED);
}
if (HF_ExclusiveLock() != HF_NORMAL) {
(void) COND_PushCondition(DB_LOCKERROR, "DB_AddStudy: HF_ExclusiveLock failed");
return (DB_LOCKERROR);
}
found = 0;
for (i = 0; i < GS_root.num_patients; i++) {
if (DB_ComparePatID(patid, &GS_root.patient_loc[i]) == DB_MATCH) {
patadd = GS_root.patient_loc[i];
found = 1;
break;
}
}
if (found == 0) {
(void) COND_PushCondition(DB_BADPATIENT, "DB_AddStudy: Can't find patient %s", patid);
return (DB_UnLock(DB_BADPATIENT));
}
if (HF_ReadRecord(&patadd, sizeof(pnode), (void *) &pnode) != HF_NORMAL) {
(void) COND_PushCondition(DB_READERROR, "DB_AddStudy: HF_ReadRecord failed");
return (DB_UnLock(DB_READERROR));
}
if (pnode.num_studies >= DB_MAXSTUDIES) {
(void) COND_PushCondition(DB_TOOMANYSTUDS, "DB_AddStudy: Too many studies");
return (DB_UnLock(DB_TOOMANYSTUDS));
}
/*
* Check for a duplicate Study...
*/
for (i = 0; i < pnode.num_studies; i++) {
if (DB_CompareStudyUID(study->StudyUID, &pnode.study_loc[i]) == DB_MATCH) {
(void) COND_PushCondition(DB_DUPSTUDY, "DB_AddStudy: Duplicate study");
return (DB_UnLock(DB_DUPSTUDY));
}
}
if (HF_AllocateRecord(sizeof(StudyNode), &studadd) != HF_NORMAL) {
(void) COND_PushCondition(DB_ALLOCATERROR, "DB_AddStudy: HF_AllocateRecord failed");
return (DB_UnLock(DB_ALLOCATERROR));
}
snode.time_stamp = time((time_t *) NULL);
snode.study = *study;
snode.num_allocated = DB_INITSIZE;
snode.num_series = 0;
for (i = 0; i < DB_INITSIZE; i++) {
snode.series_loc[i].hunk_number = HUNK_PTR_NULL;
snode.series_loc[i].node_number = HUNK_PTR_NULL;
}
pnode.study_loc[pnode.num_studies].hunk_number = studadd.hunk_number;
pnode.study_loc[pnode.num_studies].node_number = studadd.node_number;
pnode.num_studies++;
if (HF_WriteRecord(&patadd, sizeof(PatientNode), (void *) &pnode) != HF_NORMAL) {
(void) COND_PushCondition(DB_WRITERROR, "DB_AddStudy: HF_WriteRecord failed");
return (DB_UnLock(DB_WRITERROR));
}
if (HF_WriteRecord(&studadd, sizeof(StudyNode), (void *) &snode) != HF_NORMAL) {
(void) COND_PushCondition(DB_WRITERROR, "DB_AddStudy: HF_WriteRecord failed");
return (DB_UnLock(DB_WRITERROR));
}
return (DB_UnLock(DB_NORMAL));
}
/* DB_dumpDB
**
** Purpose:
** DB_dumpDB is a utility to dump the database to standard out.
**
** Parameter Dictionary:
** short dbid:
** The database identifer.
**
** Return Values:
**
** none
**
** Algorithm:
** Just a utility do dump the database mostly for diagnostic purposes...not really
** meant for end users.
*/
void
DB_DumpDB(short dbid)
{
int i,
j,
k,
l;
PatientNode pnode;
StudyNode
*stn;
SeriesNode *sen;
ImageNode inode;
HunkBufAdd paadd,
stadd,
seadd,
imadd;
if (DB_Findid(dbid) != DB_NORMAL)
return;
if (HF_SharedLock() != HF_NORMAL)
return;
printf("DB Key: %s Number of Patients: %d Number of Additions/Deletions: %ld\n\n",
GS_root.dbkey, GS_root.num_patients, HF_ReadUpdateFlag());
for (i = 0; i < DB_MAXPATIENTS; i++) {
paadd = GS_root.patient_loc[i];
printf("Patient Record %3d %3d\n", paadd.hunk_number, paadd.node_number);
if (paadd.hunk_number != HUNK_PTR_NULL &&
paadd.node_number != HUNK_PTR_NULL) {
if (HF_ReadRecord(&paadd, sizeof(pnode), (void *) &pnode) == HF_NORMAL) {
DB_PrintPatient(pnode);
} else
printf("Bad read on Patient Record %3d %3d\n",
paadd.hunk_number, paadd.node_number);
for (j = 0; j < DB_MAXSTUDIES; j++) {
stadd = pnode.study_loc[j];
printf("Study Record %3d %3d\n",
stadd.hunk_number, stadd.node_number);
if (stadd.hunk_number != HUNK_PTR_NULL &&
stadd.node_number != HUNK_PTR_NULL) {
if (DB_ReadStudyNode(&stadd, (void *) &stn) == DB_NORMAL) {
DB_PrintStudy(stn);
for (k = 0; k < stn->num_allocated; k++) {
seadd = stn->series_loc[k];
printf("Series Record %3d %3d\n",
seadd.hunk_number, seadd.node_number);
if (seadd.hunk_number != HUNK_PTR_NULL &&
seadd.node_number != HUNK_PTR_NULL) {
if (DB_ReadSeriesNode(&seadd, (void *) &sen) == DB_NORMAL) {
DB_PrintSeries(sen);
for (l = 0; l < sen->num_allocated; l++) {
imadd = sen->image_loc[l];
printf("Image Record %3d %3d\n",
imadd.hunk_number, imadd.node_number);
if (imadd.hunk_number != HUNK_PTR_NULL &&
imadd.node_number != HUNK_PTR_NULL) {
if (HF_ReadRecord(&imadd, sizeof(inode), &inode) ==
HF_NORMAL) {
DB_PrintImage(inode);
}
}
}
free(sen);
} else
printf("Bad read on Series Record %3d %3d\n",
seadd.hunk_number, seadd.node_number);
}
}
free(stn);
} else
printf("Bad read on Study Record %3d %3d\n",
stadd.hunk_number, stadd.node_number);
}
}
}
}
(void) HF_UnLock();
}
/* DB_PrintSeries
**
** Purpose:
** DB_PrintSeries is a utility to dump a series record to standard out.
**
** Parameter Dictionary:
** short dbid:
** The database identifer.
**
** Return Values:
**
** none
**
** Algorithm:
** Just a utility do dump a series record for diagnostic purposes...not really
** meant for end users.
*/
void
DB_PrintSeries(SeriesNode *snode)
{
printf("\t\tImages(act/alloc) %d %d\n",
snode->num_images, snode->num_allocated);
printf("\t\t Modality: %s\n", snode->series.Modality);
printf("\t\tSeries Number: %s\n", snode->series.SeriesNumber);
printf("\t\t Series UID: %s\n", snode->series.SeriesUID);
printf("\t\t Series Time: %ld\n", snode->time_stamp);
}
/* DB_PrintStudy
**
** Purpose:
** DB_PrintStudy is a utility to dump a study record to standard out.
**
** Parameter Dictionary:
** short dbid:
** The database identifer.
**
** Return Values:
**
** none
**
** Algorithm:
** Just a utility do dump a study record for diagnostic purposes...not really
** meant for end users.
*/
void
DB_PrintStudy(StudyNode *snode)
{
printf("\t\tSeries(act/alloc) %d %d\n",
snode->num_series, snode->num_allocated);
printf("\t\t Study Date: %s\n", snode->study.StudyDate);
printf("\t\t Study Time: %s\n", snode->study.StudyTime);
printf("\t\t Study ID: %s\n", snode->study.StudyID);
printf("\t\t Accession: %s\n", snode->study.AccessionNumber);
printf("\t\t Study UID: %s\n", snode->study.StudyUID);
printf("\t\t ReferringPhysName: %s\n", snode->study.ReferringPhysName);
printf("\t\t InterpretingPhysName: %s\n", snode->study.InterpretingPhysName);
printf("\t\t ProcedureDescription: %s\n", snode->study.ProcedureDescription);
printf("\t\t AdmittingDiagnosedDescription: %s\n", snode->study.AdmittingDiagnosedDescription);
printf("\t\tStudy Time: %ld\n", snode->time_stamp);
}
/* DB_PrintPatient
**
** Purpose:
** DB_PrintPatient is a utility to dump a patient record to standard out.
**
** Parameter Dictionary:
** short dbid:
** The database identifer.
**
** Return Values:
**
** none
**
** Algorithm:
** Just a utility do dump a patient record for diagnostic purposes...not really
** meant for end users.
*/
void
DB_PrintPatient(PatientNode pnode)
{
printf("\tBirthdate: %s\n", pnode.pat.BirthDate);
printf("\t Name: %s\n", pnode.pat.Name);
printf("\t PatID: %s\n", pnode.pat.PatID);
printf("\t Pat Time: %ld\n\n", pnode.time_stamp);
}
/* DB_PrintImage
**
** Purpose:
** DB_PrintImage is a utility to dump a image record to standard out.
**
** Parameter Dictionary:
** short dbid:
** The database identifer.
**
** Return Values:
**
** none
**
** Algorithm:
** Just a utility do dump a image record for diagnostic purposes...not really
** meant for end users.
*/
void
DB_PrintImage(ImageNode inode)
{
printf("\tImageNumber: %s\n", inode.image.ImageNumber);
printf("\t ImageUID: %s\n", inode.image.ImageUID);
printf("\t Class UID: %s\n", inode.image.ClassUID);
printf("\t FileName: %s\n", inode.image.FileName);
printf("\tImage Time: %ld\n\n", inode.time_stamp);
}
/* DB_AddSeries
**
** Purpose:
** DB_AddSeries adds a series node to an existing patient and study.
**
** Parameter Dictionary:
** short dbid:
** The database identifer.
** char *patid:
** The patient id to add the series to.
** char *studyuid:
** The study uid to add the series to.
** SeriesLevel *series:
** The series level struct to add to the specified patient and study.
**
** Return Values:
**
** DB_NORMAL: The add series succeeded.
** DB_LOCKERROR: Could not acquire a requested lock
** DB_NOTOPENED: The database has not been opened.
** DB_BADPATIENT: The patient specified counld not be found.
** DB_READERROR: The hunk record could not be read.
** DB_BADSTUDY: The study specified counld not be found.
** DB_DUPSERIES: The series being added already exists for
** this patient and study.
** DB_ALLOCATERROR:The new record could not be allocated.
** DB_WRITERROR: The new record could not be written.
** DB_NOMEMORY: Couldn't allocate any local memory for temps.
**
** Algorithm:
** We do the normal checks for database open. Then we try and find the patient
** and study specified by patid and studyuid in the database. It is an error
** not to be able to locate the patient and study. Once both are found, we
** check to see if the series about to be added already exists for this patient
** and study, if so, this is an error and we return. Otherwise, we attempt to
** allocate the new hunk records, fill them with the new data, and write them
** back to the file. There is one twist here. Since we don't have an upper
** limit on the number of series/study, we may have to reallocate the study
** record if the default number of series is exceeded. This will have to be
** rewritten to the database and the pointers in the patient record will have
** to be appropriately updated and rewritten. This is why this routine is a
** little more complex that you might normally think.
**
*/
CONDITION
DB_AddSeries(short dbid, char *patid, char *studyuid, SeriesLevel *series)
{
int i,
size_to_alloc,
study_inx = 0,
found;
HunkBufAdd
patadd,
stuadd,
newstuadd,
seradd;
PatientNode pnode;
StudyNode *snode,
*new_snode;
SeriesNode sernode;
if (DB_Findid(dbid) != DB_NORMAL) {
(void) COND_PushCondition(DB_NOTOPENED, "DB_AddSeries: DB not opened");
return (DB_NOTOPENED);
}
if (HF_ExclusiveLock() != HF_NORMAL) {
(void) COND_PushCondition(DB_LOCKERROR, "DB_AddSeries: HF_ExclusiveLock failed");
return (DB_LOCKERROR);
}
found = 0;
for (i = 0; i < GS_root.num_patients; i++) {
if (DB_ComparePatID(patid, &GS_root.patient_loc[i]) == DB_MATCH) {
patadd = GS_root.patient_loc[i];
found = 1;
break;
}
}
if (found == 0) {
(void) COND_PushCondition(DB_BADPATIENT, "DB_AddSeries: Can't find patient %s", patid);
return (DB_UnLock(DB_BADPATIENT));
}
if (HF_ReadRecord(&patadd, sizeof(pnode), (void *) &pnode) != HF_NORMAL) {
(void) COND_PushCondition(DB_READERROR, "DB_AddSeries: HF_ReadRecord failed");
return (DB_UnLock(DB_READERROR));
}
found = 0;
for (i = 0; i < pnode.num_studies; i++) {
if (DB_CompareStudyUID(studyuid, &pnode.study_loc[i]) == DB_MATCH) {
stuadd = pnode.study_loc[i];
study_inx = i;
found = 1;
break;
}
}
if (found == 0) {
(void) COND_PushCondition(DB_BADSTUDY, "DB_AddSeries: Can't find study %s", studyuid);
return (DB_UnLock(DB_BADSTUDY));
}
if (DB_ReadStudyNode(&stuadd, &snode) != DB_NORMAL) {
(void) COND_PushCondition(DB_READERROR, "DB_AddSeries: DB_ReadStudyNode failed");
return (DB_UnLock(DB_READERROR));
}
/*
* Check for a duplicate Series
*/
for (i = 0; i < snode->num_series; i++) {
if (DB_CompareSeriesUID(series->SeriesUID,
&(snode->series_loc[i])) == DB_MATCH) {
free(snode);
(void) COND_PushCondition(DB_DUPSERIES, "DB_AddSeries: Duplicate Series");
return (DB_UnLock(DB_DUPSERIES));
}
}
size_to_alloc = sizeof(StudyNode) + (snode->num_allocated - DB_INITSIZE) *
sizeof(HunkBufAdd);
if (snode->num_allocated == snode->num_series) { /* Need to expand the
* list */
size_to_alloc = sizeof(StudyNode);
size_to_alloc += (snode->num_allocated + DB_EXPANDSIZE - DB_INITSIZE) *
sizeof(HunkBufAdd);
new_snode = (StudyNode *) malloc(size_to_alloc);
if (new_snode == (StudyNode *) NULL) {
free(snode);
(void) COND_PushCondition(DB_NOMEMORY, "DB_AddSeries: No memory");
return (DB_UnLock(DB_NOMEMORY));
}
memcpy((void *) new_snode, (void *) snode,
sizeof(StudyNode) + ((snode->num_allocated - DB_INITSIZE) *
sizeof(HunkBufAdd)));
for (i = snode->num_allocated; i < snode->num_allocated + DB_EXPANDSIZE; i++) {
new_snode->series_loc[i].hunk_number = HUNK_PTR_NULL;
new_snode->series_loc[i].node_number = HUNK_PTR_NULL;
}
new_snode->num_allocated += DB_EXPANDSIZE;
free(snode);
if (HF_AllocateRecord(size_to_alloc, &newstuadd) != HF_NORMAL) {
free(new_snode);
(void) COND_PushCondition(DB_ALLOCATERROR, "DB_AddSeries: HF_AllocateRecord failed");
return (DB_UnLock(DB_ALLOCATERROR));
}
pnode.study_loc[study_inx] = newstuadd;
if (HF_WriteRecord(&patadd, sizeof(PatientNode), (void *) &pnode)
!= HF_NORMAL) {
free(new_snode);
(void) COND_PushCondition(DB_WRITERROR, "DB_AddSeries: HF_WriteRecord failed");
return (DB_UnLock(DB_WRITERROR));
}
snode = new_snode;
(void) HF_DeallocateRecord(&stuadd);
stuadd = newstuadd;
(void) HF_IncrementUpdateFlag(); /* We need to let folks know
* the db has changed */
}
if (HF_AllocateRecord(sizeof(SeriesNode), &seradd) != HF_NORMAL) {
free(snode);
(void) COND_PushCondition(DB_ALLOCATERROR, "DB_AddSeries: HF_AllocateRecord failed");
return (DB_UnLock(DB_ALLOCATERROR));
}
snode->series_loc[snode->num_series] = seradd;
snode->num_series++;
if (HF_WriteRecord(&stuadd, size_to_alloc, (void *) snode)
!= HF_NORMAL) {
free(snode);
(void) COND_PushCondition(DB_WRITERROR, "DB_AddSeries: HF_WriteRecord failed");
return (DB_UnLock(DB_WRITERROR));
}
sernode.time_stamp = time((time_t *) NULL);
sernode.series = *series;
sernode.num_allocated = DB_INITSIZE;
sernode.num_images = 0;
for (i = 0; i < DB_INITSIZE; i++) {
sernode.image_loc[i].hunk_number = HUNK_PTR_NULL;
sernode.image_loc[i].node_number = HUNK_PTR_NULL;
}
if (HF_WriteRecord(&seradd, sizeof(SeriesNode), (void *) &sernode)
!= HF_NORMAL) {
free(snode);
(void) COND_PushCondition(DB_WRITERROR, "DB_AddSeries: HF_WriteRecord failed");
return (DB_UnLock(DB_WRITERROR));
}
free(snode);
return (DB_UnLock(DB_NORMAL));
}
/* DB_AddImage
**
** Purpose:
** DB_AddImage adds an image node to an existing patient, study, and series.
**
** Parameter Dictionary:
** short dbid:
** The database identifer.
** char *patid:
** The patient id to add the image to.
** char *studyuid:
** The study uid to add the image to.
** char *seriesuid:
** The series uid to add the image to.
** ImageLevel *image:
** The image level struct to add to the specified patient, study, and series.
**
** Return Values:
**
** DB_NORMAL: The add image succeeded.
** DB_LOCKERROR: Could not acquire a requested lock
** DB_NOTOPENED: The database has not been opened.
** DB_BADPATIENT: The patient specified counld not be found.
** DB_READERROR: The hunk record could not be read.
** DB_BADSTUDY: The study specified could not be found.
** DB_BADSERIES: The series specified could not be found.
** DB_ALLOCATERROR:The new record could not be allocated.
** DB_WRITERROR: The new record could not be written.
** DB_NOMEMORY: Couldn't allocate any local memory for temps.
**
** Algorithm:
** We do the normal checks for database open. Then we try and find the patient,
** study, and series specified by patid, studyuid, and seriesuid in the database.
** It is an error not to be able to locate the patient, study, and series.
** Once all are found, we check to see if the image about to be added already
** exists for this patient, study, and series (actually, this is not done yet
** but will be added...) if so, this is an error and we return. Otherwise, we
** attempt to allocate the new hunk records, fill them with the new data, and
** write them back to the file. There is one twist here. Since we don't have
** an upper limit on the number of series/study or on the number of images/series,
** we may have to reallocate the series record if the default number of images
** is exceeded. This will have to be rewritten to the database and the pointers
** in the study record will have to be appropriately updated and rewritten.
** This is why this routine is a little more complex that you might normally think.
**
*/
CONDITION
DB_AddImage(short dbid, char *patid, char *studyuid, char *seriesuid, ImageLevel *image)
{
int i,
size_to_alloc,
series_inx = 0,
found;
HunkBufAdd
patadd,
stuadd,
seradd,
newseradd,
imgadd;
PatientNode pnode;
StudyNode *snode;
SeriesNode *sernode,
*new_sernode;
ImageNode inode;
if (DB_Findid(dbid) != DB_NORMAL) {
(void) COND_PushCondition(DB_NOTOPENED, "DB_AddImage: DB_Findid failed");
return (DB_NOTOPENED);
}
if (HF_ExclusiveLock() != HF_NORMAL) {
(void) COND_PushCondition(DB_LOCKERROR, "DB_AddImage: HF_ExclusiveLock failed");
return (DB_LOCKERROR);
}
/*
* Check for the Patient...
*/
found = 0;
for (i = 0; i < GS_root.num_patients; i++) {
if (DB_ComparePatID(patid, &GS_root.patient_loc[i]) == DB_MATCH) {
patadd = GS_root.patient_loc[i];
found = 1;
break;
}
}
if (found == 0) {
(void) COND_PushCondition(DB_BADPATIENT, "DB_AddImage: Can't find patient %s", patid);
return (DB_UnLock(DB_BADPATIENT));
}
if (HF_ReadRecord(&patadd, sizeof(pnode), (void *) &pnode) != HF_NORMAL) {
(void) COND_PushCondition(DB_READERROR, "DB_AddImage: HF_ReadRecord failed");
return (DB_UnLock(DB_READERROR));
}
/*
* Check for the Study...
*/
found = 0;
for (i = 0; i < pnode.num_studies; i++) {
if (DB_CompareStudyUID(studyuid, &pnode.study_loc[i]) == DB_MATCH) {
stuadd = pnode.study_loc[i];
found = 1;
break;
}
}
if (found == 0) {
(void) COND_PushCondition(DB_BADSTUDY, "DB_AddImage: Can't find studyuid %s", studyuid);
return (DB_UnLock(DB_BADSTUDY));
}
if (DB_ReadStudyNode(&stuadd, &snode) != DB_NORMAL) {
(void) COND_PushCondition(DB_BADSTUDY, "DB_AddImage: DB_ReadStudyNode failed");
return (DB_UnLock(DB_READERROR));
}
/*
* Check for the Series...
*/
found = 0;
for (i = 0; i < snode->num_series; i++) {
if (DB_CompareSeriesUID(seriesuid, &(snode->series_loc[i])) == DB_MATCH) {
seradd = snode->series_loc[i];
series_inx = i;
found = 1;
break;
}
}
if (found == 0) {
free(snode);
(void) COND_PushCondition(DB_BADSERIES, "DB_AddImage: Can't find series %s", seriesuid);
return (DB_UnLock(DB_BADSERIES));
}
if (DB_ReadSeriesNode(&seradd, &sernode) != DB_NORMAL) {
free(snode);
(void) COND_PushCondition(DB_READERROR, "DB_AddImage: DB_ReadSeriesNode failed");
return (DB_UnLock(DB_READERROR));
}
/*
* Check for a duplicate Image
*/
for (i = 0; i < sernode->num_images; i++) {
if (DB_CompareImageUID(image->ImageUID,
&(sernode->image_loc[i])) == DB_MATCH) {
free(snode);
free(sernode);
(void) COND_PushCondition(DB_DUPIMAGE, "DB_AddImage: Duplicate image");
return (DB_UnLock(DB_DUPIMAGE));
}
}
size_to_alloc = sizeof(SeriesNode) + (sernode->num_allocated - DB_INITSIZE) *
sizeof(HunkBufAdd);
if (sernode->num_allocated == sernode->num_images) { /* Need to expand the
* list */
int study_node_size;
size_to_alloc = sizeof(SeriesNode);
size_to_alloc += (sernode->num_allocated + DB_EXPANDSIZE - DB_INITSIZE) *
sizeof(HunkBufAdd);
new_sernode = (SeriesNode *) malloc(size_to_alloc);
if (new_sernode == (SeriesNode *) NULL) {
free(snode);
free(sernode);
(void) COND_PushCondition(DB_NOMEMORY, "DB_AddImage: No memory");
return (DB_UnLock(DB_NOMEMORY));
}
memcpy((void *) new_sernode, (void *) sernode,
sizeof(SeriesNode) + ((sernode->num_allocated - DB_INITSIZE) *
sizeof(HunkBufAdd)));
for (i = sernode->num_allocated; i < sernode->num_allocated + DB_EXPANDSIZE; i++) {
new_sernode->image_loc[i].hunk_number = HUNK_PTR_NULL;
new_sernode->image_loc[i].node_number = HUNK_PTR_NULL;
}
new_sernode->num_allocated += DB_EXPANDSIZE;
free(sernode);
if (HF_AllocateRecord(size_to_alloc, &newseradd) != HF_NORMAL) {
free(snode);
free(new_sernode);
(void) COND_PushCondition(DB_ALLOCATERROR, "DB_AddImage: HF_AllocateRecord failed");
return (DB_UnLock(DB_ALLOCATERROR));
}
snode->series_loc[series_inx] = newseradd;
study_node_size = sizeof(StudyNode) + (snode->num_allocated - DB_INITSIZE) *
sizeof(HunkBufAdd);
if (HF_WriteRecord(&stuadd, study_node_size, (void *) snode)
!= HF_NORMAL) {
free(snode);
free(new_sernode);
(void) COND_PushCondition(DB_WRITERROR, "DB_AddImage: HF_WriteRecord failed");
return (DB_UnLock(DB_WRITERROR));
}
sernode = new_sernode;
(void) HF_DeallocateRecord(&seradd);
seradd = newseradd;
(void) HF_IncrementUpdateFlag(); /* We need to let folks know
* the db has changed */
}
free(snode);
if (HF_AllocateRecord(sizeof(ImageNode), &imgadd) != HF_NORMAL) {
free(sernode);
(void) COND_PushCondition(DB_ALLOCATERROR, "DB_AddImage: HF_AllocateRecord failed");
return (DB_UnLock(DB_ALLOCATERROR));
}
sernode->image_loc[sernode->num_images] = imgadd;
sernode->num_images++;
if (HF_WriteRecord(&seradd, size_to_alloc, (void *) sernode)
!= HF_NORMAL) {
free(sernode);
(void) COND_PushCondition(DB_WRITERROR, "DB_AddImage: HF_WriteRecord failed");
return (DB_UnLock(DB_WRITERROR));
}
free(sernode);
inode.time_stamp = time((time_t *) NULL);
inode.image = *image;
if (HF_WriteRecord(&imgadd, sizeof(ImageNode), (void *) &inode)
!= HF_NORMAL) {
(void) COND_PushCondition(DB_WRITERROR, "DB_AddImage: HF_WriteRecord failed");
return (DB_UnLock(DB_WRITERROR));
}
return (DB_UnLock(DB_NORMAL));
}
/* DB_ReadSeriesNode
**
** Purpose:
** DB_ReadSeriesNode reads a series node from the database.
**
** Parameter Dictionary:
** HunkBufAdd *seradd:
** The hunk address of the series node.
** SeriesNode **snode:
** A pointer to the SeriesNode struct to read from the database.
**
** Return Values:
**
** DB_NORMAL: The series node was read successfully.
** DB_READERROR: The record could not be read.
** DB_NOMEMORY: Couldn't allocate any memory for the new node.
**
** Algorithm:
** Normally, this would be a straight HF_ReadRecord call, however, Series
** Nodes (and Study Nodes), are not fixed in length. Their length depends
** on how many children they have. So, we must read the record with the
** default length, and then check to see how large it really is, and allocate
** enough memory to hold this new structure, then re-read it with the proper
** length.
**
*/
CONDITION
DB_ReadSeriesNode(HunkBufAdd *seradd, SeriesNode **snode)
{
SeriesNode temp;
int new_size;
if (HF_ReadRecord(seradd, sizeof(temp), (void *) &temp) != HF_NORMAL)
return (DB_READERROR);
if (temp.num_allocated > DB_INITSIZE) {
new_size = sizeof(SeriesNode);
new_size += (temp.num_allocated - DB_INITSIZE) * sizeof(HunkBufAdd);
*snode = (SeriesNode *) malloc(new_size);
if (*snode == (SeriesNode *) NULL)
return (DB_NOMEMORY);
if (HF_ReadRecord(seradd, new_size, (void *) *snode) != HF_NORMAL)
return (DB_READERROR);
} else {
*snode = (SeriesNode *) malloc(sizeof(SeriesNode));
if (*snode == (SeriesNode *) NULL)
return (DB_NOMEMORY);
**snode = temp;
}
return (DB_NORMAL);
}
/* DB_WriteSeriesNode
**
** Purpose:
** DB_WriteSeriesNode writes a series node to the database.
**
** Parameter Dictionary:
** HunkBufAdd *seradd:
** The hunk address of the series node.
** SeriesNode *sernode:
** A pointer to the SeriesNode struct to write to the database.
**
** Return Values:
**
** DB_NORMAL: The series node was written successfully.
** DB_WRITEERROR: The record could not be written.
**
** Algorithm:
** Normally, this would be a straight HF_WriteRecord call, however, Series
** Nodes (and Study Nodes), are not fixed in length. Their length depends
** on how many children they have. Therefore, we must compute the length
** from information in the record so that we write out the actual length
** of the structure...this is just a convenience routine.
**
*/
CONDITION
DB_WriteSeriesNode(HunkBufAdd *seradd, SeriesNode *sernode)
{
int size_to_write;
size_to_write = sizeof(SeriesNode);
size_to_write += (sernode->num_allocated - DB_INITSIZE) * sizeof(HunkBufAdd);
if (HF_WriteRecord(seradd, size_to_write, (void *) sernode) != HF_NORMAL)
return (DB_WRITERROR);
return (DB_NORMAL);
}
/* DB_WriteStudyNode
**
** Purpose:
** DB_WriteStudyNode writes a study node to the database.
**
** Parameter Dictionary:
** HunkBufAdd *stuadd:
** The hunk address of the study node.
** SeriesNode *snode:
** A pointer to the StudyNode struct to write to the database.
**
** Return Values:
**
** DB_NORMAL: The study node was written successfully.
** DB_WRITEERROR: The record could not be written.
**
** Algorithm:
** Normally, this would be a straight HF_WriteRecord call, however, Study
** Nodes (and Series Nodes), are not fixed in length. Their length depends
** on how many children they have. Therefore, we must compute the length
** from information in the record so that we write out the actual length
** of the structure...this is just a convenience routine.
**
*/
CONDITION
DB_WriteStudyNode(HunkBufAdd *stuadd, StudyNode *snode)
{
int size_to_write;
size_to_write = sizeof(StudyNode);
size_to_write += (snode->num_allocated - DB_INITSIZE) * sizeof(HunkBufAdd);
if (HF_WriteRecord(stuadd, size_to_write, (void *) snode) != HF_NORMAL)
return (DB_WRITERROR);
return (DB_NORMAL);
}
/* DB_ReadStudyNode
**
** Purpose:
** DB_ReadStudyNode reads a study node from the database.
**
** Parameter Dictionary:
** HunkBufAdd *stuadd:
** The hunk address of the study node.
** SeriesNode **snode:
** A pointer to the StudyNode struct to read from the database.
**
** Return Values:
**
** DB_NORMAL: The study node was read successfully.
** DB_READERROR: The record could not be read.
** DB_NOMEMORY: Couldn't allocate any memory for the new node.
**
** Algorithm:
** Normally, this would be a straight HF_ReadRecord call, however, Study
** Nodes (and Series Nodes), are not fixed in length. Their length depends
** on how many children they have. So, we must read the record with the
** default length, and then check to see how large it really is, and allocate
** enough memory to hold this new structure, then re-read it with the proper
** length.
**
*/
CONDITION
DB_ReadStudyNode(HunkBufAdd *stuadd, StudyNode **snode)
{
StudyNode temp;
int new_size;
if (HF_ReadRecord(stuadd, sizeof(temp), (void *) &temp) != HF_NORMAL)
return (DB_READERROR);
if (temp.num_allocated > DB_INITSIZE) {
new_size = sizeof(StudyNode);
new_size += (temp.num_allocated - DB_INITSIZE) * sizeof(HunkBufAdd);
*snode = (StudyNode *) malloc(new_size);
if (*snode == (StudyNode *) NULL)
return (DB_NOMEMORY);
if (HF_ReadRecord(stuadd, new_size, (void *) *snode) != HF_NORMAL)
return (DB_READERROR);
} else {
*snode = (StudyNode *) malloc(sizeof(StudyNode));
if (*snode == (StudyNode *) NULL)
return (DB_NOMEMORY);
**snode = temp;
}
return (DB_NORMAL);
}
/* DB_DelPatient
**
** Purpose:
** Deletes the patient and all descendants from the database.
**
** Parameter Dictionary:
** short dbid:
** The database identifier.
** char *patid:
** Identifies the patient to be deleted.
**
** Return Values:
** DB_NORMAL: The delete operation succeeded.
** DB_LOCKERROR: Could not acquire a requested lock
** DB_NOTOPENED: The database has not been opened.
** DB_BADPATIENT: The specified patient could not be found.
** DB_READERROR: The database record could not be read from the hunk file.
** DB_?: Any valid return from DB_DelBelowStudy
** DB_WRITERROR: The modified record could not be rewritten.
**
** Algorithm:
** This routine makes the normal checks for database not open, then tries
** to locate the patient record specified by patid. Inability to find this
** record is an error. Once found, DelPatient removes everything below
** each study underneath this patient, and eventually removes the patient
** record itsself.
*/
CONDITION
DB_DelPatient(short dbid, char *patid)
{
int
i,
found,
pat_inx = 0;
PatientNode
pnode;
StudyNode
*snode;
CONDITION
retval;
HunkBufAdd
patadd;
if (DB_Findid(dbid) != DB_NORMAL) {
(void) COND_PushCondition(DB_NOTOPENED, "DB_DelPatient: DB_Findid failed");
return (DB_NOTOPENED);
}
if (HF_ExclusiveLock() != HF_NORMAL) {
(void) COND_PushCondition(DB_LOCKERROR, "DB_DelPatient: HF_ExclusiveLock failed");
return (DB_LOCKERROR);
}
/*
* Check for the Patient...
*/
found = 0;
for (i = 0; i < GS_root.num_patients; i++) {
if (DB_ComparePatID(patid, &GS_root.patient_loc[i]) == DB_MATCH) {
patadd = GS_root.patient_loc[i];
pat_inx = i;
found = 1;
break;
}
}
if (found == 0) {
(void) COND_PushCondition(DB_BADPATIENT, "DB_DelPatient: Can't find patient %s", patid);
return (DB_UnLock(DB_BADPATIENT));
}
/*
* Read the Patient Record and remove all the studies for that patient.
*/
if (HF_ReadRecord(&patadd, sizeof(pnode), (void *) &pnode) != HF_NORMAL) {
(void) COND_PushCondition(DB_READERROR, "DB_DelPatient: HF_ReadRecord failed");
return (DB_UnLock(DB_READERROR));
}
for (i = 0; i < pnode.num_studies; i++) {
if (DB_ReadStudyNode(&(pnode.study_loc[i]), &snode) != DB_NORMAL) {
(void) COND_PushCondition(DB_READERROR, "DB_DelPatient: DB_ReadStudyNode failed");
return (DB_UnLock(DB_READERROR));
}
if ((retval = DB_DelBelowStudy(snode)) != DB_NORMAL) {
(void) COND_PushCondition(retval, "DB_DelPatient: DB_DelBelowStudy failed");
return (DB_UnLock(retval));
}
(void) HF_DeallocateRecord(&(pnode.study_loc[i]));
free(snode);
}
(void) HF_DeallocateRecord(&patadd);
if (pat_inx == (GS_root.num_patients - 1)) {
GS_root.patient_loc[pat_inx].hunk_number = HUNK_PTR_NULL;
GS_root.patient_loc[pat_inx].node_number = HUNK_PTR_NULL;
} else {
for (i = pat_inx; i < (GS_root.num_patients - 1); i++) {
GS_root.patient_loc[i] = GS_root.patient_loc[i + 1];
}
GS_root.patient_loc[GS_root.num_patients - 1].hunk_number = HUNK_PTR_NULL;
GS_root.patient_loc[GS_root.num_patients - 1].node_number = HUNK_PTR_NULL;
}
GS_root.num_patients--;
if (HF_WriteStaticRecord(0, sizeof(GS_root), (void *) &GS_root) != HF_NORMAL) {
(void) COND_PushCondition(DB_WRITERROR, "DB_DelPatient: HF_WriteStaticRecord failed");
return (DB_UnLock(DB_WRITERROR));
}
(void) HF_IncrementUpdateFlag();
return (DB_UnLock(DB_NORMAL));
}
/* DB_DelStudy
**
** Purpose:
** Deletes the study and all its descendants from the database.
**
** Parameter Dictionary:
** short dbid:
** The database identifier.
** char *patid:
** Identifies the patient to be deleted.
** char *studyuid:
** Identifies the study to be deleted.
**
** Return Values:
** DB_NORMAL: The delete operation succeeded.
** DB_LOCKERROR: Could not acquire a requested lock
** DB_NOTOPENED: The database has not been opened.
** DB_BADPATIENT: The specified patient could not be found.
** DB_READERROR: The database record could not be read from the hunk file.
** DB_BADSTUDY: The specified study could not be found.
** DB_?: Any valid return from DB_DelBelowStudy
** DB_WRITERROR: The modified record could not be rewritten.
** DB_?: Any valid return from DB_DelPatient
**
** Algorithm:
** This routine makes the normal checks for database not open, then tries
** to locate the patient record specified by patid and the study record
** indicated by studyuid. Inability to either of these records is an error.
** Once found, DelBelow Study removes everything below the study underneath
** this patient, and then removes the study.
*/
CONDITION
DB_DelStudy(short dbid, char *patid, char *studyuid)
{
int
i,
found,
study_inx = 0;
PatientNode
pnode;
StudyNode
*snode;
CONDITION
retval;
HunkBufAdd
patadd,
studyadd;
if (DB_Findid(dbid) != DB_NORMAL) {
(void) COND_PushCondition(DB_NOTOPENED, "DB_DelStudy: DB_Findid failed");
return (DB_NOTOPENED);
}
if (HF_ExclusiveLock() != HF_NORMAL) {
(void) COND_PushCondition(DB_LOCKERROR, "DB_DelStudy: HF_ExclusiveLock failed");
return (DB_LOCKERROR);
}
/*
* Check for the Patient...
*/
found = 0;
for (i = 0; i < GS_root.num_patients; i++) {
if (DB_ComparePatID(patid, &GS_root.patient_loc[i]) == DB_MATCH) {
patadd = GS_root.patient_loc[i];
found = 1;
break;
}
}
if (found == 0) {
(void) COND_PushCondition(DB_BADPATIENT, "DB_DelStudy: Can't find patient %s", patid);
return (DB_UnLock(DB_BADPATIENT));
}
/*
* Read the Patient Record and remove all the studies for that patient.
*/
if (HF_ReadRecord(&patadd, sizeof(pnode), (void *) &pnode) != HF_NORMAL) {
(void) COND_PushCondition(DB_READERROR, "DB_DelStudy: HF_ReadRecord failed");
return (DB_UnLock(DB_READERROR));
}
found = 0;
for (i = 0; i < pnode.num_studies; i++) {
if (DB_CompareStudyUID(studyuid, &(pnode.study_loc[i])) == DB_MATCH) {
studyadd = pnode.study_loc[i];
study_inx = i;
found = 1;
break;
}
}
if (found == 0) {
(void) COND_PushCondition(DB_BADSTUDY, "DB_DelStudy: Can't find study %s", studyuid);
return (DB_UnLock(DB_BADSTUDY));
}
if (DB_ReadStudyNode(&studyadd, &snode) != DB_NORMAL) {
(void) COND_PushCondition(DB_READERROR, "DB_DelStudy: HF_ReadRecord failed");
return (DB_UnLock(DB_READERROR));
}
if ((retval = DB_DelBelowStudy(snode)) != DB_NORMAL) {
(void) COND_PushCondition(retval, "DB_DelStudy: DB_DelBelowStudy failed");
return (DB_UnLock(retval));
}
(void) HF_DeallocateRecord(&studyadd);
free(snode);
if (study_inx == (pnode.num_studies - 1)) {
pnode.study_loc[study_inx].hunk_number = HUNK_PTR_NULL;
pnode.study_loc[study_inx].node_number = HUNK_PTR_NULL;
} else {
for (i = study_inx; i < (pnode.num_studies - 1); i++) {
pnode.study_loc[i] = pnode.study_loc[i + 1];
}
pnode.study_loc[pnode.num_studies - 1].hunk_number = HUNK_PTR_NULL;
pnode.study_loc[pnode.num_studies - 1].node_number = HUNK_PTR_NULL;
}
pnode.num_studies--;
if (HF_WriteRecord(&patadd, sizeof(pnode), (void *) &pnode) != HF_NORMAL) {
(void) COND_PushCondition(DB_WRITERROR, "DB_DelStudy: HF_WriteRecord failed");
return (DB_UnLock(DB_WRITERROR));
}
(void) HF_IncrementUpdateFlag();
return (DB_UnLock(DB_NORMAL));
}
/* DB_DelSeries
**
** Purpose:
** Deletes the series and all its descendants(images) from the database.
**
** Parameter Dictionary:
** short dbid:
** The database identifier.
** char *patid:
** Identifies the patient to be deleted.
** char *studyuid:
** Identifies the study to be deleted.
** char *seriesuid:
** Identifies the series to be deleted.
**
** Return Values:
** DB_NORMAL: The delete operation succeeded.
** DB_LOCKERROR: Could not acquire a requested lock
** DB_NOTOPENED: The database has not been opened.
** DB_BADPATIENT: The specified patient could not be found.
** DB_READERROR: The database record could not be read from the hunk file.
** DB_BADSTUDY: The specified study could not be found.
** DB_BADSERIES: The specified series could not be found.
** DB_?: Any valid return from DB_DelBelowSereis
** DB_WRITERROR: The modified record could not be rewritten.
** DB_?: Any valid return from DB_DelStudy
**
** Algorithm:
** This routine makes the normal checks for database not open, then tries
** to locate the patient record specified by patid, the study record
** indicated by studyuid, and the series record indicated by seriesuid.
** Inability to retrieve these records is an error. Once found, DelBelowSeries
** removes everything below each series underneath this patient and study,
** and then call DelStudy if this was the last series for this particular study.
** Recall that DelStudy may delete the patient if this is the last study for that
** particular patient. The thing to remember here is that deletion of a single
** series could well result in the removal of that patient.
*/
CONDITION
DB_DelSeries(short dbid, char *patid, char *studyuid, char *seriesuid)
{
int
i,
found,
series_inx = 0;
PatientNode
pnode;
StudyNode
*snode;
SeriesNode
*sernode;
CONDITION
retval;
HunkBufAdd
patadd,
studyadd,
seriesadd;
if (DB_Findid(dbid) != DB_NORMAL) {
(void) COND_PushCondition(DB_WRITERROR, "DB_DelSeries: DB_Findid failed");
return (DB_NOTOPENED);
}
if (HF_ExclusiveLock() != HF_NORMAL) {
(void) COND_PushCondition(DB_LOCKERROR, "DB_DelSeries: HF_ExclusiveLock failed");
return (DB_LOCKERROR);
}
/*
* Check for the Patient...
*/
found = 0;
for (i = 0; i < GS_root.num_patients; i++) {
if (DB_ComparePatID(patid, &GS_root.patient_loc[i]) == DB_MATCH) {
patadd = GS_root.patient_loc[i];
found = 1;
break;
}
}
if (found == 0) {
(void) COND_PushCondition(DB_BADPATIENT, "DB_DelSeries: Can't find patient %s", patid);
return (DB_UnLock(DB_BADPATIENT));
}
/*
* Read the Patient Record and remove all the studies for that patient.
*/
if (HF_ReadRecord(&patadd, sizeof(pnode), (void *) &pnode) != HF_NORMAL) {
(void) COND_PushCondition(DB_READERROR, "DB_DelSeries: HF_ReadRecord failed");
return (DB_UnLock(DB_READERROR));
}
found = 0;
for (i = 0; i < pnode.num_studies; i++) {
if (DB_CompareStudyUID(studyuid, &(pnode.study_loc[i])) == DB_MATCH) {
studyadd = pnode.study_loc[i];
found = 1;
break;
}
}
if (found == 0) {
(void) COND_PushCondition(DB_BADSTUDY, "DB_DelSeries: Can't find study %s", studyuid);
return (DB_UnLock(DB_BADSTUDY));
}
if (DB_ReadStudyNode(&studyadd, &snode) != DB_NORMAL) {
(void) COND_PushCondition(DB_READERROR, "DB_DelSeries: HF_ReadRecord failed");
return (DB_UnLock(DB_READERROR));
}
found = 0;
for (i = 0; i < snode->num_series; i++) {
if (DB_CompareSeriesUID(seriesuid, &(snode->series_loc[i])) == DB_MATCH) {
seriesadd = snode->series_loc[i];
series_inx = i;
found = 1;
break;
}
}
if (found == 0) {
free(snode);
(void) COND_PushCondition(DB_BADSERIES, "DB_DelSeries: Can't find series %s", seriesuid);
return (DB_UnLock(DB_BADSERIES));
}
if (DB_ReadSeriesNode(&seriesadd, &sernode) != DB_NORMAL) {
free(snode);
(void) COND_PushCondition(DB_READERROR, "DB_DelSeries: HF_ReadRecord failed");
return (DB_UnLock(DB_READERROR));
}
if ((retval = DB_DelBelowSeries(sernode)) != DB_NORMAL) {
free(snode);
free(sernode);
(void) COND_PushCondition(retval, "DB_DelSeries: DB_DelBelowSeries failed");
return (DB_UnLock(retval));
}
(void) HF_DeallocateRecord(&seriesadd);
free(sernode);
if (series_inx == (snode->num_series - 1)) {
snode->series_loc[series_inx].hunk_number = HUNK_PTR_NULL;
snode->series_loc[series_inx].node_number = HUNK_PTR_NULL;
} else {
for (i = series_inx; i < (snode->num_series - 1); i++) {
snode->series_loc[i] = snode->series_loc[i + 1];
}
snode->series_loc[snode->num_series - 1].hunk_number = HUNK_PTR_NULL;
snode->series_loc[snode->num_series - 1].node_number = HUNK_PTR_NULL;
}
(snode->num_series)--;
if (DB_WriteStudyNode(&studyadd, snode) != DB_NORMAL) {
free(snode);
(void) COND_PushCondition(DB_WRITERROR, "DB_DelSeries: HF_WriteRecord failed");
return (DB_UnLock(DB_WRITERROR));
}
free(snode);
(void) HF_IncrementUpdateFlag();
return (DB_UnLock(DB_NORMAL));
}
/* DB_DelImage
**
** Purpose:
** Deletes the image from the database.
**
** Parameter Dictionary:
** short dbid:
** The database identifier.
** char *patid:
** Identifies the patient to be deleted.
** char *studyuid:
** Identifies the study to be deleted.
** char *seriesuid:
** Identifies the series to be deleted.
** char *seriesuid:
** Identifies the image to be deleted.
**
** Return Values:
** DB_NORMAL: The delete operation succeeded.
** DB_LOCKERROR: Could not acquire a requested lock
** DB_NOTOPENED: The database has not been opened.
** DB_BADPATIENT: The specified patient could not be found.
** DB_READERROR: The database record could not be read from the hunk file.
** DB_BADSTUDY: The specified study could not be found.
** DB_BADSERIES: The specified series could not be found.
** DB_BADIMAGE: The specified image could not be found.
** DB_WRITERROR: The modified record could not be rewritten.
** DB_?: Any valid return from DB_DelSeries
**
** Algorithm:
** This routine makes the normal checks for database not open, then tries
** to locate the patient record specified by patid, the study record
** indicated by studyuid, the series record indicated by seriesuid, and the image
** record specified by imageuid. Inability to retrieve these records is an
** error. Once found, the routine deletes the image record and, if this was
** the last image of the series, it calls DB_DelSeries, which may call
** DB_DelStudy, and which may call DB_DelPatient.
*/
CONDITION
DB_DelImage(short dbid, char *patid, char *studyuid, char *seriesuid, char *imageuid)
{
int
i,
bad_file_delete,
found,
image_inx = 0;
PatientNode
pnode;
StudyNode
*snode;
SeriesNode
*sernode;
HunkBufAdd
patadd,
studyadd,
seriesadd,
imageadd;
ImageNode
inode;
if (DB_Findid(dbid) != DB_NORMAL) {
(void) COND_PushCondition(DB_WRITERROR, "DB_DelImage: DB_Findid failed");
return (DB_NOTOPENED);
}
if (HF_ExclusiveLock() != HF_NORMAL) {
(void) COND_PushCondition(DB_LOCKERROR, "DB_DelImage: HF_ExclusiveLock failed");
return (DB_LOCKERROR);
}
/*
* Check for the Patient...
*/
found = 0;
for (i = 0; i < GS_root.num_patients; i++) {
if (DB_ComparePatID(patid, &GS_root.patient_loc[i]) == DB_MATCH) {
patadd = GS_root.patient_loc[i];
found = 1;
break;
}
}
if (found == 0) {
(void) COND_PushCondition(DB_BADPATIENT, "DB_DelImage: Can't find patient %s", patid);
return (DB_UnLock(DB_BADPATIENT));
}
/*
* Read the Patient Record and remove all the studies for that patient.
*/
if (HF_ReadRecord(&patadd, sizeof(pnode), (void *) &pnode) != HF_NORMAL) {
(void) COND_PushCondition(DB_READERROR, "DB_DelImage: HF_ReadRecord failed");
return (DB_UnLock(DB_READERROR));
}
found = 0;
for (i = 0; i < pnode.num_studies; i++) {
if (DB_CompareStudyUID(studyuid, &(pnode.study_loc[i])) == DB_MATCH) {
studyadd = pnode.study_loc[i];
found = 1;
break;
}
}
if (found == 0) {
(void) COND_PushCondition(DB_BADSTUDY, "DB_DelImage: Can't find study %s", studyuid);
return (DB_UnLock(DB_BADSTUDY));
}
if (DB_ReadStudyNode(&studyadd, &snode) != DB_NORMAL) {
(void) COND_PushCondition(DB_READERROR, "DB_DelImage: HF_ReadRecord failed");
return (DB_UnLock(DB_READERROR));
}
found = 0;
for (i = 0; i < snode->num_series; i++) {
if (DB_CompareSeriesUID(seriesuid, &(snode->series_loc[i])) == DB_MATCH) {
seriesadd = snode->series_loc[i];
found = 1;
break;
}
}
if (found == 0) {
free(snode);
(void) COND_PushCondition(DB_BADSERIES, "DB_DelImage: Can't find series %s", seriesuid);
return (DB_UnLock(DB_BADSERIES));
}
if (DB_ReadSeriesNode(&seriesadd, &sernode) != DB_NORMAL) {
free(snode);
(void) COND_PushCondition(DB_READERROR, "DB_DelImage: HF_ReadRecord failed");
return (DB_UnLock(DB_READERROR));
}
found = 0;
for (i = 0; i < sernode->num_images; i++) {
if (DB_CompareImageUID(imageuid, &(sernode->image_loc[i])) == DB_MATCH) {
imageadd = sernode->image_loc[i];
image_inx = i;
found = 1;
break;
}
}
if (found == 0) {
free(sernode);
free(snode);
(void) COND_PushCondition(DB_BADIMAGE, "DB_DelImage: Can't find image %s", imageuid);
return (DB_UnLock(DB_BADIMAGE));
}
if (HF_ReadRecord(&imageadd, sizeof(inode), (void *) &inode) != HF_NORMAL) {
(void) COND_PushCondition(DB_READERROR, "DB_DelImage: HF_ReadRecord failed");
return (DB_UnLock(DB_READERROR));
}
/*
* Delete the image file from the filesystem...
*/
bad_file_delete = 0;
if (unlink(inode.image.FileName) != 0) {
(void) COND_PushCondition(DB_FILEDELERROR, "DB_DelImage: Image file delete failed");
bad_file_delete = 1;
}
(void) HF_DeallocateRecord(&imageadd);
if (image_inx == (sernode->num_images - 1)) {
sernode->image_loc[image_inx].hunk_number = HUNK_PTR_NULL;
sernode->image_loc[image_inx].node_number = HUNK_PTR_NULL;
} else {
for (i = image_inx; i < (sernode->num_images - 1); i++) {
sernode->image_loc[i] = sernode->image_loc[i + 1];
}
sernode->image_loc[sernode->num_images - 1].hunk_number = HUNK_PTR_NULL;
sernode->image_loc[sernode->num_images - 1].node_number = HUNK_PTR_NULL;
}
(sernode->num_images)--;
if (DB_WriteSeriesNode(&seriesadd, sernode) != DB_NORMAL) {
free(snode);
free(sernode);
(void) COND_PushCondition(DB_WRITERROR, "DB_DelImage: HF_WriteSeriesNode failed");
return (DB_UnLock(DB_WRITERROR));
}
free(snode);
free(sernode);
(void) HF_IncrementUpdateFlag();
if (bad_file_delete)
return (DB_UnLock(DB_FILEDELERROR));
else
return (DB_UnLock(DB_NORMAL));
}
/* DB_DelBelowStudy
**
** Purpose:
** Deletes everything below the study node specified.
**
** Parameter Dictionary:
** StudyNode *snode:
** Identifies the study node.
**
** Return Values:
** DB_NORMAL: The delete operation succeeded.
** DB_?: Any valid return from DB_ReadSeriesNode
** DB_?: Any valid return from DB_DelBelowSeries
**
** Algorithm:
** This is a utility routine for the main delete interface. This routine
** simply locates each series node for the specified study and calls
** DB_DelBelowSeries to remove each series and any descendents.
*/
CONDITION
DB_DelBelowStudy(StudyNode *snode)
{
CONDITION
retval;
SeriesNode
*sernode;
int
i;
for (i = 0; i < snode->num_series; i++) {
if ((retval = DB_ReadSeriesNode(&(snode->series_loc[i]), &sernode)) != DB_NORMAL) {
return (retval);
}
if ((retval = DB_DelBelowSeries(sernode)) != DB_NORMAL) {
return (retval);
}
(void) HF_DeallocateRecord(&(snode->series_loc[i]));
free(sernode);
}
return (DB_NORMAL);
}
/* DB_DelBelowSeries
**
** Purpose:
** Deletes everything below the series node specified.
**
** Parameter Dictionary:
** SeriesNode *snode:
** Identifies the series node.
**
** Return Values:
** DB_NORMAL: The delete operation succeeded.
**
** Algorithm:
** This is a utility routine for the main delete interface. This routine
** simply locates each image node for the specified series and deletes
** the image node from the database. It has the additional effect of removing
** the image file from the filesystem.
*/
CONDITION
DB_DelBelowSeries(SeriesNode *snode)
{
ImageNode
inode;
int
i;
for (i = 0; i < snode->num_images; i++) {
if (HF_ReadRecord(&(snode->image_loc[i]),
sizeof(inode), (void *) &inode) != HF_NORMAL) {
(void) COND_PushCondition(DB_READERROR, "DB_DelBelowSeries: HF_ReadRecord failed");
return (DB_READERROR);
}
/*
* Delete the image file from the filesystem...we're not going to
* return an error here if the unlink fails...it would get just too
* messy... It's not that big a deal anyway...it has nothing to do
* with database integrity and the database shouldn't be doing this
* stuff anyway...
*/
(void) unlink(inode.image.FileName);
(void) HF_DeallocateRecord(&(snode->image_loc[i]));
}
return (DB_NORMAL);
}
/* DB_GetPatient
**
** Purpose:
** Retrieves the first patient record from the database specified.
**
** Parameter Dictionary:
** short dbid:
** Identifies the database to use.
** PatientLevel *patient:
** A pointer to an existing patient level struct.
**
** Return Values:
** DB_NORMAL: The Get operation succeeded.
** DB_LOCKERROR: Could not acquire a requested lock
** DB_NOTOPENED: The database specified by dbid is not open.
** DB_NOPATIENTS: There are no patients in the database.
** DB_READERROR: Could not read the patient records.
**
** Algorithm:
** This routine is used in conjuction with DB_GetNextPatient. Calling this
** routine will return the first patient record to the caller, and then
** will save context for subsequent calls to DB_GetNextPatient.
*/
CONDITION
DB_GetPatient(short dbid, PatientLevel *patient)
{
PatientNode
pnode;
HunkBufAdd
ploc;
if (DB_Findid(dbid) != DB_NORMAL) {
(void) COND_PushCondition(DB_NOTOPENED, "DB_GetPatient: DB_Findid failed");
return (DB_NOTOPENED);
}
if (GS_root.num_patients <= 0) {
(void) COND_PushCondition(DB_NOPATIENTS, "DB_GetPatient: No patients");
return (DB_NOPATIENTS);
}
if (HF_SharedLock() != HF_NORMAL) {
(void) COND_PushCondition(DB_LOCKERROR, "DB_GetPatient: HF_SharedLock failed");
return (DB_LOCKERROR);
}
ploc = GS_root.patient_loc[0];
if (HF_ReadRecord(&ploc, sizeof(pnode), (void *) &pnode) != HF_NORMAL) {
(void) COND_PushCondition(DB_READERROR, "DB_GetPatient: HF_ReadRecord failed");
return (DB_UnLock(DB_READERROR));
}
*patient = pnode.pat;
DB_UpdatePatientContext(dbid, &ploc, 0, DB_PATIENTCONTEXT);
ploc.hunk_number = HUNK_PTR_NULL;
ploc.node_number = HUNK_PTR_NULL;
DB_UpdatePatientContext(dbid, &ploc, -1, DB_STUDYCONTEXT);
DB_UpdatePatientContext(dbid, &ploc, -1, DB_SERIESCONTEXT);
DB_UpdatePatientContext(dbid, &ploc, -1, DB_IMAGECONTEXT);
DB_SetChangeFlag(dbid);
return (DB_UnLock(DB_NORMAL));
}
/* DB_GetImage
**
** Purpose:
** Retrieves the first image record from the database specified.
**
** Parameter Dictionary:
** short dbid:
** Identifies the database to use.
** char *patid:
** The patient ID to retrieve.
** char *studyuid:
** The study UID to retrieve.
** char *seriesuid:
** The series UID to retrieve.
** ImageLevel *image:
** A pointer to the image level struct to fill.
**
** Return Values:
** DB_NORMAL: The Get operation succeeded.
** DB_LOCKERROR: Could not acquire a requested lock
** DB_NOTOPENED: The database specified by dbid is not open.
** DB_BADPATIENT: There are no patients that match patid.
** DB_BADSTUDY: There are no studies that match studyuid.
** DB_READERROR: Could not read required records.
** DB_NOMORE: No images match the selection criteria.
**
** Algorithm:
** This routine is used in conjuction with DB_GetNextImage. Calling this
** routine will return the first image record to the caller, and then
** will save context for subsequent calls to DB_GetNextImage.
*/
CONDITION
DB_GetImage(short dbid, char *patid, char *studyuid, char *seriesuid, ImageLevel *image)
{
int i,
patindex = 0,
studyindex = 0,
seriesindex = 0,
found;
PatientNode
pnode;
ImageNode
inode;
StudyNode
*snode;
SeriesNode
*sernode;
HunkBufAdd
iloc,
serloc,
sloc,
ploc;
if (DB_Findid(dbid) != DB_NORMAL) {
(void) COND_PushCondition(DB_NOTOPENED, "DB_GetImage: DB_Findid failed");
return (DB_NOTOPENED);
}
if (HF_SharedLock() != HF_NORMAL) {
(void) COND_PushCondition(DB_LOCKERROR, "DB_GetImage: HF_SharedLock failed");
return (DB_LOCKERROR);
}
found = 0;
for (i = 0; i < GS_root.num_patients; i++) {
if (DB_ComparePatID(patid, &GS_root.patient_loc[i]) == DB_MATCH) {
ploc = GS_root.patient_loc[i];
patindex = i;
found = 1;
break;
}
}
if (found == 0) {
(void) COND_PushCondition(DB_BADPATIENT, "DB_GetImage: Can't find patient %s", patid);
return (DB_UnLock(DB_BADPATIENT));
}
if (HF_ReadRecord(&ploc, sizeof(pnode), (void *) &pnode) != HF_NORMAL) {
(void) COND_PushCondition(DB_READERROR, "DB_GetImage: HF_ReadRecord failed");
return (DB_UnLock(DB_READERROR));
}
found = 0;
for (i = 0; i < pnode.num_studies; i++) {
if (DB_CompareStudyUID(studyuid, &pnode.study_loc[i]) == DB_MATCH) {
sloc = pnode.study_loc[i];
studyindex = i;
found = 1;
break;
}
}
if (found == 0) {
(void) COND_PushCondition(DB_BADSTUDY, "DB_GetImage: Can't find study %s", studyuid);
return (DB_UnLock(DB_BADSTUDY));
}
if (DB_ReadStudyNode(&sloc, &snode) != DB_NORMAL) {
(void) COND_PushCondition(DB_READERROR, "DB_GetImage: HF_ReadRecord failed");
return (DB_UnLock(DB_READERROR));
}
if (snode->num_series <= 0) {
free(snode);
(void) COND_PushCondition(DB_NOSERIES, "DB_GetImage: No series found");
return (DB_UnLock(DB_NOSERIES));
}
found = 0;
for (i = 0; i < snode->num_series; i++) {
if (DB_CompareSeriesUID(seriesuid, &snode->series_loc[i]) == DB_MATCH) {
serloc = snode->series_loc[i];
seriesindex = i;
found = 1;
break;
}
}
if (found == 0) {
(void) COND_PushCondition(DB_BADSERIES, "DB_GetImage: Can't find series %s", seriesuid);
return (DB_UnLock(DB_BADSERIES));
}
if (DB_ReadSeriesNode(&serloc, &sernode) != DB_NORMAL) {
free(snode);
(void) COND_PushCondition(DB_READERROR, "DB_GetImage: HF_ReadRecord failed");
return (DB_UnLock(DB_READERROR));
}
if (sernode->num_images <= 0) {
free(snode);
return (DB_UnLock(DB_NOMORE));
}
iloc = sernode->image_loc[0];
if (HF_ReadRecord(&iloc, sizeof(inode), (void *) &inode) != HF_NORMAL) {
free(snode);
free(sernode);
(void) COND_PushCondition(DB_READERROR, "DB_GetImage: HF_ReadRecord failed");
return (DB_UnLock(DB_READERROR));
}
*image = inode.image;
DB_UpdateImageContext(dbid, &ploc, patindex, DB_PATIENTCONTEXT);
DB_UpdateImageContext(dbid, &sloc, studyindex, DB_STUDYCONTEXT);
DB_UpdateImageContext(dbid, &serloc, seriesindex, DB_SERIESCONTEXT);
DB_UpdateImageContext(dbid, &iloc, 0, DB_IMAGECONTEXT);
DB_SetChangeFlag(dbid);
free(snode);
free(sernode);
return (DB_UnLock(DB_NORMAL));
}
/* DB_GetSeries
**
** Purpose:
** Retrieves the first series record from the database specified.
**
** Parameter Dictionary:
** short dbid:
** Identifies the database to use.
** char *patid:
** The patient ID to retrieve.
** char *studyuid:
** The study UID to retrieve.
** SeriesLevel *series:
** A pointer to the series level struct to fill.
**
** Return Values:
** DB_NORMAL: The Get operation succeeded.
** DB_LOCKERROR: Could not acquire a requested lock
** DB_NOTOPENED: The database specified by dbid is not open.
** DB_BADPATIENT: There are no patients that match patid.
** DB_BADSTUDY: There are no studies that match studyuid.
** DB_NOSERIES: There are no series for this patient/study pair.
** DB_READERROR: Could not read required records.
**
** Algorithm:
** This routine is used in conjuction with DB_GetNextSeries. Calling this
** routine will return the first series record to the caller, and then
** will save context for subsequent calls to DB_GetNextSeries.
*/
CONDITION
DB_GetSeries(short dbid, char *patid, char *studyuid, SeriesLevel *series)
{
int i,
patindex = 0,
studyindex = 0,
found;
PatientNode
pnode;
StudyNode
*snode;
SeriesNode
sernode;
HunkBufAdd
serloc,
sloc,
ploc;
if (DB_Findid(dbid) != DB_NORMAL) {
(void) COND_PushCondition(DB_NOTOPENED, "DB_GetSeries: DB_Findid failed");
return (DB_NOTOPENED);
}
if (HF_SharedLock() != HF_NORMAL) {
(void) COND_PushCondition(DB_LOCKERROR, "DB_GetSeries: HF_SharedLock failed");
return (DB_LOCKERROR);
}
found = 0;
for (i = 0; i < GS_root.num_patients; i++) {
if (DB_ComparePatID(patid, &GS_root.patient_loc[i]) == DB_MATCH) {
ploc = GS_root.patient_loc[i];
patindex = i;
found = 1;
break;
}
}
if (found == 0) {
(void) COND_PushCondition(DB_BADPATIENT, "DB_GetSeries: Can't find patient %s", patid);
return (DB_UnLock(DB_BADPATIENT));
}
if (HF_ReadRecord(&ploc, sizeof(pnode), (void *) &pnode) != HF_NORMAL) {
(void) COND_PushCondition(DB_READERROR, "DB_GetSeries: HF_ReadRecord failed");
return (DB_UnLock(DB_READERROR));
}
found = 0;
for (i = 0; i < pnode.num_studies; i++) {
if (DB_CompareStudyUID(studyuid, &pnode.study_loc[i]) == DB_MATCH) {
sloc = pnode.study_loc[i];
studyindex = i;
found = 1;
break;
}
}
if (found == 0) {
(void) COND_PushCondition(DB_BADSTUDY, "DB_GetSeries: Can't find study %s", studyuid);
return (DB_UnLock(DB_BADSTUDY));
}
if (DB_ReadStudyNode(&sloc, &snode) != DB_NORMAL) {
(void) COND_PushCondition(DB_READERROR, "DB_GetSeries: HF_ReadRecord failed");
return (DB_UnLock(DB_READERROR));
}
if (snode->num_series <= 0) {
free(snode);
(void) COND_PushCondition(DB_NOSERIES, "DB_GetSeries: No series available");
return (DB_UnLock(DB_NOSERIES));
}
serloc = snode->series_loc[0];
if (HF_ReadRecord(&serloc, sizeof(sernode), (void *) &sernode) != HF_NORMAL) {
free(snode);
(void) COND_PushCondition(DB_READERROR, "DB_GetSeries: HF_ReadRecord failed");
return (DB_UnLock(DB_READERROR));
}
*series = sernode.series;
DB_UpdateSeriesContext(dbid, &ploc, patindex, DB_PATIENTCONTEXT);
DB_UpdateSeriesContext(dbid, &sloc, studyindex, DB_STUDYCONTEXT);
DB_UpdateSeriesContext(dbid, &serloc, 0, DB_SERIESCONTEXT);
ploc.hunk_number = HUNK_PTR_NULL;
ploc.node_number = HUNK_PTR_NULL;
DB_UpdateSeriesContext(dbid, &ploc, -1, DB_IMAGECONTEXT);
DB_SetChangeFlag(dbid);
free(snode);
return (DB_UnLock(DB_NORMAL));
}
/* DB_GetStudy
**
** Purpose:
** Retrieves the first study record from the database specified.
**
** Parameter Dictionary:
** short dbid:
** Identifies the database to use.
** char *patid:
** The patient ID to retrieve.
** StudyLevel *study:
**
** Return Values:
** DB_NORMAL: The Get operation succeeded.
** DB_LOCKERROR: Could not acquire a requested lock
** DB_NOTOPENED: The database specified by dbid is not open.
** DB_BADPATIENT: There are no patients that match patid.
** DB_NOSTUDIES: There are no studies for this patient.
** DB_READERROR: Could not read required records.
**
** Algorithm:
** This routine is used in conjuction with DB_GetNextStudy. Calling this
** routine will return the first study record to the caller, and then
** will save context for subsequent calls to DB_GetNextStudy.
*/
CONDITION
DB_GetStudy(short dbid, char *patid, StudyLevel *study)
{
int i,
patindex = 0,
found;
PatientNode
pnode;
StudyNode
snode;
HunkBufAdd
sloc,
ploc;
if (DB_Findid(dbid) != DB_NORMAL) {
(void) COND_PushCondition(DB_NOTOPENED, "DB_GetStudy: DB_Findid failed");
return (DB_NOTOPENED);
}
if (HF_SharedLock() != HF_NORMAL) {
(void) COND_PushCondition(DB_LOCKERROR, "DB_GetStudy: HF_SharedLock failed");
return (DB_LOCKERROR);
}
found = 0;
for (i = 0; i < GS_root.num_patients; i++) {
if (DB_ComparePatID(patid, &GS_root.patient_loc[i]) == DB_MATCH) {
ploc = GS_root.patient_loc[i];
patindex = i;
found = 1;
break;
}
}
if (found == 0) {
(void) COND_PushCondition(DB_BADPATIENT, "DB_GetStudy: Can't find patient %s", patid);
return (DB_UnLock(DB_BADPATIENT));
}
if (HF_ReadRecord(&ploc, sizeof(pnode), (void *) &pnode) != HF_NORMAL) {
(void) COND_PushCondition(DB_READERROR, "DB_GetStudy: HF_ReadRecord failed");
return (DB_UnLock(DB_READERROR));
}
if (pnode.num_studies <= 0) {
(void) COND_PushCondition(DB_NOSTUDIES, "DB_GetStudy: No studies available");
return (DB_UnLock(DB_NOSTUDIES));
}
sloc = pnode.study_loc[0];
if (HF_ReadRecord(&sloc, sizeof(snode), (void *) &snode) != HF_NORMAL) {
(void) COND_PushCondition(DB_READERROR, "DB_GetStudy: HF_ReadRecord failed");
return (DB_UnLock(DB_READERROR));
}
*study = snode.study;
DB_UpdateStudyContext(dbid, &ploc, patindex, DB_PATIENTCONTEXT);
DB_UpdateStudyContext(dbid, &sloc, 0, DB_STUDYCONTEXT);
ploc.hunk_number = HUNK_PTR_NULL;
ploc.node_number = HUNK_PTR_NULL;
DB_UpdateStudyContext(dbid, &ploc, -1, DB_SERIESCONTEXT);
DB_UpdateStudyContext(dbid, &ploc, -1, DB_IMAGECONTEXT);
DB_SetChangeFlag(dbid);
return (DB_UnLock(DB_NORMAL));
}
/* DB_GetNextImage
**
** Purpose:
** Retrieves the next image record from the database specified.
**
** Parameter Dictionary:
** short dbid:
** Identifies the database to use.
** ImageLevel *image:
** A pointer to the image level struct to fill.
**
** Return Values:
** DB_NORMAL: The Get operation succeeded.
** DB_LOCKERROR: Could not acquire a requested lock
** DB_NOTOPENED: The database specified by dbid is not open.
** DB_NOMORE: No more image records are available.
** DB_READERROR: Could not read required records.
** DB_CHANGED: The database has changed since the initial
** DB_GetImage call.
**
** Algorithm:
** This routine is generally called after DB_GetImage. It uses the context
** from the previous call to determine which record to return. It then
** updates the context in preparation for the next call or nulls it out if
** there are no more records left to retreive.
*/
CONDITION
DB_GetNextImage(short dbid, ImageLevel *image)
{
HunkBufAdd
iloc,
serloc,
sloc,
ploc;
int
imageindex,
serindex,
sindex,
pindex;
ImageNode
inode;
SeriesNode
*sernode;
if (DB_Findid(dbid) != DB_NORMAL) {
(void) COND_PushCondition(DB_NOTOPENED, "DB_GetNextImage: DB_Findid failed");
return (DB_NOTOPENED);
}
if (HF_SharedLock() != HF_NORMAL) {
(void) COND_PushCondition(DB_LOCKERROR, "DB_GetNextImage: HF_SharedLock failed");
return (DB_LOCKERROR);
}
if (DB_CompareChangeFlag(dbid) != DB_MATCH) {
(void) COND_PushCondition(DB_CHANGED, "DB_GetNextImage: Database has been changed");
return (DB_UnLock(DB_CHANGED));
}
DB_GetImageContext(dbid, &ploc, &pindex, DB_PATIENTCONTEXT);
DB_GetImageContext(dbid, &sloc, &sindex, DB_STUDYCONTEXT);
DB_GetImageContext(dbid, &serloc, &serindex, DB_SERIESCONTEXT);
DB_GetImageContext(dbid, &iloc, &imageindex, DB_IMAGECONTEXT);
if (ploc.hunk_number == HUNK_PTR_NULL ||
ploc.node_number == HUNK_PTR_NULL ||
sloc.hunk_number == HUNK_PTR_NULL ||
sloc.node_number == HUNK_PTR_NULL ||
serloc.hunk_number == HUNK_PTR_NULL ||
serloc.node_number == HUNK_PTR_NULL ||
iloc.hunk_number == HUNK_PTR_NULL ||
iloc.node_number == HUNK_PTR_NULL ||
pindex == -1 || sindex == -1 || serindex == -1 || imageindex == -1)
return (DB_UnLock(DB_NOMORE));
if (DB_ReadSeriesNode(&serloc, &sernode) != DB_NORMAL) {
(void) COND_PushCondition(DB_READERROR, "DB_GetNextImage: DB_ReadSeriesNode failed");
return (DB_UnLock(DB_READERROR));
}
imageindex++;
if (imageindex >= sernode->num_images) {
ploc.hunk_number = HUNK_PTR_NULL;
ploc.node_number = HUNK_PTR_NULL;
DB_UpdateImageContext(dbid, &ploc, -1, DB_PATIENTCONTEXT);
DB_UpdateImageContext(dbid, &ploc, -1, DB_STUDYCONTEXT);
DB_UpdateImageContext(dbid, &ploc, -1, DB_SERIESCONTEXT);
DB_UpdateImageContext(dbid, &ploc, -1, DB_IMAGECONTEXT);
free(sernode);
return (DB_UnLock(DB_NOMORE));
}
iloc = sernode->image_loc[imageindex];
if (HF_ReadRecord(&iloc, sizeof(inode), (void *) &inode) != HF_NORMAL) {
free(sernode);
(void) COND_PushCondition(DB_READERROR, "DB_GetNextImage: HF_ReadRecord failed");
return (DB_READERROR);
}
*image = inode.image;
DB_UpdateImageContext(dbid, &iloc, imageindex, DB_IMAGECONTEXT);
free(sernode);
return (DB_NORMAL);
}
/* DB_GetNextSeries
**
** Purpose:
** Retrieves the next series record from the database specified.
**
** Parameter Dictionary:
** short dbid:
** Identifies the database to use.
** SeriesLevel *series:
** A pointer to the series level struct to fill.
**
** Return Values:
** DB_NORMAL: The Get operation succeeded.
** DB_LOCKERROR: Could not acquire a requested lock
** DB_NOTOPENED: The database specified by dbid is not open.
** DB_NOMORE: No more series records are available.
** DB_READERROR: Could not read required records.
** DB_CHANGED: The database has changed since the initial
** DB_GetSeries call.
**
** Algorithm:
** This routine is generally called after DB_GetSeries. It uses the context
** from the previous call to determine which record to return. It then
** updates the context in preparation for the next call or nulls it out if
** there are no more records left to retreive.
*/
CONDITION
DB_GetNextSeries(short dbid, SeriesLevel *series)
{
HunkBufAdd
serloc,
sloc,
ploc;
int
serindex,
sindex,
pindex;
SeriesNode
sernode;
StudyNode
*snode;
if (DB_Findid(dbid) != DB_NORMAL) {
(void) COND_PushCondition(DB_NOTOPENED, "DB_GetNextSeries: DB_Findid failed");
return (DB_NOTOPENED);
}
if (HF_SharedLock() != HF_NORMAL) {
(void) COND_PushCondition(DB_LOCKERROR, "DB_GetNextSeries: HF_SharedLock failed");
return (DB_LOCKERROR);
}
if (DB_CompareChangeFlag(dbid) != DB_MATCH) {
(void) COND_PushCondition(DB_CHANGED, "DB_GetNextSeries: Database has been changed");
return (DB_UnLock(DB_CHANGED));
}
DB_GetSeriesContext(dbid, &ploc, &pindex, DB_PATIENTCONTEXT);
DB_GetSeriesContext(dbid, &sloc, &sindex, DB_STUDYCONTEXT);
DB_GetSeriesContext(dbid, &serloc, &serindex, DB_SERIESCONTEXT);
if (ploc.hunk_number == HUNK_PTR_NULL ||
ploc.node_number == HUNK_PTR_NULL ||
sloc.hunk_number == HUNK_PTR_NULL ||
sloc.node_number == HUNK_PTR_NULL ||
serloc.hunk_number == HUNK_PTR_NULL ||
serloc.node_number == HUNK_PTR_NULL ||
pindex == -1 || sindex == -1 || serindex == -1)
return (DB_UnLock(DB_NOMORE));
if (DB_ReadStudyNode(&sloc, &snode) != DB_NORMAL) {
(void) COND_PushCondition(DB_READERROR, "DB_GetNextSeries: DB_ReadStudyNode failed");
return (DB_UnLock(DB_READERROR));
}
serindex++;
if (serindex >= snode->num_series) {
ploc.hunk_number = HUNK_PTR_NULL;
ploc.node_number = HUNK_PTR_NULL;
DB_UpdateSeriesContext(dbid, &ploc, -1, DB_PATIENTCONTEXT);
DB_UpdateSeriesContext(dbid, &ploc, -1, DB_STUDYCONTEXT);
DB_UpdateSeriesContext(dbid, &ploc, -1, DB_SERIESCONTEXT);
DB_UpdateSeriesContext(dbid, &ploc, -1, DB_IMAGECONTEXT);
free(snode);
return (DB_UnLock(DB_NOMORE));
}
serloc = snode->series_loc[serindex];
if (HF_ReadRecord(&serloc, sizeof(sernode), (void *) &sernode) != HF_NORMAL) {
free(snode);
(void) COND_PushCondition(DB_READERROR, "DB_GetNextSeries: HF_ReadRecord failed");
return (DB_UnLock(DB_READERROR));
}
*series = sernode.series;
DB_UpdateSeriesContext(dbid, &sloc, sindex, DB_STUDYCONTEXT);
DB_UpdateSeriesContext(dbid, &serloc, serindex, DB_SERIESCONTEXT);
free(snode);
return (DB_UnLock(DB_NORMAL));
}
/* DB_GetNextStudy
**
** Purpose:
** Retrieves the next study record from the database specified.
**
** Parameter Dictionary:
** short dbid:
** Identifies the database to use.
** StudyLevel *study:
** A pointer to the study level struct to fill.
**
** Return Values:
** DB_NORMAL: The Get operation succeeded.
** DB_LOCKERROR: Could not acquire a requested lock
** DB_NOTOPENED: The database specified by dbid is not open.
** DB_NOMORE: No more study records are available.
** DB_READERROR: Could not read required records.
** DB_CHANGED: The database has changed since the initial
** DB_GetStudy call.
**
** Algorithm:
** This routine is generally called after DB_GetStudy. It uses the context
** from the previous call to determine which record to return. It then
** updates the context in preparation for the next call or nulls it out if
** there are no more records left to retreive.
*/
CONDITION
DB_GetNextStudy(short dbid, StudyLevel *study)
{
HunkBufAdd
sloc,
ploc;
int
sindex,
pindex;
StudyNode
snode;
PatientNode
pnode;
if (DB_Findid(dbid) != DB_NORMAL) {
(void) COND_PushCondition(DB_NOTOPENED, "DB_GetNextStudy: DB_Findid failed");
return (DB_NOTOPENED);
}
if (HF_SharedLock() != HF_NORMAL) {
(void) COND_PushCondition(DB_LOCKERROR, "DB_GetNextStudy: HF_SharedLock failed");
return (DB_LOCKERROR);
}
if (DB_CompareChangeFlag(dbid) != DB_MATCH) {
(void) COND_PushCondition(DB_CHANGED, "DB_GetNextStudy: Database has been changed");
return (DB_UnLock(DB_CHANGED));
}
DB_GetStudyContext(dbid, &ploc, &pindex, DB_PATIENTCONTEXT);
DB_GetStudyContext(dbid, &sloc, &sindex, DB_STUDYCONTEXT);
if (ploc.hunk_number == HUNK_PTR_NULL ||
ploc.node_number == HUNK_PTR_NULL ||
sloc.hunk_number == HUNK_PTR_NULL ||
sloc.node_number == HUNK_PTR_NULL ||
pindex == -1 || sindex == -1)
return (DB_UnLock(DB_NOMORE));
if (HF_ReadRecord(&ploc, sizeof(pnode), (void *) &pnode) != HF_NORMAL) {
(void) COND_PushCondition(DB_READERROR, "DB_GetNextStudy: HF_ReadRecord failed");
return (DB_UnLock(DB_READERROR));
}
sindex++;
if (sindex >= pnode.num_studies) {
ploc.hunk_number = HUNK_PTR_NULL;
ploc.node_number = HUNK_PTR_NULL;
DB_UpdateStudyContext(dbid, &ploc, -1, DB_PATIENTCONTEXT);
DB_UpdateStudyContext(dbid, &ploc, -1, DB_STUDYCONTEXT);
DB_UpdateStudyContext(dbid, &ploc, -1, DB_SERIESCONTEXT);
DB_UpdateStudyContext(dbid, &ploc, -1, DB_IMAGECONTEXT);
return (DB_UnLock(DB_NOMORE));
}
sloc = pnode.study_loc[sindex];
if (HF_ReadRecord(&sloc, sizeof(snode), (void *) &snode) != HF_NORMAL) {
(void) COND_PushCondition(DB_READERROR, "DB_GetNextStudy: HF_ReadRecord failed");
return (DB_UnLock(DB_READERROR));
}
*study = snode.study;
DB_UpdateStudyContext(dbid, &sloc, sindex, DB_STUDYCONTEXT);
return (DB_UnLock(DB_NORMAL));
}
/* DB_GetNextPatient
**
** Purpose:
** Retrieves the next patient record from the database specified.
**
** Parameter Dictionary:
** short dbid:
** Identifies the database to use.
** PatientLevel *patient:
** A pointer to the patient level struct to fill.
**
** Return Values:
** DB_NORMAL: The Get operation succeeded.
** DB_LOCKERROR: Could not acquire a requested lock
** DB_NOTOPENED: The database specified by dbid is not open.
** DB_NOMORE: No more patient records are available.
** DB_READERROR: Could not read required records.
** DB_CHANGED: The database has changed since the initial
** DB_GetPatient call.
**
** Algorithm:
** This routine is generally called after DB_GetPatient. It uses the context
** from the previous call to determine which record to return. It then
** updates the context in preparation for the next call or nulls it out if
** there are no more records left to retreive.
*/
CONDITION
DB_GetNextPatient(short dbid, PatientLevel *patient)
{
HunkBufAdd
ploc;
int
pindex;
PatientNode
pnode;
if (DB_Findid(dbid) != DB_NORMAL) {
(void) COND_PushCondition(DB_NOTOPENED, "DB_GetNextPatient: DB_Findid failed");
return (DB_NOTOPENED);
}
if (HF_SharedLock() != HF_NORMAL) {
(void) COND_PushCondition(DB_LOCKERROR, "DB_GetNextPatient: HF_SharedLock failed");
return (DB_LOCKERROR);
}
if (DB_CompareChangeFlag(dbid) != DB_MATCH) {
(void) COND_PushCondition(DB_CHANGED, "DB_GetNextPatient: Database has been changed");
return (DB_UnLock(DB_CHANGED));
}
DB_GetPatientContext(dbid, &ploc, &pindex, DB_PATIENTCONTEXT);
if (ploc.hunk_number == HUNK_PTR_NULL ||
ploc.node_number == HUNK_PTR_NULL ||
pindex == -1)
return (DB_UnLock(DB_NOMORE));
pindex++;
if (pindex >= GS_root.num_patients) {
ploc.hunk_number = HUNK_PTR_NULL;
ploc.node_number = HUNK_PTR_NULL;
DB_UpdatePatientContext(dbid, &ploc, -1, DB_PATIENTCONTEXT);
DB_UpdatePatientContext(dbid, &ploc, -1, DB_STUDYCONTEXT);
DB_UpdatePatientContext(dbid, &ploc, -1, DB_SERIESCONTEXT);
DB_UpdatePatientContext(dbid, &ploc, -1, DB_IMAGECONTEXT);
return (DB_UnLock(DB_NOMORE));
}
ploc = GS_root.patient_loc[pindex];
if (HF_ReadRecord(&ploc, sizeof(pnode), (void *) &pnode) != HF_NORMAL) {
(void) COND_PushCondition(DB_READERROR, "DB_GetNextPatient: HF_ReadRecord failed");
return (DB_UnLock(DB_READERROR));
}
*patient = pnode.pat;
DB_UpdatePatientContext(dbid, &ploc, pindex, DB_PATIENTCONTEXT);
return (DB_UnLock(DB_NORMAL));
}
/* DB_UpdatePatientContext
**
*/
void
DB_UpdatePatientContext(short dbid, HunkBufAdd *p, int index, int level)
{
DBidstruct
*temp;
if (GS_context == (DBidstruct *) NULL)
return;
temp = GS_context;
while (temp != (DBidstruct *) NULL) {
if (temp->dbid == dbid) {
switch (level) {
case DB_PATIENTCONTEXT:
temp->pacontxt.last_patient = *p;
temp->pacontxt.last_patientindex = index;
break;
case DB_STUDYCONTEXT:
temp->pacontxt.last_study = *p;
temp->pacontxt.last_studyindex = index;
break;
case DB_SERIESCONTEXT:
temp->pacontxt.last_series = *p;
temp->pacontxt.last_seriesindex = index;
break;
case DB_IMAGECONTEXT:
temp->pacontxt.last_image = *p;
temp->pacontxt.last_imageindex = index;
break;
}
}
temp = temp->next;
}
return;
}
/* DB_UpdateStudyContext
**
*/
void
DB_UpdateStudyContext(short dbid, HunkBufAdd *p, int index, int level)
{
DBidstruct
*temp;
if (GS_context == (DBidstruct *) NULL)
return;
temp = GS_context;
while (temp != (DBidstruct *) NULL) {
if (temp->dbid == dbid) {
switch (level) {
case DB_PATIENTCONTEXT:
temp->stcontxt.last_patient = *p;
temp->stcontxt.last_patientindex = index;
break;
case DB_STUDYCONTEXT:
temp->stcontxt.last_study = *p;
temp->stcontxt.last_studyindex = index;
break;
case DB_SERIESCONTEXT:
temp->stcontxt.last_series = *p;
temp->stcontxt.last_seriesindex = index;
break;
case DB_IMAGECONTEXT:
temp->stcontxt.last_image = *p;
temp->stcontxt.last_imageindex = index;
break;
}
}
temp = temp->next;
}
return;
}
/* DB_UpdateSeriesContext
**
*/
void
DB_UpdateSeriesContext(short dbid, HunkBufAdd *p, int index, int level)
{
DBidstruct
*temp;
if (GS_context == (DBidstruct *) NULL)
return;
temp = GS_context;
while (temp != (DBidstruct *) NULL) {
if (temp->dbid == dbid) {
switch (level) {
case DB_PATIENTCONTEXT:
temp->secontxt.last_patient = *p;
temp->secontxt.last_patientindex = index;
break;
case DB_STUDYCONTEXT:
temp->secontxt.last_study = *p;
temp->secontxt.last_studyindex = index;
break;
case DB_SERIESCONTEXT:
temp->secontxt.last_series = *p;
temp->secontxt.last_seriesindex = index;
break;
case DB_IMAGECONTEXT:
temp->secontxt.last_image = *p;
temp->secontxt.last_imageindex = index;
break;
}
}
temp = temp->next;
}
return;
}
/* DB_UpdateQueryContext
**
*/
void
DB_UpdateQueryContext(short dbid, HunkBufAdd *p, int index, int level)
{
DBidstruct
*temp;
if (GS_context == (DBidstruct *) NULL)
return;
temp = GS_context;
while (temp != (DBidstruct *) NULL) {
if (temp->dbid == dbid) {
switch (level) {
case DB_PATIENTCONTEXT:
temp->querycontxt.last_patient = *p;
temp->querycontxt.last_patientindex = index;
break;
case DB_STUDYCONTEXT:
temp->querycontxt.last_study = *p;
temp->querycontxt.last_studyindex = index;
break;
case DB_SERIESCONTEXT:
temp->querycontxt.last_series = *p;
temp->querycontxt.last_seriesindex = index;
break;
case DB_IMAGECONTEXT:
temp->querycontxt.last_image = *p;
temp->querycontxt.last_imageindex = index;
break;
}
}
temp = temp->next;
}
return;
}
/* DB_UpdateImageContext
**
*/
void
DB_UpdateImageContext(short dbid, HunkBufAdd *p, int index, int level)
{
DBidstruct
*temp;
if (GS_context == (DBidstruct *) NULL)
return;
temp = GS_context;
while (temp != (DBidstruct *) NULL) {
if (temp->dbid == dbid) {
switch (level) {
case DB_PATIENTCONTEXT:
temp->imcontxt.last_patient = *p;
temp->imcontxt.last_patientindex = index;
break;
case DB_STUDYCONTEXT:
temp->imcontxt.last_study = *p;
temp->imcontxt.last_studyindex = index;
break;
case DB_SERIESCONTEXT:
temp->imcontxt.last_series = *p;
temp->imcontxt.last_seriesindex = index;
break;
case DB_IMAGECONTEXT:
temp->imcontxt.last_image = *p;
temp->imcontxt.last_imageindex = index;
break;
}
}
temp = temp->next;
}
return;
}
/* DB_GetPatientContext
**
*/
void
DB_GetPatientContext(short dbid, HunkBufAdd *loc, int *index, int level)
{
DBidstruct
*temp;
loc->hunk_number = HUNK_PTR_NULL;
loc->node_number = HUNK_PTR_NULL;
*index = -1;
if (GS_context == (DBidstruct *) NULL)
return;
temp = GS_context;
while (temp != (DBidstruct *) NULL) {
if (temp->dbid == dbid) {
switch (level) {
case DB_PATIENTCONTEXT:
*loc = temp->pacontxt.last_patient;
*index = temp->pacontxt.last_patientindex;
break;
case DB_STUDYCONTEXT:
*loc = temp->pacontxt.last_study;
*index = temp->pacontxt.last_studyindex;
break;
case DB_SERIESCONTEXT:
*loc = temp->pacontxt.last_series;
*index = temp->pacontxt.last_seriesindex;
break;
case DB_IMAGECONTEXT:
*loc = temp->pacontxt.last_image;
*index = temp->pacontxt.last_imageindex;
break;
}
}
temp = temp->next;
}
return;
}
/* DB_GetStudyContext
**
*/
void
DB_GetStudyContext(short dbid, HunkBufAdd *loc, int *index, int level)
{
DBidstruct
*temp;
loc->hunk_number = HUNK_PTR_NULL;
loc->node_number = HUNK_PTR_NULL;
*index = -1;
if (GS_context == (DBidstruct *) NULL)
return;
temp = GS_context;
while (temp != (DBidstruct *) NULL) {
if (temp->dbid == dbid) {
switch (level) {
case DB_PATIENTCONTEXT:
*loc = temp->stcontxt.last_patient;
*index = temp->stcontxt.last_patientindex;
break;
case DB_STUDYCONTEXT:
*loc = temp->stcontxt.last_study;
*index = temp->stcontxt.last_studyindex;
break;
case DB_SERIESCONTEXT:
*loc = temp->stcontxt.last_series;
*index = temp->stcontxt.last_seriesindex;
break;
case DB_IMAGECONTEXT:
*loc = temp->stcontxt.last_image;
*index = temp->stcontxt.last_imageindex;
break;
}
}
temp = temp->next;
}
return;
}
/* DB_GetSeriesContext
**
*/
void
DB_GetSeriesContext(short dbid, HunkBufAdd *loc, int *index, int level)
{
DBidstruct
*temp;
loc->hunk_number = HUNK_PTR_NULL;
loc->node_number = HUNK_PTR_NULL;
*index = -1;
if (GS_context == (DBidstruct *) NULL)
return;
temp = GS_context;
while (temp != (DBidstruct *) NULL) {
if (temp->dbid == dbid) {
switch (level) {
case DB_PATIENTCONTEXT:
*loc = temp->secontxt.last_patient;
*index = temp->secontxt.last_patientindex;
break;
case DB_STUDYCONTEXT:
*loc = temp->secontxt.last_study;
*index = temp->secontxt.last_studyindex;
break;
case DB_SERIESCONTEXT:
*loc = temp->secontxt.last_series;
*index = temp->secontxt.last_seriesindex;
break;
case DB_IMAGECONTEXT:
*loc = temp->secontxt.last_image;
*index = temp->secontxt.last_imageindex;
break;
}
}
temp = temp->next;
}
return;
}
/* DB_GetImageContext
**
*/
void
DB_GetImageContext(short dbid, HunkBufAdd *loc, int *index, int level)
{
DBidstruct
*temp;
loc->hunk_number = HUNK_PTR_NULL;
loc->node_number = HUNK_PTR_NULL;
*index = -1;
if (GS_context == (DBidstruct *) NULL)
return;
temp = GS_context;
while (temp != (DBidstruct *) NULL) {
if (temp->dbid == dbid) {
switch (level) {
case DB_PATIENTCONTEXT:
*loc = temp->imcontxt.last_patient;
*index = temp->imcontxt.last_patientindex;
break;
case DB_STUDYCONTEXT:
*loc = temp->imcontxt.last_study;
*index = temp->imcontxt.last_studyindex;
break;
case DB_SERIESCONTEXT:
*loc = temp->imcontxt.last_series;
*index = temp->imcontxt.last_seriesindex;
break;
case DB_IMAGECONTEXT:
*loc = temp->imcontxt.last_image;
*index = temp->imcontxt.last_imageindex;
break;
}
}
temp = temp->next;
}
return;
}
/* DB_GetQueryContext
**
*/
void
DB_GetQueryContext(short dbid, HunkBufAdd *loc, int *index, int level)
{
DBidstruct
*temp;
loc->hunk_number = HUNK_PTR_NULL;
loc->node_number = HUNK_PTR_NULL;
*index = -1;
if (GS_context == (DBidstruct *) NULL)
return;
temp = GS_context;
while (temp != (DBidstruct *) NULL) {
if (temp->dbid == dbid) {
switch (level) {
case DB_PATIENTCONTEXT:
*loc = temp->querycontxt.last_patient;
*index = temp->querycontxt.last_patientindex;
break;
case DB_STUDYCONTEXT:
*loc = temp->querycontxt.last_study;
*index = temp->querycontxt.last_studyindex;
break;
case DB_SERIESCONTEXT:
*loc = temp->querycontxt.last_series;
*index = temp->querycontxt.last_seriesindex;
break;
case DB_IMAGECONTEXT:
*loc = temp->querycontxt.last_image;
*index = temp->querycontxt.last_imageindex;
break;
}
}
temp = temp->next;
}
return;
}
/* DB_GetNumberofStudies
**
** Purpose:
** Retrieves the total number of studies in the database.
**
** Parameter Dictionary:
** short dbid:
** Identifies the database to use.
** char *numstudies:
** Receives the total number of studies in the database.
**
** Return Values:
** DB_NORMAL: The Get operation succeeded.
** DB_LOCKERROR: Could not acquire a requested lock
** DB_NOTOPENED: The database specified by dbid is not open.
** DB_READERROR: Could not read required records.
**
** Algorithm:
** Very simple routine to look at all the patients and retrieve the
** number of studies each has. Accumulating this value retreives the
** the total number of studies in the database.
*/
CONDITION
DB_GetNumberofStudies(short dbid, long *numstudies)
{
int i,
num;
PatientNode
pnode;
HunkBufAdd
ploc;
if (DB_Findid(dbid) != DB_NORMAL) {
(void) COND_PushCondition(DB_NOTOPENED, "DB_GetNumberofStudies: DB_Findid failed");
return (DB_NOTOPENED);
}
if (HF_SharedLock() != HF_NORMAL) {
(void) COND_PushCondition(DB_LOCKERROR, "DB_GetNumberofStudies: HF_SharedLock failed");
return (DB_LOCKERROR);
}
num = 0;
for (i = 0; i < GS_root.num_patients; i++) {
ploc = GS_root.patient_loc[i];
if (HF_ReadRecord(&ploc, sizeof(pnode), (void *) &pnode) != HF_NORMAL) {
(void) COND_PushCondition(DB_READERROR, "DB_GetNumberofStudies: HF_ReadRecord failed");
return (DB_UnLock(DB_READERROR));
}
num += pnode.num_studies;
}
*numstudies = num;
return (DB_UnLock(DB_NORMAL));
}
/* DB_DelOldestStudy
**
** Purpose:
** Deletes the oldest study in the database..
**
** Parameter Dictionary:
** short dbid:
** Identifies the database to use.
**
** Return Values:
** DB_NORMAL: The Get operation succeeded.
** DB_LOCKERROR: Could not acquire a requested lock
** DB_NOTOPENED: The database specified by dbid is not open.
** DB_READERROR: Could not read required records.
** DB_?: Any valid return from DB_DelStudy.
**
** Algorithm:
** Very simple routine to look at all the studies and delete the oldest one.
*/
CONDITION
DB_DelOldestStudy(short dbid)
{
CONDITION
ret_val;
char
patid[DB_MAXKEYLENGTH],
studyuid[DB_MAXKEYLENGTH];
int
delpat,
i,
j;
PatientNode
pnode;
StudyNode
snode;
HunkBufAdd
sloc,
ploc;
time_t
ts;
if (DB_Findid(dbid) != DB_NORMAL) {
(void) COND_PushCondition(DB_NOTOPENED, "DB_DelOldestStudy: DB_Findid failed");
return (DB_NOTOPENED);
}
if (HF_SharedLock() != HF_NORMAL) {
(void) COND_PushCondition(DB_LOCKERROR, "DB_DelOldestStudy: HF_SharedLock failed");
return (DB_LOCKERROR);
}
delpat = 0;
ts = 0;
for (i = 0; i < GS_root.num_patients; i++) {
ploc = GS_root.patient_loc[i];
if (HF_ReadRecord(&ploc, sizeof(pnode), (void *) &pnode) != HF_NORMAL) {
(void) COND_PushCondition(DB_READERROR, "DB_DelOldestStudy: HF_ReadRecord failed");
return (DB_UnLock(DB_READERROR));
}
if (pnode.num_studies == 1)
delpat = 1;
for (j = 0; j < pnode.num_studies; j++) {
sloc = pnode.study_loc[j];
if (HF_ReadRecord(&sloc, sizeof(snode), (void *) &snode) != HF_NORMAL) {
(void) COND_PushCondition(DB_READERROR, "DB_DelOldestStudy: HF_ReadRecord failed");
return (DB_UnLock(DB_READERROR));
}
if (ts == 0) {
ts = snode.time_stamp;
strcpy(patid, pnode.pat.PatID);
strcpy(studyuid, snode.study.StudyUID);
} else {
if (ts > snode.time_stamp) {
ts = snode.time_stamp;
strcpy(patid, pnode.pat.PatID);
strcpy(studyuid, snode.study.StudyUID);
}
}
}
}
(void) HF_UnLock();
if ((ret_val = DB_DelStudy(dbid, patid, studyuid)) != DB_NORMAL)
return (ret_val);
if (delpat)
return (DB_DelPatient(dbid, patid));
return (DB_NORMAL);
}
/* DB_UnLock
**
** Purpose:
** Unlock the db file and return the proper return code.
**
** Parameter Dictionary:
** CONDITION ret_val
** The return value to return after unlocking...
**
** Return Values:
** DB_?: The return value will always be what was passed.
**
** Algorithm:
** Just a utility routine to keep the routines less messy.
*/
CONDITION
DB_UnLock(CONDITION ret_val)
{
(void) HF_UnLock();
return (ret_val);
}
/* DB_RegexMatch
**
** Purpose:
** Perform a DICOM regular expression match with the specified string, stm.
**
** Parameter Dictionary:
** char *regex:
** The DICOM regular expression to try and match.
** char *stm:
** The input string to match.
**
** Return Values:
** DB_MATCH: The input string matched the regular expression.
** DB_NOMATCH: The input string did not match the regular expression.
**
** Algorithm:
** A simple function to perform a DICOM regular expression match with the
** specified string, stm. The sematics of the DICOM patterns must be altered
** slightly to work correctly with regex under unix...more information may
** be found below.
**
*/
char *re_comp(char *);
int re_exec(char *);
CONDITION
DB_RegexMatch(char *regex, char *stm)
{
CONDITION cond;
cond = UTL_RegexMatch(regex, stm);
return (cond == UTL_MATCH) ? DB_MATCH : DB_NOMATCH;
/* This code is better replaced by the UTL regular expression matching.
* It constrains all regular expression matching to one place.
*/
#ifdef OLDREGEX
int
ret;
char
*new_rstring;
/*lint -e527 Turn off message for unreachable statement (return/break) */
new_rstring = DB_ConvertRegex(regex);
if (re_comp(new_rstring) != (char *) 0) {
free(new_rstring);
return (DB_NOMATCH);
} else {
ret = re_exec(stm);
switch (ret) {
case 0:
case -1:
free(new_rstring);
return (DB_NOMATCH);
break;
case 1:
free(new_rstring);
return (DB_MATCH);
break;
}
}
#endif
return DB_NOMATCH; /* We won't get here, but this helps lint */
/*lint +e527 Turn on checking for unreachable statements again */
}
/* DB_ConvertRegex
**
** Purpose:
** This function converts a DICOM "regular expression" to the proper
** regex semantics under unix.
**
** Parameter Dictionary:
** char *regex:
** The DICOM regular expression to convert.
**
** Return Values:
** char *: The converted regular expression which expresses DICOM pattern
** matching in regex semantics.
**
** Algorithm:
** Simple function to convert a DICOM "regular expression" to the proper
** regex semantics under unix. DICOM has only 2 meta characters, "*" for 0
** or more occurrences, and "?" for a single character. The "*" must be
** converted to ".*" for regex while the "?" must be converted to ".".
** Other special characters to regex like "[", "]", and "." must also
** be escaped with the "\". The DICOM escape character is assumed to be "\".
*/
#ifdef OLDREGEX
#define OFF 0
#define ON 1
char *
DB_ConvertRegex(char *regex)
{
char
*new_regex = (char *) NULL;
int
malloced_size = 0;
int
i,
j,
escape_on;
if (new_regex == (char *) NULL) {
malloced_size = REGEX_SIZE;
if ((new_regex = (char *) malloc(malloced_size)) == (char *) NULL) {
return ((char *) NULL);
}
}
i = j = 0;
escape_on = OFF;
new_regex[j++] = '^';
while (regex[i] != '\000') {
switch (regex[i]) {
case '*': /* Transform the "*" to ".*" or "\*" if
* escaped */
switch (escape_on) {
case OFF:
new_regex[j++] = '.';
break;
case ON:
new_regex[j++] = '\\';
escape_on = OFF;
break;
}
new_regex[j++] = '*';
i++;
break;
case '?': /* Transform the "?" to "." or "?" if escaped */
switch (escape_on) {
case OFF:
new_regex[j++] = '.';
break;
case ON:
new_regex[j++] = '?';
escape_on = OFF;
break;
}
i++;
break;
case '\\': /* Note that we have seen the escape
* character */
switch (escape_on) {
case OFF:
escape_on = ON;
break;
case ON:
escape_on = OFF;
new_regex[j++] = '\\';
new_regex[j++] = '\\';
break;
}
i++;
break;
case '.':
case '[': /* These are special to regex and need to be
* escaped */
case ']':
new_regex[j++] = '\\';
new_regex[j++] = regex[i++];
escape_on = OFF;
break;
default: /* Leave the "\" in at this juncture */
switch (escape_on) {
case ON:
new_regex[j++] = '\\';
escape_on = OFF;
break;
case OFF:
break;
}
new_regex[j++] = regex[i++];
break;
}
if (j >= (malloced_size - 2)) {
malloced_size += REGEX_SIZE;
if ((new_regex = (char *) realloc(new_regex, malloced_size)) ==
(char *) NULL) {
return ((char *) NULL);
}
}
}
new_regex[j++] = '$';
new_regex[j] = '\000';
return (new_regex);
}
#endif
/* DB_Query
**
** Purpose:
** This function performs the a generalized query of the specified database.
**
** Parameter Dictionary:
** short dbid:
** The database to query.
** Query *qstruct:
** The structure which specifies which fields to match against.
** Query *retinfo:
** The structure that receives the retreived data.
**
** Return Values:
** DB_NORMAL: The retreive operation succeeded.
** DB_NOTOPENED: The database has not been opened.
** DB_BADQUERY: The query was mal-formed.
** DB_LOCKERROR: Requested locks could not be granted.
** DB_READERROR: Records could not be read successfully.
** DB_NOMORE: No records matched the specifications in qstruct.
**
** Algorithm:
** This function is always used in conjuction with DB_NextQuery. It performs
** a search at the proper level using the DICOM pattern matching specifications.
** This function sets up the inital context, and returns the first record found
** in the database with a call to DB_NextQuery. There is a special flag used to
** alert DB_NextQuery that this is the initial invocation. Subsequent records
** based on this query are returned with DB_NextQuery.
*/
CONDITION
DB_Query(short dbid, Query *qstruct, Query *retinfo)
{
HunkBufAdd
ploc;
if (DB_Findid(dbid) != DB_NORMAL) {
(void) COND_PushCondition(DB_NOTOPENED, "DB_Query: DB_Findid failed");
return (DB_NOTOPENED);
}
if (GS_root.num_patients <= 0) {
(void) COND_PushCondition(DB_LOCKERROR, "DB_Query: HF_SharedLock failed");
return (DB_NOPATIENTS);
}
if (qstruct->QueryState & DB_K_CLASSPATSTUDY) {
if (!(qstruct->QueryState & DB_K_LEVELPAT) &&
!(qstruct->QueryState & DB_K_LEVELSTUDY)) {
(void) COND_PushCondition(DB_BADQUERY, "DB_Query: Bad state flags in query struct");
return (DB_BADQUERY);
}
}
ploc.hunk_number = 0;
ploc.node_number = 0;
if (qstruct->QueryState & DB_K_LEVELPAT) {
DB_UpdateQueryContext(dbid, &ploc, DB_QUERYFIRST, DB_PATIENTCONTEXT);
ploc.hunk_number = HUNK_PTR_NULL;
ploc.node_number = HUNK_PTR_NULL;
DB_UpdateQueryContext(dbid, &ploc, -1, DB_STUDYCONTEXT);
DB_UpdateQueryContext(dbid, &ploc, -1, DB_SERIESCONTEXT);
DB_UpdateQueryContext(dbid, &ploc, -1, DB_IMAGECONTEXT);
} else if (qstruct->QueryState & DB_K_LEVELSTUDY) {
DB_UpdateQueryContext(dbid, &ploc, DB_QUERYFIRST, DB_PATIENTCONTEXT);
DB_UpdateQueryContext(dbid, &ploc, DB_QUERYFIRST, DB_STUDYCONTEXT);
ploc.hunk_number = HUNK_PTR_NULL;
ploc.node_number = HUNK_PTR_NULL;
DB_UpdateQueryContext(dbid, &ploc, -1, DB_SERIESCONTEXT);
DB_UpdateQueryContext(dbid, &ploc, -1, DB_IMAGECONTEXT);
} else if (qstruct->QueryState & DB_K_LEVELSERIES) {
DB_UpdateQueryContext(dbid, &ploc, DB_QUERYFIRST, DB_PATIENTCONTEXT);
DB_UpdateQueryContext(dbid, &ploc, DB_QUERYFIRST, DB_STUDYCONTEXT);
DB_UpdateQueryContext(dbid, &ploc, DB_QUERYFIRST, DB_SERIESCONTEXT);
ploc.hunk_number = HUNK_PTR_NULL;
ploc.node_number = HUNK_PTR_NULL;
DB_UpdateQueryContext(dbid, &ploc, -1, DB_IMAGECONTEXT);
} else if (qstruct->QueryState & DB_K_LEVELIMAGE) {
DB_UpdateQueryContext(dbid, &ploc, DB_QUERYFIRST, DB_PATIENTCONTEXT);
DB_UpdateQueryContext(dbid, &ploc, DB_QUERYFIRST, DB_STUDYCONTEXT);
DB_UpdateQueryContext(dbid, &ploc, DB_QUERYFIRST, DB_SERIESCONTEXT);
DB_UpdateQueryContext(dbid, &ploc, DB_QUERYFIRST, DB_IMAGECONTEXT);
}
DB_SetChangeFlag(dbid);
return (DB_NextQuery(dbid, qstruct, retinfo));
}
/* DB_SetChangeFlag
**
*/
void
DB_SetChangeFlag(short dbid)
{
DBidstruct
*temp;
if (GS_context == (DBidstruct *) NULL)
return;
temp = GS_context;
while (temp != (DBidstruct *) NULL) {
if (temp->dbid == dbid) {
temp->dbchanged = HF_ReadUpdateFlag();
break;
}
temp = temp->next;
}
return;
}
/* DB_CompareChangeFlag
**
*/
CONDITION
DB_CompareChangeFlag(short dbid)
{
DBidstruct
*temp;
if (GS_context == (DBidstruct *) NULL)
return DB_NOMATCH;
temp = GS_context;
while (temp != (DBidstruct *) NULL) {
if (temp->dbid == dbid) {
if (temp->dbchanged == HF_ReadUpdateFlag())
return (DB_MATCH);
break;
}
temp = temp->next;
}
return (DB_NOMATCH);
}
/* DB_ResetQueryContext
**
*/
void
DB_ResetQueryContext(short dbid)
{
DBidstruct
*temp;
if (GS_context == (DBidstruct *) NULL)
return;
temp = GS_context;
while (temp != (DBidstruct *) NULL) {
if (temp->dbid == dbid) {
HunkBufAdd tadd;
tadd.hunk_number = HUNK_PTR_NULL;
tadd.node_number = HUNK_PTR_NULL;
temp->querycontxt.last_patient = tadd;
temp->querycontxt.last_patientindex = -1;
temp->querycontxt.last_study = tadd;
temp->querycontxt.last_studyindex = -1;
temp->querycontxt.last_series = tadd;
temp->querycontxt.last_seriesindex = -1;
temp->querycontxt.last_image = tadd;
temp->querycontxt.last_imageindex = -1;
break;
}
temp = temp->next;
}
return;
}
/* DB_NextQuery
**
** Purpose:
** This function performs the a generalized query of the specified database.
**
** Parameter Dictionary:
** short dbid:
** The database to query.
** Query *qstruct:
** The structure which specifies which fields to match against.
** Query *retinfo:
** The structure that receives the retreived data.
**
** Return Values:
** DB_NORMAL: The retreive operation succeeded.
** DB_NOTOPENED: The database has not been opened.
** DB_BADQUERY: The query was mal-formed.
** DB_LOCKERROR: Requested locks could not be granted.
** DB_READERROR: Records could not be read successfully.
** DB_NOMORE: No records matched the specifications in qstruct.
** DB_CHANGED: The database has changed since the initial DB_Query call.
**
** Algorithm:
** This function is always used in conjuction with DB_Query. It performs
** a search at the proper level using the DICOM pattern matching specifications.
** This function is responsible for getting the next record in the sequence
** of records that match the selection criteria. DB_Query must always be called
** first to intialize the context, and then DB_NextQuery is called to retreive
** additional records until it returns something other than DB_NORMAL.
*/
CONDITION
DB_NextQuery(short dbid, Query *qstruct, Query *retinfo)
{
int
pindex,
sindex,
serindex,
iindex,
i,
j,
k,
l;
PatientNode
pnode;
StudyNode
*psnode,
snode;
SeriesNode
*psernode,
sernode;
ImageNode
inode;
HunkBufAdd
ploc,
sloc,
serloc,
iloc;
retinfo->QueryState = qstruct->QueryState;
if (DB_Findid(dbid) != DB_NORMAL) {
(void) COND_PushCondition(DB_NOTOPENED, "DB_NextQuery: DB_Findid failed");
DB_ResetQueryContext(dbid);
return (DB_NOTOPENED);
}
if (GS_root.num_patients <= 0) {
DB_ResetQueryContext(dbid);
(void) COND_PushCondition(DB_NOPATIENTS, "DB_NextQuery: No patients");
return (DB_NOPATIENTS);
}
if (qstruct->QueryState & DB_K_CLASSPATSTUDY) {
if (!(qstruct->QueryState & DB_K_LEVELPAT) &&
!(qstruct->QueryState & DB_K_LEVELSTUDY)) {
DB_ResetQueryContext(dbid);
(void) COND_PushCondition(DB_BADQUERY, "DB_NextQuery: Bad state flags in query struct");
return (DB_BADQUERY);
}
}
if (HF_SharedLock() != HF_NORMAL) {
(void) COND_PushCondition(DB_LOCKERROR, "DB_NextQuery: HF_SharedLock failed");
return (DB_LOCKERROR);
}
if (DB_CompareChangeFlag(dbid) != DB_MATCH) {
(void) COND_PushCondition(DB_LOCKERROR, "DB_NextQuery: Database has changed");
return (DB_UnLock(DB_CHANGED));
}
/*
* Work on the patient context first.
*/
DB_GetQueryContext(dbid, &ploc, &pindex, DB_PATIENTCONTEXT);
if (ploc.hunk_number == HUNK_PTR_NULL ||
ploc.node_number == HUNK_PTR_NULL ||
pindex == -1) {
DB_ResetQueryContext(dbid);
return (DB_UnLock(DB_NOMORE));
}
if (qstruct->QueryState & DB_K_LEVELPAT) {
if (pindex != DB_QUERYFIRST)
pindex++;
else
pindex = 0;
for (i = pindex; i < GS_root.num_patients; i++) {
ploc = GS_root.patient_loc[i];
if (HF_ReadRecord(&ploc, sizeof(pnode), (void *) &pnode) != HF_NORMAL) {
(void) COND_PushCondition(DB_READERROR, "DB_NextQuery: HF_ReadRecord failed");
return (DB_UnLock(DB_READERROR));
}
if (DB_ComparePat(&(pnode.pat), qstruct) == DB_MATCH) {
retinfo->Patient = pnode.pat;
retinfo->Patient.Query_Flag = qstruct->Patient.Query_Flag;
retinfo->Study.Query_Flag = qstruct->Study.Query_Flag;
retinfo->Series.Query_Flag = qstruct->Series.Query_Flag;
retinfo->Image.Query_Flag = qstruct->Image.Query_Flag;
if (qstruct->Image.Query_Flag & DB_K_QIMAGEMULTUID) {
retinfo->Image.Query_Flag |= DB_K_QIMAGEUID;
retinfo->Image.Query_Flag &= ~DB_K_QIMAGEMULTUID;
}
DB_UpdateQueryContext(dbid, &ploc, i, DB_PATIENTCONTEXT);
return (DB_UnLock(DB_NORMAL));
}
}
DB_ResetQueryContext(dbid);
return (DB_UnLock(DB_NOMORE));
}
if (HF_ReadRecord(&ploc, sizeof(pnode), (void *) &pnode) != HF_NORMAL) {
(void) COND_PushCondition(DB_READERROR, "DB_NextQuery: HF_ReadRecord failed");
return (DB_UnLock(DB_READERROR));
}
retinfo->Patient = pnode.pat;
if (pindex == DB_QUERYFIRST)
pindex = 0;
/*
* Now work on the study context.
*/
DB_GetQueryContext(dbid, &sloc, &sindex, DB_STUDYCONTEXT);
if (sloc.hunk_number == HUNK_PTR_NULL ||
sloc.node_number == HUNK_PTR_NULL ||
sindex == -1) {
DB_ResetQueryContext(dbid);
return (DB_UnLock(DB_NOMORE));
}
if (qstruct->QueryState & DB_K_LEVELSTUDY) {
if (sindex != DB_QUERYFIRST)
sindex++;
else
sindex = 0;
for (i = pindex; i < GS_root.num_patients; i++) {
ploc = GS_root.patient_loc[i];
if (HF_ReadRecord(&ploc, sizeof(pnode), (void *) &pnode) != HF_NORMAL) {
(void) COND_PushCondition(DB_READERROR, "DB_NextQuery: HF_ReadRecord failed");
return (DB_UnLock(DB_READERROR));
}
if (DB_ComparePat(&(pnode.pat), qstruct) == DB_MATCH) {
for (j = sindex; j < pnode.num_studies; j++) {
sloc = pnode.study_loc[j];
if (HF_ReadRecord(&sloc, sizeof(snode), (void *) &snode) != HF_NORMAL) {
(void) COND_PushCondition(DB_READERROR, "DB_NextQuery: HF_ReadRecord failed");
return (DB_UnLock(DB_READERROR));
}
if (DB_CompareStudy(&(snode.study), qstruct) == DB_MATCH) {
retinfo->Study = snode.study;
retinfo->Patient = pnode.pat;
retinfo->Patient.Query_Flag = qstruct->Patient.Query_Flag;
retinfo->Study.Query_Flag = qstruct->Study.Query_Flag;
retinfo->Series.Query_Flag = qstruct->Series.Query_Flag;
retinfo->Image.Query_Flag = qstruct->Image.Query_Flag;
if (qstruct->Image.Query_Flag & DB_K_QIMAGEMULTUID) {
retinfo->Image.Query_Flag |= DB_K_QIMAGEUID;
retinfo->Image.Query_Flag &= ~DB_K_QIMAGEMULTUID;
}
DB_UpdateQueryContext(dbid, &sloc, j, DB_STUDYCONTEXT);
DB_UpdateQueryContext(dbid, &ploc, i, DB_PATIENTCONTEXT);
return (DB_UnLock(DB_NORMAL));
}
}
}
sindex = 0;
}
DB_ResetQueryContext(dbid);
return (DB_UnLock(DB_NOMORE));
}
if (DB_ReadStudyNode(&sloc, &psnode) != DB_NORMAL) {
(void) COND_PushCondition(DB_READERROR, "DB_NextQuery: HF_ReadRecord failed");
return (DB_UnLock(DB_READERROR));
}
retinfo->Study = psnode->study;
if (sindex == DB_QUERYFIRST)
sindex = 0;
/*
* Now work on the series context.
*/
DB_GetQueryContext(dbid, &serloc, &serindex, DB_SERIESCONTEXT);
if (serloc.hunk_number == HUNK_PTR_NULL ||
serloc.node_number == HUNK_PTR_NULL ||
serindex == -1) {
DB_ResetQueryContext(dbid);
free(psnode);
return (DB_UnLock(DB_NOMORE));
}
if (qstruct->QueryState & DB_K_LEVELSERIES) {
if (serindex != DB_QUERYFIRST)
serindex++;
else
serindex = 0;
for (i = pindex; i < GS_root.num_patients; i++) {
ploc = GS_root.patient_loc[i];
if (HF_ReadRecord(&ploc, sizeof(pnode), (void *) &pnode) != HF_NORMAL) {
free(psnode);
(void) COND_PushCondition(DB_READERROR, "DB_NextQuery: HF_ReadRecord failed");
return (DB_UnLock(DB_READERROR));
}
if (DB_ComparePat(&(pnode.pat), qstruct) == DB_MATCH) {
for (j = sindex; j < pnode.num_studies; j++) {
sloc = pnode.study_loc[j];
free(psnode);
if (DB_ReadStudyNode(&sloc, &psnode) != DB_NORMAL) {
(void) COND_PushCondition(DB_READERROR, "DB_NextQuery: HF_ReadRecord failed");
return (DB_UnLock(DB_READERROR));
}
if (DB_CompareStudy(&(psnode->study), qstruct) == DB_MATCH) {
for (k = serindex; k < psnode->num_series; k++) {
serloc = psnode->series_loc[k];
if (HF_ReadRecord(&serloc, sizeof(sernode), (void *) &sernode) != HF_NORMAL) {
free(psnode);
(void) COND_PushCondition(DB_READERROR, "DB_NextQuery: HF_ReadRecord failed");
return (DB_UnLock(DB_READERROR));
}
if (DB_CompareSeries(&(sernode.series), qstruct) == DB_MATCH) {
retinfo->Series = sernode.series;
retinfo->Study = psnode->study;
retinfo->Patient = pnode.pat;
retinfo->Patient.Query_Flag = qstruct->Patient.Query_Flag;
retinfo->Study.Query_Flag = qstruct->Study.Query_Flag;
retinfo->Series.Query_Flag = qstruct->Series.Query_Flag;
retinfo->Image.Query_Flag = qstruct->Image.Query_Flag;
if (qstruct->Image.Query_Flag & DB_K_QIMAGEMULTUID) {
retinfo->Image.Query_Flag |= DB_K_QIMAGEUID;
retinfo->Image.Query_Flag &= ~DB_K_QIMAGEMULTUID;
}
DB_UpdateQueryContext(dbid, &serloc, k, DB_SERIESCONTEXT);
DB_UpdateQueryContext(dbid, &sloc, j, DB_STUDYCONTEXT);
DB_UpdateQueryContext(dbid, &ploc, i, DB_PATIENTCONTEXT);
free(psnode);
return (DB_UnLock(DB_NORMAL));
}
}
}
serindex = 0;
}
}
sindex = 0;
}
free(psnode);
DB_ResetQueryContext(dbid);
return (DB_UnLock(DB_NOMORE));
}
if (DB_ReadSeriesNode(&serloc, &psernode) != DB_NORMAL) {
(void) COND_PushCondition(DB_READERROR, "DB_NextQuery: HF_ReadRecord failed");
return (DB_UnLock(DB_READERROR));
}
retinfo->Series = psernode->series;
if (serindex == DB_QUERYFIRST)
serindex = 0;
/*
* Now work on the image context.
*/
DB_GetQueryContext(dbid, &iloc, &iindex, DB_IMAGECONTEXT);
if (iloc.hunk_number == HUNK_PTR_NULL ||
iloc.node_number == HUNK_PTR_NULL ||
iindex == -1) {
DB_ResetQueryContext(dbid);
free(psnode);
free(psernode);
return (DB_UnLock(DB_NOMORE));
}
if (qstruct->QueryState & DB_K_LEVELIMAGE) {
if (iindex != DB_QUERYFIRST)
iindex++;
else
iindex = 0;
for (i = pindex; i < GS_root.num_patients; i++) {
ploc = GS_root.patient_loc[i];
if (HF_ReadRecord(&ploc, sizeof(pnode), (void *) &pnode) != HF_NORMAL) {
free(psnode);
free(psernode);
(void) COND_PushCondition(DB_READERROR, "DB_NextQuery: HF_ReadRecord failed");
return (DB_UnLock(DB_READERROR));
}
if (DB_ComparePat(&(pnode.pat), qstruct) == DB_MATCH) {
for (j = sindex; j < pnode.num_studies; j++) {
sloc = pnode.study_loc[j];
free(psnode);
if (DB_ReadStudyNode(&sloc, &psnode) != DB_NORMAL) {
free(psernode);
(void) COND_PushCondition(DB_READERROR, "DB_NextQuery: HF_ReadRecord failed");
return (DB_UnLock(DB_READERROR));
}
if (DB_CompareStudy(&(psnode->study), qstruct) == DB_MATCH) {
for (k = serindex; k < psnode->num_series; k++) {
serloc = psnode->series_loc[k];
free(psernode);
if (DB_ReadSeriesNode(&serloc, &psernode) != DB_NORMAL) {
free(psnode);
(void) COND_PushCondition(DB_READERROR, "DB_NextQuery: HF_ReadRecord failed");
return (DB_UnLock(DB_READERROR));
}
if (DB_CompareSeries(&(psernode->series), qstruct) == DB_MATCH) {
for (l = iindex; l < psernode->num_images; l++) {
iloc = psernode->image_loc[l];
if (HF_ReadRecord(&iloc, sizeof(inode), (void *) &inode) != HF_NORMAL) {
free(psnode);
free(psernode);
(void) COND_PushCondition(DB_READERROR, "DB_NextQuery: HF_ReadRecord failed");
return (DB_UnLock(DB_READERROR));
}
if (DB_CompareImage(&(inode.image), qstruct) == DB_MATCH) {
retinfo->Image = inode.image;
retinfo->Series = psernode->series;
retinfo->Study = psnode->study;
retinfo->Patient = pnode.pat;
retinfo->Patient.Query_Flag = qstruct->Patient.Query_Flag;
retinfo->Study.Query_Flag = qstruct->Study.Query_Flag;
retinfo->Series.Query_Flag = qstruct->Series.Query_Flag;
retinfo->Image.Query_Flag = qstruct->Image.Query_Flag;
if (qstruct->Image.Query_Flag & DB_K_QIMAGEMULTUID) {
retinfo->Image.Query_Flag |= DB_K_QIMAGEUID;
retinfo->Image.Query_Flag &= ~DB_K_QIMAGEMULTUID;
}
DB_UpdateQueryContext(dbid, &iloc, l, DB_IMAGECONTEXT);
DB_UpdateQueryContext(dbid, &serloc, k, DB_SERIESCONTEXT);
DB_UpdateQueryContext(dbid, &sloc, j, DB_STUDYCONTEXT);
DB_UpdateQueryContext(dbid, &ploc, i, DB_PATIENTCONTEXT);
free(psnode);
free(psernode);
return (DB_UnLock(DB_NORMAL));
}
}
}
iindex = 0;
}
}
serindex = 0;
}
}
sindex = 0;
}
free(psnode);
free(psernode);
DB_ResetQueryContext(dbid);
return (DB_UnLock(DB_NOMORE));
}
free(psnode);
free(psernode);
return (DB_UnLock(DB_NOMORE));
}
/* DB_ComparePat
**
*/
CONDITION
DB_ComparePat(PatientLevel *pat, Query *qstruct)
{
if (qstruct->Patient.Query_Flag & DB_K_QBIRTHDATE) {
if (strcmp(qstruct->Patient.BirthDate, "") != 0) {
if (DB_DateMatch(qstruct->Patient.BirthDate, pat->BirthDate) == DB_NOMATCH)
return (DB_NOMATCH);
}
}
if (qstruct->Patient.Query_Flag & DB_K_QNAME) {
if (strcmp(qstruct->Patient.Name, "") != 0) {
if (DB_RegexMatch(qstruct->Patient.Name, pat->Name) == DB_NOMATCH)
return (DB_NOMATCH);
}
}
if (qstruct->Patient.Query_Flag & DB_K_QID) {
if (strcmp(qstruct->Patient.PatID, "") != 0) {
if (DB_RegexMatch(qstruct->Patient.PatID, pat->PatID) == DB_NOMATCH)
return (DB_NOMATCH);
}
}
return (DB_MATCH);
}
/* DB_CompareStudy
**
*/
CONDITION
DB_CompareStudy(StudyLevel *study, Query *qstruct)
{
if (qstruct->Study.Query_Flag & DB_K_QSTUDYDATE) {
if (strcmp(qstruct->Study.StudyDate, "") != 0) {
if (DB_DateMatch(qstruct->Study.StudyDate, study->StudyDate) == DB_NOMATCH)
return (DB_NOMATCH);
}
}
if (qstruct->Study.Query_Flag & DB_K_QSTUDYTIME) {
if (strcmp(qstruct->Study.StudyTime, "") != 0) {
if (DB_TimeMatch(qstruct->Study.StudyTime, study->StudyTime) == DB_NOMATCH)
return (DB_NOMATCH);
}
}
if (qstruct->Study.Query_Flag & DB_K_QSTUDYID) {
if (strcmp(qstruct->Study.StudyID, "") != 0) {
if (DB_RegexMatch(qstruct->Study.StudyID, study->StudyID) == DB_NOMATCH)
return (DB_NOMATCH);
}
}
if (qstruct->Study.Query_Flag & DB_K_QACCESSIONNUMBER) {
if (strcmp(qstruct->Study.AccessionNumber, "") != 0) {
if (DB_RegexMatch(qstruct->Study.AccessionNumber, study->AccessionNumber) ==
DB_NOMATCH)
return (DB_NOMATCH);
}
}
if (qstruct->Study.Query_Flag & DB_K_QSTUDYUID) {
if (strcmp(qstruct->Study.StudyUID, "") != 0) {
if (DB_RegexMatch(qstruct->Study.StudyUID, study->StudyUID) == DB_NOMATCH)
return (DB_NOMATCH);
}
}
if (qstruct->Study.Query_Flag & DB_K_QREFERRINGPHYSNAME) {
if (strcmp(qstruct->Study.ReferringPhysName, "") != 0) {
if (DB_RegexMatch(qstruct->Study.ReferringPhysName,
study->ReferringPhysName) == DB_NOMATCH)
return (DB_NOMATCH);
}
}
if (qstruct->Study.Query_Flag & DB_K_QINTERPRETPHYSNAME) {
if (strcmp(qstruct->Study.InterpretingPhysName, "") != 0) {
if (DB_RegexMatch(qstruct->Study.InterpretingPhysName,
study->InterpretingPhysName) == DB_NOMATCH)
return (DB_NOMATCH);
}
}
if (qstruct->Study.Query_Flag & DB_K_QPROCEDUREDESCRIPTION) {
if (strcmp(qstruct->Study.ProcedureDescription, "") != 0) {
if (DB_RegexMatch(qstruct->Study.ProcedureDescription,
study->ProcedureDescription) == DB_NOMATCH)
return (DB_NOMATCH);
}
}
if (qstruct->Study.Query_Flag & DB_K_QADMITTINGDIAGNOSEDDESCRIPTION) {
if (strcmp(qstruct->Study.AdmittingDiagnosedDescription, "") != 0) {
if (DB_RegexMatch(qstruct->Study.AdmittingDiagnosedDescription,
study->AdmittingDiagnosedDescription) == DB_NOMATCH)
return (DB_NOMATCH);
}
}
return (DB_MATCH);
}
/* DB_CompareSeries
**
*/
CONDITION
DB_CompareSeries(SeriesLevel *series, Query *qstruct)
{
if (qstruct->Series.Query_Flag & DB_K_QMODALITY) {
if (strcmp(qstruct->Series.Modality, "") != 0) {
if (DB_RegexMatch(qstruct->Series.Modality, series->Modality) == DB_NOMATCH)
return (DB_NOMATCH);
}
}
if (qstruct->Series.Query_Flag & DB_K_QSERIESNUMBER) {
if (strcmp(qstruct->Series.SeriesNumber, "") != 0) {
if (DB_RegexMatch(qstruct->Series.SeriesNumber, series->SeriesNumber) ==
DB_NOMATCH)
return (DB_NOMATCH);
}
}
if (qstruct->Series.Query_Flag & DB_K_QSERIESUID) {
if (strcmp(qstruct->Series.SeriesUID, "") != 0) {
if (DB_RegexMatch(qstruct->Series.SeriesUID, series->SeriesUID) == DB_NOMATCH)
return (DB_NOMATCH);
}
}
return (DB_MATCH);
}
/* DB_CompareImage
**
*/
CONDITION
DB_CompareImage(ImageLevel *image, Query *qstruct)
{
int
found_match,
i;
if (qstruct->Image.Query_Flag & DB_K_QIMAGENUMBER) {
if (strcmp(qstruct->Image.ImageNumber, "") != 0) {
if (DB_RegexMatch(qstruct->Image.ImageNumber, image->ImageNumber) ==
DB_NOMATCH)
return (DB_NOMATCH);
}
}
if (qstruct->Image.Query_Flag & DB_K_QIMAGEUID) {
if (strcmp(qstruct->Image.ImageUID, "") != 0) {
if (DB_RegexMatch(qstruct->Image.ImageUID, image->ImageUID) == DB_NOMATCH)
return (DB_NOMATCH);
}
}
if (qstruct->Image.Query_Flag & DB_K_QCLASSUID) {
if (strcmp(qstruct->Image.ClassUID, "") != 0) {
if (DB_RegexMatch(qstruct->Image.ClassUID, image->ClassUID) == DB_NOMATCH)
return (DB_NOMATCH);
}
}
found_match = 0;
if (qstruct->Image.Query_Flag & DB_K_QIMAGEMULTUID) {
for (i = 0; i < qstruct->Image.ImageMultUIDCount; i++) {
if (DB_RegexMatch(qstruct->Image.ImageMultUID[i], image->ImageUID) == DB_MATCH) {
found_match = 1;
break;
}
}
if (!found_match)
return (DB_NOMATCH);
}
return (DB_MATCH);
}
/* DB_ConvertDatetoLong
** Convert a dicom date to a long for comparision ease.
*/
long
DB_ConvertDatetoLong(char *date)
{
char
year[5],
month[3],
day[3];
strncpy(year, date, 4);
year[4] = '\000';
strncpy(month, date + 4, 2);
month[2] = '\000';
strncpy(day, date + 6, 2);
day[2] = '\000';
return ((atol(year) * 10000) + (atol(month) * 100) + atol(day));
}
/* DB_ConvertTimetoFloat
** Convert a dicom time to a floating point number for comparision ease.
*/
double
DB_ConvertTimetoFloat(char *tm)
{
int
i;
char
hour[3],
minute[3],
second[3],
fracsec[5],
*p;
double
divisor,
hh,
mm,
ss,
fs;
hh = mm = ss = fs = 0.0;
hour[0] = minute[0] = second[0] = fracsec[0] = '\000';
p = tm;
/*
* Just a brute force way to tear down a dicom time...not very pretty,
* but it works... We are not guaranteed to have every field present as
* we are in the date...
*/
hour[0] = *p++;
hour[1] = *p++;
hour[2] = '\000';
if (isdigit(*p)) {
minute[0] = *p++;
minute[1] = *p++;
minute[2] = '\000';
if (isdigit(*p)) {
second[0] = *p++;
second[1] = *p++;
second[2] = '\000';
if (*p == '.') {
p++;
fracsec[0] = *p++;
if ((*p != '\000') && (isdigit(*p))) {
fracsec[1] = *p++;
if ((*p != '\000') && (isdigit(*p))) {
fracsec[2] = *p++;
if ((*p != '\000') && (isdigit(*p)))
fracsec[3] = *p++;
else
fracsec[3] = '\000';
} else
fracsec[2] = '\000';
} else
fracsec[1] = '\000';
}
}
}
hh = atof(hour);
mm = atof(minute);
ss = atof(second);
divisor = 10;
for (i = 0; i < strlen(fracsec) - 1; i++)
divisor *= 10;
fs = atof(fracsec) / divisor;
return ((hh * 3600) + (mm * 60) + ss + fs);
}
/* DB_SqueezeBlanks
**
*/
void
DB_SqueezeBlanks(char *s)
{
char
*t1,
*t2;
t1 = t2 = s;
while (*t2 != '\000') {
if (*t2 != ' ') {
*t1 = *t2;
t1++;
}
t2++;
}
*t1 = '\000';
return;
}
/* DB_DateMatch
** Match a date range as specified in the dicom standard
*/
CONDITION
DB_DateMatch(char *datestring, char *stm)
{
int
match;
char
*ndate;
long
start_date,
end_date,
date_in_question;
if ((ndate = (char *) malloc(strlen(datestring) + 1)) == (char *) NULL)
return (DB_NOMATCH);
strcpy(ndate, datestring);
DB_SqueezeBlanks(ndate);
DB_SqueezeBlanks(stm);
match = 0;
if (strchr(ndate, (int) '-') == (char *) NULL) {
if (strcmp(ndate, stm) == 0)
match = 1;
} else {
date_in_question = DB_ConvertDatetoLong(stm);
if (ndate[0] == '-') {
end_date = DB_ConvertDatetoLong(ndate + 1);
if (date_in_question <= end_date)
match = 1;
} else if (ndate[strlen(ndate) - 1] == '-') {
start_date = DB_ConvertDatetoLong(ndate);
if (date_in_question >= start_date)
match = 1;
} else {
start_date = DB_ConvertDatetoLong(ndate);
end_date = DB_ConvertDatetoLong(strchr(ndate, (int) '-') + 1);
if ((date_in_question >= start_date) &&
(date_in_question <= end_date))
match = 1;
}
}
free(ndate);
if (match)
return (DB_MATCH);
else
return (DB_NOMATCH);
}
/* DB_TimeMatch
** Match a time range as specified in the dicom standard
*/
CONDITION
DB_TimeMatch(char *timestring, char *stm)
{
int
match;
char
*ntime;
double
start_time,
end_time,
time_in_question;
if ((ntime = (char *) malloc(strlen(timestring) + 2)) == (char *) NULL)
return (DB_NOMATCH);
strcpy(ntime, timestring);
DB_SqueezeBlanks(ntime);
DB_SqueezeBlanks(stm);
match = 0;
if (strchr(ntime, (int) '-') == (char *) NULL) {
if (strcmp(ntime, stm) == 0)
match = 1;
} else {
time_in_question = DB_ConvertTimetoFloat(stm);
if (ntime[0] == '-') {
end_time = DB_ConvertTimetoFloat(ntime + 1);
if (time_in_question <= end_time)
match = 1;;
} else if (ntime[strlen(ntime) - 1] == '-') {
start_time = DB_ConvertTimetoFloat(ntime);
if (time_in_question >= start_time)
match = 1;
} else {
start_time = DB_ConvertTimetoFloat(ntime);
end_time = DB_ConvertTimetoFloat(strchr(ntime, (int) '-') + 1);
if ((time_in_question >= start_time) &&
(time_in_question <= end_time))
match = 1;;
}
}
free(ntime);
if (match)
return (DB_MATCH);
else
return (DB_NOMATCH);
}
|