HuntnGather – Diff between revs 32 and 43
?pathlinks?
Rev 32 | Rev 43 | |||
---|---|---|---|---|
Line 29... | Line 29... | |||
29 | * Creates a new stringStack with a given size. |
29 | * Creates a new stringStack with a given size. |
|
30 | */ |
30 | */ |
|
31 | stringStack* stringStackCreate(unsigned int size) { |
31 | stringStack* stringStackCreate(unsigned int size) { |
|
32 | stringStack *s; |
32 | stringStack *s; |
|
33 | s = malloc(sizeof(*s)); |
33 | s = malloc(sizeof(*s)); |
|
34 | if ((s->store = malloc(size * sizeof(*s->store))) == NULL) |
34 | if ((s->store = malloc(size * sizeof(*s->store))) == NULL) { |
|
- | 35 | free(s); |
||
- | 36 | s = NULL; |
||
35 | return NULL; |
37 | return NULL; |
|
- | 38 | } |
||
36 | s->size = size; |
39 | s->size = size; |
|
37 | s->top = 0; |
40 | s->top = 0; |
|
38 | return s; |
41 | return s; |
|
39 | } |
42 | } |
|
Line 40... | Line 43... | |||
40 | |
43 | |
|
41 | /* |
44 | /* |
|
42 | * Clears a stringStack and returns a pointer to a new empty stack. |
45 | * Clears a stringStack and returns a pointer to a new empty stack. |
|
43 | */ |
46 | */ |
|
44 | stringStack* stringStackClear(stringStack *s) { |
47 | stringStack* stringStackClear(stringStack *s) { |
|
45 | if (s != NULL) |
48 | if (s != NULL) { |
|
- | 49 | free(s); |
||
- | 50 | s = NULL; |
||
46 | free(s); |
51 | } |
|
47 | return stringStackCreate(1); |
52 | return stringStackCreate(1); |
|
Line 48... | Line 53... | |||
48 | } |
53 | } |
|
49 | |
54 | |
|
Line 68... | Line 73... | |||
68 | return NULL; |
73 | return NULL; |
|
69 | --s->top; |
74 | --s->top; |
|
70 | e = malloc((strlen(s->store[s->top]) + 1) * sizeof(*e)); |
75 | e = malloc((strlen(s->store[s->top]) + 1) * sizeof(*e)); |
|
71 | strncpy(e, s->store[s->top], strlen(s->store[s->top]) + 1); |
76 | strncpy(e, s->store[s->top], strlen(s->store[s->top]) + 1); |
|
72 | free(s->store[s->top]); |
77 | free(s->store[s->top]); |
|
- | 78 | s->store[s->top] = NULL; |
||
73 | return e; |
79 | return e; |
|
74 | } |
80 | } |
|
Line 75... | Line 81... | |||
75 | |
81 | |
|
76 | /* |
82 | /* |
|
Line 79... | Line 85... | |||
79 | void stringStackDestroy(stringStack *s) { |
85 | void stringStackDestroy(stringStack *s) { |
|
80 | char *e; |
86 | char *e; |
|
81 | while(!stringStackIsEmpty(s)) { |
87 | while(!stringStackIsEmpty(s)) { |
|
82 | e = stringStackPop(s); |
88 | e = stringStackPop(s); |
|
83 | free(e); |
89 | free(e); |
|
- | 90 | e = NULL; |
||
84 | } |
91 | } |
|
85 | free(s); |
92 | free(s); |
|
- | 93 | s = NULL; |
||
86 | } |
94 | } |
|
Line 87... | Line 95... | |||
87 | |
95 | |
|
88 | /* |
96 | /* |
|
89 | * Prints out the elements of the stringStack. |
97 | * Prints out the elements of the stringStack. |