This commit is contained in:
libretroadmin 2023-08-17 21:59:41 +02:00
parent 05ad9b439f
commit e950b63b69
6 changed files with 81 additions and 140 deletions

View file

@ -48,7 +48,7 @@ RETRO_BEGIN_DECLS
#define TOUPPER(c) ((c) & ~(lr_char_props[(unsigned char)(c)] & 0x20))
/* C standard says \f \v are space, but this one disagrees */
#define ISSPACE(c) (lr_char_props[(unsigned char)(c)] & 0x80)
#define ISSPACE(c) (lr_char_props[(unsigned char)(c)] & 0x80)
#define ISDIGIT(c) (lr_char_props[(unsigned char)(c)] & 0x40)
#define ISALPHA(c) (lr_char_props[(unsigned char)(c)] & 0x20)
@ -204,7 +204,7 @@ char *string_trim_whitespace(char *const s);
* correctly any text containing so-called 'wide' Unicode
* characters (e.g. CJK languages, emojis, etc.).
**/
void word_wrap(char *dst, size_t dst_size, const char *src, size_t src_len,
size_t word_wrap(char *dst, size_t dst_size, const char *src, size_t src_len,
int line_width, int wideglyph_width, unsigned max_lines);
/**
@ -241,7 +241,7 @@ void word_wrap(char *dst, size_t dst_size, const char *src, size_t src_len,
* on-screen pixel width deviates greatly from the set
* @wideglyph_width value.
**/
void word_wrap_wideglyph(
size_t word_wrap_wideglyph(
char *dst, size_t dst_size,
const char *src, size_t src_len,
int line_width, int wideglyph_width,
@ -331,7 +331,7 @@ int string_count_occurrences_single_character(const char *str, char c);
/**
* string_replace_whitespace_with_single_character:
*
*
* Leaf function.
*
* Replaces all spaces with given character @c.

View file

@ -43,47 +43,39 @@ static void media_zero_trailing_spaces(char *buf, size_t len)
static bool media_skip_spaces(const char **buf, size_t len)
{
bool found = false;
unsigned i;
if (!buf || !*buf || !**buf)
return false;
for (i = 0; i < len; i++)
if (buf && *buf && **buf)
{
if ((*buf)[i] == ' ' || (*buf)[i] == '\t')
continue;
size_t i;
for (i = 0; i < len; i++)
{
if ((*buf)[i] == ' ' || (*buf)[i] == '\t')
continue;
*buf += i;
found = true;
break;
*buf += i;
return true;
}
}
if (found)
return true;
return false;
}
/* Fill in "info" with detected CD info. Use this when you have a cue file and want it parsed to find the first data track and any pregap info. */
bool media_detect_cd_info_cue(const char *path, media_detect_cd_info_t *info)
{
RFILE *file = NULL;
char *line = NULL;
char track_path[PATH_MAX_LENGTH] = {0};
RFILE *file = NULL;
char *line = NULL;
char track_path[PATH_MAX_LENGTH] = {0};
char track_abs_path[PATH_MAX_LENGTH] = {0};
char track_mode[11] = {0};
bool found_file = false;
bool found_track = false;
unsigned first_data_track = 0;
uint64_t data_track_pregap_bytes = 0;
char track_mode[11] = {0};
bool found_file = false;
bool found_track = false;
unsigned first_data_track = 0;
uint64_t data_track_pregap_bytes = 0;
if (string_is_empty(path) || !info)
return false;
file = filestream_open(path, RETRO_VFS_FILE_ACCESS_READ, 0);
if (!file)
if (!(file = filestream_open(path, RETRO_VFS_FILE_ACCESS_READ, 0)))
{
#ifdef MEDIA_CUE_PARSE_DEBUG
printf("[MEDIA] Could not open cue path for reading: %s\n", path);
@ -116,8 +108,8 @@ bool media_detect_cd_info_cue(const char *path, media_detect_cd_info_t *info)
if (!string_is_empty(file))
{
const char *file_end = NULL;
size_t file_len = 0;
bool quoted = false;
size_t file_len = 0;
bool quoted = false;
if (file[0] == '"')
{
@ -198,7 +190,7 @@ bool media_detect_cd_info_cue(const char *path, media_detect_cd_info_t *info)
if (!string_is_empty(pregap))
{
media_skip_spaces(&pregap, strlen(pregap));
found_file = false;
found_file = false;
found_track = false;
if (first_data_track && !string_is_empty(track_mode))
@ -273,9 +265,7 @@ bool media_detect_cd_info(const char *path, uint64_t pregap_bytes, media_detect_
if (string_is_empty(path) || !info)
return false;
file = filestream_open(path, RETRO_VFS_FILE_ACCESS_READ, 0);
if (!file)
if (!(file = filestream_open(path, RETRO_VFS_FILE_ACCESS_READ, 0)))
{
#ifdef MEDIA_CUE_PARSE_DEBUG
printf("[MEDIA] Could not open path for reading: %s\n", path);
@ -285,11 +275,11 @@ bool media_detect_cd_info(const char *path, uint64_t pregap_bytes, media_detect_
}
{
unsigned offset = 0;
unsigned offset = 0;
unsigned sector_size = 0;
unsigned buf_size = 17 * 2352;
char *buf = (char*)calloc(1, buf_size);
int64_t read_bytes = 0;
unsigned buf_size = 17 * 2352;
char *buf = (char*)calloc(1, buf_size);
int64_t read_bytes = 0;
if (!buf)
return false;
@ -321,23 +311,14 @@ bool media_detect_cd_info(const char *path, uint64_t pregap_bytes, media_detect_
/* Assume track data contains all fields. */
sector_size = 2352;
/* Assume Mode 2 formed (formless is rarely used) */
if (buf[15] == 2)
{
/* assume Mode 2 formed (formless is rarely used) */
offset = 24;
}
else
{
/* assume Mode 1 */
offset = 16;
}
offset = 24;
else /* Assume Mode 1 */
offset = 16;
}
else
{
/* Assume sectors only contain user data instead. */
offset = 0;
else /* Assume sectors only contain user data instead. */
sector_size = 2048;
}
if (!memcmp(buf + offset, "SEGADISCSYSTEM",
STRLEN_CONST("SEGADISCSYSTEM")))
@ -551,13 +532,11 @@ bool media_detect_cd_info(const char *path, uint64_t pregap_bytes, media_detect_
else if (!memcmp(buf + offset, "\x01\x5a\x5a\x5a\x5a\x5a\x01\x00\x00\x00\x00\x00", 12))
{
info->system_id = MEDIA_CD_SYSTEM_3DO;
strcpy_literal(info->system, "3DO");
}
else if (!memcmp(buf + offset + 0x950, "PC Engine CD-ROM SYSTEM", 23))
{
info->system_id = MEDIA_CD_SYSTEM_PC_ENGINE_CD;
strcpy_literal(info->system, "TurboGrafx-CD / PC-Engine CD");
}

View file

@ -46,9 +46,7 @@ static bool fifo_initialize_internal(fifo_buffer_t *buf, size_t size)
bool fifo_initialize(fifo_buffer_t *buf, size_t size)
{
if (!buf)
return false;
return fifo_initialize_internal(buf, size);
return (buf && fifo_initialize_internal(buf, size));
}
void fifo_free(fifo_buffer_t *buffer)

View file

@ -28,12 +28,15 @@
#include <compat/strl.h>
#include <compat/posix_string.h>
static bool msg_queue_initialize_internal(msg_queue_t *queue, size_t size)
bool msg_queue_initialize(msg_queue_t *queue, size_t size)
{
struct queue_elem **elems = (struct queue_elem**)calloc(size + 1,
sizeof(struct queue_elem*));
struct queue_elem **elems = NULL;
if (!elems)
if (!queue)
return false;
if (!(elems = (struct queue_elem**)
calloc(size + 1, sizeof(struct queue_elem*))))
return false;
queue->tmp_msg = NULL;
@ -55,27 +58,18 @@ static bool msg_queue_initialize_internal(msg_queue_t *queue, size_t size)
**/
msg_queue_t *msg_queue_new(size_t size)
{
msg_queue_t *queue = (msg_queue_t*)malloc(sizeof(*queue));
msg_queue_t *queue = (msg_queue_t*)malloc(sizeof(*queue));
if (!queue)
return NULL;
if (!msg_queue_initialize_internal(queue, size))
if (!msg_queue_initialize(queue, size))
{
free(queue);
if (queue)
free(queue);
return NULL;
}
return queue;
}
bool msg_queue_initialize(msg_queue_t *queue, size_t size)
{
if (!queue)
return false;
return msg_queue_initialize_internal(queue, size);
}
/**
* msg_queue_free:
* @queue : pointer to queue object
@ -126,9 +120,7 @@ void msg_queue_push(msg_queue_t *queue, const char *msg,
if (!queue || queue->ptr >= queue->size)
return;
new_elem = (struct queue_elem*)malloc(
sizeof(struct queue_elem));
if (!new_elem)
if (!(new_elem = (struct queue_elem*)malloc(sizeof(struct queue_elem))))
return;
new_elem->duration = duration;
@ -180,7 +172,7 @@ void msg_queue_clear(msg_queue_t *queue)
queue->elems[i] = NULL;
}
}
queue->ptr = 1;
queue->ptr = 1;
free(queue->tmp_msg);
queue->tmp_msg = NULL;
}
@ -199,8 +191,6 @@ const char *msg_queue_pull(msg_queue_t *queue)
struct queue_elem *front = NULL, *last = NULL;
size_t tmp_ptr = 1;
(void)tmp_ptr;
/* Nothing in queue. */
if (!queue || queue->ptr == 1)
return NULL;

View file

@ -68,7 +68,7 @@ static slock_t *property_lock = NULL;
static slock_t *queue_lock = NULL;
static scond_t *worker_cond = NULL;
static sthread_t *worker_thread = NULL;
static bool worker_continue = true;
static bool worker_continue = true;
/* use running_lock when touching it */
#endif
@ -132,11 +132,11 @@ static void task_queue_put(task_queue_t *queue, retro_task_t *task)
if (queue->front)
{
/* Make sure to insert in order - the queue is
/* Make sure to insert in order - the queue is
* sorted by 'when' so items that aren't scheduled
* to run immediately are at the back of the queue.
* to run immediately are at the back of the queue.
* Items with the same 'when' are inserted after
* all the other items with the same 'when'.
* all the other items with the same 'when'.
* This primarily affects items with a 'when' of 0.
*/
if (queue->back)
@ -234,7 +234,7 @@ static void retro_task_regular_gather(void)
if (task->finished)
task_queue_put(&tasks_finished, task);
else
retro_task_regular_push_running(task);
task_queue_put(&tasks_running, task);
}
retro_task_internal_gather();
@ -468,18 +468,14 @@ static void retro_task_threaded_retrieve(task_retriever_data_t *data)
{
/* Protect access to running tasks */
slock_lock(running_lock);
/* Call regular retrieve function */
retro_task_regular_retrieve(data);
/* Release access to running tasks */
slock_unlock(running_lock);
}
static void threaded_worker(void *userdata)
{
(void)userdata;
for (;;)
{
retro_task_t *task = NULL;
@ -522,13 +518,13 @@ static void threaded_worker(void *userdata)
if (!finished)
{
/* Move the task to the back of the queue */
/* mimics retro_task_threaded_push_running,
/* mimics retro_task_threaded_push_running,
* but also includes a task_queue_remove */
slock_lock(running_lock);
slock_lock(queue_lock);
/* do nothing if only item in queue */
if (task->next)
if (task->next)
{
task_queue_remove(&tasks_running, task);
task_queue_put(&tasks_running, task);
@ -734,16 +730,13 @@ void task_queue_cancel_task(void *task)
void *task_queue_retriever_info_next(task_retriever_info_t **link)
{
void *data = NULL;
/* Grab data and move to next link */
if (*link)
{
data = (*link)->data;
*link = (*link)->next;
return (*link)->data;
}
return data;
return NULL;
}
void task_queue_retriever_info_free(task_retriever_info_t *list)

View file

@ -217,27 +217,24 @@ char *string_trim_whitespace(char *const s)
* correctly any text containing so-called 'wide' Unicode
* characters (e.g. CJK languages, emojis, etc.).
**/
void word_wrap(
size_t word_wrap(
char *dst, size_t dst_size,
const char *src, size_t src_len,
int line_width, int wideglyph_width, unsigned max_lines)
{
char *lastspace = NULL;
char *last_space = NULL;
unsigned counter = 0;
unsigned lines = 1;
const char *src_end = src + src_len;
/* Prevent buffer overflow */
if (dst_size < src_len + 1)
return;
return 0;
/* Early return if src string length is less
* than line width */
if (src_len < (size_t)line_width)
{
strlcpy(dst, src, dst_size);
return;
}
return strlcpy(dst, src, dst_size);
while (*src != '\0')
{
@ -245,21 +242,18 @@ void word_wrap(
counter++;
if (*src == ' ')
lastspace = dst; /* Remember the location of the whitespace */
last_space = dst; /* Remember the location of the whitespace */
else if (*src == '\n')
{
/* If newlines embedded in the input,
* reset the index */
lines++;
counter = 0;
counter = 0;
/* Early return if remaining src string
* length is less than line width */
if (src_end - src <= line_width)
{
strlcpy(dst, src, dst_size);
return;
}
return strlcpy(dst, src, dst_size);
}
while (char_len--)
@ -269,29 +263,27 @@ void word_wrap(
{
counter = 0;
if (lastspace && (max_lines == 0 || lines < max_lines))
if (last_space && (max_lines == 0 || lines < max_lines))
{
/* Replace nearest (previous) whitespace
* with newline character */
*lastspace = '\n';
*last_space = '\n';
lines++;
src -= dst - lastspace - 1;
dst = lastspace + 1;
lastspace = NULL;
src -= dst - last_space - 1;
dst = last_space + 1;
last_space = NULL;
/* Early return if remaining src string
* length is less than line width */
if (src_end - src < line_width)
{
strlcpy(dst, src, dst_size);
return;
}
return strlcpy(dst, src, dst_size);
}
}
}
*dst = '\0';
return 0;
}
/**
@ -327,7 +319,7 @@ void word_wrap(
* on-screen pixel width deviates greatly from the set
* @wideglyph_width value.
**/
void word_wrap_wideglyph(char *dst, size_t dst_size,
size_t word_wrap_wideglyph(char *dst, size_t dst_size,
const char *src, size_t src_len, int line_width,
int wideglyph_width, unsigned max_lines)
{
@ -359,14 +351,11 @@ void word_wrap_wideglyph(char *dst, size_t dst_size,
unsigned counter_normalized = 0;
int line_width_normalized = line_width * 100;
int additional_counter_normalized = wideglyph_width - 100;
/* Early return if src string length is less
* than line width */
if (src_end - src < line_width)
{
strlcpy(dst, src, dst_size);
return;
}
return strlcpy(dst, src, dst_size);
while (*src != '\0')
{
@ -389,10 +378,7 @@ void word_wrap_wideglyph(char *dst, size_t dst_size,
/* Early return if remaining src string
* length is less than line width */
if (src_end - src <= line_width)
{
strlcpy(dst, src, dst_size);
return;
}
return strlcpy(dst, src, dst_size);
}
else if (char_len >= 3)
{
@ -424,10 +410,7 @@ void word_wrap_wideglyph(char *dst, size_t dst_size,
/* Early return if remaining src string
* length is less than line width */
if (src_end - src <= line_width)
{
strlcpy(dst, src, dst_size);
return;
}
return strlcpy(dst, src, dst_size);
}
else if (lastspace)
{
@ -442,15 +425,13 @@ void word_wrap_wideglyph(char *dst, size_t dst_size,
/* Early return if remaining src string
* length is less than line width */
if (src_end - src < line_width)
{
strlcpy(dst, src, dst_size);
return;
}
return strlcpy(dst, src, dst_size);
}
}
}
*dst = '\0';
return 0;
}
/**
@ -592,7 +573,7 @@ unsigned string_hex_to_unsigned(const char *str)
if (str[0] != '\0' && str[1] != '\0')
{
if ( (str[0] == '0') &&
((str[1] == 'x') ||
((str[1] == 'x') ||
(str[1] == 'X')))
{
hex_str = str + 2;
@ -635,7 +616,7 @@ int string_count_occurrences_single_character(const char *str, char c)
/**
* string_replace_whitespace_with_single_character:
*
*
* Leaf function.
*
* Replaces all spaces with given character @c.