Subversion Repositories Kolibri OS

Rev

Rev 9010 | Rev 9060 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
8728 leency 1
(*
2
    Copyright 2021 Anton Krotov
3
 
4
    This file is part of CEdit.
5
 
6
    CEdit is free software: you can redistribute it and/or modify
7
    it under the terms of the GNU General Public License as published by
8
    the Free Software Foundation, either version 3 of the License, or
9
    (at your option) any later version.
10
 
11
    CEdit is distributed in the hope that it will be useful,
12
    but WITHOUT ANY WARRANTY; without even the implied warranty of
13
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
    GNU General Public License for more details.
15
 
16
    You should have received a copy of the GNU General Public License
17
    along with CEdit. If not, see .
18
*)
19
 
20
MODULE Lines;
21
 
22
IMPORT
23
    List, SYSTEM, API, Utils;
24
 
25
CONST
26
    WCHAR_SIZE = 2;
27
    SPACE = 20X;
28
 
29
TYPE
30
 
31
    tLine* = POINTER TO RECORD (List.tItem)
32
        ptr: INTEGER;
33
        length*: INTEGER;
34
        modified*, saved*, temp: BOOLEAN;
35
        cin*, cout*, pos*: INTEGER
36
    END;
37
 
38
    PmovInt = PROCEDURE (VAR v: INTEGER; x: INTEGER);
39
    PmovBool = PROCEDURE (VAR v: BOOLEAN; x: BOOLEAN);
40
    PmovPtr = PROCEDURE (VAR v: List.tItem; x: List.tItem);
41
(*
42
    PTypedPtr = PROCEDURE (p: List.tItem);
43
    PUntypedPtr = PROCEDURE (p: INTEGER);
44
*)
45
 
46
VAR
47
 
48
    _movInt: PmovInt;
49
    _movBool: PmovBool;
50
    _movPtr: PmovPtr;
51
(*    _typedPtr: PTypedPtr;
52
    _untypedPtr: PUntypedPtr;*)
53
 
9050 leency 54
    pMaxLength: INTEGER;
8728 leency 55
 
56
 
57
PROCEDURE movInt (VAR v: INTEGER; x: INTEGER);
58
BEGIN
59
    _movInt(v, x)
60
END movInt;
61
 
62
 
63
PROCEDURE movBool (VAR v: BOOLEAN; x: BOOLEAN);
64
BEGIN
65
    _movBool(v, x)
66
END movBool;
67
 
68
 
69
PROCEDURE movPtr (VAR v: List.tItem; x: List.tItem);
70
BEGIN
71
    _movPtr(v, x)
72
END movPtr;
73
 
74
 
75
PROCEDURE malloc (size: INTEGER): INTEGER;
76
VAR
77
    ptr: INTEGER;
9050 leency 78
    maxLength: INTEGER;
