mirror of
https://codeberg.org/vyivel/dulcepan/
synced 2025-03-12 18:59:15 +02:00
66 lines
1.3 KiB
C
66 lines
1.3 KiB
C
#include <stdarg.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <strings.h>
|
|
|
|
#include "dulcepan.h"
|
|
|
|
static void vlog_prefixed(const char *prefix, const char *fmt, va_list args) {
|
|
fprintf(stderr, "[%s] ", prefix);
|
|
vfprintf(stderr, fmt, args);
|
|
fprintf(stderr, "\n");
|
|
}
|
|
|
|
void dp_log_error(const char *fmt, ...) {
|
|
va_list args;
|
|
va_start(args, fmt);
|
|
vlog_prefixed("error", fmt, args);
|
|
va_end(args);
|
|
}
|
|
|
|
void dp_log_fatal(const char *fmt, ...) {
|
|
va_list args;
|
|
va_start(args, fmt);
|
|
vlog_prefixed("FATAL", fmt, args);
|
|
va_end(args);
|
|
exit(1);
|
|
}
|
|
|
|
void *dp_zalloc(size_t size) {
|
|
void *ptr = calloc(1, size);
|
|
if (ptr == NULL) {
|
|
dp_log_fatal("Failed to allocate %zu bytes", size);
|
|
}
|
|
return ptr;
|
|
}
|
|
|
|
char *dp_strdup(const char *str) {
|
|
char *ptr = strdup(str);
|
|
if (ptr == NULL) {
|
|
dp_log_fatal("Failed to duplicate %s", str);
|
|
}
|
|
return ptr;
|
|
}
|
|
|
|
const char *dp_ext_from_path(const char *path) {
|
|
size_t len = strlen(path);
|
|
for (size_t i = len; i-- > 0;) {
|
|
if (path[i] == '.') {
|
|
return path + i + 1;
|
|
} else if (path[i] == '/') {
|
|
break;
|
|
}
|
|
}
|
|
return path + len;
|
|
}
|
|
|
|
enum dp_file_format dp_ext_to_format(const char *ext) {
|
|
if (strcasecmp(ext, "ppm") == 0) {
|
|
return DP_FILE_PPM;
|
|
} else if (strcasecmp(ext, "png") == 0) {
|
|
return DP_FILE_PNG;
|
|
}
|
|
return DP_FILE_UNKNOWN;
|
|
}
|