Subversion Repositories Kolibri OS

Rev

Rev 9896 | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 9896 Rev 9898
Line 1... Line 1...
1
(*
1
(*
2
    Copyright 2016, 2018, 2020-2022 Anton Krotov
2
    Copyright 2016, 2018, 2020-2023 Anton Krotov
Line 3... Line 3...
3
 
3
 
Line 4... Line 4...
4
    This file is part of fb2read.
4
    This file is part of fb2read.
5
 
5
 
Line 18... Line 18...
18
*)
18
*)
Line 19... Line 19...
19
 
19
 
Line 20... Line 20...
20
MODULE SelEnc;
20
MODULE SelEnc;
21
 
-
 
22
IMPORT
21
 
Line 23... Line 22...
23
 
22
IMPORT
-
 
23
    SU := SysUtils, W := Window, S := Strings, SYSTEM, K := KOSAPI, File;
-
 
24
 
-
 
25
 
-
 
26
CONST
-
 
27
	AUTO     =  15;
-
 
28
	CP866    =  16;
Line 24... Line 29...
24
    SU := SysUtils, W := Window, OpenDlg, S := Strings, TXT := Txt2FB2, SYSTEM, K := KOSAPI, Settings, File;
29
	CP1251   =  17;
25
 
30
	CP1252   =  18;
26
 
31
	CP1250   =  19;
27
CONST
32
	UTF8     =  20;
Line 34... Line 39...
34
 
39
 
Line 35... Line 40...
35
    tempfile* = "/tmp0/1/~temp.fb2";
40
    tempfile* = "/tmp0/1/~temp.fb2";
36
 
-
 
37
 
41
 
38
VAR
42
 
39
 
-
 
Line 40... Line 43...
40
    Window    : W.TWindow;
43
VAR
41
    ENCODING* : INTEGER;
44
    Window: W.tWindow;
42
    FileName  : S.STRING;
45
    pos, mem, mem2, pos2: INTEGER;
43
 
-
 
44
 
46
 
45
PROCEDURE Buttons;
47
 
46
VAR
48
PROCEDURE Buttons;
47
    Y : INTEGER;
49
VAR
48
 
50
    Y : INTEGER;
49
BEGIN
51
BEGIN
50
    Y := BtnY;
52
    Y := BtnY;
51
    SU.CreateButton(TXT.AUTO,   BtnX, Y, BtnW, BtnH, SU.btnColor, "AUTO"   ); INC(Y, BtnH + BtnInter);
53
    SU.CreateButton(AUTO,   BtnX, Y, BtnW, BtnH, SU.btnColor, "AUTO"   ); INC(Y, BtnH + BtnInter);
52
    SU.CreateButton(TXT.CP866,  BtnX, Y, BtnW, BtnH, SU.btnColor, "CP-866" ); INC(Y, BtnH + BtnInter);
54
    SU.CreateButton(CP866,  BtnX, Y, BtnW, BtnH, SU.btnColor, "CP-866" ); INC(Y, BtnH + BtnInter);
Line 53... Line 55...
53
    SU.CreateButton(TXT.CP1251, BtnX, Y, BtnW, BtnH, SU.btnColor, "CP-1251"); INC(Y, BtnH + BtnInter);
55
    SU.CreateButton(CP1251, BtnX, Y, BtnW, BtnH, SU.btnColor, "CP-1251"); INC(Y, BtnH + BtnInter);
54
    SU.CreateButton(TXT.CP1252, BtnX, Y, BtnW, BtnH, SU.btnColor, "CP-1252"); INC(Y, BtnH + BtnInter);
56
    SU.CreateButton(CP1252, BtnX, Y, BtnW, BtnH, SU.btnColor, "CP-1252"); INC(Y, BtnH + BtnInter);
55
    SU.CreateButton(TXT.CP1250, BtnX, Y, BtnW, BtnH, SU.btnColor, "CP-1250"); INC(Y, BtnH + BtnInter);
57
    SU.CreateButton(CP1250, BtnX, Y, BtnW, BtnH, SU.btnColor, "CP-1250"); INC(Y, BtnH + BtnInter);
56
    SU.CreateButton(TXT.UTF8,   BtnX, Y, BtnW, BtnH, SU.btnColor, "UTF-8"  )
58
    SU.CreateButton(UTF8,   BtnX, Y, BtnW, BtnH, SU.btnColor, "UTF-8"  )
57
END Buttons;
59
END Buttons;
58
 
60
 
59
 
61
 
