home *** CD-ROM | disk | FTP | other *** search
- /**
- ** Routinen für das dynamische Anlegen von Nodes (GadTools-ListViews)
- ** Holgi am 29-Jul-94 mit MaxonC
- **
- ** Tabsize = 3
- **/
-
- #include <exec/types.h>
- #include <pragma/dos_lib.h>
- #include <pragma/exec_lib.h>
- #include <exec/memory.h>
- #include <string.h>
-
- #include "DynamicNodes.h"
-
- BOOL LoadTextNodes( const STRPTR Name, struct List *L )
- {
- BPTR FH;
- UBYTE Buf[ 130 ];
- size_t Len;
- struct Node *Nd;
- BOOL RC = FALSE;
-
- /* NewList(), will amiga.lib nicht einbinden ;-) */
- L->lh_Head = (struct Node *) &L->lh_Tail;
- L->lh_Tail = NULL;
- L->lh_TailPred = (struct Node *) &L->lh_Head;
- L->lh_Type = NT_UNKNOWN;
-
- if( FH = Open( Name, MODE_OLDFILE ))
- {
- while( FGets( FH, Buf, sizeof( Buf ) - 1 ))
- {
- if( Len = strlen( Buf ))
- if( Buf[ Len - 1 ] == '\n' ) Buf[ Len - 1 ] = '\0';
-
- if(!( Nd = AllocNode( Buf )))
- {
- FreeList( L );
- Close( FH );
- return( FALSE );
- }
- AddTail( L, Nd );
- }
- if( IoErr() ) FreeList( L );
- else RC = TRUE;
-
- Close( FH );
- }
- return( RC );
- }
-
- void FreeList( struct List *L )
- {
- struct Node *Succ, *Nd;
-
- if( L )
- {
- for( Nd = L->lh_Head; Succ = Nd->ln_Succ; Nd = Succ )
- {
- Remove( Nd );
- FreeNode( Nd );
- }
- }
- }
-
- struct Node *AllocNode( const STRPTR Initial )
- {
- UBYTE *Blk;
- size_t Len;
- ULONG TotLen;
-
- Len = strlen( Initial ) + 1;
- TotLen = Len + sizeof( struct Node ) + 4;
- if( Blk = AllocMem( TotLen, MEMF_PUBLIC | MEMF_CLEAR ))
- {
- *((ULONG *) Blk)++ = TotLen;
- ((struct Node *) Blk)->ln_Name = Blk + sizeof( struct Node );
- CopyMem( Initial, Blk + sizeof( struct Node ), Len );
- }
- return( (struct Node *) Blk );
- }
-
- void FreeNode( struct Node *Nd )
- {
- if( Nd ) FreeMem( ((UBYTE *) Nd) - 4, *(((ULONG *) Nd) - 1) );
- }
-
- struct Node *InitNode( struct Node *Nd, const STRPTR Initial )
- {
- size_t Len;
- ULONG TotLen;
-
- Len = strlen( Initial ) + 1;
- TotLen = Len + sizeof( struct Node ) + 4;
-
- if( (TotLen + 7) >> 3 == (*(((ULONG *) Nd) - 1) + 7) >> 3 )
- CopyMem( Initial, (APTR) (Nd + 1), Len );
- else {
- FreeNode( Nd );
- Nd = AllocNode( Initial );
- }
- return( Nd );
- }
-