Skip to content

Commit 9f89f88

Browse files
authored
[feature] Complete createLocalRecord function (#9)
2 parents c1a92d5 + ddac174 commit 9f89f88

File tree

11 files changed

+125
-18
lines changed

11 files changed

+125
-18
lines changed

.gitignore

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,15 @@ personal-*.md
77

88

99

10+
# ---------------------------------------------------------------------------- #
11+
# PROJECT FILES #
12+
# ---------------------------------------------------------------------------- #
13+
14+
# Autogenerated files
15+
src/statics/local-storage/**/*.txt
16+
17+
18+
1019
# ---------------------------------------------------------------------------- #
1120
# CODEBLOCKS #
1221
# ---------------------------------------------------------------------------- #

libs/libs.cbp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@
4141
<Unit filename="macros.h" />
4242
<Unit filename="main.h" />
4343
<Unit filename="structs.h" />
44+
<Unit filename="time/main.c">
45+
<Option compilerVar="CC" />
46+
</Unit>
47+
<Unit filename="time/main.h" />
4448
<Unit filename="utilities.c">
4549
<Option compilerVar="CC" />
4650
</Unit>

libs/list/main.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,22 @@ unsigned char getHead(const List* _list, void* store, const size_t sizeOfStore)
4040
return 1;
4141
}
4242

43+
unsigned char getElement(const List* _list, void* store, const size_t sizeOfStore,
44+
const size_t index) {
45+
size_t length = 0;
46+
47+
while ((*_list) != NULL && length != index) {
48+
_list = &(*_list)->__next;
49+
length++;
50+
}
51+
52+
if (length != index) return 1;
53+
54+
memcpy(store, (*_list)->__data, MIN(sizeOfStore, (*_list)->__sizeOfData));
55+
56+
return 0;
57+
}
58+
4359
unsigned char isListEmpty(List* _list) { return (*_list) == NULL; }
4460

4561
unsigned char isListFull(List* _list, const size_t sizeOfStore) {

libs/list/main.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ void destroyList(List* _list);
1919
// Getters
2020
unsigned char getHead(const List* _list, void* store, const size_t sizeOfStore);
2121

22+
unsigned char getElement(const List* _list, void* store, const size_t sizeOfStore,
23+
const size_t index);
24+
2225
unsigned char isListEmpty(List* _list);
2326

2427
unsigned char isListFull(List* _list, const size_t sizeOfStore);

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

src/macros.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111

1212
#define LOCAL_FILE_PATH "./statics/local-storage/informe-juego_"
1313

14-
#define LOCAL_FILE_PATH_LENGTH (sizeof(LOCAL_FILE_PATH) + sizeof("YYYY-MM-DD-HH-mm") + 1)
14+
#define LOCAL_FILE_PATH_LENGTH \
15+
(sizeof(LOCAL_FILE_PATH) + sizeof("YYYY-MM-DD-HH-mm") + sizeof(".txt") + 1)
1516

1617
/* --------------------------------- Player --------------------------------- */
1718

src/main.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@
1111
#include "./utilities.h"
1212

1313
int main(const int argsLength, char* args[]) {
14-
Configuration config;
1514
unsigned char error;
1615

1716
Team team;
17+
Configuration config;
18+
char localFilePath[LOCAL_FILE_PATH_LENGTH] = LOCAL_FILE_PATH;
1819

1920
int userInput;
2021

@@ -41,7 +42,7 @@ int main(const int argsLength, char* args[]) {
4142
switch (userInput) {
4243
case 1:
4344
printf("\n> Team %s...\n\n", team.name);
44-
playTicTacToe(&config, LOCAL_FILE_PATH);
45+
playTicTacToe(&config, localFilePath);
4546
break;
4647
case 2:
4748
printf("\n> [Showing ranking...]");

src/play/main.c

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@
88
#include "../structs.h"
99
#include "./utilities.h"
1010

11-
unsigned char playTicTacToe(const Configuration* config, const char* localFilePath) {
12-
unsigned char error;
13-
11+
unsigned char playTicTacToe(const Configuration* config, char* localFilePath) {
1412
List players;
1513
List playersAfterMatch;
1614

@@ -46,17 +44,23 @@ unsigned char playTicTacToe(const Configuration* config, const char* localFilePa
4644
if (!pushElement(&playersAfterMatch, &player, sizeof(player))) return 0;
4745
}
4846

49-
error = postAPI(config, &playersAfterMatch);
47+
if (postAPI(config, &playersAfterMatch)) {
48+
puts("> Error! An error occurred on post to the API.\n\n");
5049

51-
destroyList(&players);
52-
destroyList(&playersAfterMatch);
50+
destroyList(&players);
51+
destroyList(&playersAfterMatch);
5352

54-
if (error) {
55-
puts("> Error! An error occurred on post matches data.");
5653
return 0;
5754
};
5855

59-
createLocalFile(&playersAfterMatch, localFilePath);
56+
if (createLocalRecord(config, &playersAfterMatch, localFilePath)) {
57+
puts("> Error! An error occurred on create local record.\n\n");
58+
59+
destroyList(&players);
60+
destroyList(&playersAfterMatch);
61+
62+
return 0;
63+
};
6064

6165
return 1;
6266
}

src/play/main.h

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

44
#include "../configuration/main.h"
55

6-
unsigned char playTicTacToe(const Configuration* config, const char* nameOfTheLocalFile);
6+
unsigned char playTicTacToe(const Configuration* config, char* localFilePath);
77

8-
#endif // SRC__PLAY_H_INCLUDED
8+
#endif // SRC__PLAY_H_INCLUDED

0 commit comments

Comments
 (0)