60
PROCEDURE DrawWindow;
62
PROCEDURE DrawWindow;
61
BEGIN
63
BEGIN
Line 62... Line 64...
62
	SU.GetSystemColors;
64
	SU.GetSystemColors;
63
    SU.WindowRedrawStatus(1);
65
    SU.WindowRedrawStatus(1);
-
 
66
    SU.DefineAndDrawWindow(Window.left, Window.top, Window.width, Window.height,
-
 
67
          SU.winColor, LSL(ORD({0, 1}), 4) + 4, Window.caption);
-
 
68
    Buttons;
-
 
69
    SU.WindowRedrawStatus(2)
-
 
70
END DrawWindow;
-
 
71
 
-
 
72
 
-
 
73
PROCEDURE getch (): CHAR;
-
 
74
VAR
-
 
75
	ch: CHAR;
-
 
76
BEGIN
-
 
77
	SYSTEM.GET(mem + pos, ch);
-
 
78
	INC(pos)
-
 
79
	RETURN ch
-
 
80
END getch;
-
 
81
 
-
 
82
 
-
 
83
PROCEDURE WriteStr (s: ARRAY OF CHAR);
-
 
84
BEGIN
-
 
85
	SYSTEM.MOVE(SYSTEM.ADR(s[0]), mem2 + pos2, LENGTH(s));
-
 
86
	pos2 := pos2 + LENGTH(s)
-
 
87
END WriteStr;
-
 
88
 
-
 
89
 
-
 
90
PROCEDURE WriteChar (ch: CHAR);
-
 
91
BEGIN
-
 
92
	SYSTEM.PUT(mem2 + pos2, ch);
-
 
93
	INC(pos2)
-
 
94
END WriteChar;
-
 
95
 
-
 
96
 
-
 
97
PROCEDURE convert (ibuf, size: INTEGER; out: S.STRING; encoding: INTEGER);
-
 
98
CONST
-
 
99
	buf_size = 1024*16;
-
 
100
VAR
-
 
101
	F: File.FS;
-
 
102
	n: INTEGER;
-
 
103
	CR: BOOLEAN;
-
 
104
	ch: CHAR;
-
 
105
	buffer: ARRAY buf_size OF BYTE;
-
 
106
BEGIN
-
 
107
	mem := ibuf;
-
 
108
	pos := 0;
-
 
109
	F := File.Create(out);
-
 
110
	mem2 := SYSTEM.ADR(buffer[0]);
-
 
111
	pos2 := 0;
-
 
