home *** CD-ROM | disk | FTP | other *** search
/ Amiga Inside! / Amiga FD Inside (1995)(Ultramax).iso / forumamiga / programme / dynamicnodes / dynamicnodes.c next >
Encoding:
C/C++ Source or Header  |  1994-07-29  |  2.0 KB  |  105 lines

  1. /**
  2.  **  Routinen für das dynamische Anlegen von Nodes (GadTools-ListViews)
  3.  **  Holgi am 29-Jul-94 mit MaxonC
  4.  **
  5.  **  Tabsize = 3
  6.  **/
  7.  
  8. #include <exec/types.h>
  9. #include <pragma/dos_lib.h>
  10. #include <pragma/exec_lib.h>
  11. #include <exec/memory.h>
  12. #include <string.h>
  13.  
  14. #include "DynamicNodes.h"
  15.  
  16. BOOL LoadTextNodes( const STRPTR Name, struct List *L )
  17. {
  18.     BPTR            FH;
  19.     UBYTE            Buf[ 130 ];
  20.     size_t        Len;
  21.     struct Node *Nd;
  22.     BOOL            RC = FALSE;
  23.  
  24.     /* NewList(), will amiga.lib nicht einbinden ;-) */
  25.     L->lh_Head        = (struct Node *) &L->lh_Tail;
  26.     L->lh_Tail        = NULL;
  27.     L->lh_TailPred    = (struct Node *) &L->lh_Head;
  28.     L->lh_Type        = NT_UNKNOWN;
  29.  
  30.     if( FH = Open( Name, MODE_OLDFILE ))
  31.     {
  32.         while( FGets( FH, Buf, sizeof( Buf ) - 1 ))
  33.         {
  34.             if( Len = strlen( Buf ))
  35.                 if( Buf[ Len - 1 ] == '\n' ) Buf[ Len - 1 ] = '\0';
  36.  
  37.             if(!( Nd = AllocNode( Buf )))
  38.             {
  39.                 FreeList( L );
  40.                 Close( FH );
  41.                 return( FALSE );
  42.             }
  43.             AddTail( L, Nd );
  44.         }
  45.         if( IoErr() ) FreeList( L );
  46.         else              RC = TRUE;
  47.  
  48.         Close( FH );
  49.     }
  50.     return( RC );
  51. }
  52.  
  53. void FreeList( struct List *L )
  54. {
  55.     struct Node *Succ, *Nd;
  56.  
  57.     if( L )
  58.     {
  59.         for( Nd = L->lh_Head; Succ = Nd->ln_Succ; Nd = Succ )
  60.         {
  61.             Remove( Nd );
  62.             FreeNode( Nd );
  63.         }
  64.     }
  65. }
  66.  
  67. struct Node *AllocNode( const STRPTR Initial )
  68. {
  69.     UBYTE  *Blk;
  70.     size_t Len;
  71.     ULONG  TotLen;
  72.  
  73.     Len      = strlen( Initial ) + 1;
  74.     TotLen  = Len + sizeof( struct Node ) + 4;
  75.     if( Blk = AllocMem( TotLen, MEMF_PUBLIC | MEMF_CLEAR ))
  76.     {
  77.         *((ULONG *) Blk)++             = TotLen;
  78.         ((struct Node *) Blk)->ln_Name = Blk + sizeof( struct Node );
  79.         CopyMem( Initial, Blk + sizeof( struct Node ), Len );
  80.     }
  81.     return( (struct Node *) Blk );
  82. }
  83.  
  84. void FreeNode( struct Node *Nd )
  85. {
  86.     if( Nd ) FreeMem( ((UBYTE *) Nd) - 4, *(((ULONG *) Nd) - 1) );
  87. }
  88.  
  89. struct Node *InitNode( struct Node *Nd, const STRPTR Initial )
  90. {
  91.     size_t Len;
  92.     ULONG  TotLen;
  93.     
  94.     Len     = strlen( Initial ) + 1;
  95.     TotLen = Len + sizeof( struct Node ) + 4;
  96.  
  97.     if( (TotLen + 7) >> 3 == (*(((ULONG *) Nd) - 1) + 7) >> 3 )
  98.         CopyMem( Initial, (APTR) (Nd + 1), Len );
  99.     else {
  100.         FreeNode( Nd );
  101.         Nd = AllocNode( Initial );
  102.     }
  103.     return( Nd );
  104. }
  105.