2020-05-25 22:48:33 +02:00
|
|
|
(*
|
2019-03-11 09:59:55 +01:00
|
|
|
BSD 2-Clause License
|
|
|
|
|
2021-09-03 19:21:37 +02:00
|
|
|
Copyright (c) 2018-2021, Anton Krotov
|
2019-03-11 09:59:55 +01:00
|
|
|
All rights reserved.
|
|
|
|
*)
|
|
|
|
|
|
|
|
MODULE TEXTDRV;
|
|
|
|
|
|
|
|
IMPORT FILES, C := COLLECTIONS;
|
|
|
|
|
|
|
|
|
|
|
|
CONST
|
|
|
|
|
2021-09-03 19:21:37 +02:00
|
|
|
CR = 0DX; LF = 0AX; HT = 9X;
|
2019-03-11 09:59:55 +01:00
|
|
|
|
|
|
|
CHUNK = 1024 * 256;
|
|
|
|
|
2021-09-03 19:21:37 +02:00
|
|
|
defTabSize* = 4;
|
|
|
|
|
2019-03-11 09:59:55 +01:00
|
|
|
|
|
|
|
TYPE
|
|
|
|
|
|
|
|
TEXT* = POINTER TO RECORD (C.ITEM)
|
|
|
|
|
2019-10-06 19:55:12 +02:00
|
|
|
chunk: ARRAY CHUNK OF CHAR;
|
2019-03-11 09:59:55 +01:00
|
|
|
pos, size: INTEGER;
|
|
|
|
file: FILES.FILE;
|
|
|
|
utf8: BOOLEAN;
|
|
|
|
CR: BOOLEAN;
|
|
|
|
|
|
|
|
line*, col*: INTEGER;
|
2020-10-13 09:58:51 +02:00
|
|
|
ifc*: INTEGER;
|
|
|
|
elsec*: INTEGER;
|
2019-03-11 09:59:55 +01:00
|
|
|
eof*: BOOLEAN;
|
|
|
|
eol*: BOOLEAN;
|
2020-10-13 09:58:51 +02:00
|
|
|
skip*: BOOLEAN;
|
|
|
|
peak*: CHAR;
|
|
|
|
_skip*,
|
|
|
|
_elsif*,
|
|
|
|
_else*: ARRAY 100 OF BOOLEAN;
|
|
|
|
fname*: ARRAY 2048 OF CHAR
|
2019-03-11 09:59:55 +01:00
|
|
|
|
|
|
|
END;
|
|
|
|
|
|
|
|
|
|
|
|
VAR
|
|
|
|
|
|
|
|
texts: C.COLLECTION;
|
2021-09-03 19:21:37 +02:00
|
|
|
TabSize: INTEGER;
|
2019-03-11 09:59:55 +01:00
|
|
|
|
|
|
|
|
|
|
|
PROCEDURE load (text: TEXT);
|
|
|
|
BEGIN
|
|
|
|
IF ~text.eof THEN
|
|
|
|
text.size := FILES.read(text.file, text.chunk, LEN(text.chunk));
|
|
|
|
text.pos := 0;
|
|
|
|
IF text.size = 0 THEN
|
|
|
|
text.eof := TRUE;
|
2019-10-06 19:55:12 +02:00
|
|
|
text.chunk[0] := 0X
|
2019-09-26 22:23:06 +02:00
|
|
|
END;
|
2019-10-06 19:55:12 +02:00
|
|
|
text.peak := text.chunk[0]
|
2019-03-11 09:59:55 +01:00
|
|
|
END
|
|
|
|
END load;
|
|
|
|
|
|
|
|
|
2019-09-26 22:23:06 +02:00
|
|
|
PROCEDURE next* (text: TEXT);
|
2019-03-11 09:59:55 +01:00
|
|
|
VAR
|
|
|
|
c: CHAR;
|
2019-09-26 22:23:06 +02:00
|
|
|
|
2019-03-11 09:59:55 +01:00
|
|
|
BEGIN
|
|
|
|
IF text.pos < text.size - 1 THEN
|
2019-09-26 22:23:06 +02:00
|
|
|
INC(text.pos);
|
2019-10-06 19:55:12 +02:00
|
|
|
text.peak := text.chunk[text.pos]
|
2019-03-11 09:59:55 +01:00
|
|
|
ELSE
|
|
|
|
load(text)
|
|
|
|
END;
|
|
|
|
|
|
|
|
IF ~text.eof THEN
|
|
|
|
|
2019-09-26 22:23:06 +02:00
|
|
|
c := text.peak;
|
2019-03-11 09:59:55 +01:00
|
|
|
|
|
|
|
IF c = CR THEN
|
|
|
|
INC(text.line);
|
2019-09-26 22:23:06 +02:00
|
|
|
text.col := 0;
|
|
|
|
text.eol := TRUE;
|
|
|
|
text.CR := TRUE
|
2019-03-11 09:59:55 +01:00
|
|
|
ELSIF c = LF THEN
|
|
|
|
IF ~text.CR THEN
|
|
|
|
INC(text.line);
|
2019-09-26 22:23:06 +02:00
|
|
|
text.col := 0;
|
|
|
|
text.eol := TRUE
|
2019-03-11 09:59:55 +01:00
|
|
|
ELSE
|
2019-09-26 22:23:06 +02:00
|
|
|
text.eol := FALSE
|
2019-03-11 09:59:55 +01:00
|
|
|
END;
|
|
|
|
text.CR := FALSE
|
2021-09-03 19:21:37 +02:00
|
|
|
ELSIF c = HT THEN
|
|
|
|
text.col := text.col + TabSize - text.col MOD TabSize;
|
|
|
|
text.eol := FALSE;
|
|
|
|
text.CR := FALSE
|
2019-03-11 09:59:55 +01:00
|
|
|
ELSE
|
|
|
|
IF text.utf8 THEN
|
2019-10-06 19:55:12 +02:00
|
|
|
IF ORD(c) DIV 64 # 2 THEN
|
2019-03-11 09:59:55 +01:00
|
|
|
INC(text.col)
|
|
|
|
END
|
|
|
|
ELSE
|
|
|
|
INC(text.col)
|
|
|
|
END;
|
2021-09-03 19:21:37 +02:00
|
|
|
text.eol := FALSE;
|
|
|
|
text.CR := FALSE
|
2019-03-11 09:59:55 +01:00
|
|
|
END
|
|
|
|
|
|
|
|
END
|
|
|
|
|
|
|
|
END next;
|
|
|
|
|
|
|
|
|
|
|
|
PROCEDURE init (text: TEXT);
|
|
|
|
BEGIN
|
|
|
|
IF (text.pos = 0) & (text.size >= 3) THEN
|
2019-10-06 19:55:12 +02:00
|
|
|
IF (text.chunk[0] = 0EFX) &
|
|
|
|
(text.chunk[1] = 0BBX) &
|
|
|
|
(text.chunk[2] = 0BFX) THEN
|
2019-03-11 09:59:55 +01:00
|
|
|
text.pos := 3;
|
|
|
|
text.utf8 := TRUE
|
|
|
|
END
|
|
|
|
END;
|
|
|
|
|
|
|
|
IF text.size = 0 THEN
|
2019-10-06 19:55:12 +02:00
|
|
|
text.chunk[0] := 0X;
|
2019-03-11 09:59:55 +01:00
|
|
|
text.size := 1;
|
|
|
|
text.eof := FALSE
|
|
|
|
END;
|
|
|
|
|
|
|
|
text.line := 1;
|
2019-09-26 22:23:06 +02:00
|
|
|
text.col := 1;
|
2019-03-11 09:59:55 +01:00
|
|
|
|
2019-10-06 19:55:12 +02:00
|
|
|
text.peak := text.chunk[text.pos]
|
2019-03-11 09:59:55 +01:00
|
|
|
END init;
|
|
|
|
|
|
|
|
|
2019-09-26 22:23:06 +02:00
|
|
|
PROCEDURE close* (VAR text: TEXT);
|
2019-03-11 09:59:55 +01:00
|
|
|
BEGIN
|
2019-09-26 22:23:06 +02:00
|
|
|
IF text # NIL THEN
|
|
|
|
IF text.file # NIL THEN
|
|
|
|
FILES.close(text.file)
|
|
|
|
END;
|
2019-03-11 09:59:55 +01:00
|
|
|
|
2019-09-26 22:23:06 +02:00
|
|
|
C.push(texts, text);
|
|
|
|
text := NIL
|
2019-03-11 09:59:55 +01:00
|
|
|
END
|
2019-09-26 22:23:06 +02:00
|
|
|
END close;
|
2019-03-11 09:59:55 +01:00
|
|
|
|
|
|
|
|
2019-09-26 22:23:06 +02:00
|
|
|
PROCEDURE open* (name: ARRAY OF CHAR): TEXT;
|
2019-03-11 09:59:55 +01:00
|
|
|
VAR
|
2019-09-26 22:23:06 +02:00
|
|
|
text: TEXT;
|
2019-03-11 09:59:55 +01:00
|
|
|
citem: C.ITEM;
|
|
|
|
|
|
|
|
BEGIN
|
|
|
|
citem := C.pop(texts);
|
|
|
|
IF citem = NIL THEN
|
|
|
|
NEW(text)
|
|
|
|
ELSE
|
|
|
|
text := citem(TEXT)
|
2019-09-26 22:23:06 +02:00
|
|
|
END;
|
2019-03-11 09:59:55 +01:00
|
|
|
|
|
|
|
IF text # NIL THEN
|
2019-10-06 19:55:12 +02:00
|
|
|
text.chunk[0] := 0X;
|
2019-09-26 22:23:06 +02:00
|
|
|
text.pos := 0;
|
|
|
|
text.size := 0;
|
|
|
|
text.utf8 := FALSE;
|
|
|
|
text.CR := FALSE;
|
|
|
|
text.line := 1;
|
|
|
|
text.col := 1;
|
|
|
|
text.eof := FALSE;
|
|
|
|
text.eol := FALSE;
|
2020-10-13 09:58:51 +02:00
|
|
|
text.skip := FALSE;
|
|
|
|
text.ifc := 0;
|
|
|
|
text.elsec := 0;
|
|
|
|
text._skip[0] := FALSE;
|
2019-09-26 22:23:06 +02:00
|
|
|
text.peak := 0X;
|
|
|
|
text.file := FILES.open(name);
|
2020-10-13 09:58:51 +02:00
|
|
|
COPY(name, text.fname);
|
2019-03-11 09:59:55 +01:00
|
|
|
IF text.file # NIL THEN
|
2019-09-26 22:23:06 +02:00
|
|
|
load(text);
|
|
|
|
init(text)
|
|
|
|
ELSE
|
|
|
|
close(text)
|
|
|
|
END
|
2019-03-11 09:59:55 +01:00
|
|
|
END
|
2019-09-26 22:23:06 +02:00
|
|
|
|
|
|
|
RETURN text
|
|
|
|
END open;
|
2019-03-11 09:59:55 +01:00
|
|
|
|
|
|
|
|
2021-09-03 19:21:37 +02:00
|
|
|
PROCEDURE setTabSize* (n: INTEGER);
|
|
|
|
BEGIN
|
|
|
|
IF (0 < n) & (n <= 64) THEN
|
|
|
|
TabSize := n
|
|
|
|
ELSE
|
|
|
|
TabSize := defTabSize
|
|
|
|
END
|
|
|
|
END setTabSize;
|
|
|
|
|
|
|
|
|
2019-03-11 09:59:55 +01:00
|
|
|
BEGIN
|
2021-09-03 19:21:37 +02:00
|
|
|
TabSize := defTabSize;
|
2019-03-11 09:59:55 +01:00
|
|
|
texts := C.create()
|
2020-05-25 22:48:33 +02:00
|
|
|
END TEXTDRV.
|