|
1 | 1 |
|
2 | 2 | #include "../../libs/main.h" |
3 | 3 |
|
| 4 | +#include <stdio.h> |
| 5 | +#include <string.h> |
| 6 | + |
| 7 | +#include "../structs.h" |
4 | 8 | #include "./main.h" |
5 | 9 |
|
6 | 10 | unsigned char postAPI(const Configuration* config, List* list) { |
7 | 11 | return 0; |
8 | 12 | // TODO |
9 | 13 | } |
10 | 14 |
|
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 | + |
13 | 82 | return 0; |
14 | 83 | } |
0 commit comments