3dscontroller-ng/3DS/source/settings.c

82 lines
1.7 KiB
C
Raw Normal View History

2014-12-10 19:19:25 +00:00
#include <stdio.h>
2014-12-05 20:00:27 +00:00
#include <malloc.h>
2014-12-10 19:19:25 +00:00
#include <3ds.h>
2014-12-05 20:00:27 +00:00
#include "wireless.h"
#include "settings.h"
2014-12-12 18:44:26 +00:00
struct settings settings;
struct settings defaultSettings = {
IPString: "",
port: DEFAULT_PORT,
};
2014-12-05 20:00:27 +00:00
Handle fileHandle;
2014-12-12 18:44:26 +00:00
static bool getSetting(char *name, char *src, char *dest) {
char *start = strstr(src, name);
if(start) {
start += strlen(name);
char *end = start + strlen(start);
if(strstr(start, "\n") - 1 < end) end = strstr(start, "\n") - 1;
size_t size = (size_t)end - (size_t)start;
strncpy(dest, start, size);
dest[size] = '\0';
return true;
}
return false;
}
2014-12-05 20:00:27 +00:00
bool readSettings(void) {
2014-12-12 18:44:26 +00:00
char *buffer = NULL;
2014-12-05 20:00:27 +00:00
u64 size;
u32 bytesRead;
FS_archive sdmcArchive = (FS_archive){ARCH_SDMC, (FS_path){PATH_EMPTY, 1, (u8*)""}};
FS_path filePath = FS_makePath(PATH_CHAR, "/3DSController.ini");
Result ret = FSUSER_OpenFileDirectly(NULL, &fileHandle, sdmcArchive, filePath, FS_OPEN_READ, FS_ATTRIBUTE_NONE);
if(ret) return false;
ret = FSFILE_GetSize(fileHandle, &size);
if(ret) return false;
buffer = malloc(size);
if(!buffer) return false;
ret = FSFILE_Read(fileHandle, &bytesRead, 0x0, buffer, size);
2014-12-12 18:44:26 +00:00
if(ret || size != bytesRead) return false;
2014-12-05 20:00:27 +00:00
ret = FSFILE_Close(fileHandle);
if(ret) return false;
2014-12-12 18:44:26 +00:00
memcpy(&settings, &defaultSettings, sizeof(struct settings));
2014-12-06 12:59:27 +00:00
2014-12-12 18:44:26 +00:00
char setting[64] = { '\0' };
2014-12-10 19:19:25 +00:00
2014-12-12 18:44:26 +00:00
if(getSetting("IP: ", buffer, settings.IPString)) {
//inet_pton(AF_INET, settings.IPString, &(saout.sin_addr));
inet_pton4(settings.IPString, (unsigned char *)&(saout.sin_addr));
}
else {
free(buffer);
return false;
2014-12-10 19:01:40 +00:00
}
2014-12-12 18:44:26 +00:00
if(getSetting("Port: ", buffer, setting)) {
sscanf(setting, "%d", &settings.port);
}
2014-12-05 20:00:27 +00:00
free(buffer);
return true;
}