File: e_line.cpp

package info (click to toggle)
fte 0.50.0-1.1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 3,536 kB
  • ctags: 6,167
  • sloc: cpp: 45,854; ansic: 2,586; perl: 808; makefile: 125; sh: 104
file content (233 lines) | stat: -rw-r--r-- 5,244 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
/*    e_line.cpp
 *
 *    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"

ELine::ELine(int ACount, char *AChars) {
    Chars = NULL; 
    Count = ACount; 
    Allocate(Count); 
#ifdef CONFIG_SYNTAX_HILIT
    StateE = 0;
#endif
    if (AChars)
        memcpy(Chars, AChars, Count);
    else
        memset(Chars, ' ', Count);
}

ELine::ELine(char *AChars, int ACount) {
    Chars = AChars;
    Count = ACount; 
#ifdef CONFIG_SYNTAX_HILIT
    StateE = 0;
#endif
}

ELine::~ELine() {
    if (Chars)
	free(Chars);
}

int ELine::Allocate(unsigned int Bytes) { 
    unsigned int Allocated;
    
    Allocated = (Bytes | CHAR_TRESHOLD);
    if (Chars) 
        Chars = (char *) realloc(Chars, Allocated); 
    else
        Chars = (char *) malloc(Allocated); 
    if (Chars == NULL) 
        return 0;
    return 1;
}

int EBuffer::ScreenPos(ELine *L, int Offset) {
    int ExpandTabs = BFI(this, BFI_ExpandTabs);
    int TabSize = BFI(this, BFI_TabSize);

    if (!ExpandTabs) {
        return Offset;
    } else {
        char *p = L->Chars;
        int Len = L->Count;
        int Pos = 0;
        int Ofs = Offset;

        if (Ofs > Len) {
            while (Len > 0) {
                if (*p++ != '\t')
                    Pos++;
                else
                    Pos = NextTab(Pos, TabSize);
                Len--;
            }
            Pos += Ofs - L->Count;
        } else {
            while (Ofs > 0) {
                if (*p++ != '\t')
                    Pos++;
                else
                    Pos = NextTab(Pos, TabSize);
                Ofs--;
            }
        }
        return Pos;
    }
}

int EBuffer::CharOffset(ELine *L, int ScreenPos) {
    int ExpandTabs = BFI(this, BFI_ExpandTabs);
    int TabSize = BFI(this, BFI_TabSize);

    if (!ExpandTabs) {
        return ScreenPos;
    } else {
        int Pos = 0;
        int Ofs = 0;
        char *p = L->Chars;
        int Len = L->Count;

        while (Len > 0) {
            if (*p++ != '\t')
                Pos++;
            else
                Pos = NextTab(Pos, TabSize);
            if (Pos > ScreenPos)
                return Ofs;
            Ofs++;
            Len--;
        }
        return Ofs + ScreenPos - Pos;
    }
}

int EBuffer::Allocate(int ACount) {
    PELine *L;
    
    L = (PELine *) realloc(LL, sizeof(PELine) * (ACount + 1));
    if (L == 0 && ACount != 0)
        return 0;
    RAllocated = ACount;
    LL = L;
    return 1;
}

int EBuffer::MoveRGap(int RPos) {
    int GapSize = RAllocated - RCount;
    
    if (RGap == RPos) return 1;
    if (RPos < 0 || RPos > RCount) return 0;
    
    if (RGap < RPos) {
        if (RPos - RGap == 1) {
            LL[RGap] = LL[RGap + GapSize];
        } else {
            memmove(LL + RGap,
                LL + RGap + GapSize,
                sizeof(PELine) * (RPos - RGap));
        }
    } else {
        if (RGap - RPos == 1) {
            LL[RPos + GapSize] = LL[RPos];
        } else {
            memmove(LL + RPos + GapSize,
                    LL + RPos,
                    sizeof(PELine) * (RGap - RPos));
        }
    }
    RGap = RPos;
    return 1;
}

int EBuffer::AllocVis(int ACount) {
    int *V;
    
    V = (int *) realloc(VV, sizeof(int) * (ACount + 1));
    if (V == 0 && ACount != 0) return 0;
    VAllocated = ACount;
    VV = V;
    return 1;
}

int EBuffer::MoveVGap(int VPos) {
    int GapSize = VAllocated - VCount;
    
    if (VGap == VPos) return 1;
    if (VPos < 0 || VPos > VCount) return 0;
    
    if (VGap < VPos) {
        if (VPos - VGap == 1) {
            VV[VGap] = VV[VGap + GapSize];
        } else {
            memmove(VV + VGap,
                    VV + VGap + GapSize,
                    sizeof(VV[0]) * (VPos - VGap));
        }
    } else {
        if (VGap - VPos == 1) {
            VV[VPos + GapSize] = VV[VPos];
        } else {
            memmove(VV + VPos + GapSize,
                    VV + VPos,
                    sizeof(VV[0]) * (VGap - VPos));
        }
    }
    VGap = VPos;
    return 1;
}

int EBuffer::RToV(int No) {
    int L = 0, R = VCount, M, V;
    
    if (No > Vis(VCount - 1) + VCount - 1)   // beyond end
        return -1;
    if (No < VCount) // no folds before (direct match)
        if (Vis(No) == 0) return No;

    while (L < R) {
        M = (L + R) >> 1;
        V = Vis(M) + M;
        if (V == No)
            return M;
        else if (V > No)
            R = M;
        else
            L = M + 1;
    }
    return -1;
}

int EBuffer::RToVN(int No) {
    int L = 0, R = VCount, M, V;
    
    if (No == RCount)
        return VCount;
    if (No > Vis(VCount - 1) + VCount - 1) 
        return VCount - 1;
    if (No < VCount)
        if (Vis(No) == 0) return No;
    
    while (L < R) {
        M = (L + R) >> 1;
        V = Vis(M) + M;
        if (V == No)
            return M;
        else if (V > No)
            R = M;
        else {
            if (M == VCount - 1)
                return M;
            else if (Vis(M + 1) + M + 1 > No)
                return M;
            L = M + 1;
        }
    }
    return R;
}