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