From c47100c331e1fcc4353b6ed83a1d3485c9331eaf Mon Sep 17 00:00:00 2001 From: Mubashshir Date: Sat, 28 Oct 2023 19:57:58 +0600 Subject: Use the consume helper for processing the data Signed-off-by: Mubashshir --- src/main.c | 62 +++++++++++++++++++++++++++++++++----------------------------- 1 file changed, 33 insertions(+), 29 deletions(-) (limited to 'src') diff --git a/src/main.c b/src/main.c index 76c7cd8..9edf6a6 100644 --- a/src/main.c +++ b/src/main.c @@ -3,10 +3,33 @@ #include #include "teams.h" +#include "common.h" -#define getline(x, y) \ - while(((x)[strcspn((x), "\r\n")] = 0) == 0 && strncmp((x), "", (y)) == 0) \ - fgets((x), (y), stdin) +// ref(type) references content is always const +// mut(type) references content is mutable +void process(const ref(char) line, unsigned idx, team obj) +{ + unsigned s; + switch(idx) { + case 1: + set(team, obj, name, line); + break; + case 2: + set(team, obj, institution, line); + break; + case 3: + sscanf(line, "%u", &s); + set(team, obj, solved, s); + break; + case 4: // falls through + case 5: // falls through + case 6: + set(team, obj, member_name, idx - 3, line); + break; + default: + break; + } +} int main() { @@ -14,10 +37,9 @@ int main() char line[255] = ""; team *teams; - getline(line, 255); - sscanf(line, "%u", &num); - strncpy(line, "", 255); - teams = (team *)malloc(sizeof(team) * num); + getline(line, 255, stdin); + consume(line, 255, sscanf, "%u", &num); + teams = array(team, num); if (teams == NULL) { printf("Failed to reserve memory"); @@ -26,29 +48,10 @@ int main() for(i = 0; i < num; i++) { teams[i] = new(team); - unsigned idx = 0, s = 0; + unsigned idx = 0; while (idx ++ < 6) { - getline(line, 255); - switch(idx) { - case 1: - set(team, teams[i], name, line); - break; - case 2: - set(team, teams[i], institution, line); - break; - case 3: - sscanf(line, "%u", &s); - set(team, teams[i], solved, s); - break; - case 4: // falls through - case 5: // falls through - case 6: - set(team, teams[i], member_name, idx - 3, line); - break; - default: - break; - } - strncpy(line, "", 255); + getline(line, 255, stdin); + consume(line, 255, process, idx, teams[i]); } } @@ -57,5 +60,6 @@ int main() while(num --) del(team, teams[num]); free(teams); + return 0; } -- cgit v1.2.3