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
|
/*
* Copyright (c) 1993-1995 Vectaport Inc.
* Copyright (c) 1989 Triple Vision, Inc.
*
* Permission to use, copy, modify, distribute, and sell this software and its
* documentation for any purpose is hereby granted without fee, provided
* that the above copyright notice appear in all copies and that both that
* copyright notice and this permission notice appear in supporting
* documentation, and that the names of the copyright holders not be used in
* advertising or publicity pertaining to distribution of the software without
* specific, written prior permission. The copyright holders make no
* representations about the suitability of this software for any purpose.
* It is provided "as is" without express or implied warranty.
*
* THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
* INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
* IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL,
* INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
* FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
* NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
* WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/*
XDLL.C Extended Doubly-Linked List Support.
Externals:
Summary: These functions support the structuring and accessing
of a doubly-linked list. They are a modified version
of Scott's dll functions made more general. His should
have been directly modified, but these routines were
already laying around from another effort.
History: Written by Robert C. Fitch, 4 Mar 1989
Improvements:
- In xdll_open(): if `beg` is NULL, allocate the area for the user.
(this is being held off to see if the dmm_*() dynamic allocation
routines are going to be used for this purpose).
- All: support for `huge` memory areas. These routines really
only support normal size memory areas. Actually only int size since
offsets are integers in xdllink structure (maybe should be longs).
*/
#include <stdio.h>
#include "comutil.ci"
/* comutil.ci includes stdlib.h and malloc.h */
/*=============*/
/* This contains all information for an open linked list */
/* assumes the cells for the linked list are linear in memory */
/* i.e., from address `beg` to `end` */
typedef struct _xdllist xdllist;
struct _xdllist {
int size; /* size of node structure size */
/* 0 means cell is empty */
xdllink *beg; /* head of user's alloc structure */
int nlinks; /* max number of links in list */
xdllink *head; /* head node of the list; can be anywhere */
xdllink *curr; /* current level entry in link list */
};
#ifdef C_terp
#define XDLL_LISTS 16
#else
#define XDLL_LISTS 32
#endif
static xdllist xdllist_array[XDLL_LISTS]; /* now a static array */
/* calloc/realloc removed */
/*=============*/
/* Static Functions: */
static int next_free_link(int flag);
static int clear_links(xdllink * curr,int nlinks,void (*userfunc)(void*));
/* some pointers for the different open lists */
static xdllist *xdllist_beg,*xdllist_curr;
static int nxdlls=0; /* number of lists in list_beg array */
static int xdllist_in_use_id = -1; /* current list in use; -1 is none in use */
/* this checks to make sure there is a list in use */
#define CHECKUSE { if (xdllist_in_use_id < 0) goto error_return; }
/* These macros efficiently increment structure pointers by byte offsets */
/* this one returns a pointer to an xdllink structure entry in beg */
#define ADDBEG(nbytes) ( (xdllink *) (((char *)xdllist_curr->beg) + nbytes))
/* this one returns a byte offset from beginning of structure beg */
#define SUBBEG(pntr) ((int) (((char *) pntr) - ((char *) xdllist_curr->beg)) )
#define HEADID -2
/* this is identifier of a head node */
/*=============*/
/*!
xdll_open Open a New doubly-linked-list structure.
Summary:
#include <ComUtil/comutil.h>
*/
int xdll_open (void * beg,int nlinks,int nsize)
/*!
Return Value: >=0 identifier for this link structure, -1 if params
out of range or insufficient memory.
Parameters:
Type Name IO Description
------------ ----------- -- ----------- */
#ifdef DOC
void * beg ;/* I Pointer to link area alloc'd by user */
int nlinks ;/* I Max number of links in the list */
int nsize ;/* I Size of a link entry in bytes */
#endif
#ifdef DOC
/*!
Description:
Open's a new link list in an area of memory allocated by the user starting at
address `beg`. The number of records (links) in the memory area is
`nlinks` and the number of bytes per record (link) is `nsize`.
This routine also sets the current linked-list in use to the identifier
returned by this routine.
/* Link Structure */
The links are done using the link structure array. It contains two elements,
the first being a node pointer in to the node array. The second element
is a link to the next entry in the link list.
See `xdllink` structure. If the *node entry in listink is NULL,
then this is an empty entry.
/* List Pointer Reference Structure */
Another data structure declares a set of pointers for each link list.
See `list` structure for details.
===============
Two link lists:
A <-> B <-> C
-and-
D <-> E
links LINK Structure
index prev next
------- ----- -----
L0 NULL L1 "A"
L1 L0 L2 "B"
L2 L1 NULL "C" end of this list
L3 NULL L4 "D"
L4 L3 NULL "E" end of this list
===============
See Also: xdll_close()
Example:
/* Code to set up the earlier example */
main()
{
int i,id1,id2;
static char *strings[5] = {"A","B","C","D","E"};
void *pos,*cpos;
typedef struct _mystruct mystruct; /* define application struct */
struct _mystruct {
xdllink node; /* must be first in struct */
char *string; /* to point to a string name */
} array1[3],array2[2],*mine;
/* Note: no error checking shown. */
/* I'm assuming everything is legal. You shouldn't. */
id1 = xdll_open (array1,3,sizeof(mystruct));
id2 = xdll_open (array2,2,sizeof(mystruct));
xdll_use(id1);
xdll_head();
for (i=0; i<=2; i++) /* add A,B,C */
{
mine = (mystruct *) xdll_add ();
/* add links to "A" parent for "B","C","D" */
mine->string = strings[i]; /* add to string info to structure */
/* note: you put your stuff in after you add the link */
if (i==1) /* if "C" */
cpos = xdll_curr (); /* save position of "B" entry */
}
xdll_use(id2);
xdll_head();
for (i=3; i<=4; i++) /* add D,E */
{
mine = (mystruct *) xdll_add ();
/* add links to "A" parent for "B","C","D" */
mine->string = strings[i]; /* add to string info to structure */
/* note: you put your stuff in after you add the link */
}
xdll_use(id1);
/* print number of nodes and links left; should both be zero */
printf ("links left = %d\n",xdll_links_left());
/* Delete "B" and its children "E" and "F" from the list */
xdll_goto (cpos); /* move to "C" entry saved earlier */
xdll_delete (1); /* delete it and its children from the list */
/* reposition on "B" */
xdll_close (); /* close and release memory */
}
#endif /* DOC */
/*
!*/
{
int id;
/* check input parameter ranges */
if (beg == NULL ||
nlinks < 1 || /* must have at least one node */
nsize < sizeof(xdllink) ) /* at least size of a link */
goto error_return; /* parameter error, get out of here */
/* Now make an entry in the list struct array; calloc it as needed */
if (nxdlls <= 0) /* see if this is the first call */
{
for(id=0,xdllist_curr=xdllist_array;
id < XDLL_LISTS; id++, xdllist_curr++) /* make all lists empty */
{ xdllist_curr->size = 0; } /* 0 means empty */
xdllist_beg = xdllist_array; /* set beginning of list pointer */
nxdlls = XDLL_LISTS; /* set number of lists */
}
/* first search current list for any empty ones and use them first */
for (id=0, xdllist_curr=xdllist_beg;
id < nxdlls && xdllist_curr->size != 0; id++,xdllist_curr++)
{ /* no body */ }
if (id >= nxdlls) /* true if no empty ones found */
goto error_return; /* out of memory ; all lists in use */
xdllist_curr->size = nsize;
xdllist_curr->beg = (xdllink *) beg; /* set to user furnished pointer */
xdllist_curr->nlinks = nlinks; /* remember this too */
/* do this one with bytes since struct type is unknown */
/* set current position pointers to the start of the lists */
xdllist_curr->head = xdllist_curr->curr = NULL;
/* set the head current link in the list to none */
/* this means they are empty */
clear_links((xdllink*)beg,nlinks,NULL); /* clear all links in the table */
return (xdllist_in_use_id = id); /* OK return */
error_return: /* return an error code */
return (FUNCBAD);
}
/*!
xdll_reopen Re-Open an existing doubly-linked-list structure.
Summary:
#include <ComUtil/comutil.h>
*/
int xdll_reopen (int newflag,void * beg,int nlinks,int newhead)
/*!
Return Value: 0 the id reopened if OK,
-1 if params out of range or insufficient memory
or no list is in use
or head does not point to a head entry node.
Parameters:
Type Name IO Description
------------ ----------- -- ----------- */
#ifdef DOC
int newflag ;/* I true - this is a new list (no head) */
/* false - this is an old list (use newhead)*/
void * beg ;/* I Pointer to link area alloc'd by user */
/* NULL means use current pntr value */
int nlinks ;/* I Max number of links in the list */
/* 0 means don't change number of links */
int newhead ;/* I Byte Offset to head of linked list */
/* < 0 means leave head at old offset */
#endif
/*!
Description:
Reopen's an link list in an area of memory allocated by the user starting at
address `beg`. The number of records (links) in the memory area is
`nlinks` and the number of bytes per record (link) is `nsize`. The `head`
parameter is used to specify the entry into the linked-list. If `head`
is -1, the current position of the head is used. If `head` is >= 0, this
is the new head; if the head is not a legal head an error is returned.
If `newflag` is true, the list is set to empty and the `newhead` parameter
is not used. The current position in the list is also set to the head.
This call is mainly used to change the position and sizes of existing
link lists.
!*/
{
int old_nlinks,delta;
xdllink *curr;
int oldhead;
int retval = FUNCBAD;
CHECKUSE; /* check that there is one open */
if(xdllist_curr->head != NULL)
oldhead = SUBBEG(xdllist_curr->head); /* offset to current head */
old_nlinks = xdllist_curr->nlinks; /* what they are coming in */
if (beg != NULL) xdllist_curr->beg = (xdllink *) beg;
if (nlinks > 0) xdllist_curr->nlinks = nlinks; /* remember this too */
curr = (newflag) ? NULL : /* no head if newflag true */
((newhead >= 0) ? ADDBEG(newhead) : /* corrects for new beg */
((xdllist_curr->head == NULL) ? NULL : ADDBEG(oldhead)) );
/* use current head offset or NULL if head param negative */
/* don't set head and curr if the head node asked for is not a head node */
if (curr != NULL && curr->prev != HEADID)
goto error_return; /* illegal head, error returned */
xdllist_curr->head = xdllist_curr->curr = curr; /* set head and curr pos */
/* set current position pointers to the start of the lists */
delta = xdllist_curr->nlinks - old_nlinks; /* pos means it got bigger */
if (delta > 0) /* if size increased */
{ /* yes, you have to clear the new links on the end */
curr = ADDBEG( (old_nlinks * xdllist_curr->size) );
/* move to where new part starts and clear it to the end */
clear_links(curr,delta,NULL); /* clear from curr to end */
}
retval = FUNCOK; /* return will be OK */
error_return: /* return an error code */
return (retval);
}
/*!
xdll_use Set the current linked list in use.
Summary:
#include <ComUtil/comutil.h>
*/
int xdll_use (int id)
/*!
Return Value: >=0 id in use if OK, -1 if id is out-of-range or the
linked list is undefined.
Parameters:
Type Name IO Description
------------ ----------- -- ----------- */
#ifdef DOC
int id ;/* I identifier returned by xdll_open() */
#endif
/*!
Description:
Sets the linked list to use for all other function calls. If `id` is negative
or greater than the current number of entries in the link array (`nxdlls`) or
the entry selected by the id is not defined, then the current identifier in
use is set to and returned as a -1.
See also: xdll_open(), xdll_in_use()
Example: See end of example in xdll_open ().
!*/
{
if (id < 0 || id >= nxdlls)
goto error_return; /* out of legal range */
xdllist_curr = xdllist_beg + id; /* go to this ones entry */
if (xdllist_curr->size == 0)
goto error_return; /* selected list is not open */
xdllist_in_use_id = id; /* save identifier */
return (id); /* OK return */
error_return:
xdllist_in_use_id = -1; /* make it so none are in use */
return (-1); /* error return */
}
/*!
xdll_in_use Return id of the current linked list in use.
Summary:
#include <ComUtil/comutil.h>
*/
int xdll_in_use ()
/*!
Return Value: >=0 id in use if OK, -1 if no link list in use.
Parameters:
Type Name IO Description
------------ ----------- -- ----------- */
/*!
Description:
Returns the identifier (id) for the current linked list in use. If no
linked list is in use, it returns a -1. This routine is mainly used for
saving the current identifier in use and its associated state.
See also: xdll_use()
Example:
!*/
{
return (xdllist_in_use_id); /* pretty simple */
}
/*!
xdll_close Close the current linked list in use or close all lists.
Summary:
#include <ComUtil/comutil.h>
*/
int xdll_close (int allflag)
/*!
Return Value: 0 (FUNCOK) if close OK, -1 (FUNCBAD) if error.
Parameters:
Type Name IO Description
------------ ----------- -- ----------- */
#ifdef DOC
int allflag ;/* I allflag true, close all lists */
/* false, close list spec'd by id */
#endif
/*!
Description:
This closes an already opened list structure. The user must have already
free'd any alloc'd pointers in his structures before calling this routine.
Improvements:
- Add a free function parameter to be used to call user structure free.
See also: xdll_open(), xdll_use()
Example: See end of example in xdll_open ().
!*/
{
int i;
if (!allflag)
CHECKUSE; /* make sure one is in use */
for (i=0, xdllist_curr=xdllist_beg; i<nxdlls; i++,xdllist_curr++)
{
if ( allflag || i==xdllist_in_use_id )
/* see if closing all of them or just xdllist_in_use_id */
xdllist_curr->size = 0; /* free entry entry */
}
xdllist_in_use_id = -1; /* set list in use to no list */
return (FUNCOK); /* OK return */
error_return: /* return an error code */
return (FUNCBAD);
}
/*!
xdll_curr Return current list position.
Summary:
#include <ComUtil/comutil.h>
*/
void *xdll_curr ()
/*!
Return Value: current position if OK, NULL if list not opened.
Parameters:
Type Name IO Description
------------ ----------- -- ----------- */
/*!
Description:
Returns current position in the list.
See Also: xdll_use()
!*/
{
CHECKUSE;
if (xdllist_curr->head == NULL) /* nothing in the list ? */
goto error_return; /* no, error */
return ((void *)xdllist_curr->curr); /* return current level */
/* it could also be NULL sometimes */
error_return:
return (NULL);
}
/*!
xdll_head Go to head entry in the link list.
Summary:
#include <ComUtil/comutil.h>
*/
void *xdll_head ()
/*!
Return Value: Head position if OK, NULL if error.
Parameters:
Type Name IO Description
------------ ----------- -- ----------- */
/*!
Description:
Goes to the head position in the linked list (the top). Note that this
does not always have to be the beginning of the list.
See Also: xdll_open().
!*/
{
CHECKUSE;
if (xdllist_curr->head == NULL) /* nothing in the list ? */
goto error_return; /* no, error */
xdllist_curr->curr = xdllist_curr->head;
return ((void *) xdllist_curr->head);
error_return:
return (NULL);
}
/*!
xdll_next Go to next link in the list.
Summary:
#include <ComUtil/comutil.h>
*/
void *xdll_next ()
/*!
Return Value: New position if OK, NULL if no next.
Parameters:
Type Name IO Description
------------ ----------- -- ----------- */
/*!
Description:
Goes to the next entry in the link list. If none, it returns NULL.
See Also: xdll_use().
!*/
{
xdllink *curr;
CHECKUSE;
if (xdllist_curr->head == NULL) /* nothing in the list ? */
goto error_return; /* no, error */
curr = xdllist_curr->curr; /* pointer to current entry */
if (curr->next < 0)
goto error_return;
return ((void *) (xdllist_curr->curr = ADDBEG(curr->next) ) );
error_return:
return (NULL);
}
/*!
xdll_prev Go to previous link in the list.
Summary:
#include <ComUtil/comutil.h>
*/
void *xdll_prev ()
/*!
Return Value: New position if OK, NULL if no previous.
Parameters:
Type Name IO Description
------------ ----------- -- ----------- */
/*!
Description:
Goes to the previous entry in the link list. If none, it returns NULL.
See Also: xdll_use().
!*/
{
xdllink *curr;
CHECKUSE;
if (xdllist_curr->head == NULL) /* nothing in the list ? */
goto error_return; /* no, error */
curr = xdllist_curr->curr; /* pointer to current entry */
if (curr->prev < 0)
goto error_return;
return ((void *) (xdllist_curr->curr = ADDBEG(curr->prev)) );
error_return:
return (NULL);
}
/*!
xdll_tail Go to the last entry in the link list.
Summary:
#include <ComUtil/comutil.h>
*/
void *xdll_tail ()
/*!
Return Value: Last position if OK, NULL if error.
Parameters:
Type Name IO Description
------------ ----------- -- ----------- */
/*!
Description:
Goes to the last position in the linked list (the bottom).
See Also:
!*/
{
xdllink *curr,*next;
CHECKUSE;
if (xdllist_curr->head == NULL) /* nothing in the list ? */
goto error_return; /* no, error */
if ( (curr = (xdllink *) xdllist_curr->curr) == NULL)/* start from current */
xdll_head(); /* otherwise, start from head of list */
/* goto the end from current position */
while ( (next = (xdllink *) xdll_next()) != NULL)
{ curr = next; }
return ((void *)curr); /* this is the last one */
error_return:
return (NULL);
}
/*!
xdll_insert Insert a(nother) link in the list relative to curr pos.
Summary:
#include <ComUtil/comutil.h>
*/
void *xdll_insert (int before)
/*!
Return Value: position of new link if OK, NULL if error.
Parameters:
Type Name IO Description
------------ ----------- -- ----------- */
#ifdef DOC
int before ;/* I flag, True - insert before current link */
/* False - insert after current link */
#endif
/*!
Description:
Adds a link relative to the current position in the of the linked list
currently in use. Returns the position of where the link was added.
Returns a NULL if the list is full or no list is in use.
Adds the link relative to the current position. The `before` parameter
determines whether the child is inserted before or after the current
position.
If the current position is NULL, then the list is empty and the new
link will be the first one added.
See Also: xdll_use().
!*/
{
xdllink *curr, *newl;
int nnew,ncurr;
CHECKUSE;
/* first find a node that is empty */
if ( (nnew = next_free_link(1)) < 0 )
goto error_return; /* uh oh, out of memory; die */
newl = ADDBEG(nnew);
if (xdllist_curr->head == NULL) /* if list is empty */
{ /* first entry, make it the head */
xdllist_curr->head = xdllist_curr->curr = newl;
/* set to start of list */
newl->prev = HEADID; /* HEADID means its the head node */
newl->next = -1; /* initialize */
}
else /* there are already links in the table */
{
if ( (curr = xdllist_curr->curr) == NULL)
goto error_return; /* uh oh, NULL or ran out of space */
ncurr = SUBBEG(curr);
if ( before ) /* if inserting before curr */
{
if (curr->prev >= 0) /* not head of list ? */
ADDBEG(curr->prev)->next = nnew;
else
xdllist_curr->head = newl; /* new head of the list */
newl->prev = curr->prev; /* OK even if curr->prev is < 0 */
newl->next = ncurr; /* next is previous current entry */
curr->prev = nnew;
}
else /* !before (after) inserting after curr */
{
if (curr->next >= 0) /* not tail of list ? */
ADDBEG(curr->next)->prev = nnew;
newl->next = curr->next; /* OK even if curr->next is <0 */
newl->prev = ncurr; /* next is previous current entry */
curr->next = nnew;
}
}
return ((void *) newl); /* return position added */
error_return:
return (NULL); /* means out of memory or id not opened */
}
/*!
xdll_delete Delete the current link.
Summary:
#include <ComUtil/comutil.h>
*/
int xdll_delete (int flag)
/*!
Return Value: FUNCOK if OK, FUNCBAD if error.
Parameters:
Type Name IO Description
------------ ----------- -- ----------- */
#ifdef DOC
int flag ;/* I True - reposition to previous link */
/* False - reposition to next link */
#endif
/*!
Description:
Deletes the current link. It repositions the link to either the previous
link or next link depending on the state of `flag`.
See Also: xdll_use().
!*/
{
xdllink *prev,*curr,*next;
CHECKUSE;
if (xdllist_curr->head != NULL) /* if list is empty */
{ /* no, go ahead and delete */
curr = xdllist_curr->curr; /* pointer to current entry */
if (curr == NULL)
goto error_return; /* you forgot to position it */
if (curr->next < 0)
next = NULL;
else
(next = ADDBEG(curr->next))->prev = curr->prev;
if (curr->prev < 0) /* if curr is head of the list */
{
prev = NULL;
xdllist_curr->head = next;
}
else
(prev = ADDBEG(curr->prev))->next = curr->next;
curr->prev = curr->next = -1; /* delete this link */
xdllist_curr->curr = ( (flag) ? prev : next);
/* set to either prev or next */
/* note: could be NULL position, but thats OK */
}
return (FUNCOK);
error_return:
return (FUNCBAD);
}
/*!
xdll_goto Change to (goto) a specific position in the link list.
Summary:
#include <ComUtil/comutil.h>
*/
void *xdll_goto (void * pos)
/*!
Return Value: New position if OK, NULL if pos does not exist or is empty.
Parameters:
Type Name IO Description
------------ ----------- -- ----------- */
#ifdef DOC
void *pos ;/* I Pointer to position to goto */
#endif
/*!
Description:
The moves the current position to a new absolute position in the list. It
assumes that 'pos' was retrieved using other position returning functions.
If the new position is not in the list or is an empty cell, then a NULL is
returned. Otherwise, the new position is returned.
See Also:
!*/
{
xdllink *curr;
CHECKUSE;
if (xdllist_curr->head == NULL) /* nothing in the list ? */
goto error_return; /* no, error */
curr = (xdllink *) pos; /* convert to link struct */
/* make sure its a legal position */
/* an empty or deleted position has curr->next and curr->prev set to -1 */
/* if curr->prev == HEADID, that means its a head entry */
if ( curr->next == -1 && curr->prev == -1) /* no prev,no next, not head */
goto error_return; /* return error code if going to empty node */
xdllist_curr->curr = (xdllink *) pos;
return (pos);
error_return:
return (NULL); /* means out of memory or id not opened */
}
/*!
xdll_clear() Clear out a linked list.
Summary:
#include <ComUtil/comutil.h>
*/
int xdll_clear(int flag,void (*userfunc)(void*))
/*!
Return Value: 0 (FUNCOK) if OK, -1 (FUNCBAD) if no linked list in use.
Parameters:
Type Name IO Description
------------ ----------- -- ----------- */
#ifdef DOC
int flag ;/* I true - clear entire linked list */
/* false - clear only from head to tail */
void (*userfunc)();/* I User function that is called for every */
/* link cleared with the pointer to the *
/* user's structure; NULL if no user function */
#endif
/*!
Description:
Clears all of the cells in a linked list for the currently link list is
use (from xdll_use()). Also sets head and curr pointers to NULL to indicate
that the list is clear. If `flag` is set, all the links everywhere in the
entire list are cleared. If `flag` is false, on the links in the current
connected list from xdll_head() to xdll_tail() are cleared (deleted). This
does nothing to the user's portion of the structure; only the link info. To
clear the user info, the user must walk through their own structure or use
the `userfunc` parameter. The `userfunc` is called every entry that a link
is released for. The address of the user's structure is called as a parameter
to this function (the parameter is a `void *` type and must be promoted to the
user's structure by the user routine).
See Also:
!*/
{
xdllink *curr;
CHECKUSE;
if (flag) /* if true, clear entire linked list structure out */
clear_links(xdllist_curr->beg,xdllist_curr->nlinks,userfunc);
else /* just clear from head to tail and null out head */
{
for(curr = (xdllink *) xdll_head();
curr != NULL;
curr = (xdllink *) xdll_head() )
{
if (userfunc != NULL)
(*userfunc)((void *) curr); /* call user func to clear */
xdll_delete(0); /* delete link. Repo after */
}
}
xdllist_curr->curr = xdllist_curr->head = NULL;
/* this says its all empty */
return(FUNCOK);
error_return:
return (FUNCBAD); /* none there, exit */
}
/*!
xdll_links_left Returns number of free links left in the list.
Summary:
#include <ComUtil/comutil.h>
*/
int xdll_links_left ()
/*!
Return Value: Number of links if OK, -1 if no list in use.
Parameters:
Type Name IO Description
------------ ----------- -- ----------- */
/*!
Description:
This utility routine returns the number of free links left in the list. If
it is zero, the list is full.
See Also:
!*/
{
int num;
CHECKUSE;
for (num=0; /* set to start search at top of the link structure */
next_free_link(num==0) >= 0; /* go until none left anymore */
num++); /* set to search from current pos, incr num */
return (num);
error_return:
return (FUNCBAD); /* means out of memory or id not opened */
}
/*!
xdll_links_num Returns the number of links in the current link list.
Summary:
#include <ComUtil/comutil.h>
*/
int xdll_links_num ()
/*!
Return Value: Number of links if OK, -1 if no list in use.
Parameters:
Type Name IO Description
------------ ----------- -- ----------- */
/*!
Description:
Returns the maximum number of links possible in a tree. Its the same as
`xdllist_curr->nlinks`. The number of links used can be found as
`xdll_links_num() - xdll_links_left()'.
See Also:
!*/
{
CHECKUSE;
return (xdllist_curr->nlinks);
error_return:
return (FUNCBAD); /* means out of memory or id not opened */
}
/*!
next_free_link Returns position of next free link.
Summary:
#include <ComUtil/comutil.h>
*/
static int next_free_link(int flag)
/*!
Return Value: Offset of next free link relative to beginning of link list,
-1 if none left.
Parameters:
Type Name IO Description
------------ ----------- -- ----------- */
#ifdef DOC
int flag ;/* I If true, start at top of link structure */
/* If false, continue from last position */
#endif
/*!
Description:
This internal routine returns the address of the next free position in the
link structure. It searches from the start of the structure if flag is true or
continues from the last position if the flag is false. The flag is normally
set to true when looking for a place to add a node. It is set to false when
counting the number of remaining cells.
See Also:
!*/
{
static xdllink *curr = NULL; /* static to hold its value */
static int i; /* link number */
xdllink *tpntr;
if (flag || curr == NULL)
{
curr = xdllist_curr->beg; /* start of the table */
i = 0; /* set to start at top of list */
}
while (i++ <= xdllist_curr->nlinks)
{
tpntr = curr; /* current position */
curr = (xdllink *) (((char *)curr) + xdllist_curr->size); /* preincr */
/* an empty or deleted position has tpntr->next and tpntr->prev set to -1 */
/* if tpntr->prev == HEADID, that means its a head entry */
if ( tpntr->next == -1 && tpntr->prev == -1)
return (SUBBEG(tpntr));
}
/* if you leave while, there are no remaining links */
curr = NULL;
return (-1); /* none there, exit */
}
/*!
clear_links Clear from position in link list a spec'd # of links
Summary:
#include <ComUtil/comutil.h>
*/
static int clear_links(xdllink * curr,int nlinks,void (*userfunc)(void*))
/*!
Return Value: 0 always.
Parameters:
Type Name IO Description
------------ ----------- -- ----------- */
#ifdef DOC
xdllink *curr ;/* I Position to start clearing links for */
int nlinks ;/* I Number of links to clear */
void (*userfunc)() ;/* I User function to call to clear their own */
/* structure elements; NULL if none */
#endif
/*!
Description:
This internal routine clears the links in a user structure for `nlinks`
records starting at `curr` position in the link structure. It is used
to clear open'd and expanding re-opened link areas.
See Also:
Examples:
To clear the entire current link list:
clear_links(xdllist_curr->beg,xdllist_curr->nlinks);
!*/
{
while (nlinks-- > 0)
{
curr->prev = curr->next = -1; /* clear the links */
if (userfunc != NULL)
(*userfunc)((void *) curr); /* call user func to clear */
curr = (xdllink *) (((char *)curr) + xdllist_curr->size); /* preincr */
} /* if you leave while, there are no remaining links */
return(FUNCOK);
}
|