2019-10-06 20:55:12 +03:00
|
|
|
(*
|
2019-03-11 11:59:55 +03:00
|
|
|
BSD 2-Clause License
|
|
|
|
|
2019-10-06 20:55:12 +03:00
|
|
|
Copyright (c) 2018-2019, Anton Krotov
|
2019-03-11 11:59:55 +03:00
|
|
|
All rights reserved.
|
|
|
|
*)
|
|
|
|
|
|
|
|
MODULE TEXTDRV;
|
|
|
|
|
|
|
|
IMPORT FILES, C := COLLECTIONS;
|
|
|
|
|
|
|
|
|
|
|
|
CONST
|
|
|
|
|
|
|
|
CR = 0DX; LF = 0AX;
|
|
|
|
|
|
|
|
CHUNK = 1024 * 256;
|
|
|
|
|
|
|
|
|
|
|
|
TYPE
|
|
|
|
|
|
|
|
TEXT* = POINTER TO RECORD (C.ITEM)
|
|
|
|
|
2019-10-06 20:55:12 +03:00
|
|
|
chunk: ARRAY CHUNK OF CHAR;
|
2019-03-11 11:59:55 +03:00
|
|
|
pos, size: INTEGER;
|
|
|
|
file: FILES.FILE;
|
|
|
|
utf8: BOOLEAN;
|
|
|
|
CR: BOOLEAN;
|
|
|
|
|
|
|
|
line*, col*: INTEGER;
|
|
|
|
eof*: BOOLEAN;
|
|
|
|
eol*: BOOLEAN;
|
2019-09-26 23:23:06 +03:00
|
|
|
peak*: CHAR
|
2019-03-11 11:59:55 +03:00
|
|
|
|
|
|
|
END;
|
|
|
|
|
|
|
|
|
|
|
|
VAR
|
|
|
|
|
|
|
|
texts: C.COLLECTION;
|
|
|
|
|
|
|
|
|
|
|
|
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 20:55:12 +03:00
|
|
|
text.chunk[0] := 0X
|
2019-09-26 23:23:06 +03:00
|
|
|
END;
|
2019-10-06 20:55:12 +03:00
|
|
|
text.peak := text.chunk[0]
|
2019-03-11 11:59:55 +03:00
|
|
|
END
|
|
|
|
END load;
|
|
|
|
|
|
|
|
|
2019-09-26 23:23:06 +03:00
|
|
|
PROCEDURE next* (text: TEXT);
|
2019-03-11 11:59:55 +03:00
|
|
|
VAR
|
|
|
|
c: CHAR;
|
2019-09-26 23:23:06 +03:00
|
|
|
|
2019-03-11 11:59:55 +03:00
|
|
|
BEGIN
|
|
|
|
IF text.pos < text.size - 1 THEN
|
2019-09-26 23:23:06 +03:00
|
|
|
INC(text.pos);
|
2019-10-06 20:55:12 +03:00
|
|
|
text.peak := text.chunk[text.pos]
|
2019-03-11 11:59:55 +03:00
|
|
|
ELSE
|
|
|
|
load(text)
|
|
|
|
END;
|
|
|
|
|
|
|
|
IF ~text.eof THEN
|
|
|
|
|
2019-09-26 23:23:06 +03:00
|
|
|
c := text.peak;
|
2019-03-11 11:59:55 +03:00
|
|
|
|
|
|
|
IF c = CR THEN
|
|
|
|
INC(text.line);
|
2019-09-26 23:23:06 +03:00
|
|
|
text.col := 0;
|
|
|
|
text.eol := TRUE;
|
|
|
|
text.CR := TRUE
|
2019-03-11 11:59:55 +03:00
|
|
|
ELSIF c = LF THEN
|
|
|
|
IF ~text.CR THEN
|
|
|
|
INC(text.line);
|
2019-09-26 23:23:06 +03:00
|
|
|
text.col := 0;
|
|
|
|
text.eol := TRUE
|
2019-03-11 11:59:55 +03:00
|
|
|
ELSE
|
2019-09-26 23:23:06 +03:00
|
|
|
text.eol := FALSE
|
2019-03-11 11:59:55 +03:00
|
|
|
END;
|
|
|
|
text.CR := FALSE
|
|
|
|
ELSE
|
|
|
|
text.eol := FALSE;
|
|
|
|
IF text.utf8 THEN
|
2019-10-06 20:55:12 +03:00
|
|
|
IF ORD(c) DIV 64 # 2 THEN
|
2019-03-11 11:59:55 +03:00
|
|
|
INC(text.col)
|
|
|
|
END
|
|
|
|
ELSE
|
|
|
|
INC(text.col)
|
|
|
|
END;
|
|
|
|
text.CR := FALSE
|
|
|
|
END
|
|
|
|
|
|
|
|
END
|
|
|
|
|
|
|
|
END next;
|
|
|
|
|
|
|
|
|
|
|
|
PROCEDURE init (text: TEXT);
|
|
|
|
BEGIN
|
|
|
|
IF (text.pos = 0) & (text.size >= 3) THEN
|
2019-10-06 20:55:12 +03:00
|
|
|
IF (text.chunk[0] = 0EFX) &
|
|
|
|
(text.chunk[1] = 0BBX) &
|
|
|
|
(text.chunk[2] = 0BFX) THEN
|
2019-03-11 11:59:55 +03:00
|
|
|
text.pos := 3;
|
|
|
|
text.utf8 := TRUE
|
|
|
|
END
|
|
|
|
END;
|
|
|
|
|
|
|
|
IF text.size = 0 THEN
|
2019-10-06 20:55:12 +03:00
|
|
|
text.chunk[0] := 0X;
|
2019-03-11 11:59:55 +03:00
|
|
|
text.size := 1;
|
|
|
|
text.eof := FALSE
|
|
|
|
END;
|
|
|
|
|
|
|
|
text.line := 1;
|
2019-09-26 23:23:06 +03:00
|
|
|
text.col := 1;
|
2019-03-11 11:59:55 +03:00
|
|
|
|
2019-10-06 20:55:12 +03:00
|
|
|
text.peak := text.chunk[text.pos]
|
2019-03-11 11:59:55 +03:00
|
|
|
END init;
|
|
|
|
|
|
|
|
|
2019-09-26 23:23:06 +03:00
|
|
|
PROCEDURE close* (VAR text: TEXT);
|
2019-03-11 11:59:55 +03:00
|
|
|
BEGIN
|
2019-09-26 23:23:06 +03:00
|
|
|
IF text # NIL THEN
|
|
|
|
IF text.file # NIL THEN
|
|
|
|
FILES.close(text.file)
|
|
|
|
END;
|
2019-03-11 11:59:55 +03:00
|
|
|
|
2019-09-26 23:23:06 +03:00
|
|
|
C.push(texts, text);
|
|
|
|
text := NIL
|
2019-03-11 11:59:55 +03:00
|
|
|
END
|
2019-09-26 23:23:06 +03:00
|
|
|
END close;
|
2019-03-11 11:59:55 +03:00
|
|
|
|
|
|
|
|
2019-09-26 23:23:06 +03:00
|
|
|
PROCEDURE open* (name: ARRAY OF CHAR): TEXT;
|
2019-03-11 11:59:55 +03:00
|
|
|
VAR
|
2019-09-26 23:23:06 +03:00
|
|
|
text: TEXT;
|
2019-03-11 11:59:55 +03:00
|
|
|
citem: C.ITEM;
|
|
|
|
|
|
|
|
BEGIN
|
|
|
|
citem := C.pop(texts);
|
|
|
|
IF citem = NIL THEN
|
|
|
|
NEW(text)
|
|
|
|
ELSE
|
|
|
|
text := citem(TEXT)
|
2019-09-26 23:23:06 +03:00
|
|
|
END;
|
2019-03-11 11:59:55 +03:00
|
|
|
|
|
|
|
IF text # NIL THEN
|
2019-10-06 20:55:12 +03:00
|
|
|
text.chunk[0] := 0X;
|
2019-09-26 23:23:06 +03: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;
|
|
|
|
text.peak := 0X;
|
|
|
|
text.file := FILES.open(name);
|
2019-03-11 11:59:55 +03:00
|
|
|
IF text.file # NIL THEN
|
2019-09-26 23:23:06 +03:00
|
|
|
load(text);
|
|
|
|
init(text)
|
|
|
|
ELSE
|
|
|
|
close(text)
|
|
|
|
END
|
2019-03-11 11:59:55 +03:00
|
|
|
END
|
2019-09-26 23:23:06 +03:00
|
|
|
|
|
|
|
RETURN text
|
|
|
|
END open;
|
2019-03-11 11:59:55 +03:00
|
|
|
|
|
|
|
|
|
|
|
BEGIN
|
|
|
|
texts := C.create()
|
2019-10-06 20:55:12 +03:00
|
|
|
END TEXTDRV.
|