/* Stacks with Arrays Lab
   Version 4:  Stack contains array of strings
               push copies string to stack
*/

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

#include "stack-buffer.h"
#include "stack.h"

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

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


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 (char *)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 
     return null if invalid index specified */
  assert ( (index >= 1) && (index <= stack->topPosition+1));

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