obs-studio/libobs/util/dstr.h

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

332 lines
9.3 KiB
C
Raw Normal View History

/*
* Copyright (c) 2013 Hugh Bailey <obs.jim@gmail.com>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
2013-10-01 02:37:13 +00:00
#pragma once
2013-10-01 02:37:13 +00:00
#include <string.h>
#include <stdarg.h>
#include "c99defs.h"
#include "bmem.h"
/*
* Dynamic string
*
* Helper struct/functions for dynamically sizing string buffers.
*/
#ifdef __cplusplus
extern "C" {
#endif
struct strref;
struct dstr {
char *array;
size_t len; /* number of characters, excluding null terminator */
size_t capacity;
};
#ifndef _MSC_VER
#define PRINTFATTR(f, a) __attribute__((__format__(__printf__, f, a)))
#else
#define PRINTFATTR(f, a)
#endif
2013-10-01 02:37:13 +00:00
EXPORT int astrcmpi(const char *str1, const char *str2);
EXPORT int wstrcmpi(const wchar_t *str1, const wchar_t *str2);
EXPORT int astrcmp_n(const char *str1, const char *str2, size_t n);
EXPORT int wstrcmp_n(const wchar_t *str1, const wchar_t *str2, size_t n);
EXPORT int astrcmpi_n(const char *str1, const char *str2, size_t n);
EXPORT int wstrcmpi_n(const wchar_t *str1, const wchar_t *str2, size_t n);
EXPORT char *astrstri(const char *str, const char *find);
EXPORT wchar_t *wstrstri(const wchar_t *str, const wchar_t *find);
Split output/input audio capture sources - Split input and output audio captures so that they're different sources. This allows easier handling and enumeration of audio devices without having to do some sort of string processing. This way the user interface code can handle this a bit more easily, and so that it doesn't confuse users either. This should be done for all audio capture sources for all operating systems. You don't have to duplicate any code, you just need to create input/output wrapper functions to designate the audio as input or output before creation. - Make it detect soundflower and wavtap devices as mac "output" devices (even though they're actually input) for the mac output capture, and make it so that users can select a default output capture and automatically use soundflower or wavtap. I'm not entirely happy about having to do this, but because mac is designed this way, this is really the only way to handle it that makes it easier for users and UI code to deal with. Note that soundflower and wavtap are still also designated as input devices, so will still show up in input device enumeration. - Remove pragma messages because they were kind polluting the other compiler messages and just getting in the way. In the future we can just do a grep for TODO to find them. - Redo list property again, this time using a safer internal array, rather than requiring sketchy array inputs. Having functions handle everything behind the scenes is much safer. - Remove the reference counter debug log code, as it was included unintentionally in a commit.
2014-03-03 09:56:54 +00:00
2013-10-01 02:37:13 +00:00
EXPORT char *strdepad(char *str);
EXPORT wchar_t *wcsdepad(wchar_t *str);
Implement encoder interface (still preliminary) - Implement OBS encoder interface. It was previously incomplete, but now is reaching some level of completion, though probably should still be considered preliminary. I had originally implemented it so that encoders only have a 'reset' function to reset their parameters, but I felt that having both a 'start' and 'stop' function would be useful. Encoders are now assigned to a specific video/audio media output each rather than implicitely assigned to the main obs video/audio contexts. This allows separate encoder contexts that aren't necessarily assigned to the main video/audio context (which is useful for things such as recording specific sources). Will probably have to do this for regular obs outputs as well. When creating an encoder, you must now explicitely state whether that encoder is an audio or video encoder. Audio and video can optionally be automatically converted depending on what the encoder specifies. When something 'attaches' to an encoder, the first attachment starts the encoder, and the encoder automatically attaches to the media output context associated with it. Subsequent attachments won't have the same effect, they will just start receiving the same encoder data when the next keyframe plays (along with SEI if any). When detaching from the encoder, the last detachment will fully stop the encoder and detach the encoder from the media output context associated with the encoder. SEI must actually be exported separately; because new encoder attachments may not always be at the beginning of the stream, the first keyframe they get must have that SEI data in it. If the encoder has SEI data, it needs only add one small function to simply query that SEI data, and then that data will be handled automatically by libobs for all subsequent encoder attachments. - Implement x264 encoder plugin, move x264 files to separate plugin to separate necessary dependencies. - Change video/audio frame output structures to not use const qualifiers to prevent issues with non-const function usage elsewhere. This was an issue when writing the x264 encoder, as the x264 encoder expects non-const frame data. Change stagesurf_map to return a non-const data type to prevent this as well. - Change full range parameter of video scaler to be an enum rather than boolean
2014-03-16 23:21:34 +00:00
EXPORT char **strlist_split(const char *str, char split_ch, bool include_empty);
EXPORT void strlist_free(char **strlist);
2013-10-01 02:37:13 +00:00
static inline void dstr_init(struct dstr *dst);
static inline void dstr_init_move(struct dstr *dst, struct dstr *src);
static inline void dstr_init_move_array(struct dstr *dst, char *str);
static inline void dstr_init_copy(struct dstr *dst, const char *src);
static inline void dstr_init_copy_dstr(struct dstr *dst,
const struct dstr *src);
EXPORT void dstr_init_copy_strref(struct dstr *dst, const struct strref *src);
2013-10-01 02:37:13 +00:00
static inline void dstr_free(struct dstr *dst);
static inline void dstr_array_free(struct dstr *array, const size_t count);
static inline void dstr_move(struct dstr *dst, struct dstr *src);
static inline void dstr_move_array(struct dstr *dst, char *str);
EXPORT void dstr_copy(struct dstr *dst, const char *array);
static inline void dstr_copy_dstr(struct dstr *dst, const struct dstr *src);
EXPORT void dstr_copy_strref(struct dstr *dst, const struct strref *src);
EXPORT void dstr_ncopy(struct dstr *dst, const char *array, const size_t len);
EXPORT void dstr_ncopy_dstr(struct dstr *dst, const struct dstr *src,
const size_t len);
static inline void dstr_resize(struct dstr *dst, const size_t num);
static inline void dstr_reserve(struct dstr *dst, const size_t num);
static inline bool dstr_is_empty(const struct dstr *str);
2013-10-01 02:37:13 +00:00
static inline void dstr_cat(struct dstr *dst, const char *array);
EXPORT void dstr_cat_dstr(struct dstr *dst, const struct dstr *str);
EXPORT void dstr_cat_strref(struct dstr *dst, const struct strref *str);
static inline void dstr_cat_ch(struct dstr *dst, char ch);
EXPORT void dstr_ncat(struct dstr *dst, const char *array, const size_t len);
EXPORT void dstr_ncat_dstr(struct dstr *dst, const struct dstr *str,
const size_t len);
EXPORT void dstr_insert(struct dstr *dst, const size_t idx, const char *array);
EXPORT void dstr_insert_dstr(struct dstr *dst, const size_t idx,
const struct dstr *str);
EXPORT void dstr_insert_ch(struct dstr *dst, const size_t idx, const char ch);
EXPORT void dstr_remove(struct dstr *dst, const size_t idx, const size_t count);
PRINTFATTR(2, 3)
2013-10-01 02:37:13 +00:00
EXPORT void dstr_printf(struct dstr *dst, const char *format, ...);
PRINTFATTR(2, 3)
2013-10-01 02:37:13 +00:00
EXPORT void dstr_catf(struct dstr *dst, const char *format, ...);
EXPORT void dstr_vprintf(struct dstr *dst, const char *format, va_list args);
EXPORT void dstr_vcatf(struct dstr *dst, const char *format, va_list args);
EXPORT void dstr_safe_printf(struct dstr *dst, const char *format,
const char *val1, const char *val2,
const char *val3, const char *val4);
Split output/input audio capture sources - Split input and output audio captures so that they're different sources. This allows easier handling and enumeration of audio devices without having to do some sort of string processing. This way the user interface code can handle this a bit more easily, and so that it doesn't confuse users either. This should be done for all audio capture sources for all operating systems. You don't have to duplicate any code, you just need to create input/output wrapper functions to designate the audio as input or output before creation. - Make it detect soundflower and wavtap devices as mac "output" devices (even though they're actually input) for the mac output capture, and make it so that users can select a default output capture and automatically use soundflower or wavtap. I'm not entirely happy about having to do this, but because mac is designed this way, this is really the only way to handle it that makes it easier for users and UI code to deal with. Note that soundflower and wavtap are still also designated as input devices, so will still show up in input device enumeration. - Remove pragma messages because they were kind polluting the other compiler messages and just getting in the way. In the future we can just do a grep for TODO to find them. - Redo list property again, this time using a safer internal array, rather than requiring sketchy array inputs. Having functions handle everything behind the scenes is much safer. - Remove the reference counter debug log code, as it was included unintentionally in a commit.
2014-03-03 09:56:54 +00:00
static inline const char *dstr_find_i(const struct dstr *str, const char *find);
2013-10-01 02:37:13 +00:00
static inline const char *dstr_find(const struct dstr *str, const char *find);
EXPORT void dstr_replace(struct dstr *str, const char *find,
const char *replace);
static inline int dstr_cmp(const struct dstr *str1, const char *str2);
static inline int dstr_cmpi(const struct dstr *str1, const char *str2);
static inline int dstr_ncmp(const struct dstr *str1, const char *str2,
const size_t n);
static inline int dstr_ncmpi(const struct dstr *str1, const char *str2,
const size_t n);
EXPORT void dstr_depad(struct dstr *dst);
EXPORT void dstr_left(struct dstr *dst, const struct dstr *str,
const size_t pos);
EXPORT void dstr_mid(struct dstr *dst, const struct dstr *str,
const size_t start, const size_t count);
EXPORT void dstr_right(struct dstr *dst, const struct dstr *str,
const size_t pos);
static inline char dstr_end(const struct dstr *str);
2013-10-01 02:37:13 +00:00
EXPORT void dstr_from_mbs(struct dstr *dst, const char *mbstr);
EXPORT char *dstr_to_mbs(const struct dstr *str);
EXPORT void dstr_from_wcs(struct dstr *dst, const wchar_t *wstr);
EXPORT wchar_t *dstr_to_wcs(const struct dstr *str);
EXPORT void dstr_to_upper(struct dstr *str);
EXPORT void dstr_to_lower(struct dstr *str);
#undef PRINTFATTR
2013-10-01 02:37:13 +00:00
/* ------------------------------------------------------------------------- */
static inline void dstr_init(struct dstr *dst)
{
dst->array = NULL;
dst->len = 0;
dst->capacity = 0;
}
static inline void dstr_init_move_array(struct dstr *dst, char *str)
{
dst->array = str;
dst->len = (!str) ? 0 : strlen(str);
dst->capacity = dst->len + 1;
2013-10-01 02:37:13 +00:00
}
static inline void dstr_init_move(struct dstr *dst, struct dstr *src)
{
*dst = *src;
dstr_init(src);
}
static inline void dstr_init_copy(struct dstr *dst, const char *str)
{
dstr_init(dst);
dstr_copy(dst, str);
}
static inline void dstr_init_copy_dstr(struct dstr *dst, const struct dstr *src)
{
dstr_init(dst);
dstr_copy_dstr(dst, src);
}
static inline void dstr_free(struct dstr *dst)
{
bfree(dst->array);
dst->array = NULL;
dst->len = 0;
dst->capacity = 0;
}
static inline void dstr_array_free(struct dstr *array, const size_t count)
{
size_t i;
for (i = 0; i < count; i++)
dstr_free(array + i);
}
static inline void dstr_move_array(struct dstr *dst, char *str)
{
dstr_free(dst);
dst->array = str;
dst->len = (!str) ? 0 : strlen(str);
dst->capacity = dst->len + 1;
2013-10-01 02:37:13 +00:00
}
static inline void dstr_move(struct dstr *dst, struct dstr *src)
{
dstr_free(dst);
dstr_init_move(dst, src);
}
static inline void dstr_ensure_capacity(struct dstr *dst, const size_t new_size)
{
size_t new_cap;
if (new_size <= dst->capacity)
return;
new_cap = (!dst->capacity) ? new_size : dst->capacity * 2;
if (new_size > new_cap)
new_cap = new_size;
dst->array = (char *)brealloc(dst->array, new_cap);
dst->capacity = new_cap;
}
static inline void dstr_copy_dstr(struct dstr *dst, const struct dstr *src)
{
dstr_free(dst);
2013-10-01 02:37:13 +00:00
if (src->len) {
dstr_ensure_capacity(dst, src->len + 1);
memcpy(dst->array, src->array, src->len + 1);
dst->len = src->len;
}
2013-10-01 02:37:13 +00:00
}
static inline void dstr_reserve(struct dstr *dst, const size_t capacity)
{
if (capacity == 0 || capacity <= dst->len)
return;
dst->array = (char *)brealloc(dst->array, capacity);
dst->capacity = capacity;
}
static inline void dstr_resize(struct dstr *dst, const size_t num)
{
if (!num) {
dstr_free(dst);
return;
}
dstr_ensure_capacity(dst, num + 1);
dst->array[num] = 0;
dst->len = num;
}
static inline bool dstr_is_empty(const struct dstr *str)
2013-10-01 02:37:13 +00:00
{
if (!str->array || !str->len)
return true;
if (!*str->array)
return true;
return false;
}
static inline void dstr_cat(struct dstr *dst, const char *array)
{
size_t len;
if (!array || !*array)
return;
len = strlen(array);
dstr_ncat(dst, array, len);
}
static inline void dstr_cat_ch(struct dstr *dst, char ch)
{
dstr_ensure_capacity(dst, ++dst->len + 1);
dst->array[dst->len - 1] = ch;
dst->array[dst->len] = 0;
}
Split output/input audio capture sources - Split input and output audio captures so that they're different sources. This allows easier handling and enumeration of audio devices without having to do some sort of string processing. This way the user interface code can handle this a bit more easily, and so that it doesn't confuse users either. This should be done for all audio capture sources for all operating systems. You don't have to duplicate any code, you just need to create input/output wrapper functions to designate the audio as input or output before creation. - Make it detect soundflower and wavtap devices as mac "output" devices (even though they're actually input) for the mac output capture, and make it so that users can select a default output capture and automatically use soundflower or wavtap. I'm not entirely happy about having to do this, but because mac is designed this way, this is really the only way to handle it that makes it easier for users and UI code to deal with. Note that soundflower and wavtap are still also designated as input devices, so will still show up in input device enumeration. - Remove pragma messages because they were kind polluting the other compiler messages and just getting in the way. In the future we can just do a grep for TODO to find them. - Redo list property again, this time using a safer internal array, rather than requiring sketchy array inputs. Having functions handle everything behind the scenes is much safer. - Remove the reference counter debug log code, as it was included unintentionally in a commit.
2014-03-03 09:56:54 +00:00
static inline const char *dstr_find_i(const struct dstr *str, const char *find)
{
return astrstri(str->array, find);
}
static inline const char *dstr_find(const struct dstr *str, const char *find)
2013-10-01 02:37:13 +00:00
{
return strstr(str->array, find);
}
static inline int dstr_cmp(const struct dstr *str1, const char *str2)
{
return strcmp(str1->array, str2);
}
static inline int dstr_cmpi(const struct dstr *str1, const char *str2)
{
return astrcmpi(str1->array, str2);
}
static inline int dstr_ncmp(const struct dstr *str1, const char *str2,
const size_t n)
{
return astrcmp_n(str1->array, str2, n);
}
static inline int dstr_ncmpi(const struct dstr *str1, const char *str2,
const size_t n)
{
return astrcmpi_n(str1->array, str2, n);
}
static inline char dstr_end(const struct dstr *str)
{
if (dstr_is_empty(str))
return 0;
return str->array[str->len - 1];
}
2013-10-01 02:37:13 +00:00
#ifdef __cplusplus
}
#endif