Design Pattern Examples
Overview of object-oriented design patterns
conststringlist.c
Go to the documentation of this file.
1
5
6#include <stdlib.h>
7#include <string.h>
8#include <memory.h>
9
10#include "conststringlist.h"
11
13// ConstStringList_Initialize()
16{
17 if (stringList != NULL)
18 {
19 stringList->strings = NULL;
20 stringList->strings_count = 0;
21 stringList->allocated_count = 0;
22 }
23}
24
26// ConstStringList_Clear()
29{
30 if (stringList != NULL)
31 {
32 if (stringList->strings != NULL && stringList->strings_count != 0)
33 {
34 // The strings in the list are constant and are not supposed to be
35 // deleted. Just forget they existed.
36 free(stringList->strings);
38 }
39 }
40}
41
43// ConstStringList_AddString()
45bool ConstStringList_AddString(ConstStringList* stringList, const char* string)
46{
47 bool stringAdded = false;
48
49 if (stringList != NULL && string != NULL)
50 {
51 const char** new_list = NULL;
52 if (stringList->strings == NULL)
53 {
54 new_list = malloc(sizeof(const char*));
55 stringList->allocated_count = 1;
56 }
57 else if (stringList->strings_count < stringList->allocated_count)
58 {
59 new_list = stringList->strings;
60 }
61 else
62 {
63 size_t newCount = stringList->allocated_count + 1;
64 new_list = realloc(stringList->strings, newCount * sizeof(const char*));
65 stringList->allocated_count = newCount;
66 }
67
68 if (new_list != NULL)
69 {
70 stringList->strings = new_list;
71 stringList->strings[stringList->strings_count] = string;
72 stringList->strings_count++;
73 stringAdded = true;
74 }
75 }
76 return stringAdded;
77}
78
80// ConstStringList_AddStrings()
82bool ConstStringList_AddStrings(ConstStringList* stringList, const char** strings, size_t numStrings)
83{
84 bool stringsAdded = false;
85
86 if (stringList != NULL && strings != NULL)
87 {
88 const char** new_list = NULL;
89 if (stringList->strings == NULL)
90 {
91 new_list = calloc(numStrings, sizeof(const char*));
92 stringList->allocated_count = numStrings;
93 }
94 else if ((stringList->strings_count + numStrings) < stringList->allocated_count)
95 {
96 new_list = stringList->strings;
97 }
98 else
99 {
100 size_t newCount = stringList->allocated_count + numStrings;
101 new_list = realloc(stringList->strings, newCount * sizeof(const char*));
102 stringList->allocated_count = newCount;
103 }
104 if (new_list != NULL)
105 {
106 stringsAdded = true;
107 stringList->strings = new_list;
108 size_t offset = stringList->strings_count;
109 for (size_t index = 0; index < numStrings; index++)
110 {
111 stringList->strings[offset + index] = strings[index];
112 stringList->strings_count++;
113 }
114 }
115 }
116
117 return stringsAdded;
118}
119
121// ConstStringList_Remove()
123void ConstStringList_Remove(ConstStringList* stringList, int removeIndex)
124{
125 if (stringList != NULL && stringList->strings != NULL)
126 {
127 if (removeIndex >= 0 && (size_t)removeIndex < stringList->strings_count)
128 {
129 for (size_t stringIndex = removeIndex; stringIndex < stringList->allocated_count - 1; stringIndex++)
130 {
131 stringList->strings[stringIndex] = stringList->strings[stringIndex + 1];
132 }
133 stringList->strings_count--;
134 }
135 }
136}
137
139// ConstStringList_Find()
141int ConstStringList_Find(ConstStringList* stringList, const char* string)
142{
143 int foundIndex = -1;
144
145 if (stringList != NULL && string != NULL)
146 {
147 for (size_t index = 0; index < stringList->strings_count; index++)
148 {
149 if (strcmp(string, stringList->strings[index]) == 0)
150 {
151 foundIndex = (int)index;
152 break;
153 }
154 }
155 }
156
157 return foundIndex;
158}
159
161// ConstStringList_Find()
164{
165 bool matched = false;
166
167 if (left != NULL && right != NULL)
168 {
169 if (left->strings_count == right->strings_count)
170 {
171 matched = true;
172 for (size_t index = 0; index < left->strings_count; index++)
173 {
174 int found_index = ConstStringList_Find(right, left->strings[index]);
175 if (found_index == -1)
176 {
177 matched = false;
178 break;
179 }
180 }
181 }
182 }
183
184 return matched;
185}
int ConstStringList_Find(ConstStringList *stringList, const char *string)
Search the given string list for the given string. If found, return the index of the found string.
void ConstStringList_Initialize(ConstStringList *stringList)
Initialize the given string list.
bool ConstStringList_AddStrings(ConstStringList *stringList, const char **strings, size_t numStrings)
Add an array of strings to the given string list.
bool ConstStringList_AreListsEqual(ConstStringList *left, ConstStringList *right)
Compare two strings lists to determine if they have the same contents.
bool ConstStringList_AddString(ConstStringList *stringList, const char *string)
Add a string to the given string list.
void ConstStringList_Remove(ConstStringList *stringList, int removeIndex)
Remove the specified string from the given string list.
void ConstStringList_Clear(ConstStringList *stringList)
Clear the specified string list. The strings in the list are left alone, but the list itself is delet...
Declaration of the ConstStringList structure and supporting functions to work with a list of constant...
Represents a list of pointers to zero-terminated strings that are to remain constant and never delete...
size_t strings_count
Number of strings in the strings list.
const char ** strings
Pointer to an array of zero-terminated string pointers. These strings are constant and will not be du...
size_t allocated_count
The number of strings that can be held in the strings list.