8728 leency 79
BEGIN
9050 leency 80
    ASSERT(pMaxLength # 0);
81
    SYSTEM.GET(pMaxLength, maxLength);
8728 leency 82
    IF size > maxLength THEN
9050 leency 83
        SYSTEM.PUT(pMaxLength, size)
8728 leency 84
    END;
85
    size := size*WCHAR_SIZE + 4;
86
    INC(size, (-size) MOD 32);
87
    ptr := API._NEW(size)
88
    RETURN ptr
89
END malloc;
90
 
91
 
92
PROCEDURE free (line: tLine; newPtr: INTEGER);
93
BEGIN
94
    IF line.ptr # 0 THEN
95
        IF line.temp THEN
96
            line.ptr := API._DISPOSE(line.ptr)
97
        ELSE
98
            line.ptr := 0
99
        END
100
    END;
101
    IF ~line.temp THEN
102
        movInt(line.ptr, newPtr);
103
(*        IF newPtr # 0 THEN
104
            _untypedPtr(newPtr)
105
        END*)
106
    END;
107
    line.ptr := newPtr
108
END free;
109
 
110
 
111
PROCEDURE create* (temp: BOOLEAN): tLine;
112
VAR
113
    line: tLine;
114
BEGIN
115
    NEW(line);
116
    ASSERT(line # NIL);
117
(*    IF ~temp THEN
118
        _typedPtr(line)
119
    END;*)
120
    line.next := NIL;
121
    line.prev := NIL;
122
    IF ~temp THEN
123
        movPtr(line.next, NIL);
124
        movPtr(line.prev, NIL)
125
    END;
126
    line.ptr := malloc(1);
127
    ASSERT(line.ptr # 0);
128
    IF ~temp THEN
129
        (*_untypedPtr(line.ptr);*)
130
        movInt(line.ptr, line.ptr)
131
    END;
132
    SYSTEM.PUT16(line.ptr, 0);
133
    line.length := 0;
134
    IF ~temp THEN
135
        movInt(line.length, 0)
136
    END;
137
    line.temp := temp;
138
    line.modified := FALSE;
139
    line.saved := FALSE;
140
    IF ~temp THEN
141
        movBool(line.modified, FALSE);
142
        movBool(line.saved, FALSE)
143
    END;
144
    line.cin := 0;
145
    line.cout := 0;
146
    line.pos := 0
147
    RETURN line
148
END create;
149
 
150
 
151
PROCEDURE destroy* (VAR line: tLine);
152
BEGIN
153
    IF line.temp THEN
154
        free(line, 0);
155
        DISPOSE(line)
156
    ELSE
157
        line := NIL
158
    END
159
END destroy;
160
 
161
 
162
PROCEDURE modify* (line: tLine);
163
BEGIN
164
    IF ~line.temp THEN
165
        movBool(line.modified, TRUE);
166
        movBool(line.saved, FALSE)
167
    END;
168
    line.modified := TRUE;
169
    line.saved := FALSE
170
END modify;
171
 
172
 
173
PROCEDURE save* (line: tLine);
174
BEGIN
175
    IF ~line.temp THEN
176
        movBool(line.saved, TRUE);
177
        movBool(line.modified, FALSE)
178
    END;
179
    line.modified := FALSE;
180
    line.saved := TRUE
181
END save;
182
 
183
 
184
PROCEDURE getChar* (line: tLine; i: INTEGER): WCHAR;
185
VAR
186
    c: WCHAR;
187
BEGIN
188
    SYSTEM.GET(line.ptr + i*WCHAR_SIZE, c)
189
    RETURN c
190
END getChar;
191
 
192
 
193
PROCEDURE trimLength* (line: tLine): INTEGER;
194
VAR
195
    i: INTEGER;
196
BEGIN
197
    i := line.length - 1;
198
    WHILE (i >= 0) & (getChar(line, i) = SPACE) DO
199
        DEC(i)
200
    END
201
    RETURN i + 1
202
END trimLength;
203
 
204
 
205
PROCEDURE getPChar* (line: tLine; i: INTEGER): INTEGER;
206
    RETURN line.ptr + i*WCHAR_SIZE
207
END getPChar;
208
 
209
 
210
PROCEDURE setChar* (line: tLine; i: INTEGER; c: WCHAR);
211
BEGIN
212
    SYSTEM.PUT(line.ptr + i*WCHAR_SIZE, c)
213
END setChar;
214
 
215
 
9010 leency 216
PROCEDURE move* (src, dst: tLine);
217
BEGIN
218
    SYSTEM.MOVE(src.ptr, dst.ptr, (MIN(src.length, dst.length) + 1)*WCHAR_SIZE)
219
END move;
220
 
221
 
8728 leency 222
PROCEDURE concat* (line: tLine; s: ARRAY OF WCHAR);
223
VAR
224
    Len: INTEGER;
225
    ptr: INTEGER;
226
BEGIN
227
    Len := LENGTH(s);
228
    ptr := malloc(line.length + Len + 1);
229
    ASSERT(ptr # 0);
230
    SYSTEM.MOVE(line.ptr, ptr, line.length*WCHAR_SIZE);
231
    SYSTEM.MOVE(SYSTEM.ADR(s[0]), ptr + line.length*WCHAR_SIZE, Len*WCHAR_SIZE);
232
    SYSTEM.PUT16(ptr + (line.length + Len)*WCHAR_SIZE, 0);
233
    IF ~line.temp THEN
234
        movInt(line.length, line.length + Len)
235
    END;
236
    INC(line.length, Len);
237
    free(line, ptr)
238
END concat;
239
 
240
 
241
PROCEDURE delChar* (line: tLine; pos: INTEGER);
242
VAR
243
    ptr: INTEGER;
244
BEGIN
245
    IF pos < line.length THEN
246
        ptr := malloc(line.length);
247
        ASSERT(ptr # 0);
248
        IF ~line.temp THEN
249
            movInt(line.length, line.length - 1)
250
        END;
251
        DEC(line.length);
252
        SYSTEM.MOVE(line.ptr, ptr, pos*WCHAR_SIZE);
253
        SYSTEM.MOVE(line.ptr + pos*WCHAR_SIZE + WCHAR_SIZE, ptr + pos*WCHAR_SIZE, (line.length - pos)*WCHAR_SIZE);
254
        SYSTEM.PUT16(ptr + line.length*WCHAR_SIZE, 0);
255
        free(line, ptr)
256
    END
257
END delChar;
258
 
259
 
260
PROCEDURE insert* (line: tLine; pos: INTEGER; c: WCHAR);
261
VAR
262
    ptr: INTEGER;
263
BEGIN
264
    ptr := malloc(line.length + 2);
265
    ASSERT(ptr # 0);
266
    SYSTEM.MOVE(line.ptr, ptr, pos*WCHAR_SIZE);
267
    SYSTEM.PUT(ptr + pos*WCHAR_SIZE, c);
268
    SYSTEM.MOVE(line.ptr + pos*WCHAR_SIZE, ptr + pos*WCHAR_SIZE + WCHAR_SIZE, (line.length - pos)*WCHAR_SIZE);
269
    IF ~line.temp THEN
270
        movInt(line.length, line.length + 1)
271
    END;
272
    INC(line.length);
273
    SYSTEM.PUT16(ptr + line.length*WCHAR_SIZE, 0);
274
    free(line, ptr)
275
END insert;
276
 
277
 
278
PROCEDURE insert2* (line1: tLine; pos: INTEGER; line2: tLine);
279
VAR
280
    ptr: INTEGER;
281
BEGIN
282
    IF line2.length > 0 THEN
283
        ptr := malloc(line1.length + line2.length + 1);
284
        ASSERT(ptr # 0);
285
        SYSTEM.MOVE(line1.ptr, ptr, pos*WCHAR_SIZE);
286
        SYSTEM.MOVE(line2.ptr, ptr + pos*WCHAR_SIZE, line2.length*WCHAR_SIZE);
287
        SYSTEM.MOVE(line1.ptr + pos*WCHAR_SIZE, ptr + (pos + line2.length)*WCHAR_SIZE, (line1.length - pos)*WCHAR_SIZE);
288
        SYSTEM.PUT16(ptr + (line1.length + line2.length)*WCHAR_SIZE, 0);
289
        IF ~line1.temp THEN
290
            movInt(line1.length, line1.length + line2.length)
291
        END;
292
        IF ~line2.temp THEN
293
            movInt(line2.length, 0)
294
        END;
295
        INC(line1.length, line2.length);
296
        line2.length := 0;
297
        free(line1, ptr);
298
        free(line2, 0)
299
    END
300
END insert2;
301
 
302
 
303
PROCEDURE insert3* (line: tLine; pos, n: INTEGER);
304
VAR
305
    ptr: INTEGER;
306
BEGIN
307
    IF n > 0 THEN
308
        ptr := malloc(line.length + n + 1);
309
        ASSERT(ptr # 0);
310
        SYSTEM.MOVE(line.ptr, ptr, pos*WCHAR_SIZE);
311
        SYSTEM.MOVE(line.ptr + pos*WCHAR_SIZE, ptr + (pos + n)*WCHAR_SIZE, (line.length - pos)*WCHAR_SIZE);
312
        SYSTEM.PUT16(ptr + (line.length + n)*WCHAR_SIZE, 0);
313
        IF ~line.temp THEN
314
            movInt(line.length, line.length + n)
315
        END;
316
        INC(line.length, n);
317
        free(line, ptr)
318
    END
319
END insert3;
320
 
321
 
322
PROCEDURE delCharN* (line: tLine; pos, n: INTEGER);
323
VAR
324
    ptr: INTEGER;
325
BEGIN
326
    IF n > 0 THEN
327
        ptr := malloc(line.length - n + 1);
328
        ASSERT(ptr # 0);
329
        SYSTEM.MOVE(line.ptr, ptr, pos*WCHAR_SIZE);
330
        SYSTEM.MOVE(line.ptr + (pos + n)*WCHAR_SIZE, ptr + pos*WCHAR_SIZE, (line.length - pos - n)*WCHAR_SIZE);
331
        SYSTEM.PUT16(ptr + (line.length - n)*WCHAR_SIZE, 0);
332
        IF ~line.temp THEN
333
            movInt(line.length, line.length - n)
334
        END;
335
        DEC(line.length, n);
336
        free(line, ptr)
337
    END
338
END delCharN;
339
 
340
 
341
PROCEDURE wrap* (line, nextLine: tLine; pos: INTEGER);
342
VAR
343
    ptr1, ptr2: INTEGER;
344
    n: INTEGER;
345
BEGIN
346
    ptr1 := malloc(pos + 1);
347
    ASSERT(ptr1 # 0);
348
    n := line.length - pos;
349
    ptr2 := malloc(n + 1);
350
    ASSERT(ptr2 # 0);
351
    SYSTEM.MOVE(line.ptr, ptr1, pos*WCHAR_SIZE);
352
    SYSTEM.PUT16(ptr1 + pos*WCHAR_SIZE, 0);
353
    SYSTEM.MOVE(line.ptr + pos*WCHAR_SIZE, ptr2, n*WCHAR_SIZE);
354
    SYSTEM.PUT16(ptr2 + n*WCHAR_SIZE, 0);
355
    IF ~line.temp THEN
356
        movInt(line.length, pos)
357
    END;
358
    IF ~nextLine.temp THEN
359
        movInt(nextLine.length, n)
360
    END;
361
    line.length := pos;
362
    nextLine.length := n;
363
    free(line, ptr1);
364
    free(nextLine, ptr2)
365
END wrap;
366
 
367
 
368
PROCEDURE copy* (line: tLine);
369
VAR
370
    ptr: INTEGER;
371
BEGIN
372
    ptr := malloc(line.length + 1);
373
    ASSERT(ptr # 0);
374
    SYSTEM.MOVE(line.ptr, ptr, line.length*WCHAR_SIZE);
375
    SYSTEM.PUT16(ptr + line.length*WCHAR_SIZE, 0);
376
    free(line, ptr)
377
END copy;
378
 
379
 
380
PROCEDURE chCase* (line: tLine; pos1, pos2: INTEGER; upper: BOOLEAN): BOOLEAN;
381
VAR
382
    i: INTEGER;
383
    modified: BOOLEAN;
384
    c: WCHAR;
385
    func: PROCEDURE (VAR c: WCHAR): BOOLEAN;
386
BEGIN
387
    modified := FALSE;
388
    IF upper THEN
389
        func := Utils.cap
390
    ELSE
391
        func := Utils.low
392
    END;
393
    i := pos2;
394
    WHILE i >= pos1 DO
395
        c := getChar(line, i);
396
        IF func(c) THEN
397
            modified := TRUE
398
        END;
399
        DEC(i)
400
    END;
401
 
402
    IF modified THEN
403
        copy(line);
404
        i := pos2;
405
        WHILE i >= pos1 DO
406
            c := getChar(line, i);
407
            IF func(c) THEN
408
                setChar(line, i, c)
409
            END;
410
            DEC(i)
411
        END;
412
        modify(line)
413
    END
414
    RETURN modified
415
END chCase;
416
 
417
 
418
PROCEDURE init* (movInt: PmovInt; movPtr: PmovPtr; movBool: PmovBool(*; typedPtr: PTypedPtr; untypedPtr: PUntypedPtr*));
419
BEGIN
420
    _movInt := movInt;
421
    _movPtr := movPtr;
422
    _movBool := movBool;
423
(*    _typedPtr := typedPtr;
424
    _untypedPtr := untypedPtr;*)
425
END init;
426
 
427
 
9050 leency 428
PROCEDURE setMaxLength* (VAR maxLength: INTEGER);
8728 leency 429
BEGIN
9050 leency 430
    pMaxLength := SYSTEM.ADR(maxLength)
431
END setMaxLength;
432
 
433
 
434
BEGIN
435
    pMaxLength := 0
8728 leency 436
END Lines.