HuntnGather – Blame information for rev 30
?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 | |||
19 | #include <stdio.h> |
||
20 | #include <stdlib.h> |
||
21 | #include <string.h> |
||
22 | #include <dirent.h> |
||
23 | |||
24 | #include <sys/types.h> |
||
25 | #include <sys/stat.h> |
||
26 | |||
27 | #include "StringStack.h" |
||
28 | |||
29 | /* |
||
30 | * Creates a new stringStack with a given size. |
||
31 | */ |
||
32 | stringStack* stringStackCreate(unsigned int size) { |
||
19 | office | 33 | stringStack *s = (stringStack*)malloc(sizeof(stringStack)); |
34 | if ((s->store = (char**)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) |
||
19 | office | 55 | s->store = (char**)realloc(s->store, ++s->size * sizeof(*s->store)); |
56 | s->store[s->top] = (char*)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; |
||
19 | office | 70 | e = (char *)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) { |
||
80 | while(!stringStackIsEmpty(s)) { |
||
81 | stringStackPop(s); |
||
82 | } |
||
83 | free(s); |
||
84 | } |
||
85 | |||
86 | /* |
||
87 | * Prints out the elements of the stringStack. |
||
88 | */ |
||
89 | void stringStackPrint(stringStack *s) { |
||
90 | int i; |
||
91 | if (stringStackIsEmpty(s)) { |
||
92 | printf("Stack is empty.\n"); |
||
93 | return; |
||
94 | } |
||
95 | printf("Elements in the stack: "); |
||
96 | i = s->top - 1; |
||
97 | do { |
||
98 | printf("%s ", s->store[i]); |
||
99 | } while (--i > -1); |
||
100 | printf("\n"); |
||
101 | } |