File: e_trans.cpp

package info (click to toggle)
efte 1.1-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid, trixie
  • size: 3,800 kB
  • sloc: cpp: 43,587; ansic: 1,228; makefile: 271; objc: 92; sh: 40
file content (272 lines) | stat: -rw-r--r-- 6,273 bytes parent folder | download | duplicates (3)
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/*
 *    e_trans.cpp
 *
 *    Copyright (c) 2008, eFTE SF Group (see AUTHORS file)
 *    Copyright (c) 1994-1996, Marko Macek
 *
 *    You may distribute under the terms of either the GNU General Public
 *    License or the Artistic License, as specified in the README file.
 *
 */

#include "fte.h"
#include <ctype.h>

// FLAW: NULL characters can not be translated, need escaping
int ParseTrans(unsigned char *S, unsigned char *D, TransTable tab) {
    unsigned char Dest[512];
    unsigned char A, B;
    unsigned int i;

    if (S == 0 || D == 0)
        return 0;

    strncpy((char *)Dest, (char *)D, sizeof(Dest) - 1);
    Dest[sizeof(Dest) - 1] = 0;
    D = Dest;

    // no translation
    for (i = 0; i < 256; i++)
        tab[i] = (unsigned char)i;

    while (*S && *D) {
        if (S[0] && S[1] == '-' && S[2]) {
            if (S[0] <= S[2]) {
                A = (*S)++;
                if (S[0] >= S[2])
                    S += 2;
            } else {
                A = (*S)--;
                if (S[0] <= S[2])
                    S += 2;
            }
        } else {
            A = *S++;
        }
        if (D[0] && D[1] == '-' && D[2]) {
            if (D[0] <= D[2]) {
                B = (*D)++;
                if (D[0] >= D[2])
                    D += 2;
            } else {
                B = (*D)--;
                if (D[0] <= D[2])
                    D += 2;
            }
        } else {
            B = *D++;
        }
        tab[A] = B;
    }
    if (*S != *D) // one was too short
        return 0;
    return 1;
}

int MakeTrans(TransTable tab, int What) {
    int i;

    // no translation
    for (i = 0; i <= 255; i++)
        tab[i] = (unsigned char)i;

    switch (What) {
    case ccToggle:
    case ccUp:
        for (i = 33; i <= 255; i++)
            if (isalpha(i) && (toupper(i) != i))
                tab[i] = (unsigned char) toupper(i);
        if (What != ccToggle)
            break;
    case ccDown:
        for (i = 33; i <= 255; i++)
            if (isalpha(i) && (i == tab[i]) && (tolower(i) != i))
                tab[i] = (unsigned char) tolower(i);
        break;
    default:
        return 0;
    }
    return 1;
}

int EBuffer::BlockTrans(TransTable tab) {
    int L, I, B, E;
    PELine LL;

    if (CheckBlock() == 0) return 0;
    if (RCount == 0) return 0;

    for (L = BB.Row; L <= BE.Row; L++) {
        LL = RLine(L);
        B = 0;
        E = 0;
        switch (BlockMode) {
        case bmLine:
            if (L == BE.Row)
                E = 0;
            else
                E = LL->Count;
            break;
        case bmColumn:
            if (L == BE.Row)
                E = 0;
            else {
                B = CharOffset(LL, BB.Col);
                E = CharOffset(LL, BE.Col);
            }
            break;
        case bmStream:
            if (L == BB.Row && L == BE.Row) {
                B = CharOffset(LL, BB.Col);
                E = CharOffset(LL, BE.Col);
            } else if (L == BB.Row) {
                B = CharOffset(LL, BB.Col);
                E = LL->Count;
            } else if (L == BE.Row) {
                B = 0;
                E = CharOffset(LL, BE.Col);
            } else {
                B = 0;
                E = LL->Count;
            }
            break;
        }
        if (B > LL->Count)
            B = LL->Count;
        if (E > LL->Count)
            E = LL->Count;
        if (E > B) {
            if (ChgChars(L, B, E - B, 0) == 0) return 0;
            for (I = B; I < E; I++)
                LL->Chars[I] = tab[(unsigned char)LL->Chars[I]];
        }
    }
    Draw(BB.Row, BE.Row);
    return 1;
}

int EBuffer::CharTrans(TransTable tab) {
    PELine L = VLine(CP.Row);
    unsigned int P = CharOffset(L, CP.Col);

    if (P >= (unsigned int)L->Count) return 0;
    if (ChgChars(CP.Row, P, 1, 0) == 0) return 0;
    L->Chars[P] = tab[(unsigned char)L->Chars[P]];
    return 1;
}

int EBuffer::LineTrans(TransTable tab) {
    PELine L = VLine(CP.Row);
    int I;

    if (L->Count > 0) {
        if (ChgChars(CP.Row, 0, L->Count, 0) == 0) return 0;
        for (I = 0; I < L->Count; I++)
            L->Chars[I] = tab[(unsigned char)L->Chars[I]];
    }
    return 1;
}

int EBuffer::CharCaseUp() {
    TransTable tab;

    MakeTrans(tab, ccUp);
    return CharTrans(tab);
}

int EBuffer::CharCaseDown() {
    TransTable tab;

    MakeTrans(tab, ccDown);
    return CharTrans(tab);
}

int EBuffer::CharCaseToggle() {
    TransTable tab;

    MakeTrans(tab, ccToggle);
    return CharTrans(tab);
}

int EBuffer::LineCaseUp() {
    TransTable tab;

    MakeTrans(tab, ccUp);
    return LineTrans(tab);
}

int EBuffer::LineCaseDown() {
    TransTable tab;

    MakeTrans(tab, ccDown);
    return LineTrans(tab);
}

int EBuffer::LineCaseToggle()  {
    TransTable tab;

    MakeTrans(tab, ccToggle);
    return LineTrans(tab);
}

int EBuffer::BlockCaseUp() {
    TransTable tab;

    MakeTrans(tab, ccUp);
    return BlockTrans(tab);
}

int EBuffer::BlockCaseDown() {
    TransTable tab;

    MakeTrans(tab, ccDown);
    return BlockTrans(tab);
}

int EBuffer::BlockCaseToggle() {
    TransTable tab;

    MakeTrans(tab, ccToggle);
    return BlockTrans(tab);
}

int EBuffer::GetTrans(ExState &State, TransTable tab) {
    unsigned char TrS[512] = "";
    unsigned char TrD[512] = "";

    if (State.GetStrParam(View, (char *)TrS, sizeof(TrS)) == 0)
        if (View->MView->Win->GetStr("Trans From", sizeof(TrS), (char *)TrS, HIST_TRANS) == 0)
            return 0;
    if (State.GetStrParam(View, (char *)TrD, sizeof(TrD)) == 0)
        if (View->MView->Win->GetStr("Trans To", sizeof(TrS), (char *)TrD, HIST_TRANS) == 0)
            return 0;
    if (ParseTrans(TrS, TrD, tab) == 0) {
        Msg(S_ERROR, "Bad Trans Arguments %s %s.", TrS, TrD);
        return 0;
    }
    return 1;
}

int EBuffer::CharTrans(ExState &State) {
    TransTable tab;

    if (GetTrans(State, tab) == 0)
        return 0;
    return CharTrans(tab);
}

int EBuffer::LineTrans(ExState &State) {
    TransTable tab;

    if (GetTrans(State, tab) == 0)
        return 0;
    return LineTrans(tab);
}

int EBuffer::BlockTrans(ExState &State) {
    TransTable tab;

    if (GetTrans(State, tab) == 0)
        return 0;
    return BlockTrans(tab);
}