From: Thadeu Lima de Souza Cascardo Date: Wed, 17 Jul 2013 21:44:19 +0000 (-0300) Subject: Add a name to the group. X-Git-Url: http://git.cascardo.eti.br/?p=cascardo%2Fsgp.git;a=commitdiff_plain;h=97412b6c5891a2e8bc0811046a38935a48371ac1 Add a name to the group. While at it, make it more robust to fail to create a new group, and allow it to be destroyed. --- diff --git a/include/sgp/group.h b/include/sgp/group.h index 26f940a..cc72252 100644 --- a/include/sgp/group.h +++ b/include/sgp/group.h @@ -24,7 +24,9 @@ struct sgp_group; -struct sgp_group * sgp_group_new(); +struct sgp_group * sgp_group_new(char *); +void sgp_group_destroy(struct sgp_group *); +char * sgp_group_get_name(struct sgp_group *); int sgp_group_add_friend(struct sgp_group *, struct sgp_friend *); #define sgp_foreach_friend(group, friend, pfriend) \ diff --git a/src/group.c b/src/group.c index 41bfb07..d8046a1 100644 --- a/src/group.c +++ b/src/group.c @@ -22,17 +22,39 @@ #include struct sgp_group { + char *name; struct sgp_friend **friends; int nr_friends; }; -struct sgp_group * sgp_group_new() +struct sgp_group * sgp_group_new(char *name) { struct sgp_group *group; group = malloc(sizeof(*group)); + if (!group) + return NULL; + group->name = strdup(name); + if (!group->name) + goto out; group->nr_friends = 0; group->friends = NULL; return group; +out: + free(group); + return NULL; +} + +void sgp_group_destroy(struct sgp_group *group) +{ + if (group->friends) + free(group->friends); + free(group->name); + free(group); +} + +char * sgp_group_get_name(struct sgp_group *group) +{ + return group->name; } /* diff --git a/src/main.c b/src/main.c index 9ce1fb7..4bb2b50 100644 --- a/src/main.c +++ b/src/main.c @@ -24,7 +24,7 @@ int main(int argc, char **argv) struct sgp_msg *msg; struct sgp_friend *friend; /* TODO: access a database here? */ - group = sgp_group_new(); + group = sgp_group_new("Myself"); friend = sgp_friend_new("Thadeu Cascardo"); msg = sgp_msg_new("New message for you"); sgp_group_add_friend(group, friend);