annotate src/utils.c @ 12:dd427b7cc459 default tip

json: replace with nxjson library more lightweight, reduces the binary size by about 40 kb
author Paper <paper@paper.us.eu.org>
date Fri, 15 Mar 2024 20:46:18 -0400
parents e6a594f16403
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 }