all repos — mgba @ 6d89c37886d461c0fad868a4c1dc12fd29a2b8af

mGBA Game Boy Advance Emulator

src/gba/gba-config.c (view raw)

 1#include "gba-config.h"
 2
 3#include "platform/commandline.h"
 4#include "util/configuration.h"
 5
 6#define SECTION_NAME_MAX 128
 7
 8static const char* _lookupValue(const struct Configuration* config, const char* key, const char* port) {
 9	if (port) {
10		char sectionName[SECTION_NAME_MAX];
11		snprintf(sectionName, SECTION_NAME_MAX, "ports.%s", port);
12		sectionName[SECTION_NAME_MAX - 1] = '\0';
13		const char* value = ConfigurationGetValue(config, sectionName, key);
14		if (value) {
15			return value;
16		}
17	}
18	return ConfigurationGetValue(config, 0, key);
19}
20
21static bool _lookupCharValue(const struct Configuration* config, const char* key, const char* port, char** out) {
22	const char* value = _lookupValue(config, key, port);
23	if (!value) {
24		return false;
25	}
26	*out = strdup(value);
27	return true;
28}
29
30static bool _lookupIntValue(const struct Configuration* config, const char* key, const char* port, int* out) {
31	const char* charValue = _lookupValue(config, key, port);
32	if (!charValue) {
33		return false;
34	}
35	char* end;
36	long value = strtol(charValue, &end, 10);
37	if (*end) {
38		return false;
39	}
40	*out = value;
41	return true;
42}
43
44static bool _lookupUIntValue(const struct Configuration* config, const char* key, const char* port, unsigned* out) {
45	const char* charValue = _lookupValue(config, key, port);
46	if (!charValue) {
47		return false;
48	}
49	char* end;
50	unsigned long value = strtoul(charValue, &end, 10);
51	if (*end) {
52		return false;
53	}
54	*out = value;
55	return true;
56}
57
58static bool _lookupFloatValue(const struct Configuration* config, const char* key, const char* port, float* out) {
59	const char* charValue = _lookupValue(config, key, port);
60	if (!charValue) {
61		return false;
62	}
63	char* end;
64	float value = strtof(charValue, &end);
65	if (*end) {
66		return false;
67	}
68	*out = value;
69	return true;
70}
71
72bool GBAConfigLoad(struct Configuration* config) {
73	return ConfigurationRead(config, BINARY_NAME ".ini");
74}
75
76void GBAConfigMapGeneralOpts(const struct Configuration* config, const char* port, struct GBAOptions* opts) {
77	_lookupCharValue(config, "bios", port, &opts->bios);
78	_lookupIntValue(config, "logLevel", port, &opts->logLevel);
79	_lookupIntValue(config, "frameskip", port, &opts->frameskip);
80	_lookupIntValue(config, "rewindBufferCapacity", port, &opts->rewindBufferCapacity);
81	_lookupIntValue(config, "rewindBufferInterval", port, &opts->rewindBufferInterval);
82	_lookupFloatValue(config, "fpsTarget", port, &opts->fpsTarget);
83	unsigned audioBuffers;
84	if (_lookupUIntValue(config, "audioBuffers", port, &audioBuffers)) {
85		opts->audioBuffers = audioBuffers;
86	}
87}
88
89void GBAConfigMapGraphicsOpts(const struct Configuration* config, const char* port, struct GBAOptions* opts) {
90	_lookupIntValue(config, "fullscreen", port, &opts->fullscreen);
91	_lookupIntValue(config, "width", port, &opts->width);
92	_lookupIntValue(config, "height", port, &opts->height);
93}
94
95void GBAConfigFreeOpts(struct GBAOptions* opts) {
96	free(opts->bios);
97	opts->bios = 0;
98}