Survival Royale 1.0.0
A very simple yet funny card game.
Loading...
Searching...
No Matches
config_file.c
Go to the documentation of this file.
1// Copyright (C) 2025 Giulio Salvi, Jacopo Paradisi
2//
3// This program is free software: you can redistribute it and/or modify
4// it under the terms of the GNU General Public License as published by
5// the Free Software Foundation, either version 3 of the License, or
6// (at your option) any later version.
7//
8// This program is distributed in the hope that it will be useful,
9// but WITHOUT ANY WARRANTY; without even the implied warranty of
10// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11// GNU General Public License for more details.
12//
13// You should have received a copy of the GNU General Public License
14// along with this program. If not, see <https://www.gnu.org/licenses/>.
15
20
21#include "config_file.h"
22
24 FILE* cfgFile = fopen("./game.cfg", "r");
25 if(!cfgFile)
26 return false;
27 else {
28 fclose(cfgFile);
29 return true;
30 }
31}
32
35 FILE* cfgFile = fopen("./game.cfg", "r");
36
37 while(feof(cfgFile) == 0) {
38 char line[128];
39
40 char* _ = fgets(line, 128, cfgFile);
41 if(!containsFrom(line, '=', 0))
42 continue;
43 else {
45 errno = 0;
46 char* endptr;
47 const int equalPosition = offsetFromNext(line, '=', 0);
48 char* settingName = substring(line, 0, equalPosition);
49 char* settingValue = substring(line, equalPosition + 1, strlen(line) - equalPosition - 1);
50
51 if(settingValue[strlen(settingValue) - 1] == '\n')
52 settingValue[strlen(settingValue) - 1] = '\0';
53
54 if(strcmp("useTui", settingName) == 0)
55 cfg.useTui = strcmp("true", settingValue) == 0;
56 if(strcmp("beVerbose", settingName) == 0)
57 cfg.beVerbose = strcmp("true", settingValue) == 0;
58 else if(strcmp("allowSameRank", settingName) == 0)
59 cfg.allowSameRank = strcmp("true", settingValue) == 0;
60 else if(strcmp("allowSameSuit", settingName) == 0)
61 cfg.allowSameSuit = strcmp("true", settingValue) == 0;
62 else if(strcmp("defaultPlayersLPs", settingName) == 0) {
63 const int rs = strtol(settingValue, &endptr, 10);
64
65 if(errno == ERANGE || *endptr != '\0')
66 continue;
67 else
68 cfg.defaultPlayersLPs = rs;
69 } else if(strcmp("defaultLPsOnField", settingName) == 0) {
70 const int rs = strtol(settingValue, &endptr, 10);
71
72 if(errno == ERANGE || *endptr != '\0')
73 continue;
74 else
75 cfg.defaultLPsOnField = rs;
76 }
77
78 free(settingName);
79 free(settingValue);
80 }
81 }
82
83 fclose(cfgFile);
84 }
85
87}
88
89void saveConfigurationToFile(GameConfiguration configuration, bool overwriteIfExists) {
90 if(!overwriteIfExists && existsConfigurationFile())
91 return;
92
93 FILE* cfgFile = fopen("game.cfg", "w+");
94
95 fprintf(cfgFile, "useTui=%s\n", configuration.useTui ? "true" : "false");
96 fprintf(cfgFile, "beVerbose=%s\n", configuration.beVerbose ? "true" : "false");
97 fprintf(cfgFile, "allowSameRank=%s\n", configuration.allowSameRank ? "true" : "false");
98 fprintf(cfgFile, "allowSameSuit=%s\n", configuration.allowSameSuit ? "true" : "false");
99 fprintf(cfgFile, "defaultPlayersLPs=%d\n", configuration.defaultPlayersLPs);
100 fprintf(cfgFile, "defaultLPsOnField=%d\n", configuration.defaultLPsOnField);
101
102 fclose(cfgFile);
103}
bool existsConfigurationFile()
Checks by filename if a game configuration file exists.
Definition config_file.c:23
void saveConfigurationToFile(GameConfiguration configuration, bool overwriteIfExists)
Save the given configuration to the game configuration file. It overwrites an existing file if and on...
Definition config_file.c:89
GameConfiguration getConfigurationFromFile()
Load the game configuration from the file. It checks if the file exists.
Definition config_file.c:33
Definition of functions which handles the configuration file.
GameConfiguration getDefaultConfiguration()
Retrieves the default game configuration.
Definition config.c:23
The game configuration structure.
bool allowSameRank
Determines if a player can have cards with the same rank. It increases randomness if set to false.
bool useTui
Determines if the gama has to use the terminal user interface (TUI).
int defaultLPsOnField
The default amount of LPs on the playing field at the beginning of a match.
bool beVerbose
Determines if the program has to have a verbose logging behaviour.
int defaultPlayersLPs
The default amount of LPs that every player have at the beginning of a match.
bool allowSameSuit
Determines if a player can have cards with the same suit It increases randomness if set to false.
bool containsFrom(const char *string, char character, int startPosition)
Checks if there is an occoruncy of the passed char from the passed position in the passed string.
Definition utility.c:63
int offsetFromNext(const char *string, char character, int startPosition)
Counts the offset in the string between the position and the next occoruncy of the passed char.
Definition utility.c:38
char * substring(const char *string, int startPosition, int length)
Copies a substring (heap-allocated) from the passed string. If fails when allocating memory for the s...
Definition utility.c:23