view 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
line wrap: on
line source

/**
 * utils.c:
 *
 * Useful utilities for general use.
**/
#include "utils.h"

#include <sysinfoapi.h>
#include <windef.h>
#include <winbase.h>
#include <winuser.h>
#include <stringapiset.h>
#include <stdint.h>

/* self explanatory. */
uint64_t get_system_time_in_milliseconds(void) {
 	FILETIME ft;
	GetSystemTimeAsFileTime(&ft);
	uint64_t ul = (uint64_t) ft.dwHighDateTime << 32 | ft.dwLowDateTime;
	return ((ul - 116444736000000000ULL) * (1.0/10000000));
}

/*
 * appends the contents of a UTF-8 string with the content of a wide string
 *
 * [in] LPCWSTR wstr, the wstring to append
 * [out] LPSTR str, the utf-8 string to append to
 * [in] size_t offset, an offset in bytes of `str` to append at
 * [in] size_t size, the total size of `str` in bytes
 *
 * if the function fails, it returns 0.
 * if the string is empty (i.e. str[0] == '\0'), this function returns 0.
 * if the wide string is too large to be appended within `size`, it returns 0.
 * on success, it returns the amount of characters written ***NOT INCLUDING THE
 * NUL BYTE***.
*/
size_t append_wstr_to_utf8(LPCWSTR wstr, LPSTR str, size_t offset, size_t size) {
	if (!wstr || !str)
		return 0;

	const int wstr_len = WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, wstr, -1, NULL, 0, NULL, NULL);
	if (wstr_len < 0 || offset + wstr_len >= size)
		return 0;

	const int actual_len = WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, wstr, -1, str + offset, wstr_len, NULL, NULL);
	if (actual_len < 1)
		return 0;

	return actual_len - 1; // remove nul byte from offset
}