HuntnGather – Diff between revs 16 and 19

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