| 1 | /*************************************************************************** |
| 2 | stack.cpp - description |
| 3 | ------------------- |
| 4 | begin : Sat Mar 16 2002 |
| 5 | copyright : (C) 2002 by Michael Bridak |
| 6 | email : [email protected] |
| 7 | $Id: stack.cpp,v 1.5 2003/04/14 05:51:04 mbridak Exp $ |
| 8 | ***************************************************************************/ |
| 9 | |
| 10 | /*************************************************************************** |
| 11 | * * |
| 12 | * This program is free software; you can redistribute it and/or modify * |
| 13 | * it under the terms of the GNU General Public License as published by * |
| 14 | * the Free Software Foundation; either version 2 of the License. * |
| 15 | * . * |
| 16 | * * |
| 17 | ***************************************************************************/ |
| 18 | |
| 19 | #include <stdlib.h> |
| 20 | #include "stack.h" |
| 21 | |
| 22 | Stack::Stack() |
| 23 | { |
| 24 | counter = 0; |
| 25 | stack = NULL; |
| 26 | top_of_stack = NULL; |
| 27 | } |
| 28 | |
| 29 | |
| 30 | Stack::~Stack() |
| 31 | { |
| 32 | STACK_DATA *my_stack, *previous; |
| 33 | |
| 34 | my_stack = stack; |
| 35 | while (my_stack) |
| 36 | { |
| 37 | previous = my_stack; |
| 38 | my_stack = (STACK_DATA *) my_stack->next; |
| 39 | free(previous); |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | int Stack::RegisterEvent(int direction, float signal, int ship_type) |
| 44 | { |
| 45 | STACK_DATA *current; |
| 46 | |
| 47 | current = (STACK_DATA *) calloc(1, sizeof(STACK_DATA)); |
| 48 | if (! current) |
| 49 | return FALSE; |
| 50 | |
| 51 | current->direction = direction; |
| 52 | current->signal_strength = signal; |
| 53 | current->ship_type = ship_type; |
| 54 | if (top_of_stack) |
| 55 | { |
| 56 | top_of_stack->next = current; |
| 57 | top_of_stack = current; |
| 58 | } |
| 59 | else // nothing in stack |
| 60 | { |
| 61 | top_of_stack = stack = current; |
| 62 | } |
| 63 | counter++; |
| 64 | return TRUE; |
| 65 | } |
| 66 | |
| 67 | int Stack::GetEvent(int event, int *rdirection, float *rsignal, int *ship_type) |
| 68 | { |
| 69 | int index = 1; |
| 70 | STACK_DATA *current; |
| 71 | |
| 72 | current = stack; |
| 73 | while ( (current) && (index < event) ) |
| 74 | { |
| 75 | current = (STACK_DATA *) current->next; |
| 76 | index++; |
| 77 | } |
| 78 | |
| 79 | if ( (index == event) && (current) ) |
| 80 | { |
| 81 | *rdirection = current->direction; |
| 82 | *ship_type = current->ship_type; |
| 83 | *rsignal = current->signal_strength; |
| 84 | return TRUE; |
| 85 | } |
| 86 | else |
| 87 | return FALSE; |
| 88 | } |
| 89 | |
| 90 | int Stack::GetCount() |
| 91 | { |
| 92 | return counter; |
| 93 | } |
| 94 | |
| 95 | void Stack::AdvanceSonarHistory() |
| 96 | { |
| 97 | STACK_DATA *current, *previous; |
| 98 | current = stack; |
| 99 | while (current) |
| 100 | { |
| 101 | previous = current; |
| 102 | current = (STACK_DATA *) current->next; |
| 103 | free(previous); |
| 104 | } |
| 105 | stack = top_of_stack = NULL; |
| 106 | counter = 0; |
| 107 | } |
| 108 | |
| 109 |