/* Stacks with Arrays Lab
   Version 1:  Stack contains pointers to strings
*/

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <stdbool.h>
#include <assert.h>

#include "stack-pointer.h" /* Include definition of reference-based stack */
#include "stack.h"

#include "stack.c" /* Include the common/shared function implementations */

bool
push (struct string_stack * stack, char * item)
/* post-conditions:  item string is added to stack and returns true
                     or else stack is unmodified and returns false 
                     (when stack is full).
*/
{
  /* return false if stack full */
  if (isFull(stack))
    return false;
  
  /* add item to stack */
  (stack->topPosition) ++;
  stack->items[stack->topPosition] = item;
  return true;
} // push


char *
pop (struct string_stack * stack) 
/* pre-condition: stack is not empty
   post-condition: top element is removed from stack */
{
  assert ( !isEmpty(stack) );
  
  char * item;  /* string [base address] to return */
    
  /* remove item from stack */
  item = stack->items[stack->topPosition];
  (stack->topPosition) --;
  return item;
} // pop


char *
top (const struct string_stack * stack) 
/* pre-condition: stack is not empty
   post-condition: stack is unmodified
   produces: reference to item at top of stack */
{  
  assert ( !isEmpty(stack) );
  
  return stack->items[stack->topPosition];
} // top


char *
get (const struct string_stack * stack, int index)
{
  /* assume nth indexing starts at 1
     translate to 0-indexed arrays, counting from topPosition 
     abort if invalid index specified */
  assert ( (index >= 1) && (index <= stack->topPosition+1));

  return stack->items[(stack->topPosition) - index + 1];
} // get
