comparison src/config.c @ 0:d91dfd53b8b4

Initial commit
author Paper <mrpapersonic@gmail.com>
date Sun, 07 Aug 2022 07:26:27 -0400
parents
children 7abb5d8b20ea
comparison
equal deleted inserted replaced
-1:000000000000 0:d91dfd53b8b4
1 #include <assert.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include "dirtools.h"
6 #include "config.h"
7 #ifndef WIN32_LEAN_AND_MEAN
8 # define WIN32_LEAN_AND_MEAN
9 #endif
10 #include <windows.h>
11 #define MAX_LINE_LENGTH 128
12
13 static unsigned int crc32b(unsigned char *message) {
14 int i, j;
15 unsigned int byte, crc, mask;
16
17 i = 0;
18 crc = 0xFFFFFFFF;
19 while (message[i] != 0) {
20 byte = message[i]; // Get next byte.
21 crc = crc ^ byte;
22 for (j = 7; j >= 0; j--) { // Do eight times.
23 mask = -(crc & 1);
24 crc = (crc >> 1) ^ (0xEDB88320 & mask);
25 }
26 i = i + 1;
27 }
28 return ~crc;
29 }
30
31 int cfg_load(struct config_t* config) {
32 char* path = NULL;
33 char line[MAX_LINE_LENGTH] = {0};
34 FILE* config_fp;
35 path = dirtools_concat_paths(getenv("APPDATA"), "Winamp\\Plugins\\wgsdk\\config.txt");
36 config_fp = fopen(path, "r");
37 if (config_fp == NULL) {
38 free(path);
39 return 1;
40 }
41 while (fgets(line, MAX_LINE_LENGTH, config_fp)) {
42 switch (crc32b((unsigned char*)strtok(line, "="))) {
43 case 0x2a666380: // display_title
44 config->display_title = atoi(strtok(NULL, "="));
45 break;
46 case 0xbb631f7f: // show_elapsed_time
47 config->show_elapsed_time = atoi(strtok(NULL, "="));
48 break;
49 default:
50 break;
51 }
52 }
53 free(path);
54 return 0;
55 }
56
57 int cfg_save(struct config_t config) {
58 char* path = NULL;
59 FILE* config_fp;
60 path = dirtools_concat_paths(getenv("APPDATA"), "Winamp\\Plugins\\wgsdk");
61 assert(dirtools_create_directory(path));
62 free(path);
63 path = dirtools_concat_paths(getenv("APPDATA"), "Winamp\\Plugins\\wgsdk\\config.txt");
64 config_fp = fopen(path, "w");
65 if (config_fp == NULL) {
66 return 1;
67 }
68 fprintf(config_fp, "display_title=%d\n", config.display_title);
69 fprintf(config_fp, "show_elapsed_time=%d\n", config.show_elapsed_time);
70 fclose(config_fp);
71 return 0;
72 }