diff --git a/janus/src/au.c b/janus/src/au.c index 494ddfa..8300a96 100644 --- a/janus/src/au.c +++ b/janus/src/au.c @@ -38,7 +38,7 @@ bool us_au_probe(const char *name) { // the existence of it in /proc/asound/. // It's enough for our case. - if (name == NULL) { + if (!us_str_is_ok(name)) { return false; } diff --git a/janus/src/config.c b/janus/src/config.c index b2adce2..73fc358 100644 --- a/janus/src/config.c +++ b/janus/src/config.c @@ -64,7 +64,7 @@ us_config_s *us_config_init(const char *config_dir_path) { if ((config->acap_dev_name = _get_value(jcfg, "acap", "device")) != NULL) { config->acap_hz = _get_uint(jcfg, "acap", "sampling_rate", 0); config->tc358743_dev_path = _get_value(jcfg, "acap", "tc358743"); - if (config->acap_hz == 0 && config->tc358743_dev_path == NULL) { + if (config->acap_hz == 0 && !us_str_is_ok(config->tc358743_dev_path)) { US_LOG_ERROR("Either acap.sampling_rate or acap.tc358743 required"); goto error; } @@ -95,7 +95,7 @@ void us_config_destroy(us_config_s *config) { static char *_get_value(janus_config *jcfg, const char *section, const char *option) { janus_config_category *section_obj = janus_config_get_create(jcfg, NULL, janus_config_type_category, section); janus_config_item *option_obj = janus_config_get(jcfg, section_obj, janus_config_type_item, option); - if (option_obj == NULL || option_obj->value == NULL || option_obj->value[0] == '\0') { + if (option_obj == NULL || !us_str_is_ok(option_obj->value)) { return NULL; } return us_strdup(option_obj->value); diff --git a/janus/src/plugin.c b/janus/src/plugin.c index 18c5ff2..eb06ee5 100644 --- a/janus/src/plugin.c +++ b/janus/src/plugin.c @@ -192,10 +192,7 @@ static int _get_acap_hz(uint *hz) { *hz = _g_config->acap_hz; return 0; } - if (_g_config->tc358743_dev_path == NULL) { - US_LOG_ERROR("No configured sampling rate"); - return -1; - } + US_A(us_str_is_ok(_g_config->tc358743_dev_path)); const int fd = open(_g_config->tc358743_dev_path, O_RDWR); if (fd < 0) { US_LOG_PERROR("Can't open TC358743 V4L2 device"); @@ -408,7 +405,10 @@ static int _plugin_init(janus_callbacks *gw, const char *config_dir_path) { US_LOGGING_INIT; US_LOG_INFO("Initializing PiKVM uStreamer plugin %s ...", US_VERSION); - if (gw == NULL || config_dir_path == NULL || ((_g_config = us_config_init(config_dir_path)) == NULL)) { + if (gw == NULL || !us_str_is_ok(config_dir_path)) { + return -1; + } + if ((_g_config = us_config_init(config_dir_path)) == NULL) { return -1; } _g_gw = gw; diff --git a/src/dump/main.c b/src/dump/main.c index a2d9c5a..1596026 100644 --- a/src/dump/main.c +++ b/src/dump/main.c @@ -177,14 +177,14 @@ int main(int argc, char *argv[]) { # undef OPT_NUMBER # undef OPT_SET - if (sink_name == NULL || sink_name[0] == '\0') { + if (!us_str_is_ok(sink_name)) { puts("Missing option --sink. See --help for details."); return 1; } _output_context_s ctx = {0}; - if (out_path && out_path[0] != '\0') { + if (us_str_is_ok(out_path)) { if ((ctx.v_out = (void*)us_output_file_init(out_path, out_json)) == NULL) { return 1; } diff --git a/src/libs/capture.c b/src/libs/capture.c index a8590b2..85044b6 100644 --- a/src/libs/capture.c +++ b/src/libs/capture.c @@ -197,7 +197,7 @@ int us_capture_open(us_capture_s *cap) { } _LOG_DEBUG("Capture device fd=%d opened", run->fd); - if (cap->dv_timings && cap->media_path[0] != '\0') { + if (cap->dv_timings && us_str_is_ok(cap->media_path)) { if (_capture_open_media_pads(cap) < 0) { goto error_no_device; } @@ -232,7 +232,7 @@ int us_capture_open(us_capture_s *cap) { if (cap->dv_timings && _capture_open_dv_timings(cap, true) < 0) { goto error; } - if (cap->dv_timings && cap->media_path[0] != '\0') { + if (cap->dv_timings && us_str_is_ok(cap->media_path)) { US_ARRAY_ITERATE(run->media_pads, 0, pad, { if (pad->fd >= -1 && _capture_open_media_format(cap, pad) < 0) { goto error; diff --git a/src/libs/threading.h b/src/libs/threading.h index e06090b..e9bd761 100644 --- a/src/libs/threading.h +++ b/src/libs/threading.h @@ -100,7 +100,7 @@ INLINE void us_thread_get_name(char *name) { // Always required for logging || (defined(__OpenBSD__) && defined(OpenBSD) && OpenBSD >= 201905) \ || defined(__DragonFly__) pthread_get_name_np(pthread_self(), name, US_THREAD_NAME_SIZE); // Also null-terminated - if (name[0] != '\0') { + if (us_str_is_ok(name)) { retval = 0; } # else diff --git a/src/libs/tools.h b/src/libs/tools.h index c8c5b80..d35e1c7 100644 --- a/src/libs/tools.h +++ b/src/libs/tools.h @@ -92,6 +92,10 @@ INLINE char *us_strdup(const char *str) { return new; } +INLINE bool us_str_is_ok(const char *str) { + return (str != NULL && str[0] != '\0'); +} + INLINE const char *us_bool_to_string(bool flag) { return (flag ? "true" : "false"); } diff --git a/src/ustreamer/http/server.c b/src/ustreamer/http/server.c index cbd34c6..a9bd0c3 100644 --- a/src/ustreamer/http/server.c +++ b/src/ustreamer/http/server.c @@ -183,7 +183,7 @@ int us_server_listen(us_server_s *server) { us_stream_s *const stream = server->stream; { - if (server->static_path[0] != '\0') { + if (us_str_is_ok(server->static_path)) { _LOG_INFO("Enabling the file server: %s", server->static_path); evhttp_set_gencb(run->http, _http_callback_static, (void*)server); } else { @@ -202,7 +202,7 @@ int us_server_listen(us_server_s *server) { evhttp_set_timeout(run->http, server->timeout); - if (server->user[0] != '\0') { + if (us_str_is_ok(server->user)) { char *encoded_token = NULL; char *raw_token; @@ -216,7 +216,7 @@ int us_server_listen(us_server_s *server) { _LOG_INFO("Using HTTP basic auth"); } - if (server->unix_path[0] != '\0') { + if (us_str_is_ok(server->unix_path)) { _LOG_DEBUG("Binding server to UNIX socket '%s' ...", server->unix_path); if ((run->ext_fd = us_evhttp_bind_unix( run->http, @@ -264,7 +264,7 @@ static int _http_preprocess_request(struct evhttp_request *req, us_server_s *ser atomic_store(&server->stream->run->http->last_req_ts, us_get_now_monotonic()); - if (server->allow_origin[0] != '\0') { + if (us_str_is_ok(server->allow_origin)) { const char *const cors_headers = us_evhttp_get_header(req, "Access-Control-Request-Headers"); const char *const cors_method = us_evhttp_get_header(req, "Access-Control-Request-Method"); @@ -666,7 +666,7 @@ static void _http_callback_stream_write(struct bufferevent *buf_event, void *v_c if (client->need_initial) { _A_EVBUFFER_ADD_PRINTF(buf, "HTTP/1.0 200 OK" RN); - if (client->server->allow_origin[0] != '\0') { + if (us_str_is_ok(client->server->allow_origin)) { const char *const cors_headers = us_evhttp_get_header(client->req, "Access-Control-Request-Headers"); const char *const cors_method = us_evhttp_get_header(client->req, "Access-Control-Request-Method"); @@ -693,8 +693,8 @@ static void _http_callback_stream_write(struct bufferevent *buf_event, void *v_c "Content-Type: multipart/x-mixed-replace;boundary=" BOUNDARY RN RN "--" BOUNDARY RN, - (server->instance_id[0] == '\0' ? "" : "_"), - server->instance_id, + (us_str_is_ok(server->instance_id) ? "_" : ""), + (us_str_is_ok(server->instance_id) ? server->instance_id : ""), (client->key != NULL ? client->key : "0"), client->id); diff --git a/src/ustreamer/http/static.c b/src/ustreamer/http/static.c index 99dc271..be3af47 100644 --- a/src/ustreamer/http/static.c +++ b/src/ustreamer/http/static.c @@ -39,7 +39,7 @@ char *us_find_static_file_path(const char *root_path, const char *req_path) { char *path = NULL; char *const simplified_path = us_simplify_request_path(req_path); - if (simplified_path[0] == '\0') { + if (!us_str_is_ok(simplified_path)) { US_LOG_VERBOSE("HTTP: Invalid request path %s to static", req_path); goto error; } diff --git a/src/ustreamer/options.c b/src/ustreamer/options.c index d4b00e5..8d1bf38 100644 --- a/src/ustreamer/options.c +++ b/src/ustreamer/options.c @@ -569,7 +569,7 @@ int us_options_parse( US_LOG_INFO("Starting PiKVM uStreamer %s ...", US_VERSION); # define ADD_SINK(x_label, x_prefix) { \ - if (x_prefix##_name && x_prefix##_name[0] != '\0') { \ + if (us_str_is_ok(x_prefix##_name)) { \ opts->x_prefix = us_memsink_init_opened( \ x_label, \ x_prefix##_name, \