Design Pattern Examples
Overview of object-oriented design patterns
stringlist.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 "strdup.h"
11#include "stringlist.h"
12
13
15// StringList_Initialize()
18{
19 if (stringList != NULL)
20 {
21 stringList->strings = NULL;
22 stringList->strings_count = 0;
23 stringList->allocated_count = 0;
24 }
25}
26
28// StringList_Clear()
31{
32 if (stringList != NULL)
33 {
34 if (stringList->strings != NULL && stringList->strings_count != 0)
35 {
36 for (size_t index = 0; index < stringList->strings_count; index++)
37 {
38 free((void *)stringList->strings[index]);
39 }
40 free(stringList->strings);
41 StringList_Initialize(stringList);
42 }
43 }
44}
45
47// StringList_AddString()
49bool StringList_AddString(StringList* stringList, const char* string)
50{
51 bool stringAdded = false;
52
53 if (stringList != NULL && string != NULL)
54 {
55 const char** new_list = NULL;
56 if (stringList->strings == NULL)
57 {
58 new_list = malloc(sizeof(const char*));
59 stringList->allocated_count = 1;
60 }
61 else if (stringList->strings_count < stringList->allocated_count)
62 {
63 new_list = stringList->strings;
64 }
65 else
66 {
67 size_t newCount = stringList->allocated_count + 1;
68 new_list = realloc(stringList->strings, newCount * sizeof(const char*));
69 stringList->allocated_count = newCount;
70 }
71
72 if (new_list != NULL)
73 {
74 stringList->strings = new_list;
75 char* newString = STRDUP(string);
76 if (newString != NULL)
77 {
78 stringList->strings[stringList->strings_count] = newString;
79 stringList->strings_count++;
80 stringAdded = true;
81 }
82 }
83 }
84 return stringAdded;
85}
86
88// StringList_AddStrings()
90bool StringList_AddStrings(StringList* stringList, const char** strings, size_t numStrings)
91{
92 bool stringsAdded = false;
93
94 if (stringList != NULL && strings != NULL)
95 {
96 const char** new_list = NULL;
97 if (stringList->strings == NULL)
98 {
99 new_list = calloc(numStrings, sizeof(const char*));
100 stringList->allocated_count = numStrings;
101 }
102 else if ((stringList->strings_count + numStrings) < stringList->allocated_count)
103 {
104 new_list = stringList->strings;
105 }
106 else
107 {
108 size_t newCount = stringList->allocated_count + numStrings;
109 new_list = realloc(stringList->strings, newCount * sizeof(const char*));
110 stringList->allocated_count = newCount;
111 }
112 if (new_list != NULL)
113 {
114 stringsAdded = true;
115 stringList->strings = new_list;
116 size_t offset = stringList->strings_count;
117 for (size_t index = 0; index < numStrings; index++)
118 {
119 char *newString = STRDUP(strings[index]);
120 if (newString == NULL)
121 {
122 stringsAdded = false;
123 break;
124 }
125 stringList->strings[offset + index] = newString;
126 stringList->strings_count++;
127 }
128 }
129 }
130
131 return stringsAdded;
132}
133
135// StringList_Remove()
137void StringList_Remove(StringList* stringList, int removeIndex)
138{
139 if (stringList != NULL && stringList->strings != NULL)
140 {
141 if (removeIndex >= 0 && (size_t)removeIndex < stringList->strings_count)
142 {
143 const char* stringToRemove = stringList->strings[removeIndex];
144 for (size_t stringIndex = removeIndex; stringIndex < stringList->allocated_count - 1; stringIndex++)
145 {
146 stringList->strings[stringIndex] = stringList->strings[stringIndex + 1];
147 }
148 stringList->strings_count--;
149 free((char*)stringToRemove);
150 }
151 }
152}
153
155// StringList_Find()
157int StringList_Find(StringList* stringList, const char* string)
158{
159 int foundIndex = -1;
160
161 if (stringList != NULL && string != NULL)
162 {
163 for (size_t index = 0; index < stringList->strings_count; index++)
164 {
165 if (strcmp(string, stringList->strings[index]) == 0)
166 {
167 foundIndex = (int)index;
168 break;
169 }
170 }
171 }
172
173 return foundIndex;
174}
Declaration of the StringList and StringListW typedefs for declaring a vector of strings.
std::vector< std::string > StringList
Typedef for a vector of std::string.
Declaration of the STRDUP macro that hides the differences between how strdup() is declared in differ...
#define STRDUP
Define STRDUP to be the operating system-specific version of strdup().
Definition: strdup.h:17
void StringList_Clear(StringList *stringList)
Clear the specified string list. All strings in the list are released. The string list can then be us...
Definition: stringlist.c:30
bool StringList_AddStrings(StringList *stringList, const char **strings, size_t numStrings)
Add an array of strings to the given string list.
Definition: stringlist.c:90
int StringList_Find(StringList *stringList, const char *string)
Search the given string list for the given string. If found, return the index of the found string.
Definition: stringlist.c:157
void StringList_Initialize(StringList *stringList)
Initialize the given string list.
Definition: stringlist.c:17
void StringList_Remove(StringList *stringList, int removeIndex)
Remove the specified string from the given string list.
Definition: stringlist.c:137
bool StringList_AddString(StringList *stringList, const char *string)
Add a string to the given string list.
Definition: stringlist.c:49