diff options
author | 2023-10-28 19:57:58 +0600 | |
---|---|---|
committer | 2023-10-28 19:57:58 +0600 | |
commit | c47100c331e1fcc4353b6ed83a1d3485c9331eaf (patch) | |
tree | ea9a3242c22511a94705ddb5bbdfd958a1dea65a /src | |
parent | d9bd6f314d7fe3efedae6a5c64078fdccc07831f (diff) | |
download | c-obp-example-c47100c331e1fcc4353b6ed83a1d3485c9331eaf.tar.gz c-obp-example-c47100c331e1fcc4353b6ed83a1d3485c9331eaf.zip |
Use the consume helper for processing the data
Signed-off-by: Mubashshir <ahmubashshir@gmail.com>
Diffstat (limited to 'src')
-rw-r--r-- | src/main.c | 62 |
1 files changed, 33 insertions, 29 deletions
@@ -3,10 +3,33 @@ #include <stdlib.h> #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; } |