MWCC/command_line/CmdLine/Src/OSLib/StringUtils.c

152 lines
3.0 KiB
C
Raw Normal View History

#include "oslib.h"
2022-10-12 17:07:57 +00:00
#include "macemul.h"
2022-10-12 17:07:57 +00:00
#define OPTION_ASSERT(cond) do { if (!(cond)) { printf("%s:%u: failed assertion\n", __FILE__, __LINE__); abort(); } } while(0)
static char pfbuf[256];
2022-10-12 17:07:57 +00:00
StringPtr _pstrcpy(StringPtr dst, ConstStringPtr src) {
memcpy(dst, src, src[0] + 1);
return dst;
}
void _pstrcat(StringPtr dst, ConstStringPtr src) {
2022-10-12 17:07:57 +00:00
int cnt = dst[0] + src[0];
if (cnt > 255)
cnt = 255;
memcpy(dst + 1 + dst[0], src + 1, cnt - dst[0]);
dst[0] = cnt;
}
void _pstrcharcat(StringPtr to, char ch) {
2022-10-12 17:07:57 +00:00
if (to[0] < 255) {
to[0]++;
to[to[0]] = ch;
}
}
void pstrncpy(StringPtr to, ConstStringPtr from, int max) {
2022-10-12 17:07:57 +00:00
SInt16 i = (from[0] > max) ? max : from[0];
while (i >= 0) {
*(to++) = *(from++);
--i;
}
}
void pstrncat(StringPtr to, ConstStringPtr append, int max) {
2022-10-12 17:07:57 +00:00
SInt16 i;
SInt16 n;
i = *(append++);
n = to[0];
if (i + n > max)
i = max - n;
to[0] += i;
to += n + 1;
while (i-- > 0) {
*(to++) = *(append++);
}
}
int pstrcmp(ConstStringPtr a, ConstStringPtr b) {
2022-10-12 17:07:57 +00:00
int d = *(b++), n = *(a++);
if (n - d)
return n - d;
while (n-- > 0) {
d = *(a++) != *(b++);
if (d)
return d;
}
2022-10-12 17:07:57 +00:00
return 0;
}
int pstrchr(ConstStringPtr str, char find) {
2022-10-12 17:07:57 +00:00
int idx = 0;
2022-10-12 17:07:57 +00:00
while (idx++ < str[0]) {
if (find == str[idx])
return idx;
}
return 0;
}
void c2pstrcpy(StringPtr dst, const char *src) {
2022-10-12 17:07:57 +00:00
int len = strlen(src);
if (len > 255)
len = 255;
2022-10-12 17:07:57 +00:00
memmove(dst + 1, src, len);
dst[0] = len;
}
void p2cstrcpy(char *dst, ConstStringPtr src) {
2022-10-12 17:07:57 +00:00
memcpy(dst, src + 1, src[0]);
dst[src[0]] = 0;
}
char *mvprintf(char *mybuf, unsigned int len, const char *format, va_list va) {
2022-10-12 17:07:57 +00:00
int maxlen;
int ret;
char *buf;
#line 134
OPTION_ASSERT(mybuf != NULL);
maxlen = len - 1;
buf = mybuf;
ret = vsnprintf(mybuf, maxlen, format, va);
if (ret < 0) {
do {
if (buf != mybuf)
free(buf);
maxlen <<= 1;
buf = malloc(maxlen);
if (!buf)
return strncpy(mybuf, "<out of memory>", len);
ret = vsnprintf(buf, maxlen, format, va);
} while (ret < 0);
} else if (ret > maxlen) {
maxlen = ret + 1;
buf = malloc(maxlen);
if (!buf)
return strncpy(mybuf, "<out of memory>", len);
vsnprintf(buf, maxlen, format, va);
}
return buf;
}
char *mprintf(char *mybuf, unsigned int len, const char *format, ...) {
2022-10-12 17:07:57 +00:00
char *ret;
va_list ap;
va_start(ap, format);
ret = mvprintf(mybuf, len, format, ap);
va_end(ap);
2022-10-12 17:07:57 +00:00
return ret;
}
int HPrintF(Handle text, const char *format, ...) {
2022-10-12 17:07:57 +00:00
char *buf;
int ret;
va_list ap;
va_start(ap, format);
buf = mvprintf(pfbuf, sizeof(pfbuf), format, ap);
va_end(ap);
ret = strlen(buf);
if (PtrAndHand(buf, text, strlen(buf)))
return 0;
if (buf != pfbuf)
free(buf);
2022-10-12 17:07:57 +00:00
return ret;
}