home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / the25.zip / thesrc251.zip / linked.c < prev    next >
C/C++ Source or Header  |  1998-01-05  |  31KB  |  975 lines

  1. /***********************************************************************/
  2. /* LINKED.C - Linked list routines.                                    */
  3. /***********************************************************************/
  4. /*
  5.  * THE - The Hessling Editor. A text editor similar to VM/CMS xedit.
  6.  * Copyright (C) 1991-1997 Mark Hessling
  7.  *
  8.  * This program is free software; you can redistribute it and/or
  9.  * modify it under the terms of the GNU General Public License as
  10.  * published by the Free Software Foundation; either version 2 of
  11.  * the License, or any later version.
  12.  *
  13.  * This program is distributed in the hope that it will be useful,
  14.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16.  * General Public License for more details.
  17.  *
  18.  * You should have received a copy of the GNU General Public License
  19.  * along with this program; if not, write to:
  20.  *
  21.  *    The Free Software Foundation, Inc.
  22.  *    675 Mass Ave,
  23.  *    Cambridge, MA 02139 USA.
  24.  *
  25.  *
  26.  * If you make modifications to this software that you feel increases
  27.  * it usefulness for the rest of the community, please email the
  28.  * changes, enhancements, bug fixes as well as any and all ideas to me.
  29.  * This software is going to be maintained and enhanced as deemed
  30.  * necessary by the community.
  31.  *
  32.  * Mark Hessling                 Email:             M.Hessling@qut.edu.au
  33.  * PO Box 203                    Phone:                    +617 3802 0800
  34.  * Bellara                       http://www.gu.edu.au/gext/the/markh.html
  35.  * QLD 4507                      **** Maintainer PDCurses & REXX/SQL ****
  36.  * Australia                     ************* Author of THE ************
  37.  */
  38.  
  39. /*
  40. $Id: linked.c 2.1 1995/06/24 16:30:19 MH Rel MH $
  41. */
  42.  
  43. #include <the.h>
  44. #include <proto.h>
  45.  
  46. /*-------------------------- external data ----------------------------*/
  47.  
  48. /***********************************************************************/
  49. #ifdef HAVE_PROTO
  50. LINE *lll_add(LINE *first,LINE *curr,unsigned short size)
  51. #else
  52. LINE *lll_add(first,curr,size)
  53. LINE *first;
  54. LINE *curr;
  55. unsigned short size;
  56. #endif
  57. /***********************************************************************/
  58. /* Adds a LINE to the current linked list after the current member.    */
  59. /* PARAMETERS:                                                         */
  60. /* first      - pointer to first LINE in linked list                   */
  61. /* curr       - pointer to current LINE in linked list                 */
  62. /* size       - size of a LINE item                                    */
  63. /* RETURN:    - pointer to next LINE item                              */
  64. /***********************************************************************/
  65. {
  66. /*--------------------------- local data ------------------------------*/
  67.  LINE *next=NULL;
  68. /*--------------------------- processing ------------------------------*/
  69. #ifdef TRACE
  70.  trace_function("linked.c:    lll_add");
  71. #endif
  72.  
  73.  if ((next=(LINE *)(*the_malloc)(size)) != (LINE *)NULL)
  74.    {
  75.     if (curr == NULL)
  76.       {
  77.        if (first == NULL)
  78.          {
  79.           /*
  80.            * First entry in LL 
  81.            */
  82. /*          first = next; */
  83.           next->next = NULL;
  84.           next->prev = NULL;
  85.          }
  86.        else
  87.          {
  88.           /* 
  89.            * Insert this entry before first. Calling routine
  90.            * must reset first to returned pointer.
  91.            */
  92.           next->next = first;
  93.           next->prev = NULL;
  94.           first->prev = next;
  95.          }
  96.       }
  97.     else
  98.       {
  99.        if (curr->next != NULL)
  100.           curr->next->prev = next;
  101.        next->next = curr->next;
  102.        curr->next = next;
  103.        next->prev = curr;
  104.       }
  105.    }
  106. /*---------------------------------------------------------------------*/
  107. /* Ensure all pointers in the structure are set to NULL                */
  108. /*---------------------------------------------------------------------*/
  109.  next->line = NULL;
  110.  next->name = NULL;
  111.  next->pre = NULL;
  112. #ifdef TRACE
  113.  trace_return();
  114. #endif
  115.  return(next);
  116. }
  117. /***********************************************************************/
  118. #ifdef HAVE_PROTO
  119. LINE *lll_del(LINE **first,LINE **last,LINE *curr,short direction)
  120. #else
  121. LINE *lll_del(first,last,curr,direction)
  122. LINE **first;
  123. LINE **last;
  124. LINE *curr;
  125. short direction;
  126. #endif
  127. /***********************************************************************/
  128. {
  129. /*--------------------------- local data ------------------------------*/
  130.  LINE *new_curr=NULL;
  131. /*--------------------------- processing ------------------------------*/
  132. #ifdef TRACE
  133.  trace_function("linked.c:    lll_del");
  134. #endif
  135. /*---------------------------------------------------------------------*/
  136. /* Delete the only record                                              */
  137. /*---------------------------------------------------------------------*/
  138.  if (curr->prev == NULL && curr->next == NULL)
  139.    {
  140.     (*the_free)(curr);
  141.     *first = NULL;
  142.     if (last != NULL)
  143.        *last = NULL;
  144. #ifdef TRACE
  145.     trace_return();
  146. #endif
  147.     return(NULL);
  148.    }
  149. /*---------------------------------------------------------------------*/
  150. /* Delete the first record                                             */
  151. /*---------------------------------------------------------------------*/
  152.  if (curr->prev == NULL)
  153.    {
  154.     curr->next->prev = NULL;
  155.     *first = new_curr = curr->next;
  156.     (*the_free)(curr);
  157.     curr = new_curr;
  158. #ifdef TRACE
  159.     trace_return();
  160. #endif
  161.     return(curr);
  162.    }
  163. /*---------------------------------------------------------------------*/
  164. /* Delete the last  record                                             */
  165. /*---------------------------------------------------------------------*/
  166.  if (curr->next == NULL)
  167.    {
  168.     curr->prev->next = NULL;
  169.     new_curr = curr->prev;
  170.     if (last != NULL)
  171.        *last = curr->prev;
  172.     (*the_free)(curr);
  173.     curr = new_curr;
  174. #ifdef TRACE
  175.     trace_return();
  176. #endif
  177.     return(curr);
  178.    }
  179. /*---------------------------------------------------------------------*/
  180. /* All others                                                          */
  181. /*---------------------------------------------------------------------*/
  182.  curr->prev->next = curr->next;
  183.  curr->next->prev = curr->prev;
  184.  if (direction == DIRECTION_FORWARD)
  185.     new_curr = curr->next;
  186.  else
  187.     new_curr = curr->prev;
  188.  
  189.  (*the_free)(curr);
  190.  curr = new_curr;
  191. #ifdef TRACE
  192.  trace_return();
  193. #endif
  194.  return(curr);
  195. }
  196. /***********************************************************************/
  197. #ifdef HAVE_PROTO
  198. LINE *lll_free(LINE *first)
  199. #else
  200. LINE *lll_free(first)
  201. LINE *first;
  202. #endif
  203. /***********************************************************************/
  204. /* Free up all allocated memory until the last item in the linked-list */
  205. /* PARAMETERS:                                                         */
  206. /* first      - pointer to first line for the file                     */
  207. /* RETURN:    - NULL                                                   */
  208. /***********************************************************************/
  209. {
  210. /*--------------------------- local data ------------------------------*/
  211.  LINE *curr=NULL;
  212.  LINE *new_curr=NULL;
  213. /*--------------------------- processing ------------------------------*/
  214. #ifdef TRACE
  215.  trace_function("linked.c:    lll_free");
  216. #endif
  217.  curr = first;
  218.  while (curr != NULL)
  219.    {
  220.     if (curr->line) (*the_free)(curr->line);
  221.     if (curr->name) (*the_free)(curr->name);
  222.     new_curr = curr->next;
  223.     (*the_free)(curr);
  224.     curr = new_curr;
  225.    }
  226. #ifdef TRACE
  227.  trace_return();
  228. #endif
  229.  return((LINE *)NULL);
  230. }
  231. /***********************************************************************/
  232. #ifdef HAVE_PROTO
  233. LINE *lll_find(LINE *first,LINE *last,LINETYPE line_number,LINETYPE max_lines)
  234. #else
  235. LINE *lll_find(first,last,line_number,max_lines)
  236. LINE *first,*last;
  237. LINETYPE line_number,max_lines;
  238. #endif
  239. /***********************************************************************/
  240. {
  241. /*--------------------------- local data ------------------------------*/
  242.  LINE *curr=NULL;
  243.  LINETYPE i=0L;
  244. /*--------------------------- processing ------------------------------*/
  245. #ifdef TRACE
  246.  trace_function("linked.c:    lll_find");
  247. #endif
  248.  if (line_number < (max_lines/2))
  249.    {
  250.     curr = first;
  251.     if (curr != NULL)
  252.        for(i=0L;i<line_number && curr->next != NULL; i++, curr=curr->next);
  253.    }
  254.  else
  255.    {
  256.     curr = last;
  257.     if (curr != NULL)
  258.        for(i=max_lines+1L;i>line_number && curr->prev != NULL; i--, curr=curr->prev);
  259.    }
  260. #ifdef TRACE
  261.  trace_return();
  262. #endif
  263.  return(curr);
  264. }
  265. /***********************************************************************/
  266. #ifdef HAVE_PROTO
  267. LINE *lll_locate(LINE *first,CHARTYPE *value)
  268. #else
  269. LINE *lll_locate(first,value)
  270. LINE *first;
  271. CHARTYPE *value;
  272. #endif
  273. /***********************************************************************/
  274. {
  275. /*--------------------------- local data ------------------------------*/
  276.  LINE *curr=NULL;
  277. /*--------------------------- processing ------------------------------*/
  278. #ifdef TRACE
  279.  trace_function("linked.c:    lll_locate");
  280. #endif
  281.  curr = first;
  282.  while (curr)
  283.    {
  284.     if (curr->name
  285.     &&  strcmp((DEFCHAR*)curr->name,(DEFCHAR*)value) == 0)
  286.        break;
  287.     curr = curr->next;
  288.    }
  289. #ifdef TRACE
  290.  trace_return();
  291. #endif
  292.  return(curr);
  293. }
  294. /***********************************************************************/
  295. #ifdef HAVE_PROTO
  296. VIEW_DETAILS *vll_add(VIEW_DETAILS *first,VIEW_DETAILS *curr,unsigned short size)
  297. #else
  298. VIEW_DETAILS *vll_add(first,curr,size)
  299. VIEW_DETAILS *first;
  300. VIEW_DETAILS *curr;
  301. unsigned short size;
  302. #endif
  303. /***********************************************************************/
  304. /* Adds a VIEW_DETAILS to the current linked list after the current member.    */
  305. /* PARAMETERS:                                                         */
  306. /* first      - pointer to first VIEW_DETAILS in linked list                   */
  307. /* curr       - pointer to current VIEW_DETAILS in linked list                 */
  308. /* size       - size of a VIEW_DETAILS item                                    */
  309. /* RETURN:    - pointer to next VIEW_DETAILS item                              */
  310. /***********************************************************************/
  311. {
  312. /*--------------------------- local data ------------------------------*/
  313.  VIEW_DETAILS *next=(VIEW_DETAILS *)NULL;
  314. /*--------------------------- processing ------------------------------*/
  315. #ifdef TRACE
  316.  trace_function("linked.c:    vll_add");
  317. #endif
  318.  
  319.  if ((next=(VIEW_DETAILS *)(*the_malloc)(size)) != (VIEW_DETAILS *)NULL)
  320.    {
  321.     if (curr == (VIEW_DETAILS *)NULL)
  322.       {
  323.        first = next;
  324.        next->next = (VIEW_DETAILS *)NULL;
  325.       }
  326.     else
  327.       {
  328.        if (curr->next != (VIEW_DETAILS *)NULL)
  329.           curr->next->prev = next;
  330.        next->next = curr->next;
  331.        curr->next = next;
  332.       }
  333.     next->prev = curr;
  334.    }
  335. /*---------------------------------------------------------------------*/
  336. /* Ensure all pointers in the structure are set to NULL                */
  337. /*---------------------------------------------------------------------*/
  338.  next->file_for_view = (FILE_DETAILS *)NULL;
  339. #ifdef TRACE
  340.  trace_return();
  341. #endif
  342.  return(next);
  343. }
  344. /***********************************************************************/
  345. #ifdef HAVE_PROTO
  346. VIEW_DETAILS *vll_del(VIEW_DETAILS **first,VIEW_DETAILS **last,VIEW_DETAILS *curr,short direction)
  347. #else
  348. VIEW_DETAILS *vll_del(first,last,curr,direction)
  349. VIEW_DETAILS **first;
  350. VIEW_DETAILS **last;
  351. VIEW_DETAILS *curr;
  352. short direction;
  353. #endif
  354. /***********************************************************************/
  355. /* This ll_del() function is different to others!!!!!!!!               */
  356. /***********************************************************************/
  357. {
  358. /*--------------------------- local data ------------------------------*/
  359.  VIEW_DETAILS *new_curr=NULL;
  360. /*--------------------------- processing ------------------------------*/
  361. #ifdef TRACE
  362.  trace_function("linked.c:    vll_del");
  363. #endif
  364. /*---------------------------------------------------------------------*/
  365. /* Delete the only record                                              */
  366. /*---------------------------------------------------------------------*/
  367.  if (curr->prev == NULL && curr->next == NULL)
  368.    {
  369.     (*the_free)(curr);
  370.     *first = NULL;
  371.     if (last != NULL)
  372.        *last = NULL;
  373. #ifdef TRACE
  374.     trace_return();
  375. #endif
  376.     return(NULL);
  377.    }
  378. /*---------------------------------------------------------------------*/
  379. /* Delete the first record                                             */
  380. /*---------------------------------------------------------------------*/
  381.  if (curr->prev == NULL)
  382.    {
  383.     curr->next->prev = NULL;
  384.     *first = new_curr = curr->next;
  385.     (*the_free)(curr);
  386.     curr = new_curr;
  387. #ifdef TRACE
  388.     trace_return();
  389. #endif
  390.     return(curr);
  391.    }
  392. /*---------------------------------------------------------------------*/
  393. /* Delete the last  record                                             */
  394. /* If DIRECTION_FORWARD, curr becomes first, otherwise curr becomes prev*/
  395. /*---------------------------------------------------------------------*/
  396.  if (curr->next == NULL)
  397.    {
  398.     curr->prev->next = NULL;
  399.     if (direction == DIRECTION_FORWARD)
  400.        new_curr = *first;
  401.     else
  402.        new_curr = curr->prev;
  403.     if (last != NULL)
  404.        *last = curr->prev;
  405.     (*the_free)(curr);
  406.     curr = new_curr;
  407. #ifdef TRACE
  408.     trace_return();
  409. #endif
  410.     return(curr);
  411.    }
  412. /*---------------------------------------------------------------------*/
  413. /* All others                                                          */
  414. /*---------------------------------------------------------------------*/
  415.  curr->prev->next = curr->next;
  416.  curr->next->prev = curr->prev;
  417.  if (direction == DIRECTION_FORWARD)
  418.    new_curr = curr->next;
  419.  else
  420.    new_curr = curr->prev;
  421.  
  422.  (*the_free)(curr);
  423.  curr = new_curr;
  424. #ifdef TRACE
  425.  trace_return();
  426. #endif
  427.  return(curr);
  428. }
  429. /***********************************************************************/
  430. #ifdef HAVE_PROTO
  431. DEFINE *dll_add(DEFINE *first,DEFINE *curr,unsigned short size)
  432. #else
  433. DEFINE *dll_add(first,curr,size)
  434. DEFINE *first;
  435. DEFINE *curr;
  436. unsigned short size;
  437. #endif
  438. /***********************************************************************/
  439. /* Adds a DEFINE to the current linked list after the current member.  */
  440. /* PARAMETERS:                                                         */
  441. /* first      - pointer to first DEFINE in linked list                 */
  442. /* curr       - pointer to current DEFINE in linked list               */
  443. /* size       - size of a DEFINE item                                  */
  444. /* RETURN:    - pointer to next DEFINE item                            */
  445. /***********************************************************************/
  446. {
  447. /*--------------------------- local data ------------------------------*/
  448.  DEFINE *next=NULL;
  449. /*--------------------------- processing ------------------------------*/
  450. #ifdef TRACE
  451.  trace_function("linked.c:    dll_add");
  452. #endif
  453.  if ((next=(DEFINE *)(*the_malloc)(size)) != (DEFINE *)NULL)
  454.    {
  455.     if (curr == NULL)
  456.       {
  457.        first = next;
  458.        next->next = NULL;
  459.       }
  460.     else
  461.       {
  462.        if (curr->next != NULL)
  463.           curr->next->prev = next;
  464.        next->next = curr->next;
  465.        curr->next = next;
  466.       }
  467.     next->prev = curr;
  468.    }
  469. /*---------------------------------------------------------------------*/
  470. /* Ensure all pointers in the structure are set to NULL                */
  471. /*---------------------------------------------------------------------*/
  472.  next->def_params = NULL;
  473. #ifdef TRACE
  474.  trace_return();
  475. #endif
  476.  return(next);
  477. }
  478. /***********************************************************************/
  479. #ifdef HAVE_PROTO
  480. DEFINE *dll_del(DEFINE **first,DEFINE **last,DEFINE *curr,short direction)
  481. #else
  482. DEFINE *dll_del(first,last,curr,direction)
  483. DEFINE **first;
  484. DEFINE **last;
  485. DEFINE *curr;
  486. short direction;
  487. #endif
  488. /***********************************************************************/
  489. {
  490. /*--------------------------- local data ------------------------------*/
  491.  DEFINE *new_curr=NULL;
  492. /*--------------------------- processing ------------------------------*/
  493. #ifdef TRACE
  494.  trace_function("linked.c:    dll_del");
  495. #endif
  496. /*---------------------------------------------------------------------*/
  497. /* Delete the only record                                              */
  498. /*---------------------------------------------------------------------*/
  499.  if (curr->prev == NULL && curr->next == NULL)
  500.    {
  501.     (*the_free)(curr);
  502.     *first = NULL;
  503.     if (last != NULL)
  504.        *last = NULL;
  505. #ifdef TRACE
  506.     trace_return();
  507. #endif
  508.     return(NULL);
  509.    }
  510. /*---------------------------------------------------------------------*/
  511. /* Delete the first record                                             */
  512. /*---------------------------------------------------------------------*/
  513.  if (curr->prev == NULL)
  514.    {
  515.     curr->next->prev = NULL;
  516.     *first = new_curr = curr->next;
  517.     (*the_free)(curr);
  518.     curr = new_curr;
  519. #ifdef TRACE
  520.     trace_return();
  521. #endif
  522.     return(curr);
  523.    }
  524. /*---------------------------------------------------------------------*/
  525. /* Delete the last  record                                             */
  526. /*---------------------------------------------------------------------*/
  527.  if (curr->next == NULL)
  528.    {
  529.     curr->prev->next = NULL;
  530.     new_curr = curr->prev;
  531.     if (last != NULL)
  532.        *last = curr->prev;
  533.     (*the_free)(curr);
  534.     curr = new_curr;
  535. #ifdef TRACE
  536.     trace_return();
  537. #endif
  538.     return(curr);
  539.    }
  540. /*---------------------------------------------------------------------*/
  541. /* All others                                                          */
  542. /*---------------------------------------------------------------------*/
  543.  curr->prev->next = curr->next;
  544.  curr->next->prev = curr->prev;
  545.  if (direction == DIRECTION_FORWARD)
  546.    new_curr = curr->next;
  547.  else
  548.    new_curr = curr->prev;
  549.  
  550.  (*the_free)(curr);
  551.  curr = new_curr;
  552. #ifdef TRACE
  553.  trace_return();
  554. #endif
  555.  return(curr);
  556. }
  557. /***********************************************************************/
  558. #ifdef HAVE_PROTO
  559. DEFINE *dll_free(DEFINE *first)
  560. #else
  561. DEFINE *dll_free(first)
  562. DEFINE *first;
  563. #endif
  564. /***********************************************************************/
  565. {
  566. /*--------------------------- local data ------------------------------*/
  567.  DEFINE *curr=NULL;
  568.  DEFINE *new_curr=NULL;
  569. /*--------------------------- processing ------------------------------*/
  570. #ifdef TRACE
  571.  trace_function("linked.c:    dll_free");
  572. #endif
  573.  curr = first;
  574.  while (curr != (DEFINE *)NULL)
  575.    {
  576.     if (curr->def_params != NULL)
  577.        (*the_free)(curr->def_params);
  578.     new_curr = curr->next;
  579.     (*the_free)(curr);
  580.     curr = new_curr;
  581.    }
  582. #ifdef TRACE
  583.  trace_return();
  584. #endif
  585.  return((DEFINE *)NULL);
  586. }
  587. /***********************************************************************/
  588. #ifdef HAVE_PROTO
  589. PPC *pll_add(PPC *first,PPC *curr,unsigned short size)
  590. #else
  591. PPC *pll_add(first,curr,size)
  592. PPC *first;
  593. PPC *curr;
  594. unsigned short size;
  595. #endif
  596. /***********************************************************************/
  597. /* Adds a PPC to the current linked list after the current member.     */
  598. /* PARAMETERS:                                                         */
  599. /* first      - pointer to first PPC in linked list                    */
  600. /* curr       - pointer to current PPC in linked list                  */
  601. /* size       - size of a PPC item                                     */
  602. /* RETURN:    - pointer to next PPC item                               */
  603. /***********************************************************************/
  604. {
  605. /*--------------------------- local data ------------------------------*/
  606.  PPC *next=NULL;
  607. /*--------------------------- processing ------------------------------*/
  608. #ifdef TRACE
  609.  trace_function("linked.c:    pll_add");
  610. #endif
  611.  
  612.  if ((next=(PPC *)(*the_malloc)(size)) != (PPC *)NULL)
  613.    {
  614.     if (curr == NULL)
  615.       {
  616.        first = next;
  617.        next->next = NULL;
  618.       }
  619.     else
  620.       {
  621.        if (curr->next != NULL)
  622.           curr->next->prev = next;
  623.        next->next = curr->next;
  624.        curr->next = next;
  625.       }
  626.     next->prev = curr;
  627.    }
  628. /*---------------------------------------------------------------------*/
  629. /* Ensure all pointers in the structure are set to NULL                */
  630. /*---------------------------------------------------------------------*/
  631. #ifdef TRACE
  632.  trace_return();
  633. #endif
  634.  return(next);
  635. }
  636. /***********************************************************************/
  637. #ifdef HAVE_PROTO
  638. PPC *pll_del(PPC **first,PPC **last,PPC *curr,short direction)
  639. #else
  640. PPC *pll_del(first,last,curr,direction)
  641. PPC **first;
  642. PPC **last;
  643. PPC *curr;
  644. short direction;
  645. #endif
  646. /***********************************************************************/
  647. {
  648. /*--------------------------- local data ------------------------------*/
  649.  PPC *new_curr=NULL;
  650. /*--------------------------- processing ------------------------------*/
  651. #ifdef TRACE
  652.  trace_function("linked.c:    pll_del");
  653. #endif
  654. /*---------------------------------------------------------------------*/
  655. /* Delete the only record                                              */
  656. /*---------------------------------------------------------------------*/
  657.  if (curr->prev == NULL && curr->next == NULL)
  658.    {
  659.     (*the_free)(curr);
  660.     *first = NULL;
  661.     if (last != NULL)
  662.        *last = NULL;
  663. #ifdef TRACE
  664.     trace_return();
  665. #endif
  666.     return(NULL);
  667.    }
  668. /*---------------------------------------------------------------------*/
  669. /* Delete the first record                                             */
  670. /*---------------------------------------------------------------------*/
  671.  if (curr->prev == NULL)
  672.    {
  673.     curr->next->prev = NULL;
  674.     *first = new_curr = curr->next;
  675.     (*the_free)(curr);
  676.     curr = new_curr;
  677. #ifdef TRACE
  678.     trace_return();
  679. #endif
  680.     return(curr);
  681.    }
  682. /*---------------------------------------------------------------------*/
  683. /* Delete the last  record                                             */
  684. /*---------------------------------------------------------------------*/
  685.  if (curr->next == NULL)
  686.    {
  687.     curr->prev->next = NULL;
  688.     new_curr = curr->prev;
  689.     if (last != NULL)
  690.        *last = curr->prev;
  691.     (*the_free)(curr);
  692.     curr = new_curr;
  693. #ifdef TRACE
  694.     trace_return();
  695. #endif
  696.     return(curr);
  697.    }
  698. /*---------------------------------------------------------------------*/
  699. /* All others                                                          */
  700. /*---------------------------------------------------------------------*/
  701.  curr->prev->next = curr->next;
  702.  curr->next->prev = curr->prev;
  703.  if (direction == DIRECTION_FORWARD)
  704.    new_curr = curr->next;
  705.  else
  706.    new_curr = curr->prev;
  707.  
  708.  (*the_free)(curr);
  709.  curr = new_curr;
  710. #ifdef TRACE
  711.  trace_return();
  712. #endif
  713.  return(curr);
  714. }
  715. /***********************************************************************/
  716. #ifdef HAVE_PROTO
  717. PPC *pll_free(PPC *first)
  718. #else
  719. PPC *pll_free(first)
  720. PPC *first;
  721. #endif
  722. /***********************************************************************/
  723. /* Free up all allocated memory until the last item in the linked-list */
  724. /* PARAMETERS:                                                         */
  725. /* first      - pointer to first line for the file                     */
  726. /* RETURN:    - NULL                                                   */
  727. /***********************************************************************/
  728. {
  729. /*--------------------------- local data ------------------------------*/
  730.  PPC *curr=NULL;
  731.  PPC *new_curr=NULL;
  732. /*--------------------------- processing ------------------------------*/
  733. #ifdef TRACE
  734.  trace_function("linked.c:    pll_free");
  735. #endif
  736.  curr = first;
  737.  while (curr != NULL)
  738.    {
  739.     new_curr = curr->next;
  740.     (*the_free)(curr);
  741.     curr = new_curr;
  742.    }
  743. #ifdef TRACE
  744.  trace_return();
  745. #endif
  746.  return((PPC *)NULL);
  747. }
  748. /***********************************************************************/
  749. #ifdef HAVE_PROTO
  750. PPC *pll_find(PPC *first,LINETYPE line_number)
  751. #else
  752. PPC *pll_find(first,line_number)
  753. PPC *first;
  754. LINETYPE line_number;
  755. #endif
  756. /***********************************************************************/
  757. {
  758. /*--------------------------- local data ------------------------------*/
  759.  PPC *curr_ppc=NULL;
  760. /*--------------------------- processing ------------------------------*/
  761. #ifdef TRACE
  762.  trace_function("linked.c:    pll_find");
  763. #endif
  764.  curr_ppc = first;
  765.  while (curr_ppc != NULL)
  766.    {
  767.     if (curr_ppc->ppc_line_number == line_number)
  768.       {
  769. #ifdef TRACE
  770.        trace_return();
  771. #endif
  772.        return(curr_ppc);
  773.       }
  774.     curr_ppc = curr_ppc->next;
  775.    }
  776. #ifdef TRACE
  777.  trace_return();
  778. #endif
  779.  return(NULL);
  780. }
  781. /***********************************************************************/
  782. #ifdef HAVE_PROTO
  783. RESERVED *rll_add(RESERVED *first,RESERVED *curr,unsigned short size)
  784. #else
  785. RESERVED *rll_add(first,curr,size)
  786. RESERVED *first;
  787. RESERVED *curr;
  788. unsigned short size;
  789. #endif
  790. /***********************************************************************/
  791. /* Adds a RESERVED to the current linked list after the current member.    */
  792. /* PARAMETERS:                                                         */
  793. /* first      - pointer to first RESERVED in linked list                   */
  794. /* curr       - pointer to current RESERVED in linked list                 */
  795. /* size       - size of a RESERVED item                                    */
  796. /* RETURN:    - pointer to next RESERVED item                              */
  797. /***********************************************************************/
  798. {
  799. /*--------------------------- local data ------------------------------*/
  800.  RESERVED *next=NULL;
  801. /*--------------------------- processing ------------------------------*/
  802. #ifdef TRACE
  803.  trace_function("linked.c:    rll_add");
  804. #endif
  805.  
  806.  if ((next=(RESERVED *)(*the_malloc)(size)) != (RESERVED *)NULL)
  807.    {
  808.     if (curr == NULL)
  809.       {
  810.        first = next;
  811.        next->next = NULL;
  812.       }
  813.     else
  814.       {
  815.        if (curr->next != NULL)
  816.           curr->next->prev = next;
  817.        next->next = curr->next;
  818.        curr->next = next;
  819.       }
  820.     next->prev = curr;
  821.    }
  822. /*---------------------------------------------------------------------*/
  823. /* Ensure all pointers in the structure are set to NULL                */
  824. /*---------------------------------------------------------------------*/
  825.  next->line = NULL;
  826.  next->spec = NULL;
  827. #ifdef TRACE
  828.  trace_return();
  829. #endif
  830.  return(next);
  831. }
  832. /***********************************************************************/
  833. #ifdef HAVE_PROTO
  834. RESERVED *rll_del(RESERVED **first,RESERVED **last,RESERVED *curr,short direction)
  835. #else
  836. RESERVED *rll_del(first,last,curr,direction)
  837. RESERVED **first;
  838. RESERVED **last;
  839. RESERVED *curr;
  840. short direction;
  841. #endif
  842. /***********************************************************************/
  843. {
  844. /*--------------------------- local data ------------------------------*/
  845.  RESERVED *new_curr=NULL;
  846. /*--------------------------- processing ------------------------------*/
  847. #ifdef TRACE
  848.  trace_function("linked.c:    rll_del");
  849. #endif
  850. /*---------------------------------------------------------------------*/
  851. /* Delete the only record                                              */
  852. /*---------------------------------------------------------------------*/
  853.  if (curr->prev == NULL && curr->next == NULL)
  854.    {
  855.     (*the_free)(curr);
  856.     *first = NULL;
  857.     if (last != NULL)
  858.        *last = NULL;
  859. #ifdef TRACE
  860.     trace_return();
  861. #endif
  862.     return(NULL);
  863.    }
  864. /*---------------------------------------------------------------------*/
  865. /* Delete the first record                                             */
  866. /*---------------------------------------------------------------------*/
  867.  if (curr->prev == NULL)
  868.    {
  869.     curr->next->prev = NULL;
  870.     *first = new_curr = curr->next;
  871.     (*the_free)(curr);
  872.     curr = new_curr;
  873. #ifdef TRACE
  874.     trace_return();
  875. #endif
  876.     return(curr);
  877.    }
  878. /*---------------------------------------------------------------------*/
  879. /* Delete the last  record                                             */
  880. /*---------------------------------------------------------------------*/
  881.  if (curr->next == NULL)
  882.    {
  883.     curr->prev->next = NULL;
  884.     new_curr = curr->prev;
  885.     if (last != NULL)
  886.        *last = curr->prev;
  887.     (*the_free)(curr);
  888.     curr = new_curr;
  889. #ifdef TRACE
  890.     trace_return();
  891. #endif
  892.     return(curr);
  893.    }
  894. /*---------------------------------------------------------------------*/
  895. /* All others                                                          */
  896. /*---------------------------------------------------------------------*/
  897.  curr->prev->next = curr->next;
  898.  curr->next->prev = curr->prev;
  899.  if (direction == DIRECTION_FORWARD)
  900.    new_curr = curr->next;
  901.  else
  902.    new_curr = curr->prev;
  903.  
  904.  (*the_free)(curr);
  905.  curr = new_curr;
  906. #ifdef TRACE
  907.  trace_return();
  908. #endif
  909.  return(curr);
  910. }
  911. /***********************************************************************/
  912. #ifdef HAVE_PROTO
  913. RESERVED *rll_free(RESERVED *first)
  914. #else
  915. RESERVED *rll_free(first)
  916. RESERVED *first;
  917. #endif
  918. /***********************************************************************/
  919. /* Free up all allocated memory until the last item in the linked-list */
  920. /* PARAMETERS:                                                         */
  921. /* first      - pointer to first line for the file                     */
  922. /* RETURN:    - NULL                                                   */
  923. /***********************************************************************/
  924. {
  925. /*--------------------------- local data ------------------------------*/
  926.  RESERVED *curr=NULL;
  927.  RESERVED *new_curr=NULL;
  928. /*--------------------------- processing ------------------------------*/
  929. #ifdef TRACE
  930.  trace_function("linked.c:    rll_free");
  931. #endif
  932.  curr = first;
  933.  while (curr != NULL)
  934.    {
  935.     if (curr->line != (CHARTYPE *)NULL)
  936.        (*the_free)(curr->line);
  937.     if (curr->spec != (CHARTYPE *)NULL)
  938.        (*the_free)(curr->spec);
  939.     if (curr->attr != (COLOUR_ATTR *)NULL)
  940.        (*the_free)(curr->attr);
  941.     new_curr = curr->next;
  942.     (*the_free)(curr);
  943.     curr = new_curr;
  944.    }
  945. #ifdef TRACE
  946.  trace_return();
  947. #endif
  948.  return((RESERVED *)NULL);
  949. }
  950. /***********************************************************************/
  951. #ifdef HAVE_PROTO
  952. RESERVED *rll_find(RESERVED *first,short row)
  953. #else
  954. RESERVED *rll_find(first,row)
  955. RESERVED *first;
  956. short row;
  957. #endif
  958. /***********************************************************************/
  959. {
  960. /*--------------------------- local data ------------------------------*/
  961.  RESERVED *curr=NULL;
  962.  short i=0;
  963. /*--------------------------- processing ------------------------------*/
  964. #ifdef TRACE
  965.  trace_function("linked.c:    rll_find");
  966. #endif
  967.  curr = first;
  968.  if (curr != NULL)
  969.     for(i=0;i<row && curr->next != NULL; i++, curr=curr->next);
  970. #ifdef TRACE
  971.  trace_return();
  972. #endif
  973.  return(curr);
  974. }
  975.