HuntnGather – Blame information for rev 31
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | /////////////////////////////////////////////////////////////////////////// |
2 | // Copyright (C) 2021 Wizardry and Steamworks - License: MIT // |
||
3 | /////////////////////////////////////////////////////////////////////////// |
||
4 | // // |
||
5 | // stringStack // |
||
6 | // // |
||
7 | // An implementation of a stack of strings. // |
||
8 | // Implemented functions: // |
||
9 | // - push // |
||
10 | // - pop // |
||
11 | // - is empty // |
||
12 | // - count // |
||
13 | // - size // |
||
14 | // - print // |
||
15 | // // |
||
16 | /////////////////////////////////////////////////////////////////////////// |
||
17 | |||
31 | office | 18 | #if defined MWDEBUG |
19 | #include "memwatch.h" |
||
20 | #endif |
||
1 | office | 21 | |
22 | #include <stdio.h> |
||
23 | #include <stdlib.h> |
||
24 | #include <string.h> |
||
25 | #include <dirent.h> |
||
26 | |||
27 | #include <sys/types.h> |
||
28 | #include <sys/stat.h> |
||
29 | |||
30 | #include "StringStack.h" |
||
31 | |||
32 | /* |
||
33 | * Creates a new stringStack with a given size. |
||
34 | */ |
||
35 | stringStack* stringStackCreate(unsigned int size) { |
||
31 | office | 36 | stringStack *s; |
37 | s = malloc(sizeof(*s)); |
||
38 | if ((s->store = malloc(size * sizeof(*s->store))) == NULL) |
||
1 | office | 39 | return NULL; |
40 | s->size = size; |
||
41 | s->top = 0; |
||
42 | return s; |
||
43 | } |
||
44 | |||
45 | /* |
||
46 | * Clears a stringStack and returns a pointer to a new empty stack. |
||
47 | */ |
||
48 | stringStack* stringStackClear(stringStack *s) { |
||
49 | if (s != NULL) |
||
50 | free(s); |
||
51 | return stringStackCreate(1); |
||
52 | } |
||
53 | |||
54 | /* |
||
55 | * Pushes an element onto the stringStack. |
||
56 | */ |
||
57 | void stringStackPush(stringStack *s, char *e) { |
||
58 | if (s->top > s->size - 1) |
||
31 | office | 59 | s->store = realloc(s->store, ++s->size * sizeof(*s->store)); |
60 | s->store[s->top] = malloc((strlen(e) + 1) * sizeof(*s->store[s->top])); |
||
1 | office | 61 | strncpy(s->store[s->top], e, strlen(e) + 1); |
62 | ++s->top; |
||
63 | } |
||
64 | |||
65 | /* |
||
66 | * Pops an element off the stringStack or returns NULL in case the |
||
67 | * stack is empty. |
||
68 | */ |
||
69 | char *stringStackPop(stringStack *s) { |
||
70 | char *e; |
||
71 | if (stringStackIsEmpty(s)) |
||
72 | return NULL; |
||
73 | --s->top; |
||
31 | office | 74 | e = malloc((strlen(s->store[s->top]) + 1) * sizeof(*e)); |
1 | office | 75 | strncpy(e, s->store[s->top], strlen(s->store[s->top]) + 1); |
76 | free(s->store[s->top]); |
||
77 | return e; |
||
78 | } |
||
79 | |||
80 | /* |
||
81 | * Delete a stringStack. |
||
82 | */ |
||
83 | void stringStackDestroy(stringStack *s) { |
||
31 | office | 84 | char *e; |
1 | office | 85 | while(!stringStackIsEmpty(s)) { |
31 | office | 86 | e = stringStackPop(s); |
87 | free(e); |
||
1 | office | 88 | } |
89 | free(s); |
||
90 | } |
||
91 | |||
92 | /* |
||
93 | * Prints out the elements of the stringStack. |
||
94 | */ |
||
95 | void stringStackPrint(stringStack *s) { |
||
96 | int i; |
||
97 | if (stringStackIsEmpty(s)) { |
||
98 | printf("Stack is empty.\n"); |
||
99 | return; |
||
100 | } |
||
101 | printf("Elements in the stack: "); |
||
102 | i = s->top - 1; |
||
103 | do { |
||
104 | printf("%s ", s->store[i]); |
||
105 | } while (--i > -1); |
||
106 | printf("\n"); |
||
107 | } |