diff --git a/inc/lektor/bufferfd.h b/inc/lektor/bufferfd.h index 97bc0237268efdc33f5757032c108be91187dc35..c6e4ccbc826bcc4608bc1f67618aa2bff3c36f71 100644 --- a/inc/lektor/bufferfd.h +++ b/inc/lektor/bufferfd.h @@ -23,7 +23,7 @@ #pragma once -#include <lektor/define.h> +#include <lektor/common.h> #include <stdint.h> #include <stdlib.h> diff --git a/inc/lektor/cmd.h b/inc/lektor/cmd.h index 3ac6c3ad47698fa9996cce8320b3462ce945bc5e..de18772650d1e335c7fdfc9e1aa937a4ee2e555e 100644 --- a/inc/lektor/cmd.h +++ b/inc/lektor/cmd.h @@ -1,6 +1,5 @@ #pragma once -#include <lektor/define.h> #include <stdnoreturn.h> #include <stddef.h> diff --git a/inc/lektor/commands.h b/inc/lektor/commands.h index a7ada9fbd7854d1183a71aca85767ecfe5aa1feb..8ea3e82fbb321e50a1f17a144296a5949fb4f52c 100644 --- a/inc/lektor/commands.h +++ b/inc/lektor/commands.h @@ -1,6 +1,6 @@ #pragma once -#include <lektor/define.h> +#include <lektor/common.h> #include <lektor/net.h> #include <lektor/window.h> diff --git a/inc/lektor/common.h b/inc/lektor/common.h new file mode 100644 index 0000000000000000000000000000000000000000..64c434566054b89467841b8698f018b0fb5344fe --- /dev/null +++ b/inc/lektor/common.h @@ -0,0 +1,120 @@ +#pragma once + +#include <common/macro.h> +#include <common/define.h> +#include <stdint.h> +#include <stdlib.h> + +/* Defines */ + +#define LKT_MAX_SQLITE_STATEMENT 1024 +#define PROTECTED_DATABASE "disk" + +#define LKT_DATABASE_NAME_KID "id" +#define LKT_DATABASE_NAME_KNAME "source_name" +#define LKT_DATABASE_NAME_KTITLE "song_title" +#define LKT_DATABASE_NAME_KCAT "song_type" +#define LKT_DATABASE_NAME_KTYPE "category" +#define LKT_DATABASE_NAME_KAUTHOR "author_name" +#define LKT_DATABASE_NAME_KAUTHOR_YEAR "author_year" +#define LKT_DATABASE_NAME_KLANG "language" +#define LKT_DATABASE_KARA_COLUMNT_ANY "any_col" +#define LKT_DATABASE_KARA_ALL "string" + +#define LEKTOR_TAG_MAX 256 +#define LKT_MESSAGE_ARGS_MAX 32 +#define LKT_MESSAGE_MAX 2048 +#define LKT_DEFAULT_LIST_SIZE 10 + +enum mpd_idle_flag { + MPD_IDLE_NONE = 0, + + MPD_IDLE_DATABASE = ( 1 << 1 ), + MPD_IDLE_UPDATE = ( 1 << 2 ), + MPD_IDLE_STORED_PLAYLIST = ( 1 << 3 ), + MPD_IDLE_PLAYLIST = ( 1 << 4 ), + MPD_IDLE_PLAYER = ( 1 << 5 ), + MPD_IDLE_MIXER = ( 1 << 6 ), + MPD_IDLE_OUTPUT = ( 1 << 7 ), + MPD_IDLE_OPTIONS = ( 1 << 8 ), + MPD_IDLE_PARTITION = ( 1 << 9 ), + MPD_IDLE_STICKER = ( 1 << 10 ), + MPD_IDLE_SUBSCRIPTION = ( 1 << 11 ), + MPD_IDLE_MESSAGE = ( 1 << 12 ), + + MPD_IDLE_ALL = + MPD_IDLE_DATABASE | MPD_IDLE_UPDATE | MPD_IDLE_STORED_PLAYLIST | + MPD_IDLE_PLAYLIST | MPD_IDLE_PLAYER | MPD_IDLE_MIXER | + MPD_IDLE_OUTPUT | MPD_IDLE_OPTIONS | MPD_IDLE_PARTITION | + MPD_IDLE_STICKER | MPD_IDLE_SUBSCRIPTION | MPD_IDLE_MESSAGE, +}; + +#define LKT_BACKLOG 32 +#define COMMAND_LIST_MAX 64 +#define BUFFER_OUT_MAX 16 +#define MPD_VERSION "0.21.16" + +/* Macros */ + +#define SQLITE_PREPARE(db, stmt, SQL, goto_label) \ + if (sqlite3_prepare_v2(db, SQL, -1, &(stmt), 0) != SQLITE_OK) { \ + LOG_ERROR_SCT("DB", "Failed to prepare statement: %s", \ + sqlite3_errmsg(db)); \ + goto goto_label; \ + } + +#define SQLITE_EXEC(db, SQL, goto_label) \ + if (sqlite3_exec(db, SQL, NULL, NULL, NULL) != SQLITE_OK) { \ + LOG_ERROR_SCT("DB", "Failed to exec statement: %s", \ + sqlite3_errmsg(db)); \ + goto goto_label; \ + } + +#define SQLITE_BIND_TEXT(db, stmt, pos, text, error) \ + if (sqlite3_bind_text(stmt, pos, text, -1, 0) != SQLITE_OK) { \ + LOG_ERROR_SCT("DB", "Failed to bind text %s at pos %d: %s", \ + text, pos, sqlite3_errmsg(db)); \ + goto error; \ + } + +#define SQLITE_BIND_INT(db, stmt, pos, integer, error) \ + if (sqlite3_bind_int(stmt, pos, integer) != SQLITE_OK) { \ + LOG_ERROR_SCT("DB", "Failed to bind int %d at pos %d: %s", \ + integer, pos, sqlite3_errmsg(db)); \ + goto error; \ + } + +#define SQLITE_STEP(db, stmt, code, error) \ + if (sqlite3_step(stmt) != code) { \ + LOG_ERROR_SCT("DB", "Failed to step and get a row: %s", \ + sqlite3_errmsg(db)); \ + goto error; \ + } + +#define SQLITE_STEP_ROW(db, stmt, error) SQLITE_STEP(db, stmt, SQLITE_ROW, error) +#define SQLITE_STEP_DONE(db, stmt, error) SQLITE_STEP(db, stmt, SQLITE_DONE, error) +#define SQLITE_STEP_OK(db, stmt, error) SQLITE_STEP(db, stmt, SQLITE_OK, error) + +#define SQLITE_DO_ROLLBACK(db) \ + sqlite3_exec(db, "ROLLBACK TRANSACTION;\n", NULL, NULL, NULL); + +/* Random things */ + +/* Read `bytes` as the big endian representation of a 32-bit unsigned integer. + `n` is the number of bytes in `bytes` + Returns 0 if n is 0. + Restriction: n <= 4 */ +uint32_t be_uint32_t(const uint8_t bytes[], size_t n); + +/* Same as `be_uint32_t` but for 64-bit unsigned integers. + Restriction: n <= 8 */ +uint64_t be_uint64_t(const uint8_t bytes[], size_t n); + +/* Trim the string `str` of the char `c` from the right and the left. + The string may not be null terminated. + Returns a pointer to the the trimmed string + Restrictions: the len of the string must be `len`. */ +char *trim(char *str, size_t len, char c); + +/* Get a line from stdin safely. */ +int get_stdin_line(const char *prompt, char *buf, size_t len); diff --git a/inc/lektor/config.h b/inc/lektor/config.h index fc013160204b3dd3f45f7fc5ff0aa9789a38da8a..618de57c06aacfaf1cb041d704032f1e7dbe7441 100644 --- a/inc/lektor/config.h +++ b/inc/lektor/config.h @@ -1,6 +1,6 @@ #pragma once -#include <lektor/define.h> +#include <lektor/common.h> #include <stddef.h> #include <sqlite3.h> diff --git a/inc/lektor/database.h b/inc/lektor/database.h index 194bb101bb000215d07e66b15eb8a90866cb193e..ed5a5fae6a4423599eca367ba714c44db574a076 100644 --- a/inc/lektor/database.h +++ b/inc/lektor/database.h @@ -1,6 +1,6 @@ #pragma once -#include <lektor/define.h> +#include <lektor/common.h> #include <lektor/mkv.h> #include <lektor/uri.h> diff --git a/inc/lektor/define.h b/inc/lektor/define.h deleted file mode 100644 index d68144f42a19e9dae0c1378489aa92e005361c8e..0000000000000000000000000000000000000000 --- a/inc/lektor/define.h +++ /dev/null @@ -1,51 +0,0 @@ -#pragma once - -#include <common/define.h> - -#define LKT_MAX_SQLITE_STATEMENT 1024 -#define PROTECTED_DATABASE "disk" - -#define LKT_DATABASE_NAME_KID "id" -#define LKT_DATABASE_NAME_KNAME "source_name" -#define LKT_DATABASE_NAME_KTITLE "song_title" -#define LKT_DATABASE_NAME_KCAT "song_type" -#define LKT_DATABASE_NAME_KTYPE "category" -#define LKT_DATABASE_NAME_KAUTHOR "author_name" -#define LKT_DATABASE_NAME_KAUTHOR_YEAR "author_year" -#define LKT_DATABASE_NAME_KLANG "language" -#define LKT_DATABASE_KARA_COLUMNT_ANY "any_col" -#define LKT_DATABASE_KARA_ALL "string" - -#define LEKTOR_TAG_MAX 256 -#define LKT_MESSAGE_ARGS_MAX 32 -#define LKT_MESSAGE_MAX 2048 -#define LKT_DEFAULT_LIST_SIZE 10 - -enum mpd_idle_flag { - MPD_IDLE_NONE = 0, - - MPD_IDLE_DATABASE = ( 1 << 1 ), - MPD_IDLE_UPDATE = ( 1 << 2 ), - MPD_IDLE_STORED_PLAYLIST = ( 1 << 3 ), - MPD_IDLE_PLAYLIST = ( 1 << 4 ), - MPD_IDLE_PLAYER = ( 1 << 5 ), - MPD_IDLE_MIXER = ( 1 << 6 ), - MPD_IDLE_OUTPUT = ( 1 << 7 ), - MPD_IDLE_OPTIONS = ( 1 << 8 ), - MPD_IDLE_PARTITION = ( 1 << 9 ), - MPD_IDLE_STICKER = ( 1 << 10 ), - MPD_IDLE_SUBSCRIPTION = ( 1 << 11 ), - MPD_IDLE_MESSAGE = ( 1 << 12 ), - - MPD_IDLE_ALL = - MPD_IDLE_DATABASE | MPD_IDLE_UPDATE | MPD_IDLE_STORED_PLAYLIST | - MPD_IDLE_PLAYLIST | MPD_IDLE_PLAYER | MPD_IDLE_MIXER | - MPD_IDLE_OUTPUT | MPD_IDLE_OPTIONS | MPD_IDLE_PARTITION | - MPD_IDLE_STICKER | MPD_IDLE_SUBSCRIPTION | MPD_IDLE_MESSAGE, -}; - -#define LKT_BACKLOG 32 -#define COMMAND_LIST_MAX 64 -#define BUFFER_OUT_MAX 16 -#define MPD_VERSION "0.21.16" - diff --git a/inc/lektor/macro.h b/inc/lektor/macro.h deleted file mode 100644 index 48c66716fec43acae1c01471611bcd0c95a87113..0000000000000000000000000000000000000000 --- a/inc/lektor/macro.h +++ /dev/null @@ -1,44 +0,0 @@ -#pragma once -#include <common/macro.h> - -#define SQLITE_PREPARE(db, stmt, SQL, goto_label) \ - if (sqlite3_prepare_v2(db, SQL, -1, &(stmt), 0) != SQLITE_OK) { \ - LOG_ERROR_SCT("DB", "Failed to prepare statement: %s", \ - sqlite3_errmsg(db)); \ - goto goto_label; \ - } - -#define SQLITE_EXEC(db, SQL, goto_label) \ - if (sqlite3_exec(db, SQL, NULL, NULL, NULL) != SQLITE_OK) { \ - LOG_ERROR_SCT("DB", "Failed to exec statement: %s", \ - sqlite3_errmsg(db)); \ - goto goto_label; \ - } - -#define SQLITE_BIND_TEXT(db, stmt, pos, text, error) \ - if (sqlite3_bind_text(stmt, pos, text, -1, 0) != SQLITE_OK) { \ - LOG_ERROR_SCT("DB", "Failed to bind text %s at pos %d: %s", \ - text, pos, sqlite3_errmsg(db)); \ - goto error; \ - } - -#define SQLITE_BIND_INT(db, stmt, pos, integer, error) \ - if (sqlite3_bind_int(stmt, pos, integer) != SQLITE_OK) { \ - LOG_ERROR_SCT("DB", "Failed to bind int %d at pos %d: %s", \ - integer, pos, sqlite3_errmsg(db)); \ - goto error; \ - } - -#define SQLITE_STEP(db, stmt, code, error) \ - if (sqlite3_step(stmt) != code) { \ - LOG_ERROR_SCT("DB", "Failed to step and get a row: %s", \ - sqlite3_errmsg(db)); \ - goto error; \ - } - -#define SQLITE_STEP_ROW(db, stmt, error) SQLITE_STEP(db, stmt, SQLITE_ROW, error) -#define SQLITE_STEP_DONE(db, stmt, error) SQLITE_STEP(db, stmt, SQLITE_DONE, error) -#define SQLITE_STEP_OK(db, stmt, error) SQLITE_STEP(db, stmt, SQLITE_OK, error) - -#define SQLITE_DO_ROLLBACK(db) \ - sqlite3_exec(db, "ROLLBACK TRANSACTION;\n", NULL, NULL, NULL); diff --git a/inc/lektor/mkv.h b/inc/lektor/mkv.h index 3dfdf09a3a9c1c0b0db01ed08e3edaa4d75f01bc..c88114269b99f7087f27a031160f77922faf0cff 100644 --- a/inc/lektor/mkv.h +++ b/inc/lektor/mkv.h @@ -23,7 +23,7 @@ */ #pragma once -#include <lektor/define.h> +#include <lektor/common.h> #include <linux/limits.h> #include <stddef.h> diff --git a/inc/lektor/module/module_sdl2.h b/inc/lektor/module/module_sdl2.h index 9821268e3c973402f26e0a58e7a163320e7b9ffc..90eacc578aa4d769643c33c59e9eff5ff847fd22 100644 --- a/inc/lektor/module/module_sdl2.h +++ b/inc/lektor/module/module_sdl2.h @@ -1,6 +1,5 @@ #pragma once -#include <lektor/define.h> #include <stdbool.h> #include <sqlite3.h> #include <lektor/window.h> diff --git a/inc/lektor/module/module_x11.h b/inc/lektor/module/module_x11.h index 16c01718185bdd3e28a1b8c9835de65df21ab7b1..0ea84737be6fed8b92296da2d9dc3b46483ed552 100644 --- a/inc/lektor/module/module_x11.h +++ b/inc/lektor/module/module_x11.h @@ -1,6 +1,5 @@ #pragma once -#include <lektor/define.h> #include <stdbool.h> #include <sqlite3.h> #include <lektor/window.h> diff --git a/inc/lektor/net.h b/inc/lektor/net.h index 19a8a9278ef9571b47fafe91faba73d83df91758..ac9c15065ec93514f88aad02022ba2bd156491be 100644 --- a/inc/lektor/net.h +++ b/inc/lektor/net.h @@ -1,11 +1,19 @@ #pragma once -#include <lektor/repo.h> -#include <lektor/define.h> +#include <lektor/mkv.h> +#include <lektor/common.h> #include <lektor/config.h> #include <lektor/window.h> +#include <lektor/thread.h> +#include <lektor/uri.h> + #include <sqlite3.h> #include <mpv/client.h> +#include <curl/curl.h> +#include <sqlite3.h> +#include <json-c/json.h> +#include <inttypes.h> +#include <stdbool.h> #include <stddef.h> @@ -15,6 +23,39 @@ struct lkt_command { long cont; }; +/* Repository, e.g. Kurisu */ +struct lkt_repo { + /* Just the repo */ + const char *name; + const char *base_url; + const char *kara_dir; + const char *get_all_json; + const char *get_id_json; + const char *get_id_file; + const uint64_t version; + + /* The database */ + volatile sqlite3 *db; +}; + +int repo_new(struct lkt_repo *const repo, const char *name, const char *url, volatile sqlite3 *db); +void repo_free(struct lkt_repo *const repo); + +/* Get metadata of a kara. */ +int repo_get_id (struct lkt_repo *const repo, const uint64_t id, struct kara_metadata *mdt); +int repo_get_alljson_sync(struct lkt_repo *const repo, struct json_object **json); +int repo_get_allid_async (struct lkt_repo *const repo); + +/* Download a kara. */ +int repo_download_id_sync (struct lkt_repo *const repo, const uint64_t id, const char *kara_path, struct kara_metadata *mdt); +int repo_download_id_async(struct lkt_repo *const repo, const size_t id); + +/* Scan and update the DB. + If only update from the kurisu repo, rescan must be null, if you want to + also scan the directories, set rescan to a non zero value. If the uri is + null, will just update/rescan everything. */ +int repo_sync(struct lkt_repo *const repo, struct lkt_uri *uri, int rescan); + /* Create and destruct a command structure. */ struct lkt_command lkt_command_parse(char *raw); struct lkt_command lkt_command_clone(struct lkt_command *c); @@ -65,3 +106,4 @@ bool lkt_client_auth(struct lkt_state *srv, size_t c, bool set_auth); /* The server's listen function. */ int lkt_listen(void); + diff --git a/inc/lektor/repo.h b/inc/lektor/repo.h deleted file mode 100644 index ffd5bc8eac25b6bb3144d21b1043804de776b646..0000000000000000000000000000000000000000 --- a/inc/lektor/repo.h +++ /dev/null @@ -1,44 +0,0 @@ -#pragma once - -#include <lektor/define.h> -#include <lektor/mkv.h> -#include <lektor/thread.h> -#include <lektor/uri.h> - -#include <curl/curl.h> -#include <sqlite3.h> -#include <json-c/json.h> -#include <inttypes.h> -#include <stdbool.h> - -struct lkt_repo { - /* Just the repo */ - const char *name; - const char *base_url; - const char *kara_dir; - const char *get_all_json; - const char *get_id_json; - const char *get_id_file; - const uint64_t version; - - /* The database */ - volatile sqlite3 *db; -}; - -int repo_new(struct lkt_repo *const repo, const char *name, const char *url, volatile sqlite3 *db); -void repo_free(struct lkt_repo *const repo); - -/* Get metadata of a kara. */ -int repo_get_id (struct lkt_repo *const repo, const uint64_t id, struct kara_metadata *mdt); -int repo_get_alljson_sync(struct lkt_repo *const repo, struct json_object **json); -int repo_get_allid_async (struct lkt_repo *const repo); - -/* Download a kara. */ -int repo_download_id_sync (struct lkt_repo *const repo, const uint64_t id, const char *kara_path, struct kara_metadata *mdt); -int repo_download_id_async(struct lkt_repo *const repo, const size_t id); - -/* Scan and update the DB. - If only update from the kurisu repo, rescan must be null, if you want to - also scan the directories, set rescan to a non zero value. If the uri is - null, will just update/rescan everything. */ -int repo_sync(struct lkt_repo *const repo, struct lkt_uri *uri, int rescan); diff --git a/inc/lektor/thread.h b/inc/lektor/thread.h index e91546ed3918c89958c28388d14d3a3352fbe05e..3e5e07a7ec809eedcda4642e8ac47233f2fd4e75 100644 --- a/inc/lektor/thread.h +++ b/inc/lektor/thread.h @@ -1,6 +1,6 @@ #pragma once -#include <lektor/define.h> +#include <lektor/common.h> #include <pthread.h> #include <sys/types.h> diff --git a/inc/lektor/uri.h b/inc/lektor/uri.h index 0c4cff92f74681da6fc13d49671be8ed7f3c270e..d278c9e96521e8a462d3d11f5d4fdc4b86318aeb 100644 --- a/inc/lektor/uri.h +++ b/inc/lektor/uri.h @@ -1,6 +1,6 @@ #pragma once -#include <lektor/define.h> +#include <lektor/common.h> #include <stddef.h> #include <stddef.h> #include <stdbool.h> diff --git a/inc/lektor/utils.h b/inc/lektor/utils.h deleted file mode 100644 index aa175cc8f40d0410a82acdd9677487e98a7edfb3..0000000000000000000000000000000000000000 --- a/inc/lektor/utils.h +++ /dev/null @@ -1,25 +0,0 @@ -/* Random things. */ -#pragma once - -#include <lektor/define.h> -#include <stdint.h> -#include <stdlib.h> - -/* Read `bytes` as the big endian representation of a 32-bit unsigned integer. - `n` is the number of bytes in `bytes` - Returns 0 if n is 0. - Restriction: n <= 4 */ -uint32_t be_uint32_t(const uint8_t bytes[], size_t n); - -/* Same as `be_uint32_t` but for 64-bit unsigned integers. - Restriction: n <= 8 */ -uint64_t be_uint64_t(const uint8_t bytes[], size_t n); - -/* Trim the string `str` of the char `c` from the right and the left. - The string may not be null terminated. - Returns a pointer to the the trimmed string - Restrictions: the len of the string must be `len`. */ -char *trim(char *str, size_t len, char c); - -/* Get a line from stdin safely. */ -int get_stdin_line(const char *prompt, char *buf, size_t len); diff --git a/inc/lektor/window.h b/inc/lektor/window.h index 69c74ddb96125305927fb27d99b9f45a04925d8e..624c05c8284414c2f9b00e2c9ce570d7a9a4b281 100644 --- a/inc/lektor/window.h +++ b/inc/lektor/window.h @@ -1,6 +1,6 @@ #pragma once -#include <lektor/define.h> +#include <lektor/common.h> #include <stdbool.h> #include <sqlite3.h> diff --git a/meson.build b/meson.build index 091dd75832c62eabeef329eeab8df89277a56bcd..ca35a64f34b65daac92a55f45650d7fb25f19446 100644 --- a/meson.build +++ b/meson.build @@ -57,7 +57,6 @@ core_sources = [ 'src/mkv/bufferfd.c' , 'src/net/listen.c' , 'src/net/message.c' , 'src/config.c' - , 'src/utils.c' , 'src/uri.c' , 'src/ini/ini.c' , 'src/repo/downloader.c' diff --git a/src/cmd.c b/src/cmd.c index f5c0c61991087bd5e05d4e287b8e191aae8e6435..ae46c8b299db796e69d335b267d9ffdfa827b2db 100644 --- a/src/cmd.c +++ b/src/cmd.c @@ -1,7 +1,7 @@ #define _POSIX_C_SOURCE 200809L #include <lektor/cmd.h> -#include <lektor/macro.h> +#include <lektor/common.h> #include <sys/types.h> #include <stdlib.h> #include <strings.h> diff --git a/src/commands.c b/src/commands.c index fd48677b6c892826bdc722bdd1d65b0397323166..7189a6cf3b8c627702dfb1e14de390fdb37708cd 100644 --- a/src/commands.c +++ b/src/commands.c @@ -1,12 +1,11 @@ #define _POSIX_C_SOURCE 200809L #include <common/common.h> -#include <lektor/macro.h> +#include <lektor/common.h> #include <lektor/commands.h> #include <lektor/database.h> #include <lektor/net.h> #include <lektor/uri.h> -#include <lektor/repo.h> #include <errno.h> #include <linux/limits.h> diff --git a/src/common.c b/src/common.c index 50186eff6de15ad5e4c0b5f3cbe1359b120d64f1..b14f8269d36c73ebc8f836e1bedda4a63b94bc56 100644 --- a/src/common.c +++ b/src/common.c @@ -1,6 +1,8 @@ #include <common/common.h> +#include <stdint.h> #include <stdio.h> #include <stdlib.h> +#include <string.h> void __not_implemented(const char *func, char *file, int line) @@ -14,3 +16,114 @@ __unused(void *dummy, ...) { (void) dummy; } + +uint32_t +be_uint32_t(const uint8_t bytes[], size_t n) +{ + uint32_t res = 0; + for (size_t i = 0; i < n; i++) + res = (res << 8u) | bytes[i]; + return res; +} + +uint64_t +be_uint64_t(const uint8_t bytes[], size_t n) +{ + uint64_t res = 0; + for (size_t i = 0; i < n; i++) + res = (res << 8u) | bytes[i]; + return res; +} + +char * +trim(char *str, size_t len, char c) +{ + char *res = (char *) str; + char *end; + + for (; len != 0 && *res == c; ++str, --len) + continue; + + if (*res == 0) + return res; + + end = res + len - sizeof(char); + len = 0; + + for (; res < end && *end == c; --end, ++len) + continue; + + if (len > 0) + end[1] = 0; + + return res; +} + +int +is_utf8(const char *string) +{ + if (!string) + return 1; + + const unsigned char *bytes = (const unsigned char *)string; + while (*bytes) { + /* ASCII */ + if (bytes[0] == 0x09 || bytes[0] == 0x0A || + bytes[0] == 0x0D || (0x20 <= bytes[0] && bytes[0] <= 0x7E)) { + bytes += 1; + continue; + } + + /* non-overlong 2-byte */ + if ((0xC2 <= bytes[0] && bytes[0] <= 0xDF) && + (0x80 <= bytes[1] && bytes[1] <= 0xBF)) { + bytes += 2; + continue; + } + + if ( (bytes[0] == 0xE0 && + (0xA0 <= bytes[1] && bytes[1] <= 0xBF) && + (0x80 <= bytes[2] && bytes[2] <= 0xBF)) || /* excluding overlongs */ + (((0xE1 <= bytes[0] && bytes[0] <= 0xEC) || + bytes[0] == 0xEE || bytes[0] == 0xEF) && + (0x80 <= bytes[1] && bytes[1] <= 0xBF) && + (0x80 <= bytes[2] && bytes[2] <= 0xBF)) || /* straight 3-byte */ + (bytes[0] == 0xED && + (0x80 <= bytes[1] && bytes[1] <= 0x9F) && + (0x80 <= bytes[2] && bytes[2] <= 0xBF))) { /* excluding surrogates */ + bytes += 3; + continue; + } + + if ( (bytes[0] == 0xF0 && /* planes 1-3 */ + (0x90 <= bytes[1] && bytes[1] <= 0xBF) && + (0x80 <= bytes[2] && bytes[2] <= 0xBF) && + (0x80 <= bytes[3] && bytes[3] <= 0xBF)) || + ((0xF1 <= bytes[0] && bytes[0] <= 0xF3) && + (0x80 <= bytes[1] && bytes[1] <= 0xBF) && + (0x80 <= bytes[2] && bytes[2] <= 0xBF) && + (0x80 <= bytes[3] && bytes[3] <= 0xBF)) || /* planes 4-15 */ + (bytes[0] == 0xF4 && + (0x80 <= bytes[1] && bytes[1] <= 0x8F) && + (0x80 <= bytes[2] && bytes[2] <= 0xBF) && + (0x80 <= bytes[3] && bytes[3] <= 0xBF))) { /* plane 16 */ + bytes += 4; + continue; + } + + return 2; + } + + return 0; +} + +int +get_stdin_line(const char *prompt, char *buf, size_t len) +{ + if (prompt) + fprintf(stdout, "%s", prompt); + if (!fgets(buf, len, stdin)) + return 1; + buf[len - 1] = 0u; + return is_utf8(buf); +} diff --git a/src/database/config.c b/src/database/config.c index a2aa57d5c9d17c37d66585e918b325dd4ce83d70..60c59427d60ebee07a688a36882d9f37cfb6c279 100644 --- a/src/database/config.c +++ b/src/database/config.c @@ -1,7 +1,7 @@ #define _POSIX_C_SOURCE 200809L #include <lektor/database.h> -#include <lektor/macro.h> +#include <lektor/common.h> #include <limits.h> #include <stdio.h> diff --git a/src/database/find.c b/src/database/find.c index 56ac88822330b08ef006a2050704fc3f8bb47447..e801b631e548c0ed88c097fa3928b848e32cfbc7 100644 --- a/src/database/find.c +++ b/src/database/find.c @@ -1,7 +1,7 @@ #define _POSIX_C_SOURCE 200809L #include <lektor/database.h> -#include <lektor/macro.h> +#include <lektor/common.h> #include <limits.h> #include <stdio.h> diff --git a/src/database/open.c b/src/database/open.c index 6ad2ee22e4fda1c4cfcebc61bb42e5081e8716e5..685d6b8138aa2a026fe088a3a63ee70bc128c9ae 100644 --- a/src/database/open.c +++ b/src/database/open.c @@ -1,7 +1,7 @@ #define _POSIX_C_SOURCE 200809L #include <lektor/database.h> -#include <lektor/macro.h> +#include <lektor/common.h> #include <stdio.h> #include <stdlib.h> #include <string.h> diff --git a/src/database/playlist.c b/src/database/playlist.c index 20c0cb812bfc0f2ff011aa8d4a207551f9a507e2..086f580bd911e19526e4bb4ad3b248e918995323 100644 --- a/src/database/playlist.c +++ b/src/database/playlist.c @@ -2,7 +2,7 @@ #include <common/common.h> #include <lektor/database.h> -#include <lektor/macro.h> +#include <lektor/common.h> #include <stdio.h> #include <strings.h> diff --git a/src/database/queue.c b/src/database/queue.c index 78abdd7bea5c36117955bad5b1548c6b3c02da02..341455f016c363839f23ba996643ef70a84ef045 100644 --- a/src/database/queue.c +++ b/src/database/queue.c @@ -1,7 +1,7 @@ #define _POSIX_C_SOURCE 200809L #include <lektor/database.h> -#include <lektor/macro.h> +#include <lektor/common.h> #include <linux/limits.h> #include <stdio.h> diff --git a/src/database/stickers.c b/src/database/stickers.c index 0130cfb703941982d2fc550db89c4f30dc62ba66..1b4c023b00baf88ff10bbe0f93e4a7fd1332c435 100644 --- a/src/database/stickers.c +++ b/src/database/stickers.c @@ -1,6 +1,6 @@ #define _POSIX_C_SOURCE 200809L -#include <lektor/macro.h> +#include <lektor/common.h> #include <lektor/database.h> #include <string.h> #include <strings.h> diff --git a/src/database/update.c b/src/database/update.c index a3b64b2a1cbac804dfa26fccf0c65f929d962291..a3b641a6c5a0cbb6a3c711fe4d04c3cb02a22be3 100644 --- a/src/database/update.c +++ b/src/database/update.c @@ -2,7 +2,7 @@ #define _DEFAULT_SOURCE #include <lektor/database.h> -#include <lektor/macro.h> +#include <lektor/common.h> #include <stdbool.h> #include <sqlite3.h> #include <stdio.h> diff --git a/src/database/user.c b/src/database/user.c index b6330b2d3319591a86e21683105afc25d6d88176..11936d7492527e567ad371ad362987e18f093161 100644 --- a/src/database/user.c +++ b/src/database/user.c @@ -1,7 +1,7 @@ #define _POSIX_C_SOURCE 200809L #include <lektor/database.h> -#include <lektor/macro.h> +#include <lektor/common.h> #include <stdio.h> bool diff --git a/src/main/lktadm.c b/src/main/lktadm.c index 78a7e25058815822ec6bf29a21611ccb6d7751c2..79be9a80bb20aaeec6dee83a2b88a57c7b910ec5 100644 --- a/src/main/lktadm.c +++ b/src/main/lktadm.c @@ -7,8 +7,8 @@ #include <lektor/config.h> #include <lektor/mkv.h> #include <lektor/database.h> -#include <lektor/repo.h> -#include <lektor/utils.h> +#include <lektor/net.h> +#include <lektor/common.h> #include <stdio.h> #include <stdlib.h> diff --git a/src/mkv/mkv.c b/src/mkv/mkv.c index 8cb9dc6aa9cc9091dd9e319d2f8feacaa211dde0..39efb961d862e45aed4616d9259ae61693524455 100644 --- a/src/mkv/mkv.c +++ b/src/mkv/mkv.c @@ -7,7 +7,7 @@ #include <lektor/bufferfd.h> #include <lektor/mkv.h> -#include <lektor/utils.h> +#include <lektor/common.h> #define MKV_TAG_MAX 64 diff --git a/src/module/module_x11.c b/src/module/module_x11.c index 4ecd945427dcbcbefbdfc5023ef5059b8a3a01bb..bec80557594f741c7df43ad4f07fa5230d75afec 100644 --- a/src/module/module_x11.c +++ b/src/module/module_x11.c @@ -2,7 +2,7 @@ #include <common/common.h> #include <lektor/module/module_x11.h> -#include <lektor/macro.h> +#include <lektor/common.h> #include <lektor/module/mpv.h> #include <lektor/database.h> #include <lektor/commands.h> diff --git a/src/module/mpv.c b/src/module/mpv.c index 115bafd8989bcaba20d4716cfbb4f59150a39bdd..735ba8d91509a97efeef61f41e4e9a62c0b9fc46 100644 --- a/src/module/mpv.c +++ b/src/module/mpv.c @@ -4,7 +4,7 @@ #include <lektor/module/mpv.h> #include <lektor/commands.h> #include <lektor/database.h> -#include <lektor/macro.h> +#include <lektor/common.h> #include <stdio.h> #include <string.h> #include <unistd.h> diff --git a/src/net/listen.c b/src/net/listen.c index f8b6d603e0acf210802d8faf4d2a4f96639eac85..4a1c0498a3e4490f8e5c323019e35f4ce28fefdb 100644 --- a/src/net/listen.c +++ b/src/net/listen.c @@ -1,10 +1,8 @@ #define _POSIX_C_SOURCE 200809L #include <lektor/commands.h> -#include <lektor/macro.h> -#include <lektor/define.h> +#include <lektor/common.h> #include <lektor/database.h> -#include <lektor/repo.h> #include <lektor/net.h> #include <ini/ini.h> diff --git a/src/repo/downloader.c b/src/repo/downloader.c index 10f27fd5e4fb20b126e4ef08b39b7d07121c92c5..593af7044859bb65731bc8a160412d0ca1c0c17d 100644 --- a/src/repo/downloader.c +++ b/src/repo/downloader.c @@ -12,9 +12,9 @@ #include <common/common.h> #include <mthread/mthread.h> -#include <lektor/macro.h> -#include <lektor/repo.h> +#include <lektor/common.h> #include <lektor/database.h> +#include <lektor/net.h> static volatile unsigned int curl_init = false; diff --git a/src/repo/sync.c b/src/repo/sync.c index 028cca724ea0ae40d6dab66f4fa384b2025d57e9..5d952bbf873aacc51f983870d6413ad55f86ba6f 100644 --- a/src/repo/sync.c +++ b/src/repo/sync.c @@ -1,7 +1,7 @@ #define _POSIX_C_SOURCE 200809L #include <common/common.h> -#include <lektor/repo.h> +#include <lektor/net.h> int repo_sync(struct lkt_repo *const repo, struct lkt_uri *uri, int rescan) diff --git a/src/utils.c b/src/utils.c deleted file mode 100644 index c01ce5a628ed3c640f8bf76051d1b31653defefe..0000000000000000000000000000000000000000 --- a/src/utils.c +++ /dev/null @@ -1,114 +0,0 @@ -#include <lektor/utils.h> -#include <string.h> -#include <stdio.h> - -uint32_t -be_uint32_t(const uint8_t bytes[], size_t n) -{ - uint32_t res = 0; - for (size_t i = 0; i < n; i++) - res = (res << 8u) | bytes[i]; - return res; -} - -uint64_t -be_uint64_t(const uint8_t bytes[], size_t n) -{ - uint64_t res = 0; - for (size_t i = 0; i < n; i++) - res = (res << 8u) | bytes[i]; - return res; -} - -char * -trim(char *str, size_t len, char c) -{ - char *res = (char *) str; - char *end; - - for (; len != 0 && *res == c; ++str, --len) - continue; - - if (*res == 0) - return res; - - end = res + len - sizeof(char); - len = 0; - - for (; res < end && *end == c; --end, ++len) - continue; - - if (len > 0) - end[1] = 0; - - return res; -} - -int -is_utf8(const char *string) -{ - if (!string) - return 1; - - const unsigned char *bytes = (const unsigned char *)string; - while (*bytes) { - /* ASCII */ - if (bytes[0] == 0x09 || bytes[0] == 0x0A || - bytes[0] == 0x0D || (0x20 <= bytes[0] && bytes[0] <= 0x7E)) { - bytes += 1; - continue; - } - - /* non-overlong 2-byte */ - if ((0xC2 <= bytes[0] && bytes[0] <= 0xDF) && - (0x80 <= bytes[1] && bytes[1] <= 0xBF)) { - bytes += 2; - continue; - } - - if ( (bytes[0] == 0xE0 && - (0xA0 <= bytes[1] && bytes[1] <= 0xBF) && - (0x80 <= bytes[2] && bytes[2] <= 0xBF)) || /* excluding overlongs */ - (((0xE1 <= bytes[0] && bytes[0] <= 0xEC) || - bytes[0] == 0xEE || bytes[0] == 0xEF) && - (0x80 <= bytes[1] && bytes[1] <= 0xBF) && - (0x80 <= bytes[2] && bytes[2] <= 0xBF)) || /* straight 3-byte */ - (bytes[0] == 0xED && - (0x80 <= bytes[1] && bytes[1] <= 0x9F) && - (0x80 <= bytes[2] && bytes[2] <= 0xBF))) { /* excluding surrogates */ - bytes += 3; - continue; - } - - if ( (bytes[0] == 0xF0 && /* planes 1-3 */ - (0x90 <= bytes[1] && bytes[1] <= 0xBF) && - (0x80 <= bytes[2] && bytes[2] <= 0xBF) && - (0x80 <= bytes[3] && bytes[3] <= 0xBF)) || - ((0xF1 <= bytes[0] && bytes[0] <= 0xF3) && - (0x80 <= bytes[1] && bytes[1] <= 0xBF) && - (0x80 <= bytes[2] && bytes[2] <= 0xBF) && - (0x80 <= bytes[3] && bytes[3] <= 0xBF)) || /* planes 4-15 */ - (bytes[0] == 0xF4 && - (0x80 <= bytes[1] && bytes[1] <= 0x8F) && - (0x80 <= bytes[2] && bytes[2] <= 0xBF) && - (0x80 <= bytes[3] && bytes[3] <= 0xBF))) { /* plane 16 */ - bytes += 4; - continue; - } - - return 2; - } - - return 0; -} - -int -get_stdin_line(const char *prompt, char *buf, size_t len) -{ - if (prompt) - fprintf(stdout, "%s", prompt); - if (!fgets(buf, len, stdin)) - return 1; - buf[len - 1] = 0u; - return is_utf8(buf); -}