mdelibs/core/string.c

82 lines
1.5 KiB
C
Raw Normal View History

2025-11-22 02:29:49 +09:00
#include <MDE/Core/String.h>
2025-11-16 12:01:34 +09:00
2025-12-28 18:17:59 +09:00
#include <stb_ds.h>
2025-11-17 18:20:56 +09:00
char* MDEStringDuplicate(const char* src) {
2025-11-16 12:01:34 +09:00
char* s = malloc(strlen(src) + 1);
strcpy(s, src);
return s;
}
2025-11-23 17:23:35 +09:00
char* MDEStringConcatenate(const char* str1, const char* str2) {
char* r = malloc(strlen(str1) + strlen(str2) + 1);
strcpy(r, str1);
strcat(r, str2);
return r;
}
2025-12-28 16:06:21 +09:00
char* MDEStringConcatenate3(const char* str1, const char* str2, const char* str3) {
char* r = malloc(strlen(str1) + strlen(str2) + strlen(str3) + 1);
strcpy(r, str1);
strcat(r, str2);
strcat(r, str3);
return r;
}
char* MDEStringConcatenate4(const char* str1, const char* str2, const char* str3, const char* str4) {
char* r = malloc(strlen(str1) + strlen(str2) + strlen(str3) + strlen(str4) + 1);
strcpy(r, str1);
strcat(r, str2);
strcat(r, str3);
strcat(r, str4);
return r;
}
2025-12-28 18:17:59 +09:00
char** MDEStringToExec(const char* exec, const char* file) {
char** a = NULL;
int i;
char* buf = malloc(1);
buf[0] = 0;
for(i = 0;; i++) {
if(exec[i] == ' ' || exec[i] == 0) {
if(strlen(buf) > 0) arrput(a, buf);
buf = malloc(1);
buf[0] = 0;
if(exec[i] == 0) break;
} else if(exec[i] == '%') {
char c = exec[++i];
if(c == 'f' && file != NULL) {
char* old = buf;
buf = malloc(strlen(old) + strlen(file) + 1);
strcpy(buf, old);
strcpy(buf + strlen(old), file);
free(old);
}
} else {
char* old = buf;
buf = malloc(strlen(old) + 2);
strcpy(buf, old);
buf[strlen(old)] = exec[i];
buf[strlen(old) + 1] = 0;
free(old);
}
}
arrput(a, NULL);
return a;
}