US_ARRAY_ITERATE()

This commit is contained in:
Maxim Devaev
2022-07-20 12:54:13 +03:00
parent 2c9334d53f
commit 1c1e3b0875
10 changed files with 78 additions and 38 deletions

View File

@@ -36,6 +36,7 @@
#include <opus/opus.h> #include <opus/opus.h>
#include "uslibs/tools.h" #include "uslibs/tools.h"
#include "uslibs/array.h"
#include "uslibs/threading.h" #include "uslibs/threading.h"
#include "logging.h" #include "logging.h"

1
janus/src/uslibs/array.h Symbolic link
View File

@@ -0,0 +1 @@
../../../src/libs/array.h

37
src/libs/array.h Normal file
View File

@@ -0,0 +1,37 @@
/*****************************************************************************
# #
# uStreamer - Lightweight and fast MJPEG-HTTP streamer. #
# #
# Copyright (C) 2018-2022 Maxim Devaev <mdevaev@gmail.com> #
# #
# This program is free software: you can redistribute it and/or modify #
# it under the terms of the GNU General Public License as published by #
# the Free Software Foundation, either version 3 of the License, or #
# (at your option) any later version. #
# #
# This program is distributed in the hope that it will be useful, #
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
# GNU General Public License for more details. #
# #
# You should have received a copy of the GNU General Public License #
# along with this program. If not, see <https://www.gnu.org/licenses/>. #
# #
*****************************************************************************/
#pragma once
#include <assert.h>
#define US_ARRAY_LEN(x_array) (sizeof(x_array) / sizeof((x_array)[0]))
#define US_ARRAY_ITERATE(x_array, x_start, x_item_ptr, ...) { \
const int m_len = US_ARRAY_LEN(x_array); \
assert(x_start <= m_len); \
for (int m_index = x_start; m_index < m_len; ++m_index) { \
__typeof__((x_array)[0]) *const x_item_ptr = &x_array[m_index]; \
__VA_ARGS__ \
} \
}

View File

@@ -60,8 +60,6 @@
#define US_ASPRINTF(x_dest, x_fmt, ...) assert(asprintf(&(x_dest), (x_fmt), ##__VA_ARGS__) >= 0) #define US_ASPRINTF(x_dest, x_fmt, ...) assert(asprintf(&(x_dest), (x_fmt), ##__VA_ARGS__) >= 0)
#define US_ARRAY_LEN(x_array) (sizeof(x_array) / sizeof((x_array)[0]))
INLINE char *us_strdup(const char *str) { INLINE char *us_strdup(const char *str) {
char *const new = strdup(str); char *const new = strdup(str);

View File

@@ -111,29 +111,29 @@ void us_device_destroy(us_device_s *dev) {
} }
int us_device_parse_format(const char *str) { int us_device_parse_format(const char *str) {
for (unsigned index = 0; index < US_ARRAY_LEN(_FORMATS); ++index) { US_ARRAY_ITERATE(_FORMATS, 0, item, {
if (!strcasecmp(str, _FORMATS[index].name)) { if (!strcasecmp(item->name, str)) {
return _FORMATS[index].format; return item->format;
} }
} });
return US_FORMAT_UNKNOWN; return US_FORMAT_UNKNOWN;
} }
v4l2_std_id us_device_parse_standard(const char *str) { v4l2_std_id us_device_parse_standard(const char *str) {
for (unsigned index = 1; index < US_ARRAY_LEN(_STANDARDS); ++index) { US_ARRAY_ITERATE(_STANDARDS, 1, item, {
if (!strcasecmp(str, _STANDARDS[index].name)) { if (!strcasecmp(item->name, str)) {
return _STANDARDS[index].standard; return item->standard;
} }
} });
return US_STANDARD_UNKNOWN; return US_STANDARD_UNKNOWN;
} }
int us_device_parse_io_method(const char *str) { int us_device_parse_io_method(const char *str) {
for (unsigned index = 0; index < US_ARRAY_LEN(_IO_METHODS); ++index) { US_ARRAY_ITERATE(_IO_METHODS, 0, item, {
if (!strcasecmp(str, _IO_METHODS[index].name)) { if (!strcasecmp(item->name, str)) {
return _IO_METHODS[index].io_method; return item->io_method;
} }
} });
return US_IO_METHOD_UNKNOWN; return US_IO_METHOD_UNKNOWN;
} }
@@ -868,11 +868,11 @@ static void _device_set_control(
} }
static const char *_format_to_string_nullable(unsigned format) { static const char *_format_to_string_nullable(unsigned format) {
for (unsigned index = 0; index < US_ARRAY_LEN(_FORMATS); ++index) { US_ARRAY_ITERATE(_FORMATS, 0, item, {
if (format == _FORMATS[index].format) { if (item->format == format) {
return _FORMATS[index].name; return item->name;
} }
} });
return NULL; return NULL;
} }
@@ -882,19 +882,19 @@ static const char *_format_to_string_supported(unsigned format) {
} }
static const char *_standard_to_string(v4l2_std_id standard) { static const char *_standard_to_string(v4l2_std_id standard) {
for (unsigned index = 0; index < US_ARRAY_LEN(_STANDARDS); ++index) { US_ARRAY_ITERATE(_STANDARDS, 0, item, {
if (standard == _STANDARDS[index].standard) { if (item->standard == standard) {
return _STANDARDS[index].name; return item->name;
} }
} });
return _STANDARDS[0].name; return _STANDARDS[0].name;
} }
static const char *_io_method_to_string_supported(enum v4l2_memory io_method) { static const char *_io_method_to_string_supported(enum v4l2_memory io_method) {
for (unsigned index = 0; index < US_ARRAY_LEN(_IO_METHODS); ++index) { US_ARRAY_ITERATE(_IO_METHODS, 0, item, {
if (io_method == _IO_METHODS[index].io_method) { if (item->io_method == io_method) {
return _IO_METHODS[index].name; return item->name;
} }
} });
return "unsupported"; return "unsupported";
} }

View File

@@ -42,6 +42,7 @@
#include <linux/v4l2-controls.h> #include <linux/v4l2-controls.h>
#include "../libs/tools.h" #include "../libs/tools.h"
#include "../libs/array.h"
#include "../libs/logging.h" #include "../libs/logging.h"
#include "../libs/threading.h" #include "../libs/threading.h"
#include "../libs/frame.h" #include "../libs/frame.h"

View File

@@ -74,20 +74,20 @@ void us_encoder_destroy(us_encoder_s *enc) {
} }
us_encoder_type_e us_encoder_parse_type(const char *str) { us_encoder_type_e us_encoder_parse_type(const char *str) {
for (unsigned index = 0; index < US_ARRAY_LEN(_ENCODER_TYPES); ++index) { US_ARRAY_ITERATE(_ENCODER_TYPES, 0, item, {
if (!strcasecmp(str, _ENCODER_TYPES[index].name)) { if (!strcasecmp(item->name, str)) {
return _ENCODER_TYPES[index].type; return item->type;
} }
} });
return US_ENCODER_TYPE_UNKNOWN; return US_ENCODER_TYPE_UNKNOWN;
} }
const char *us_encoder_type_to_string(us_encoder_type_e type) { const char *us_encoder_type_to_string(us_encoder_type_e type) {
for (unsigned index = 0; index < US_ARRAY_LEN(_ENCODER_TYPES); ++index) { US_ARRAY_ITERATE(_ENCODER_TYPES, 0, item, {
if (_ENCODER_TYPES[index].type == type) { if (item->type == type) {
return _ENCODER_TYPES[index].name; return item->name;
} }
} });
return _ENCODER_TYPES[0].name; return _ENCODER_TYPES[0].name;
} }

View File

@@ -31,6 +31,7 @@
#include <linux/videodev2.h> #include <linux/videodev2.h>
#include "../libs/tools.h" #include "../libs/tools.h"
#include "../libs/array.h"
#include "../libs/threading.h" #include "../libs/threading.h"
#include "../libs/logging.h" #include "../libs/logging.h"
#include "../libs/frame.h" #include "../libs/frame.h"

View File

@@ -54,11 +54,11 @@ const char *us_guess_mime_type(const char *path) {
} }
const char *ext = dot + 1; const char *ext = dot + 1;
for (unsigned index = 0; index < US_ARRAY_LEN(_MIME_TYPES); ++index) { US_ARRAY_ITERATE(_MIME_TYPES, 0, item, {
if (!evutil_ascii_strcasecmp(ext, _MIME_TYPES[index].ext)) { if (!evutil_ascii_strcasecmp(item->ext, ext)) {
return _MIME_TYPES[index].mime; return item->mime;
} }
} });
misc: misc:
return "application/misc"; return "application/misc";

View File

@@ -27,6 +27,7 @@
#include <event2/util.h> #include <event2/util.h>
#include "../../libs/tools.h" #include "../../libs/tools.h"
#include "../../libs/array.h"
const char *us_guess_mime_type(const char *str); const char *us_guess_mime_type(const char *str);