Skip to content

Commit ddac174

Browse files
committed
feature: complete createLocalRecord function
1 parent d397cdf commit ddac174

File tree

2 files changed

+73
-4
lines changed

2 files changed

+73
-4
lines changed

src/api/main.c

Lines changed: 71 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,83 @@
11

22
#include "../../libs/main.h"
33

4+
#include <stdio.h>
5+
#include <string.h>
6+
7+
#include "../structs.h"
48
#include "./main.h"
59

610
unsigned char postAPI(const Configuration* config, List* list) {
711
return 0;
812
// TODO
913
}
1014

11-
unsigned char createLocalFile(List* list, const char* localFilePath) {
12-
// TODO
15+
unsigned char createLocalRecord(const Configuration* config, List* players, char* localFilePath) {
16+
unsigned char error;
17+
18+
FILE* file;
19+
20+
// Chars per column
21+
const int indexCol = 2;
22+
const int nameCol = PLAYER_NAME_LENGTH;
23+
const int pointsCol = 6;
24+
const int columns = 3;
25+
const int separatorsBetweenCols = columns - 1;
26+
27+
const int lineLength = (indexCol + nameCol + pointsCol) + (columns * 2) + separatorsBetweenCols;
28+
29+
int i;
30+
Player player;
31+
size_t playersLength;
32+
33+
error = concatCurrentTime(localFilePath);
34+
if (error) return 1;
35+
36+
strcat(localFilePath, ".txt");
37+
38+
file = fopen(localFilePath, "wt");
39+
if (file == NULL) return 1;
40+
41+
// Header - Team name
42+
fprintf(file, "> Team name: %s.\n\n", config->teamName);
43+
44+
// Table - Start line
45+
fputc('+', file);
46+
for (i = 0; i < lineLength; i++) fputc('-', file);
47+
fprintf(file, "+\n");
48+
49+
// Table - Titles
50+
fprintf(file, "| N° | %-*s | Points |\n", nameCol, "Player name");
51+
52+
// Table - Separator line
53+
fputc('|', file);
54+
for (i = 0; i < lineLength; i++) {
55+
if (i == indexCol + 2 || i == indexCol + 2 + 1 + nameCol + 2) {
56+
fputc('|', file);
57+
continue;
58+
};
59+
60+
fputc('-', file);
61+
};
62+
fprintf(file, "|\n");
63+
64+
playersLength = getLength(players);
65+
66+
// Table - Content
67+
for (i = 0; i < playersLength; i++) {
68+
error = getElement(players, &player, sizeof(player), i);
69+
if (error) break;
70+
71+
fprintf(file, "| %0*d | %-*s | %0*d |\n", indexCol, i + 1, nameCol, player.name, pointsCol,
72+
player.points);
73+
};
74+
75+
// Table - End line
76+
fputc('+', file);
77+
for (i = 0; i < lineLength; i++) fputc('-', file);
78+
fputc('+', file);
79+
80+
fclose(file);
81+
1382
return 0;
1483
}

src/api/main.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@
66

77
unsigned char postAPI(const Configuration* config, List* list);
88

9-
unsigned char createLocalFile(List* list, const char* localFilePath);
9+
unsigned char createLocalRecord(const Configuration* config, List* players, char* localFilePath);
1010

11-
#endif // SRC__API_H_INCLUDED
11+
#endif // SRC__API_H_INCLUDED

0 commit comments

Comments
 (0)