home *** CD-ROM | disk | FTP | other *** search
- unit STI_STAK; {generic stack unit }
- {$R-,V-,B-,D-}
- Interface
-
- Const
- MAXSTACK = 16383; {max size of stack }
-
- STACK_ALL_OK = 0; {no problems }
- STACK_OVERFLOW = 1; {underflow }
- STACK_UNDERFLOW = 2; {overflow }
- STACK_NO_MEMORY = 3; {no memory }
-
- Type
- STI_StackRec = array[1..MAXSTACK] of pointer; {dummy record }
- STI_StackPtr = ^STI_Stack; {pointer to a stack }
- STI_Stack = record {the actual stack record }
- Stack : ^STI_StackRec; {the stack }
- Size : word; {stack size }
- StackP : word; {stack pointer }
- end;
-
- Var
- STI_StackError : byte; {stack error }
-
- procedure STI_Create_Stack(Var NewStack : STI_StackPtr; Size : word);
- procedure STI_Destroy_Stack(Var OldStack : STI_StackPtr);
- procedure STI_Push(Var Stack : STI_StackPtr; Var Data; Size : word);
- procedure STI_Pop(Var Stack : STI_StackPtr; Var Data; Size : word);
-
- Implementation
-
- {---------------------------------------------------------------------------}
-
- procedure STI_Create_Stack(Var NewStack : STI_StackPtr; Size : word);
-
- Var
- Loop : word; {general loop variable }
-
- begin
- STI_StackError := STACK_ALL_OK; {default to all ok }
- if MaxAvail < (sizeof(STI_Stack) + (sizeof(pointer)*Size)) then
- begin
- STI_StackError := STACK_NO_MEMORY; {can't alloacte the stack }
- Exit; {return an error }
- end;
- new(NewStack); {get the stack }
- NewStack^.Size := Size; {set the size }
- NewStack^.StackP := 1; {first position }
- getmem(NewStack^.Stack,sizeof(Pointer)*Size); {get the stack memory }
- for Loop := 1 to Size do {loop over entries }
- begin
- NewStack^.Stack^[Loop] := NIL; {and set them to nil }
- end;
- end;
-
- {---------------------------------------------------------------------------}
-
- procedure STI_Destroy_Stack(Var OldStack : STI_StackPtr);
-
- begin
- if OldStack <> NIL then {check for nill pointer }
- begin
- freemem(OldStack^.Stack,sizeof(Pointer)*OldStack^.Size); {free stack }
- dispose(OldStack); {kill the stack record }
- end;
- STI_StackError := STACK_ALL_OK; {no errors here }
- end;
-
- {---------------------------------------------------------------------------}
-
- procedure STI_Push(Var Stack : STI_StackPtr; Var Data; Size : word);
-
- begin
- STI_StackError := STACK_ALL_OK; {default to all ok }
- if MaxAvail < Size then {check the memory }
- begin
- STI_StackError := STACK_NO_MEMORY; {not enough, return an error }
- Exit;
- end;
- if Stack^.StackP >= Stack^.Size then {check for overflow }
- begin
- STI_StackError := STACK_OVERFLOW; {overflow lads !!! }
- Stack^.StackP := Stack^.Size;
- Exit;
- end;
- getmem(Stack^.Stack^[Stack^.StackP],Size); {get the data memory }
- move(Data,Stack^.Stack^[Stack^.StackP]^,Size); {move the data over }
- inc(Stack^.StackP); {increment stack pointer }
- end;
-
- {---------------------------------------------------------------------------}
-
- procedure STI_Pop(Var Stack : STI_StackPtr; Var Data; Size : word);
-
- begin
- dec(Stack^.StackP); {because this points to NEXT }
- if Stack^.StackP < 1 then {check for underflow }
- begin
- STI_StackError := STACK_UNDERFlOW; {undeflow lasses !!!! }
- Stack^.StackP := 1;
- Exit;
- end;
- STI_StackError := STACK_ALL_OK; {everything ok }
- move(Stack^.Stack^[Stack^.StackP]^,Data,Size); {copy the data }
- freemem(Stack^.Stack^[Stack^.StackP],Size); {get rid of the memory }
- end;
-
- {---------------------------------------------------------------------------}
-
- begin
- STI_StackError := STACK_ALL_OK; {default to all ok }
- end.