diff options
author | 2023-05-23 18:40:25 +0600 | |
---|---|---|
committer | 2023-05-23 18:45:10 +0600 | |
commit | e382419d65562d5f79065a30931bfaef094ff984 (patch) | |
tree | b61cdb0f226c231c77b1f907b58965b53dafb575 /src | |
parent | 09133381cad0a1dec37595867eadf9c6858ce8ee (diff) | |
download | c-obp-example-e382419d65562d5f79065a30931bfaef094ff984.tar.gz c-obp-example-e382419d65562d5f79065a30931bfaef094ff984.zip |
Clean up more code
Signed-off-by: Mubashshir <ahmubashshir@gmail.com>
Diffstat (limited to 'src')
-rw-r--r-- | src/main.c | 9 | ||||
-rw-r--r-- | src/teams-hell.h | 38 | ||||
-rw-r--r-- | src/teams.c | 32 | ||||
-rw-r--r-- | src/teams.h | 28 |
4 files changed, 55 insertions, 52 deletions
@@ -12,12 +12,12 @@ int main() { unsigned num = 0, i = 0; char line[255] = ""; - TEAM *teams; + team *teams; getline(line, 255); sscanf(line, "%u", &num); strncpy(line, "", 255); - teams = (TEAM *)malloc(sizeof(TEAM) * num); + teams = (team *)malloc(sizeof(team) * num); if (teams == NULL) { printf("Failed to reserve memory"); @@ -52,6 +52,9 @@ int main() } } - fun(team, printf, fun(team, find_champion, NULL, teams, num)); + team champion = ask(team, find_champion, NULL, teams, num); + ask(team, printf, champion); + + while(num --) del(team, teams[num]); return 0; } diff --git a/src/teams-hell.h b/src/teams-hell.h index 72fbdbd..a59a4e1 100644 --- a/src/teams-hell.h +++ b/src/teams-hell.h @@ -8,7 +8,7 @@ klass##_set_##field(instance, ## __VA_ARGS__) # define new(klass, ...) klass##_new(__VA_ARGS__) # define del(klass, ...) klass##_del(__VA_ARGS__) -# define fun(klass, fun, ...) klass##_##fun(__VA_ARGS__) +# define ask(klass, fun, ...) klass##_##fun(__VA_ARGS__) # endif /* __TEAMS_HELL_H__ */ # if defined(__TEAMS_PRIVATE_UNDEF__) @@ -21,30 +21,30 @@ # undef __TEAMS_PRIVATE_H__ # elif defined(__IMPL_TEAMS_INTERNAL) # define UNUSED(arg) while(0 && (arg)) -# define GETTER(klass, field, struct, type, body, ...) \ - type klass##_get_##field(struct self, ## __VA_ARGS__) \ +# define GETTER(klass, field, type, body, ...) \ + type klass##_get_##field(klass self, ## __VA_ARGS__) \ body -# define SETTER(klass, field, struct, type, body, ...) \ - bool klass##_set_##field(struct self, ## __VA_ARGS__, type value) \ +# define SETTER(klass, field, type, body, ...) \ + bool klass##_set_##field(klass self, ## __VA_ARGS__, type value) \ body -# define DEFINE(klass, name, struct, type, body, ...) \ - type klass##_##name(struct self, ## __VA_ARGS__) \ +# define DEFINE(klass, name, type, body, ...) \ + type klass##_##name(klass self, ## __VA_ARGS__) \ body -# define NEW(klass, struct, body, ...) \ - struct klass ## _new(__VA_ARGS__) \ +# define NEW(klass, body, ...) \ + klass klass ## _new(__VA_ARGS__) \ body -# define DEL(klass, struct, body, ...) \ - void klass ## _del(struct self, ## __VA_ARGS__) \ +# define DEL(klass, body, ...) \ + void klass ## _del(klass self, ## __VA_ARGS__) \ body # elif defined(__TEAMS_PRIVATE_H__) -# define GETTER(klass, field, struct, type, ...) \ - type klass##_get_##field(struct self, ## __VA_ARGS__) -# define SETTER(klass, field, struct, type, ...) \ - bool klass##_set_##field(struct self, ## __VA_ARGS__, type value) -# define DEFINE(klass, name, struct, type, ...) \ - type klass##_##name(struct self, ## __VA_ARGS__) -# define NEW(klass, struct, ...) struct klass ## _new(__VA_ARGS__) -# define DEL(klass, struct, ...) void klass ## _del(struct self, ## __VA_ARGS__) +# define GETTER(klass, field, type, ...) \ + type klass##_get_##field(klass self, ## __VA_ARGS__) +# define SETTER(klass, field, type, ...) \ + bool klass##_set_##field(klass self, ## __VA_ARGS__, type value) +# define DEFINE(klass, name, type, ...) \ + type klass##_##name(klass self, ## __VA_ARGS__) +# define NEW(klass, ...) klass klass ## _new(__VA_ARGS__) +# define DEL(klass, ...) void klass ## _del(klass self, ## __VA_ARGS__) # endif #endif diff --git a/src/teams.c b/src/teams.c index f75f161..2f0814a 100644 --- a/src/teams.c +++ b/src/teams.c @@ -14,32 +14,32 @@ typedef struct team_impl_struct { } TEAM_impl; // base functions -NEW(team, TEAM, { +NEW(team, { return (TEAM_impl *)malloc(sizeof(TEAM_impl)); }) -DEL(team, TEAM, { +DEL(team, { if(self) free(self); }) // Setter definitions // @set char[]: TEAM.name -> bool -SETTER(team, name, TEAM, char *, { +SETTER(team, name, char *, { return !(strncpy(self->name, value, 255) == NULL); }) // @set char[]: TEAM.institution -> bool -SETTER(team, institution, TEAM, char *, { +SETTER(team, institution, char *, { return !(strncpy(self->institution, value, 255) == NULL); }) // @set char[]: TEAM.member_name, int: id -> bool -SETTER(team, member_name, TEAM, char *, { +SETTER(team, member_name, char *, { return (id > 0 && id < 4 && !(strncpy(self->members[id-1], value, 255) == NULL)); }, int id) // @set int: TEAM.solved -> bool -SETTER(team, solved, TEAM, uint8_t, { +SETTER(team, solved, uint8_t, { if (value <= 10) self->solved = value; return (value <= 10); @@ -47,30 +47,30 @@ SETTER(team, solved, TEAM, uint8_t, { // Getter definitions // @get TEAM.name -> char[] -GETTER(team, name, TEAM, char *, { +GETTER(team, name, char *, { return self->name; }) // @get TEAM.institution -> char[] -GETTER(team, institution, TEAM, char *, { +GETTER(team, institution, char *, { return self->institution; }) // @get TEAM.solved -> int -GETTER(team, solved, TEAM, uint8_t, { +GETTER(team, solved, uint8_t, { return self->solved; }) // @get TEAM.member_name, id -> char[] -GETTER(team, member_name, TEAM, char *, { +GETTER(team, member_name, char *, { return (id > 0 && id < 4)? self->members[id - 1] : "(incorrect id)"; }, int id) -DEFINE(team, printf, TEAM, int, { - return fun(team, fprintf, self, stdout); +DEFINE(team, printf, int, { + return ask(team, fprintf, self, stdout); }) -DEFINE(team, fprintf, TEAM, int, { +DEFINE(team, fprintf, int, { return fprintf(file, "Team name : %s\n" "Institution: %s\n" @@ -86,12 +86,12 @@ DEFINE(team, fprintf, TEAM, int, { self->members[2]); }, FILE *file) -DEFINE(team, find_champion, TEAM, TEAM, { +DEFINE(team, find_champion, team, { UNUSED(self); - TEAM champion = teams[num - 1]; + team champion = teams[num - 1]; while(num --) if ( teams[num]->solved > champion->solved ) champion = teams[num]; return champion; -}, TEAM *teams, unsigned num) +}, team *teams, unsigned num) diff --git a/src/teams.h b/src/teams.h index 435ac4a..3eba2f7 100644 --- a/src/teams.h +++ b/src/teams.h @@ -5,28 +5,28 @@ # include <stdbool.h> # include "teams-hell.h" // Type definition -typedef struct team_impl_struct * TEAM; +typedef struct team_impl_struct * team; // base functions -NEW(team, TEAM); -DEL(team, TEAM); +NEW(team); +DEL(team); // field getters -SETTER(team, name, TEAM, char *); -SETTER(team, institution, TEAM, char *); -SETTER(team, member_name, TEAM, char *, int); -SETTER(team, solved, TEAM, uint8_t); +SETTER(team, name, char *); +SETTER(team, institution, char *); +SETTER(team, member_name, char *, int); +SETTER(team, solved, uint8_t); // field setters -GETTER(team, name, TEAM, char *); -GETTER(team, institution, TEAM, char *); -GETTER(team, member_name, TEAM, char *, int); -GETTER(team, solved, TEAM, uint8_t); +GETTER(team, name, char *); +GETTER(team, institution, char *); +GETTER(team, member_name, char *, int); +GETTER(team, solved, uint8_t); // helper functions -DEFINE(team, printf, TEAM, int); -DEFINE(team, fprintf, TEAM, int, FILE *); -DEFINE(team, find_champion, TEAM, TEAM, TEAM *, unsigned num); +DEFINE(team, printf, int); +DEFINE(team, fprintf, int, FILE *); +DEFINE(team, find_champion, team, team *, unsigned num); # define __TEAMS_PRIVATE_UNDEF__ # include "teams-hell.h" |