Skip to content

Commit e4c532d

Browse files
committed
feature: create configuration module
1 parent 767c523 commit e4c532d

File tree

8 files changed

+127
-0
lines changed

8 files changed

+127
-0
lines changed

src/configuration/functions.c

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
2+
#include <stdio.h>
3+
#include <string.h>
4+
5+
#include "./macros.h"
6+
#include "./structs.h"
7+
8+
unsigned char getConfiguration(Configuration* config, const char* path) {
9+
char line[CONFIGURATION_LINE_LENGTH];
10+
11+
char* fieldStart;
12+
char* fieldEnd;
13+
14+
char* lineBreak;
15+
16+
FILE* file = fopen(path, "rt");
17+
if (file == NULL) return 1;
18+
19+
if (fgets(line, sizeof(line), file) == NULL) return 1;
20+
21+
fieldStart = line;
22+
fieldEnd = strchr(fieldStart, ' ');
23+
if (fieldEnd == NULL) return 1;
24+
25+
*fieldEnd = '\0';
26+
if (strlen(fieldStart) == 0) return 1;
27+
strcpy(config->apiURL, fieldStart);
28+
29+
fieldStart = fieldEnd + 1;
30+
fieldEnd = strchr(fieldStart, ' ');
31+
if (fieldEnd == NULL) return 1;
32+
33+
fieldStart = fieldEnd + 1;
34+
lineBreak = strrchr(fieldStart, '\n');
35+
if (lineBreak != NULL) {
36+
*lineBreak = '\0';
37+
};
38+
39+
strcpy(config->teamName, fieldStart);
40+
41+
if (fgets(line, sizeof(line), file) == NULL) return 1;
42+
43+
sscanf(line, "%d", &config->gamesPerPlayer);
44+
45+
return 0;
46+
}

src/configuration/functions.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
2+
#ifndef SRC__CONFIG_FUNCTIONS_H_INCLUDED
3+
#define SRC__CONFIG_FUNCTIONS_H_INCLUDED
4+
5+
#include "./structs.h"
6+
7+
unsigned char getConfiguration(Configuration* config, const char* path);
8+
9+
#endif // SRC__CONFIG_FUNCTIONS_H_INCLUDED

src/configuration/logs/main.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#include "./main.h"
2+
3+
#include <stdio.h>
4+
5+
void reprConfiguration(Configuration* config) {
6+
if (config == NULL) {
7+
printf("Configuration 0x%p (NULL);", (void*)&config);
8+
return;
9+
};
10+
11+
printf("Configuration 0x%p {\n", (void*)&config);
12+
printf(" char apiURL[MAXIMUM_API_URL_LENGTH] = \"%s\",\n", config->apiURL);
13+
printf(" char teamName[TEAM_NAME_LENGTH] = \"%s\",\n", config->teamName);
14+
printf(" int gamesPerPlayer = %d,\n", config->gamesPerPlayer);
15+
printf("};");
16+
}

src/configuration/logs/main.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#ifndef SRC__CONFIG_LOG_H_INCLUDED
2+
#define SRC__CONFIG_LOG_H_INCLUDED
3+
4+
#include "../structs.h"
5+
6+
void reprConfiguration(Configuration* config);
7+
8+
#endif // SRC__CONFIG_LOG_H_INCLUDED

src/configuration/macros.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#ifndef SRC__CONFIG_MACRO_H_INCLUDED
2+
#define SRC__CONFIG_MACRO_H_INCLUDED
3+
4+
#define MAXIMUM_API_URL_LENGTH 256
5+
6+
#define CONFIGURATION_LINE_LENGTH 256
7+
8+
#endif // SRC__CONFIG_MACRO_H_INCLUDED

src/configuration/main.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
2+
#ifndef SRC__CONFIG_MAIN_H_INCLUDED
3+
#define SRC__CONFIG_MAIN_H_INCLUDED
4+
5+
// Logs
6+
#include "./logs/main.h"
7+
8+
// Root
9+
#include "./functions.h"
10+
#include "./macros.h"
11+
#include "./structs.h"
12+
13+
#endif // SRC__CONFIG_MAIN_H_INCLUDED

src/configuration/structs.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
2+
#ifndef SRC__CONFIG_STRUCTS_H_INCLUDED
3+
#define SRC__CONFIG_STRUCTS_H_INCLUDED
4+
5+
#include "../macros.h"
6+
#include "./macros.h"
7+
8+
typedef struct {
9+
char apiURL[MAXIMUM_API_URL_LENGTH];
10+
char teamName[TEAM_NAME_LENGTH];
11+
int gamesPerPlayer;
12+
} Configuration;
13+
14+
#endif // SRC__CONFIG_STRUCTS_H_INCLUDED

src/src.cbp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,22 @@
3535
<Add library="../libs/bin/Debug/libs.a" />
3636
<Add library="../libs/bin/Release/libs.a" />
3737
</Linker>
38+
<Unit filename="configuration/functions.c">
39+
<Option compilerVar="CC" />
40+
</Unit>
41+
<Unit filename="configuration/functions.h" />
42+
<Unit filename="configuration/logs/main.c">
43+
<Option compilerVar="CC" />
44+
</Unit>
45+
<Unit filename="configuration/logs/main.h" />
46+
<Unit filename="configuration/macros.h" />
47+
<Unit filename="configuration/main.h" />
48+
<Unit filename="configuration/structs.h" />
49+
<Unit filename="macros.h" />
3850
<Unit filename="main.c">
3951
<Option compilerVar="CC" />
4052
</Unit>
53+
<Unit filename="structs.h" />
4154
<Unit filename="utilities.c">
4255
<Option compilerVar="CC" />
4356
</Unit>

0 commit comments

Comments
 (0)