treewide: unify signal handling, error reporting, and string utils
Signed-off-by: NotAShelf <raf@notashelf.dev> Change-Id: I8351ecccb03281e438dba666390021306a6a6964
This commit is contained in:
parent
dab6b3b0ae
commit
987f4fcc99
4 changed files with 13 additions and 102 deletions
51
src/utils.c
51
src/utils.c
|
|
@ -442,57 +442,6 @@ const char *chroma_get_file_extension(const char *path) {
|
|||
return last_dot + 1;
|
||||
}
|
||||
|
||||
// Case-insensitive string comparison
|
||||
int chroma_strcasecmp(const char *s1, const char *s2) {
|
||||
if (!s1 || !s2) {
|
||||
return (s1 == s2) ? 0 : (s1 ? 1 : -1);
|
||||
}
|
||||
|
||||
while (*s1 && *s2) {
|
||||
int c1 = tolower((unsigned char)*s1);
|
||||
int c2 = tolower((unsigned char)*s2);
|
||||
|
||||
if (c1 != c2) {
|
||||
return c1 - c2;
|
||||
}
|
||||
|
||||
s1++;
|
||||
s2++;
|
||||
}
|
||||
|
||||
return tolower((unsigned char)*s1) - tolower((unsigned char)*s2);
|
||||
}
|
||||
|
||||
// Safe string copy
|
||||
size_t chroma_strlcpy(char *dst, const char *src, size_t size) {
|
||||
size_t src_len = strlen(src);
|
||||
|
||||
if (size > 0) {
|
||||
size_t copy_len = (src_len < size - 1) ? src_len : size - 1;
|
||||
memcpy(dst, src, copy_len);
|
||||
dst[copy_len] = '\0';
|
||||
}
|
||||
|
||||
return src_len;
|
||||
}
|
||||
|
||||
// Safe string concatenation
|
||||
size_t chroma_strlcat(char *dst, const char *src, size_t size) {
|
||||
size_t dst_len = strnlen(dst, size);
|
||||
size_t src_len = strlen(src);
|
||||
|
||||
if (dst_len < size) {
|
||||
size_t copy_len = size - dst_len - 1;
|
||||
if (src_len < copy_len) {
|
||||
copy_len = src_len;
|
||||
}
|
||||
memcpy(dst + dst_len, src, copy_len);
|
||||
dst[dst_len + copy_len] = '\0';
|
||||
}
|
||||
|
||||
return dst_len + src_len;
|
||||
}
|
||||
|
||||
// Get current time in milliseconds
|
||||
long long chroma_get_time_ms(void) {
|
||||
struct timeval tv;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue