kolibrios/programs/other/fb2reader/SRC/Txt2fb2.ob07
Anton Krotov 4c20c082c1 FB2 Reader: upload source, small changes
git-svn-id: svn://kolibrios.org@9896 a494cfbc-eb01-0410-851d-a64ba20cac60
2023-01-22 14:20:23 +00:00

130 lines
2.9 KiB
Plaintext

(*
Copyright 2016, 2020 Anton Krotov
This file is part of fb2read.
fb2read is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
fb2read is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with fb2read. If not, see <http://www.gnu.org/licenses/>.
*)
MODULE Txt2FB2;
IMPORT File, sys := SYSTEM, K := KOSAPI, S := Strings, SU := SysUtils;
CONST
AUTO* = 15;
CP866* = 16;
CP1251* = 17;
CP1252* = 18;
CP1250* = 19;
UTF8* = 20;
VAR F: File.FS; ch: CHAR; pos, mem, mem2, pos2: INTEGER;
PROCEDURE getch;
BEGIN
sys.GET(mem + pos, ch);
INC(pos)
END getch;
PROCEDURE WriteStr(s: ARRAY OF CHAR);
BEGIN
sys.MOVE(sys.ADR(s[0]), mem2 + pos2, LENGTH(s));
pos2 := pos2 + LENGTH(s)
END WriteStr;
PROCEDURE WriteChar(ch: CHAR);
BEGIN
sys.PUT(mem2 + pos2, ch);
INC(pos2)
END WriteChar;
PROCEDURE convert*(in, out: S.STRING; encoding: INTEGER);
CONST buf_size = 1024*16;
VAR n, size: INTEGER; CR: BOOLEAN;
BEGIN
F := File.Open(in);
size := File.Seek(F, 0, 2);
n := File.Seek(F, 0, 0);
mem := K.malloc(size + 1024);
SU.MemError(mem = 0);
n := File.Read(F, mem, size);
File.Close(F);
pos := 0;
F := File.Create(out);
mem2 := K.malloc(buf_size);
SU.MemError(mem2 = 0);
pos2 := 0;
WriteStr("<?xml encoding = ");
WriteStr(22X);
CASE encoding OF
|CP866 : WriteStr("cp866")
|CP1251 : WriteStr("windows-1251")
|CP1252 : WriteStr("windows-1252")
|CP1250 : WriteStr("windows-1250")
|UTF8 : WriteStr("utf-8")
ELSE
SU.Halt
END;
WriteStr(22X);
WriteStr("?>");
WriteChar(0DX);
WriteChar(0AX);
WriteStr("<FictionBook><body>");
WHILE pos < size DO
IF pos2 > buf_size - 32 THEN
n := File.Write(F, mem2, pos2);
pos2 := 0
END;
getch;
IF ch = "<" THEN
WriteStr("&lt;")
ELSIF ch = ">" THEN
WriteStr("&gt;")
ELSIF ch = "&" THEN
WriteStr("&amp;")
ELSIF ch = "'" THEN
WriteStr("&apos;")
ELSIF ch = 22X THEN
WriteStr("&quot;")
ELSIF ch = 0DX THEN
WriteStr("<empty-line/>")
ELSIF ch = 0AX THEN
IF ~CR THEN
WriteStr("<empty-line/>")
END
ELSIF ch = 0X THEN
WriteChar(20X)
ELSE
WriteChar(ch)
END;
CR := ch = 0DX
END;
WriteStr("</body></FictionBook>");
n := File.Write(F, mem2, pos2);
File.Close(F);
mem := K.free(mem);
mem2 := K.free(mem2)
END convert;
END Txt2FB2.