zeroSquitto – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 /*************************************************************************/
2 /* Queue implementation. */
3 /*************************************************************************/
4  
5 #include "queue.h"
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
9  
10 /*************************************************************************/
11 /* Constructor with given size. */
12 /*************************************************************************/
13 Queue *queueCreate(int size) {
14 Queue *q = (Queue *)calloc(1, sizeof(Queue));
15 if ((q->store = (char **)calloc(size, sizeof(char *))) == NULL)
16 return NULL;
17 q->size = size;
18 q->head = 0;
19 q->tail = 0;
20 return q;
21 }
22  
23 /*************************************************************************/
24 /* Enqueue an item to the queue. */
25 /*************************************************************************/
26 void queueEnqueue(Queue *q, char *e) {
27 if (q->tail > q->size - 1)
28 q->store = (char **)realloc(q->store, ++q->size * sizeof(char *));
29 q->store[q->tail] = (char *)calloc(strlen(e) + 1, sizeof(char));
30 memcpy(q->store[q->tail], e, strlen(e) + 1);
31 ++q->tail;
32 }
33  
34 /*************************************************************************/
35 /* Dequeue an item off the queue. */
36 /*************************************************************************/
37 char *queueDequeue(Queue *q) {
38 char *e;
39 if (queueIsEmpty(q))
40 return NULL;
41 e = (char *)calloc(strlen(q->store[q->head]) + 1, sizeof(char));
42 memcpy(e, q->store[q->head], strlen(q->store[q->head]) + 1);
43 ++q->head;
44 return e;
45 }
46  
47 /*************************************************************************/
48 /* Clears the queue. */
49 /*************************************************************************/
50 Queue *queueClear(Queue *q) {
51 if (q != NULL)
52 free(q);
53 return queueCreate(1);
54 }
55  
56 /*************************************************************************/
57 /* Prints the items in the queue. */
58 /*************************************************************************/
59 void queuePrint(Queue *q) {
60 int i;
61 if (queueIsEmpty(q)) {
62 printf("Queue is empty.\n");
63 return;
64 }
65 printf("Items: ");
66 i = q->head;
67 do {
68 printf("%s ", q->store[i]);
69 } while (++i < q->tail);
70 printf("\n");
71 }
72