stringQueue.c
/*************************************************************************/
/*    Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3    */
/*************************************************************************/
/*                                                                       */
/*  stringQueue                                                          */
/*                                                                       */
/*  An implementation of a queue of strings.                             */
/*  Implemented functions:                                               */
/*      - create                                                         */
/*      - clear                                                          */
/*      - is empty                                                       */
/*      - size                                                           */
/*      - count                                                          */
/*      - enqueue                                                        */
/*      - dequeue                                                        */
/*      - print                                                          */
/*                                                                       */
/*************************************************************************/
 
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
#include "stringQueue.h"
 
/*
 * Creates a new stringQueue with a given length.
 */
stringQueue* stringQueueCreate(int size) {
    stringQueue *q = (stringQueue*)calloc(1, sizeof(stringQueue));
    if ((q->store = (char**)calloc(size, sizeof(char **))) == NULL)
        return NULL;
    q->size = size;
    q->front = 0;
    q->rear = 0;
    return q;
}
 
/*
 * Clears an stringQueue and returns a pointer to a new empty queue.
 */
stringQueue* stringQueueClear(stringQueue *q) {
    if (q != NULL)
        free(q);
    return stringQueueCreate(1);
}
 
/*
 * Takes as parameter an stringQueue and returns 1 if the queue is empty
 * or 0 if the queue is not empty.
 */
int stringQueueIsEmpty(stringQueue *q) {
    return q->rear == q->front;
}
 
/*
 * Returns allocated queue size (not the number of elements).
 */
int stringQueueSize(stringQueue *q) {
    return q->size;
}
 
/*
 * Returns the number of elements in stringQueue.
 */
int stringQueueCount(stringQueue *q) {
    return q->rear - q->front;
}
 
/*
 * Enqueues an element to the stringQueue.
 */
void stringQueueEnqueue(stringQueue *q, char *e) {
    if (q->rear > q->size - 1)
        q->store = (char**)realloc(q->store, ++q->size * sizeof(char **));
    q->store[q->rear] = (char*)calloc(strlen(e) + 1, sizeof(char *));
    memcpy(q->store[q->rear], e, strlen(e) + 1);
    ++q->rear;
}
 
/*
 * Dequeues an element from the stringQueue or returns -1 in case the
 * queue is empty.
 */
char *stringQueueDequeue(stringQueue *q) {
    char *e = (char*)calloc(strlen(q->store[q->front]), sizeof(char *));
    if (stringQueueIsEmpty(q)) {
        free(e);
        return NULL;
    }
    memcpy(e, q->store[q->front], strlen(q->store[q->front]) + 1);
    ++q->front;
    return e;
}
 
/*
 * Prints out the elements of the stringQueue.
 */
void stringQueuePrint(stringQueue *q) {
    int i;
    if (stringQueueIsEmpty(q)) {
        printf("Queue is empty.\n");
        return;
    }
    printf("Elements in the queue: ");
    i = q->front;
    do {
        printf("%s ", q->store[i]);
    } while (++i < q->rear);
    printf("\n");
}