Design Pattern Examples
Overview of object-oriented design patterns
Mediator_Groups.c
Go to the documentation of this file.
1
5
6#include <stdlib.h>
7
9#include "Mediator_Groups.h"
10
11
15static GroupList _groups = { 0 };
16
17//=============================================================================
18
20// Groups_Clear()
22void Groups_Clear(void)
23{
25}
26
28// Groups_AddGroup()
30GroupErrorCode Groups_AddGroup(const char* groupName)
31{
33
34 if (groupName != NULL)
35 {
36 int foundIndex = GroupList_FindGroup(&_groups, groupName);
37 if (foundIndex == -1)
38 {
39 errorCode = GroupErrorCode_No_Memory;
40 Group* newGroup = Group_Create(groupName);
41 if (newGroup != NULL)
42 {
43 if (GroupList_AddGroup(&_groups, newGroup))
44 {
45 errorCode = GroupErrorCode_No_Error;
46 }
47 else
48 {
49 Group_Destroy(newGroup);
50 newGroup = NULL;
51 }
52 }
53 }
54 }
55
56 return errorCode;
57}
58
60// Groups_RemoveGroup()
62GroupErrorCode Groups_RemoveGroup(const char* groupName)
63{
65
66 if (groupName != NULL)
67 {
69 int groupIndex = GroupList_FindGroup(&_groups, groupName);
70 if (groupIndex != -1)
71 {
72 GroupList_RemoveGroup(&_groups, groupIndex);
73 errorCode = GroupErrorCode_No_Error;
74 }
75 }
76
77 return errorCode;
78}
79
80
82// Groups_FindGroup()
84Group* Groups_FindGroup(const char* groupName)
85{
86 Group* foundGroup = NULL;
87
88 if (groupName != NULL)
89 {
90 int foundIndex = GroupList_FindGroup(&_groups, groupName);
91 if (foundIndex != -1)
92 {
93 foundGroup = _groups.groups[foundIndex];
94 }
95 }
96
97 return foundGroup;
98}
99
100
102// Groups_UserInGroup()
104bool Groups_UserInGroup(const char* userName, const char* groupName)
105{
106 bool userFound = false;
107
108 if (userName != NULL && groupName != NULL)
109 {
110 int groupIndex = GroupList_FindGroup(&_groups, groupName);
111 if (groupIndex != -1)
112 {
113 int userIndex = Group_FindUser(_groups.groups[groupIndex], userName);
114 userFound = userIndex != -1;
115 }
116 }
117
118 return userFound;
119}
120
122// Groups_AddUser()
124GroupErrorCode Groups_AddUserGroup(const char* userName, const char* groupName)
125{
127
128 if (userName != NULL && groupName != NULL)
129 {
131 int groupIndex = GroupList_FindGroup(&_groups, groupName);
132 if (groupIndex != -1)
133 {
134 errorCode = GroupErrorCode_No_Memory;
135 Group* group = _groups.groups[groupIndex];
136 if (Group_AddUser(group, userName))
137 {
138 errorCode = GroupErrorCode_No_Error;
139 }
140 }
141 }
142
143 return errorCode;
144}
145
147// Groups_RemoveUser()
149GroupErrorCode Groups_RemoveUserFromGroup(const char* userName, const char* groupName)
150{
152
153 if (userName != NULL && groupName != NULL)
154 {
156 int groupIndex = GroupList_FindGroup(&_groups, groupName);
157 if (groupIndex != -1)
158 {
159 Group* group = _groups.groups[groupIndex];
160 int userIndex = Group_FindUser(group, userName);
161 if (userIndex != -1)
162 {
163 Group_RemoveUser(group, userIndex);
164 errorCode = GroupErrorCode_No_Error;
165 }
166 }
167 }
168
169 return errorCode;
170}
171
173// Groups_RemoveUserFromAllGroups()
176{
178
179 if (userName != NULL)
180 {
181 errorCode = GroupErrorCode_No_Error;
182 for (size_t index = 0; index < _groups.groups_count; index++)
183 {
184 Group* group = _groups.groups[index];
185 int userIndex = Group_FindUser(group, userName);
186 if (userIndex != -1)
187 {
188 Group_RemoveUser(group, userIndex);
189 }
190 }
191 }
192
193 return errorCode;
194}
195
196
198// Groups_GetAllGroups()
201{
203
204 if (groups != NULL)
205 {
206 errorCode = GroupErrorCode_No_Error;
207 for (size_t index = 0; index < _groups.groups_count; index++)
208 {
209 Group* group = _groups.groups[index];
210 if (!StringList_AddString(groups, group->Name))
211 {
212 errorCode = GroupErrorCode_No_Memory;
213 break;
214 }
215 }
216 }
217
218 return errorCode;
219}
220
222// Groups_GetAllUsersInGroup()
225{
227
228 if (groupName != NULL && users != NULL)
229 {
231 int groupIndex = GroupList_FindGroup(&_groups, groupName);
232 if (groupIndex != -1)
233 {
234 errorCode = GroupErrorCode_No_Error;
235 Group* group = _groups.groups[groupIndex];
236 if (!Group_GetAllUsers(group, users))
237 {
238 errorCode = GroupErrorCode_No_Memory;
239 }
240 }
241 }
242
243 return errorCode;
244}
245
246
248// Groups_GetGroupsWithUser()
251{
253
254 if (userName != NULL && groups != NULL)
255 {
256 errorCode = GroupErrorCode_No_Error;
257 for (size_t index = 0; index < _groups.groups_count; index++)
258 {
259 Group* group = _groups.groups[index];
260 int userIndex = Group_FindUser(group, userName);
261 if (userIndex != -1)
262 {
263 if (!StringList_AddString(groups, group->Name))
264 {
265 errorCode = GroupErrorCode_No_Memory;
266 break;
267 }
268 }
269 }
270 }
271
272 return errorCode;
273}
void Group_Destroy(Group *group)
Release the memory of the specified Group object. After this function returns, the pointer to the Gro...
int Group_FindUser(Group *group, const char *userName)
Search the specified group for the specified user.
void Group_RemoveUser(Group *group, int removeIndex)
Remove a user from the specified group. Does nothing if the user is not in the group.
bool Group_AddUser(Group *group, const char *userName)
Add a user to the specified group. Does nothing if the user is already in the group.
Group * Group_Create(const char *groupName)
Create a new Group object with the specified name.
bool Group_GetAllUsers(Group *group, StringList *users)
Retrieve the names of all users in the group.
bool GroupList_AddGroup(GroupList *groupList, Group *group)
Add the specified Group object to the specified GroupList object. The list takes ownership of the Gro...
void GroupList_RemoveGroup(GroupList *groupList, int removeIndex)
Remove the group at the specified index in the given GroupList object.
void GroupList_Clear(GroupList *groupList)
Clear the given GroupList object to release all associated resources.
int GroupList_FindGroup(GroupList *groupList, const char *groupName)
Search the given GroupList object for the specified group by name and return the index of the group i...
Declaration of the GroupList structure and the associated support functions as used in the Mediator P...
Group * Groups_FindGroup(const char *groupName)
Find a group from the list of groups given the group's name.
GroupErrorCode Groups_GetAllUsersInGroup(const char *groupName, StringList *users)
Retrieve a list of all users in the specified group.
GroupErrorCode Groups_GetGroupsWithUser(const char *userName, StringList *groups)
Retrieve a list of all groups that contains the given user.
static GroupList _groups
The list of all groups.
GroupErrorCode Groups_RemoveUserFromAllGroups(const char *userName)
Remove the specified user from all groups.
GroupErrorCode Groups_AddUserGroup(const char *userName, const char *groupName)
Add the specified user to the specified group. If the user is already in the group,...
GroupErrorCode Groups_RemoveGroup(const char *groupName)
Remove a group from the list of groups.
void Groups_Clear(void)
Release all memory associated with list of groups.
GroupErrorCode Groups_GetAllGroups(StringList *groups)
Retrieve a list of all group names.
GroupErrorCode Groups_RemoveUserFromGroup(const char *userName, const char *groupName)
Remove the specified user from the specified group. If the user is not in the group,...
bool Groups_UserInGroup(const char *userName, const char *groupName)
Determine if the specified user is in this group. This is a case- sensitive search.
GroupErrorCode Groups_AddGroup(const char *groupName)
Add a group to the list of groups.
The front end of the Groups sub-system. Contains the declarations of the support functions for workin...
GroupErrorCode
Represents error codes that can be returned from the Group functions used in the Mediator Pattern.
@ GroupErrorCode_No_Memory
Indicates an out of memory condition.
@ GroupErrorCode_No_Error
Indicates success.
@ GroupErrorCode_Null_Argument
Indicates an argument is NULL.
@ GroupErrorCode_Group_Does_Not_Exist
Indicates the group does not exist.
std::vector< std::string > StringList
Typedef for a vector of std::string.
bool StringList_AddString(StringList *stringList, const char *string)
Add a string to the given string list.
Definition: stringlist.c:49
Represents a single group. A group has a name and zero or more users. Users are tracked by name.
const char * Name
Name of this group.
Represents a list of groups. Call GroupList_Initialize() to start and GroupList_Clear() to release al...
size_t groups_count
Number of pointers in the groups list.
Group ** groups
Array of pointers to Group objects.