summaryrefslogtreecommitdiff
path: root/command_line/CmdLine/Src/MacEmul/TextUtils.c
blob: a23ac7e18290fe4be7e3cc4c678d1e0f29fa4bbf (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
#include "mwcc_decomp.h"

StringPtr c2pstr(char *s) {
    unsigned int l;
    l = strlen(s);
    if (l > 255)
        l = 255;
    memmove(s + 1, s, l);
    s[0] = l;
    return (StringPtr) s;
}

char *p2cstr(StringPtr s) {
    unsigned int l;
    l = s[0];
    memmove(s, s + 1, l);
    s[l] = 0;
    return (char *) s;
}

SInt16 CharacterByteType(Ptr textBuf, SInt16 textOffset, ScriptCode script) {
    return 0;
}

SInt16 CharByte() {
    return 0;
}

void GetDateTime(UInt32 *secs) {
    time_t time;
    OS_GetTime(&time);
    OS_TimeToMac(time, secs);
}

Boolean EqualString(ConstStr255Param strA, ConstStr255Param strB, Boolean caseSens, Boolean diacSens) {
    int i, length;
    Boolean equal;

    equal = 0;
    length = strA[0];
    if (strA && strB && length == strB[0]) {
        if (caseSens) {
            equal = !memcmp(strA + 1, strB + 1, length);
        } else {
            equal = 1;
            i = 0;
            while (equal && i < length) {
                equal = toupper(strA[i]) == toupper(strB[i]);
                ++i;
            }
        }
    }

    return equal;
}

void GetIndString(Str255 theString, SInt16 strListID, SInt16 index) {
    SInt16 last;
    SInt16 idc;
    SInt32 len;
    Handle strs;
    const char *ret;
    StringPtr ptr;
    StringPtr dptr;
    Str255 tmp;

    ret = Res_GetResource(strListID, index);
    if (ret) {
        strcpy((char *) tmp, ret);
        c2pstr((char *) tmp);
    } else {
        sprintf((char *) tmp, "[Resource string id=%d index=%d not found]", strListID, index);
        c2pstr((char *) tmp);

        strs = GetResource('STR#', strListID);
        if (strs) {
            last = (((unsigned char) (*strs)[0]) << 8) + ((unsigned char) (*strs)[1]);
            if (index > 0 && index <= last) {
                len = GetHandleSize(strs);
                HLock(strs);
                ptr = (StringPtr) (*strs + 2);
                idc = index;
                dptr = (StringPtr) (*strs + len);
                while (ptr < dptr && --idc) {
                    ptr += *ptr + 1;
                }
                if (ptr < dptr)
                    _pstrcpy(tmp, ptr);
                HUnlock(strs);
            }
        }
    }

    ptr = &tmp[1];
    dptr = &theString[1];
    while (ptr <= &tmp[tmp[0]]) {
        if (*ptr == 0xD4) {
            *dptr = '`';
        } else if (*ptr == 0xD5) {
            *dptr = '\'';
        } else if (*ptr == 0xD2 || *ptr == 0xD3) {
            *dptr = '"';
        } else if (*ptr == 0xC9 && (dptr - theString) < 253) {
            dptr[0] = '.';
            dptr[1] = '.';
            dptr[2] = '.';
            dptr += 2;
        } else {
            *dptr = *ptr;
        }
        ++ptr;
        ++dptr;
    }

    theString[0] = (dptr - theString) - 1;
}

char *getindstring(char *theString, SInt16 strListID, SInt16 index) {
    GetIndString((StringPtr) theString, strListID, index);
    return p2cstr((StringPtr) theString);
}

void NumToString(SInt32 theNum, Str255 theString) {
    sprintf((char *) theString, "%d", theNum);
    c2pstr((char *) theString);
}

void StringToNum(ConstStr255Param theString, SInt32 *theNum) {
    p2cstr((StringPtr) theString);
    sscanf((char *) theString, "%d", theNum);
    c2pstr((char *) theString);
}