HuntnGather – Rev 45

Subversion Repositories:
Rev:
///////////////////////////////////////////////////////////////////////////
//    Copyright (C) 2021 Wizardry and Steamworks - License: MIT          //
///////////////////////////////////////////////////////////////////////////
//                                                                       //
//   stringStack                                                         //
//                                                                       //
//   An implementation of a stack of strings.                            //
//   Implemented functions:                                              //
//       - push                                                          //
//       - pop                                                           //
//       - is empty                                                      //
//       - count                                                         //
//       - size                                                          //
//       - print                                                         //
//                                                                       //
///////////////////////////////////////////////////////////////////////////

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>

#include <sys/types.h>
#include <sys/stat.h>

#include "StringStack.h"

/*
    * Creates a new stringStack with a given size.
    */
stringStack* stringStackCreate(unsigned int size) {
    stringStack *s;
    s = malloc(sizeof(*s));
    if ((s->store = malloc(size * sizeof(*s->store))) == NULL) {
        free(s);
        s = NULL;
        return NULL;
    }
    s->size = size;
    s->top = 0;
    return s;
}

/*
    * Clears a stringStack and returns a pointer to a new empty stack.
    */
stringStack* stringStackClear(stringStack *s) {
    if (s != NULL) {
        free(s);
        s = NULL;
    }
    return stringStackCreate(1);
}

/*
    * Pushes an element onto the stringStack.
    */
void stringStackPush(stringStack *s, char *e) {
    if (s->top > s->size - 1)
        s->store = realloc(s->store, ++s->size * sizeof(*s->store));
    s->store[s->top] = malloc((strlen(e) + 1) * sizeof(*s->store[s->top]));
    strncpy(s->store[s->top], e, strlen(e) + 1);
    ++s->top;
}

/*
    * Pops an element off the stringStack or returns NULL in case the
    * stack is empty.
    */
char *stringStackPop(stringStack *s) {
    char *e;
    if (stringStackIsEmpty(s))
        return NULL;
    --s->top;
    e = malloc((strlen(s->store[s->top]) + 1) * sizeof(*e));
    strncpy(e, s->store[s->top], strlen(s->store[s->top]) + 1);
    free(s->store[s->top]);
    s->store[s->top] = NULL;
    return e;
}

/*
         * Delete a stringStack.
         */
void stringStackDestroy(stringStack *s) {
        char *e;
        while(!stringStackIsEmpty(s)) {
                e = stringStackPop(s);
                free(e);
                e = NULL;
        }
        free(s);
        s = NULL;
}

/*
    * Prints out the elements of the stringStack.
    */
void stringStackPrint(stringStack *s) {
    int i;
    if (stringStackIsEmpty(s)) {
        printf("Stack is empty.\n");
        return;
    }
    printf("Elements in the stack: ");
    i = s->top - 1;
    do {
        printf("%s ", s->store[i]);
    } while (--i > -1);
    printf("\n");
}