HuntnGather
/trunk/HuntnGather/Gather/StringStack.c |
@@ -15,6 +15,9 @@ |
// // |
/////////////////////////////////////////////////////////////////////////// |
|
#if defined MWDEBUG |
#include "memwatch.h" |
#endif |
|
#include <stdio.h> |
#include <stdlib.h> |
@@ -30,8 +33,9 @@ |
* Creates a new stringStack with a given size. |
*/ |
stringStack* stringStackCreate(unsigned int size) { |
stringStack *s = (stringStack*)malloc(sizeof(stringStack)); |
if ((s->store = (char**)malloc(size * sizeof(*s->store))) == NULL) |
stringStack *s; |
s = malloc(sizeof(*s)); |
if ((s->store = malloc(size * sizeof(*s->store))) == NULL) |
return NULL; |
s->size = size; |
s->top = 0; |
@@ -52,8 +56,8 @@ |
*/ |
void stringStackPush(stringStack *s, char *e) { |
if (s->top > s->size - 1) |
s->store = (char**)realloc(s->store, ++s->size * sizeof(*s->store)); |
s->store[s->top] = (char*)malloc((strlen(e) + 1) * sizeof(*s->store[s->top])); |
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; |
} |
@@ -67,7 +71,7 @@ |
if (stringStackIsEmpty(s)) |
return NULL; |
--s->top; |
e = (char *)malloc((strlen(s->store[s->top]) + 1) * sizeof(*e)); |
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]); |
return e; |
@@ -77,8 +81,10 @@ |
* Delete a stringStack. |
*/ |
void stringStackDestroy(stringStack *s) { |
char *e; |
while(!stringStackIsEmpty(s)) { |
stringStackPop(s); |
e = stringStackPop(s); |
free(e); |
} |
free(s); |
} |