summaryrefslogtreecommitdiff
path: root/command_line/CmdLine/Src/OSLib/StringUtils.c
blob: b2a71e72670466439147bf451ae1e9b930e53aa4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include "oslib.h"
#include "macemul.h"

static char pfbuf[256];

StringPtr _pstrcpy(StringPtr dst, ConstStringPtr src) {
    memcpy(dst, src, src[0] + 1);
    return dst;
}

void _pstrcat(StringPtr dst, ConstStringPtr src) {
    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) {
    if (to[0] < 255) {
        to[0]++;
        to[to[0]] = ch;
    }
}

void pstrncpy(StringPtr to, ConstStringPtr from, int max) {
    SInt16 i = (from[0] > max) ? max : from[0];
    while (i >= 0) {
        *(to++) = *(from++);
        --i;
    }
}

void pstrncat(StringPtr to, ConstStringPtr append, int max) {
    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) {
    int d = *(b++), n = *(a++);
    if (n - d)
        return n - d;

    while (n-- > 0) {
        d = *(a++) != *(b++);
        if (d)
            return d;
    }

    return 0;
}

int pstrchr(ConstStringPtr str, char find) {
    int idx = 0;

    while (idx++ < str[0]) {
        if (find == str[idx])
            return idx;
    }

    return 0;
}

void c2pstrcpy(StringPtr dst, const char *src) {
    int len = strlen(src);
    if (len > 255)
        len = 255;

    memmove(dst + 1, src, len);
    dst[0] = len;
}

void p2cstrcpy(char *dst, ConstStringPtr src) {
    memcpy(dst, src + 1, src[0]);
    dst[src[0]] = 0;
}

char *mvprintf(char *mybuf, unsigned int len, const char *format, va_list va) {
    int maxlen;
    int ret;
    char *buf;

    OS_ASSERT(134, 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, ...) {
    char *ret;

    va_list ap;
    va_start(ap, format);
    ret = mvprintf(mybuf, len, format, ap);
    va_end(ap);

    return ret;
}

int HPrintF(Handle text, const char *format, ...) {
    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);

    return ret;
}