From 0e83679e69a38c605d3adfd32ac693d9caa38941 Mon Sep 17 00:00:00 2001 From: Kubat <mael.martin31@gmail.com> Date: Thu, 30 Apr 2020 12:20:02 +0200 Subject: [PATCH] Reduce header files --- inc/lektor/bufferfd.h | 2 +- inc/lektor/cmd.h | 1 - inc/lektor/commands.h | 2 +- inc/lektor/common.h | 120 ++++++++++++++++++++++++++++++++ inc/lektor/config.h | 2 +- inc/lektor/database.h | 2 +- inc/lektor/define.h | 51 -------------- inc/lektor/macro.h | 44 ------------ inc/lektor/mkv.h | 2 +- inc/lektor/module/module_sdl2.h | 1 - inc/lektor/module/module_x11.h | 1 - inc/lektor/net.h | 46 +++++++++++- inc/lektor/repo.h | 44 ------------ inc/lektor/thread.h | 2 +- inc/lektor/uri.h | 2 +- inc/lektor/utils.h | 25 ------- inc/lektor/window.h | 2 +- meson.build | 1 - src/cmd.c | 2 +- src/commands.c | 3 +- src/common.c | 113 ++++++++++++++++++++++++++++++ src/database/config.c | 2 +- src/database/find.c | 2 +- src/database/open.c | 2 +- src/database/playlist.c | 2 +- src/database/queue.c | 2 +- src/database/stickers.c | 2 +- src/database/update.c | 2 +- src/database/user.c | 2 +- src/main/lktadm.c | 4 +- src/mkv/mkv.c | 2 +- src/module/module_x11.c | 2 +- src/module/mpv.c | 2 +- src/net/listen.c | 4 +- src/repo/downloader.c | 4 +- src/repo/sync.c | 2 +- src/utils.c | 114 ------------------------------ 37 files changed, 304 insertions(+), 314 deletions(-) create mode 100644 inc/lektor/common.h delete mode 100644 inc/lektor/define.h delete mode 100644 inc/lektor/macro.h delete mode 100644 inc/lektor/repo.h delete mode 100644 inc/lektor/utils.h delete mode 100644 src/utils.c diff --git a/inc/lektor/bufferfd.h b/inc/lektor/bufferfd.h index 97bc0237..c6e4ccbc 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 3ac6c3ad..de187726 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 a7ada9fb..8ea3e82f 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 00000000..64c43456 --- /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 fc013160..618de57c 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 194bb101..ed5a5fae 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 d68144f4..00000000 --- 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 48c66716..00000000 --- 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 3dfdf09a..c8811426 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 9821268e..90eacc57 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 16c01718..0ea84737 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 19a8a927..ac9c1506 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 ffd5bc8e..00000000 --- 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 e91546ed..3e5e07a7 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 0c4cff92..d278c9e9 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 aa175cc8..00000000 --- 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 69c74ddb..624c05c8 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 091dd758..ca35a64f 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 f5c0c619..ae46c8b2 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 fd48677b..7189a6cf 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 50186eff..b14f8269 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 a2aa57d5..60c59427 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 56ac8882..e801b631 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 6ad2ee22..685d6b81 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 20c0cb81..086f580b 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 78abdd7b..341455f0 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 0130cfb7..1b4c023b 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 a3b64b2a..a3b641a6 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 b6330b2d..11936d74 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 78a7e250..79be9a80 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 8cb9dc6a..39efb961 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 4ecd9454..bec80557 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 115bafd8..735ba8d9 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 f8b6d603..4a1c0498 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 10f27fd5..593af704 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 028cca72..5d952bbf 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 c01ce5a6..00000000 --- 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); -} -- GitLab