112
	WriteStr('
-
 
113
	CASE encoding OF
-
 
114
	|CP866  : WriteStr("cp866")
-
 
115
	|CP1251 : WriteStr("windows-1251")
-
 
116
	|CP1252 : WriteStr("windows-1252")
-
 
117
	|CP1250 : WriteStr("windows-1250")
-
 
118
	|UTF8   : WriteStr("utf-8")
-
 
119
	ELSE
-
 
120
		SU.Halt
-
 
121
	END;
-
 
122
	WriteStr('"?>' + 0DX + 0AX + "");
-
 
123
 
-
 
124
	WHILE pos < size DO
-
 
125
		IF pos2 > buf_size - 32 THEN
-
 
126
			n := File.Write(F, mem2, pos2);
-
 
127
			pos2 := 0
-
 
128
		END;
-
 
129
		ch := getch();
-
 
130
		CASE ch OF
-
 
131
		|"<": WriteStr("<")
-
 
132
		|">": WriteStr(">")
-
 
133
		|"&": WriteStr("&")
-
 
134
		|"'": WriteStr("'")
-
 
135
		|'"': WriteStr(""")
-
 
136
		|0DX: WriteStr("")
-
 
137
		|0AX: IF ~CR THEN WriteStr("") END
-
 
138
		| 0X: WriteChar(20X)
-
 
139
		ELSE
-
 
140
			WriteChar(ch)
-
 
141
		END;
64
    SU.DefineAndDrawWindow(Window.Left, Window.Top, Window.Width, Window.Height,
142
		CR := ch = 0DX
-
 
143
	END;
-
 
144
 
Line 65... Line 145...
65
          SU.winColor, LSL(ORD({0, 1}), 4) + 4, Window.Caption);
145
	WriteStr("");
66
    Buttons;
146
	n := File.Write(F, mem2, pos2);
67
    SU.WindowRedrawStatus(2)
147
	File.Close(F)
Line 94... Line 174...
94
        RETURN res
174
        RETURN res
95
    END SearchPair;
175
    END SearchPair;
Line 96... Line 176...
96
 
176
 
97
 
-
 
98
BEGIN
-
 
99
    data := File.Load(fname, size);
-
 
100
    SU.ErrorIf(data = 0, 1);
-
 
101
    ptr := data;
177
 
102
 
178
BEGIN
103
    IF SearchPair(ptr, size, 208, 190) THEN
179
    IF SearchPair(ptr, size, 208, 190) THEN
104
        enc := TXT.UTF8
180
        enc := UTF8
105
    ELSE
181
    ELSE
106
        IF SearchPair(ptr, size, 239, 240) OR SearchPair(ptr, size, 241, 242) THEN
182
        IF SearchPair(ptr, size, 239, 240) OR SearchPair(ptr, size, 241, 242) THEN
107
            enc := TXT.CP1251
183
            enc := CP1251
-
 
184
        ELSE
108
        ELSE
185
            enc := CP866
109
            enc := TXT.CP866
-
 
110
        END
-
 
111
    END;
-
 
112
 
-
 
113
    data := K.free(data)
186
        END
114
 
187
    END
Line 115... Line 188...
115
    RETURN enc
188
    RETURN enc
116
END auto;
189
END auto;
117
 
190
 
118
 
191
 
119
PROCEDURE ButtonClick;
-
 
-
 
192
PROCEDURE ButtonClick (fname: S.STRING);
120
VAR
193
VAR
121
    btn_code: INTEGER;
194
    encoding: INTEGER;
122
    program, file: S.STRING;
195
    program, file: S.STRING;
123
 
196
    data, size: INTEGER;
124
BEGIN
197
BEGIN
125
    btn_code := SU.GetButtonCode();
198
    data := File.Load(fname, size);
126
    IF btn_code = TXT.AUTO THEN
199
    SU.ErrorIf(data = 0, 1);
127
        ENCODING := auto(FileName)
200
    encoding := SU.GetButtonCode();
128
    ELSE
201
    IF encoding = AUTO THEN
129
        ENCODING := btn_code
202
        encoding := auto(data, size)
130
    END;
203
    END;
131
    TXT.convert(FileName, tempfile, ENCODING);
204
    convert(data, size, tempfile, encoding);
132
    S.PtrToString(K.GetName(), program);
205
    S.PtrToString(K.GetName(), program);
133
    file := tempfile;
206
    file := tempfile;
Line 134... Line 207...
134
    file[0] := "!";
207
    file[0] := "!";
135
    SU.Run(program, SYSTEM.ADR(file));
208
    SU.Run(program, SYSTEM.ADR(file));
136
    SU.Halt
209
    SU.Halt
137
END ButtonClick;
-
 
138
 
210
END ButtonClick;
139
 
-
 
140
PROCEDURE Show*(FName: S.STRING);
211
 
141
VAR
212
 
142
    X1, Y1, X2, Y2: INTEGER;
213
PROCEDURE Show* (fname: S.STRING);
143
 
214
VAR
144
BEGIN
215
    X1, Y1, X2, Y2: INTEGER;
145
    FileName := FName;
216
BEGIN
146
    SU.SetEventsMask({0, 2, 31});
217
    SU.SetEventsMask({0, 2, 31});
147
    SU.GetScreenArea(X1, Y1, X2, Y2);
218
    SU.GetScreenArea(X1, Y1, X2, Y2);
148
    W.InitWindow(Window, 0, 0, BtnX * 2 + BtnW + 10, (BtnH + BtnInter) * 6 + BtnY * 2 + SU.SkinHeight() - 5, "Encoding");
219
    W.init(Window, 0, 0, BtnX * 2 + BtnW + 10, (BtnH + BtnInter) * 6 + BtnY * 2 + SU.SkinHeight() - 5, "Encoding");
149
    Window.Left := (X2 - X1 - Window.Width) DIV 2;
220
    Window.left := (X2 - X1 - Window.width) DIV 2;
150
    Window.Top  := (Y2 - Y1 - Window.Height) DIV 2;
221
    Window.top  := (Y2 - Y1 - Window.height) DIV 2;
151
    DrawWindow;
222
    DrawWindow;
152
    WHILE TRUE DO
223
    WHILE TRUE DO
Line 153... Line -...
153
        CASE SU.WaitForEvent() OF
-
 
154
        |1 : DrawWindow
-
 
155
        |3 : ButtonClick
224
        CASE SU.WaitForEvent() OF
156
        END
225
        |1: DrawWindow