Design Pattern Examples
Overview of object-oriented design patterns
Mediator_Functions.c
Go to the documentation of this file.
1
6
7#include <stdlib.h>
8
9#include "Mediator_Users.h"
10#include "Mediator_Groups.h"
11
12#include "Mediator_Functions.h"
13
14
16// Mediator_ClearAll()
19{
22}
23
25// Mediator_AddUser()
28{
30
31 if (userName != NULL)
32 {
33 UserErrorCode userErrorCode = Users_AddUser(userName);
34 if (userErrorCode == UserErrorCode_No_Memory)
35 {
37 }
38 else
39 {
41 }
42 }
43
44 return errorCode;
45}
46
48// Mediator_RemoveUser()
51{
53
54 if (userName != NULL)
55 {
56 errorCode = Mediator_RemoveUserFromAllGroups(userName);
57 if (errorCode == MediatorErrorCode_No_Error)
58 {
59 UserErrorCode userErrorCode = Users_RemoveUser(userName);
60 if (userErrorCode == UserErrorCode_User_Does_Not_Exist)
61 {
63 }
64 else
65 {
67 }
68 }
69 }
70
71 return errorCode;
72}
73
75// Mediator_AddGroup()
78{
80
81 if (groupName != NULL)
82 {
83 GroupErrorCode groupErrorCode = Groups_AddGroup(groupName);
84 if (groupErrorCode == GroupErrorCode_No_Memory)
85 {
87 }
88 else
89 {
91 }
92 }
93
94 return errorCode;
95}
96
98// Mediator_RemoveGroup()
101{
103
104 if (groupName != NULL)
105 {
106 GroupErrorCode groupErrorCode = Groups_RemoveGroup(groupName);
107 if (groupErrorCode == GroupErrorCode_Group_Does_Not_Exist)
108 {
110 }
111 else
112 {
113 errorCode = MediatorErrorCode_No_Error;
114 }
115 }
116
117 return errorCode;
118}
119
121// Mediator_AddUserToGroup()
123MediatorErrorCode Mediator_AddUserToGroup(const char* userName, const char* groupName)
124{
126
127 if (userName != NULL && groupName != NULL)
128 {
130 if (!Groups_UserInGroup(userName, groupName))
131 {
132 GroupErrorCode groupErrorCode = Groups_AddUserGroup(userName, groupName);
133 if (groupErrorCode == GroupErrorCode_No_Memory)
134 {
135 errorCode = MediatorErrorCode_No_Memory;
136 }
137 else if (groupErrorCode == GroupErrorCode_Group_Does_Not_Exist)
138 {
140 }
141 else
142 {
143 errorCode = MediatorErrorCode_No_Error;
144 }
145 }
146 }
147
148 return errorCode;
149}
150
152// Mediator_RemoveUserFromGroup()
154MediatorErrorCode Mediator_RemoveUserFromGroup(const char* userName, const char* groupName)
155{
157
158 if (userName != NULL && groupName != NULL)
159 {
161 if (Groups_UserInGroup(userName, groupName))
162 {
163 GroupErrorCode groupErrorCode = Groups_RemoveUserFromGroup(userName, groupName);
164 if (groupErrorCode == GroupErrorCode_Group_Does_Not_Exist)
165 {
167 }
168 else
169 {
170 errorCode = MediatorErrorCode_No_Error;
171 }
172 }
173 }
174
175 return errorCode;
176}
177
179// Mediator_RemoveUserFromAllGroups()
182{
184
185 if (userName != NULL)
186 {
188 User* user = Users_FindUser(userName);
189 if (user != NULL)
190 {
191 GroupErrorCode groupErrorCode = Groups_RemoveUserFromAllGroups(userName);
192 if (groupErrorCode == GroupErrorCode_No_Error)
193 {
194 errorCode = MediatorErrorCode_No_Error;
195 }
196 }
197 }
198
199 return errorCode;
200}
201
203// Mediator_GetAllGroups()
206{
208 if (groupNames != NULL)
209 {
210 GroupErrorCode groupErrorCode = Groups_GetAllGroups(groupNames);
211 if (groupErrorCode == GroupErrorCode_No_Memory)
212 {
213 errorCode = MediatorErrorCode_No_Memory;
214 }
215 else
216 {
217 errorCode = MediatorErrorCode_No_Error;
218 }
219 }
220
221 return errorCode;
222}
223
225// Mediator_GetAllUsers()
228{
230 if (userNames != NULL)
231 {
232 UserErrorCode userErrorCode = Users_GetAllUsers(userNames);
233 if (userErrorCode == UserErrorCode_No_Memory)
234 {
235 errorCode = MediatorErrorCode_No_Memory;
236 }
237 else
238 {
239 errorCode = MediatorErrorCode_No_Error;
240 }
241 }
242
243 return errorCode;
244}
245
247// Mediator_IsUserInGroup()
249bool Mediator_IsUserInGroup(const char* userName, const char* groupName)
250{
251 bool inGroup = false;
252
253 if (userName != NULL && groupName != NULL)
254 {
255 inGroup = Groups_UserInGroup(userName, groupName);
256 }
257
258 return inGroup;
259}
260
262// Mediator_GetUsersInGroup()
264MediatorErrorCode Mediator_GetUsersInGroup(const char* groupName, StringList* userNames)
265{
267
268 if (groupName != NULL && userNames != NULL)
269 {
270 GroupErrorCode groupErrorCode = Groups_GetAllUsersInGroup(groupName, userNames);
271 if (groupErrorCode == GroupErrorCode_No_Memory)
272 {
273 errorCode = MediatorErrorCode_No_Memory;
274 }
275 else
276 {
277 errorCode = MediatorErrorCode_No_Error;
278 }
279 }
280
281 return errorCode;
282}
283
285// Mediator_GetGroupsWithUser()
288{
290
291 if (userName != NULL && groupNames != NULL)
292 {
293 GroupErrorCode groupErrorCode = Groups_GetGroupsWithUser(userName, groupNames);
294 if (groupErrorCode == GroupErrorCode_No_Memory)
295 {
296 errorCode = MediatorErrorCode_No_Memory;
297 }
298 else
299 {
300 errorCode = MediatorErrorCode_No_Error;
301 }
302 }
303
304 return errorCode;
305}
MediatorErrorCode Mediator_GetGroupsWithUser(const char *userName, StringList *groupNames)
Retrieve a list of all groups that contain the specified user.
bool Mediator_IsUserInGroup(const char *userName, const char *groupName)
Determine if the specified user is in the specified group.
MediatorErrorCode Mediator_AddGroup(const char *groupName)
Add a group to the list of known groups. If the group is already in the list, the request to add is i...
void Mediator_ClearAll(void)
Clear all memory associated with groups and users.
MediatorErrorCode Mediator_RemoveUserFromGroup(const char *userName, const char *groupName)
Remove the specified user from the specified group.
MediatorErrorCode Mediator_AddUser(const char *userName)
Add a user to the list of known users. If the name is already in the list of users,...
MediatorErrorCode Mediator_GetAllUsers(StringList *userNames)
Retrieve a list of all known users.
MediatorErrorCode Mediator_AddUserToGroup(const char *userName, const char *groupName)
Add the specified user to the specified group. If the user is already in the group,...
MediatorErrorCode Mediator_RemoveUser(const char *userName)
Removes the specified user from the list of known users, if the user exists. Also removes the user fr...
MediatorErrorCode Mediator_RemoveUserFromAllGroups(const char *userName)
Remove the specified user from all existing groups. The user still exists.
MediatorErrorCode Mediator_GetUsersInGroup(const char *groupName, StringList *userNames)
Retrieve a list of users in the specified group.
MediatorErrorCode Mediator_RemoveGroup(const char *groupName)
Remove the specified group from the list of known groups if the group exists.
MediatorErrorCode Mediator_GetAllGroups(StringList *groupNames)
Retrieve a list of all known groups.
The front end of the Mediator system that mediates between the Users and the Groups sub-systems....
MediatorErrorCode
Represents error codes that can be returned from the Mediator functions used in the Mediator Pattern.
@ MediatorErrorCode_Null_Argument
One of the arguments was null or empty.
@ MediatorErrorCode_No_Memory
Indicates an out of memory condition.
@ MediatorErrorCode_Group_Does_Not_Exist
The specified group does not exist.
@ MediatorErrorCode_No_Error
No error occurred.
@ MediatorErrorCode_User_Does_Not_Exist
The specified user does not exist.
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.
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_Group_Does_Not_Exist
Indicates the group does not exist.
void Users_Clear(void)
Release all memory associated with the list of users.
User * Users_FindUser(const char *userName)
Find a user from the list of users given the user's name.
UserErrorCode Users_RemoveUser(const char *userName)
Remove a user from the list of users.
UserErrorCode Users_GetAllUsers(StringList *userNames)
Retrieve a list of all users.
UserErrorCode Users_AddUser(const char *userName)
Add a user to the list of users.
The front end of the Users sub-system. Contains the declarations of the support functions for working...
UserErrorCode
Represents error codes that can be returned from the User functions used in the Mediator Pattern.
@ UserErrorCode_User_Does_Not_Exist
Indicates the user does not exist.
@ UserErrorCode_No_Memory
Indicates an out of memory condition.
std::vector< std::string > StringList
Typedef for a vector of std::string.
Represents a user with a name.
Definition: Mediator_User.h:14