1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
|
//|_ _ _. _ ._ |_ _. _ |
//| | (/_ (_| \/ (/_ | | | | (_| (_ |<
#include "setting.h"
const char* get_path() {
static char out_dir[260]{};
if( !out_dir[0] ) {
strcat( out_dir, getenv( "USERPROFILE" ) );
strcat( out_dir, "/" );
strcat( out_dir, ".godword" );
}
return out_dir;
}
// credits: gcc
size_t getline(char **lineptr, size_t *n, FILE *stream) {
char *bufptr = NULL;
char *p = bufptr;
size_t size;
int c;
if (lineptr == NULL) {
return -1;
}
if (stream == NULL) {
return -1;
}
if (n == NULL) {
return -1;
}
bufptr = *lineptr;
size = *n;
c = fgetc(stream);
if (c == EOF) {
return -1;
}
if (bufptr == NULL) {
bufptr = (char*)malloc(128);
if (bufptr == NULL) {
return -1;
}
size = 128;
}
p = bufptr;
while(c != EOF) {
if ((p - bufptr) > int(size - 1)) {
size = size + 128;
bufptr = (char*)realloc(bufptr, size);
if (bufptr == NULL) {
return -1;
}
}
*p++ = c;
if (c == '\n') {
break;
}
c = fgetc(stream);
}
*p++ = '\0';
*lineptr = bufptr;
*n = size;
return p - bufptr - 1;
}
void setting_save( const char* name, const void* src, U32 size ) {
char* buffer = (char*)malloc( size * 2 + 1 );
const U8* data = (const U8*)( src );
memset( buffer, 0, size * 2 + 1 );
for( U32 i = 0; i < size; ++i ) {
sprintf( &buffer[2 * i], "%02x", data[i] );
}
FILE* f = fopen( get_path(), "a" );
if( !f )
return free( buffer );
fprintf( f, "%s = %s\n", name, buffer );
fflush( f );
free( buffer );
fclose( f );
}
void setting_load( const char* name, const void* dst, U32 size ) {
char* buffer = (char*)malloc( size * 2 + 1 );
U8* data = (U8*)( dst );
memset( buffer, 0, size * 2 + 1 );
FILE* f = fopen( get_path(), "r+" );
if( !f )
return free( buffer );
char read_name[64]{};
char* line = nullptr;
U32 len = 128;
/*
* probably not the proper way
* don't wanna fuck with an entire ini parser
*/
while( getline( &line, &len, f ) != -1 ) {
if( line ) {
sscanf( line, "%s = %s", read_name, buffer );
if( !strcmp( name, read_name ) )
break;
free( line );
line = nullptr;
}
}
fclose( f );
if( !buffer[0] )
return free( buffer );
for( U32 i = 0; i < size; ++i ) {
unsigned temp;
sscanf( &buffer[2 * i], "%02x", &temp );
data[i] = temp;
}
free( buffer );
}
|