annotate src/utils.c @ 11:e6a594f16403

*: huge refactor the config file has changed drastically, moving to an ini file from that custom format; i *would* have used the win32 functions for those, but they were barely functional, so I decided on using ini.h which is lightweight enough. additionally, I've added Deezer support so album art will be displayed! unfortunately though winhttp is a pain in the ass so if I send a request with any form of unicode chars in it it just returns a "bad request" error. I've tried debugging this but I could never really come up with anything: my hypothesis is that deezer expects their characters in percent-encoded UTF-8, but winhttp is sending them in some other encoding. the config dialog was moved out of config.c (overdue) and many more options are given in the config as well. main.c has been renamed to plugin.c to better differentiate it from... everything else.
author Paper <paper@paper.us.eu.org>
date Thu, 14 Mar 2024 20:25:37 -0400
parents 42ac054c0231
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
7
be4835547dd0 clean up code, convert git files to hg, etc.
Paper
parents:
diff changeset
1 /**
be4835547dd0 clean up code, convert git files to hg, etc.
Paper
parents:
diff changeset
2 * utils.c:
be4835547dd0 clean up code, convert git files to hg, etc.
Paper
parents:
diff changeset
3 *
be4835547dd0 clean up code, convert git files to hg, etc.
Paper
parents:
diff changeset
4 * Useful utilities for general use.
be4835547dd0 clean up code, convert git files to hg, etc.
Paper
parents:
diff changeset
5 **/
10
42ac054c0231 *: huge refactoring
Paper <paper@paper.us.eu.org>
parents: 9
diff changeset
6 #include "utils.h"
42ac054c0231 *: huge refactoring
Paper <paper@paper.us.eu.org>
parents: 9
diff changeset
7
42ac054c0231 *: huge refactoring
Paper <paper@paper.us.eu.org>
parents: 9
diff changeset
8 #include <sysinfoapi.h>
11
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents: 10
diff changeset
9 #include <windef.h>
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents: 10
diff changeset
10 #include <winbase.h>
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents: 10
diff changeset
11 #include <winuser.h>
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents: 10
diff changeset
12 #include <stringapiset.h>
7
be4835547dd0 clean up code, convert git files to hg, etc.
Paper
parents:
diff changeset
13 #include <stdint.h>
be4835547dd0 clean up code, convert git files to hg, etc.
Paper
parents:
diff changeset
14
11
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents: 10
diff changeset
15 /* self explanatory. */
7
be4835547dd0 clean up code, convert git files to hg, etc.
Paper
parents:
diff changeset
16 uint64_t get_system_time_in_milliseconds(void) {
be4835547dd0 clean up code, convert git files to hg, etc.
Paper
parents:
diff changeset
17 FILETIME ft;
be4835547dd0 clean up code, convert git files to hg, etc.
Paper
parents:
diff changeset
18 GetSystemTimeAsFileTime(&ft);
be4835547dd0 clean up code, convert git files to hg, etc.
Paper
parents:
diff changeset
19 uint64_t ul = (uint64_t) ft.dwHighDateTime << 32 | ft.dwLowDateTime;
11
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents: 10
diff changeset
20 return ((ul - 116444736000000000ULL) * (1.0/10000000));
7
be4835547dd0 clean up code, convert git files to hg, etc.
Paper
parents:
diff changeset
21 }
be4835547dd0 clean up code, convert git files to hg, etc.
Paper
parents:
diff changeset
22
11
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents: 10
diff changeset
23 /*
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents: 10
diff changeset
24 * appends the contents of a UTF-8 string with the content of a wide string
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents: 10
diff changeset
25 *
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents: 10
diff changeset
26 * [in] LPCWSTR wstr, the wstring to append
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents: 10
diff changeset
27 * [out] LPSTR str, the utf-8 string to append to
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents: 10
diff changeset
28 * [in] size_t offset, an offset in bytes of `str` to append at
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents: 10
diff changeset
29 * [in] size_t size, the total size of `str` in bytes
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents: 10
diff changeset
30 *
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents: 10
diff changeset
31 * if the function fails, it returns 0.
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents: 10
diff changeset
32 * if the string is empty (i.e. str[0] == '\0'), this function returns 0.
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents: 10
diff changeset
33 * if the wide string is too large to be appended within `size`, it returns 0.
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents: 10
diff changeset
34 * on success, it returns the amount of characters written ***NOT INCLUDING THE
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents: 10
diff changeset
35 * NUL BYTE***.
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents: 10
diff changeset
36 */
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents: 10
diff changeset
37 size_t append_wstr_to_utf8(LPCWSTR wstr, LPSTR str, size_t offset, size_t size) {
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents: 10
diff changeset
38 if (!wstr || !str)
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents: 10
diff changeset
39 return 0;
7
be4835547dd0 clean up code, convert git files to hg, etc.
Paper
parents:
diff changeset
40
11
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents: 10
diff changeset
41 const int wstr_len = WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, wstr, -1, NULL, 0, NULL, NULL);
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents: 10
diff changeset
42 if (wstr_len < 0 || offset + wstr_len >= size)
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents: 10
diff changeset
43 return 0;
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents: 10
diff changeset
44
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents: 10
diff changeset
45 const int actual_len = WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, wstr, -1, str + offset, wstr_len, NULL, NULL);
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents: 10
diff changeset
46 if (actual_len < 1)
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents: 10
diff changeset
47 return 0;
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents: 10
diff changeset
48
e6a594f16403 *: huge refactor
Paper <paper@paper.us.eu.org>
parents: 10
diff changeset
49 return actual_len - 1; // remove nul byte from offset
7
be4835547dd0 clean up code, convert git files to hg, etc.
Paper
parents:
diff changeset
50 }