Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. (*
  2.     Copyright 2016, 2018, 2020-2022 Anton Krotov
  3.  
  4.     This file is part of fb2read.
  5.  
  6.     fb2read 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.     fb2read 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 fb2read. If not, see <http://www.gnu.org/licenses/>.
  18. *)
  19.  
  20. MODULE SelEnc;
  21.  
  22. IMPORT
  23.  
  24.     SU := SysUtils, W := Window, OpenDlg, S := Strings, TXT := Txt2FB2, SYSTEM, K := KOSAPI, Settings, File;
  25.  
  26.  
  27. CONST
  28.  
  29.     BtnH      =  30;
  30.     BtnW      = 150;
  31.     BtnX      =   5;
  32.     BtnY      =  10;
  33.     BtnInter  =  10;
  34.  
  35.     tempfile* = "/tmp0/1/~temp.fb2";
  36.  
  37.  
  38. VAR
  39.  
  40.     Window    : W.TWindow;
  41.     ENCODING* : INTEGER;
  42.     FileName  : S.STRING;
  43.  
  44.  
  45. PROCEDURE Buttons;
  46. VAR
  47.     Y : INTEGER;
  48.  
  49. BEGIN
  50.     Y := BtnY;
  51.     SU.CreateButton(TXT.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);
  53.     SU.CreateButton(TXT.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);
  55.     SU.CreateButton(TXT.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"  )
  57. END Buttons;
  58.  
  59.  
  60. PROCEDURE DrawWindow;
  61. BEGIN
  62.         SU.GetSystemColors;
  63.     SU.WindowRedrawStatus(1);
  64.     SU.DefineAndDrawWindow(Window.Left, Window.Top, Window.Width, Window.Height,
  65.           SU.winColor, LSL(ORD({0, 1}), 4) + 4, Window.Caption);
  66.     Buttons;
  67.     SU.WindowRedrawStatus(2)
  68. END DrawWindow;
  69.  
  70.  
  71. PROCEDURE auto (fname: S.STRING): INTEGER;
  72. VAR
  73.     enc, data, size, ptr: INTEGER;
  74.  
  75.  
  76.     PROCEDURE SearchPair (ptr, size: INTEGER; chr1, chr2: BYTE): BOOLEAN;
  77.     VAR
  78.         c, c0: BYTE;
  79.         res: BOOLEAN;
  80.  
  81.     BEGIN
  82.         c := 0;
  83.         res := FALSE;
  84.         WHILE (size > 0) & ~res DO
  85.             c0 := c;
  86.             SYSTEM.GET(ptr, c);
  87.             IF (c = chr2) & (c0 = chr1) THEN
  88.                 res := TRUE
  89.             END;
  90.             INC(ptr);
  91.             DEC(size)
  92.         END
  93.  
  94.         RETURN res
  95.     END SearchPair;
  96.  
  97.  
  98. BEGIN
  99.     data := File.Load(fname, size);
  100.     SU.ErrorIf(data = 0, 1);
  101.     ptr := data;
  102.  
  103.     IF SearchPair(ptr, size, 208, 190) THEN
  104.         enc := TXT.UTF8
  105.     ELSE
  106.         IF SearchPair(ptr, size, 239, 240) OR SearchPair(ptr, size, 241, 242) THEN
  107.             enc := TXT.CP1251
  108.         ELSE
  109.             enc := TXT.CP866
  110.         END
  111.     END;
  112.  
  113.     data := K.free(data)
  114.  
  115.     RETURN enc
  116. END auto;
  117.  
  118.  
  119. PROCEDURE ButtonClick;
  120. VAR
  121.     btn_code: INTEGER;
  122.     program, file: S.STRING;
  123.  
  124. BEGIN
  125.     btn_code := SU.GetButtonCode();
  126.     IF btn_code = TXT.AUTO THEN
  127.         ENCODING := auto(FileName)
  128.     ELSE
  129.         ENCODING := btn_code
  130.     END;
  131.     TXT.convert(FileName, tempfile, ENCODING);
  132.     S.PtrToString(K.GetName(), program);
  133.     file := tempfile;
  134.     file[0] := "!";
  135.     SU.Run(program, SYSTEM.ADR(file));
  136.     SU.Halt
  137. END ButtonClick;
  138.  
  139.  
  140. PROCEDURE Show*(FName: S.STRING);
  141. VAR
  142.     X1, Y1, X2, Y2: INTEGER;
  143.  
  144. BEGIN
  145.     FileName := FName;
  146.     SU.SetEventsMask({0, 2, 31});
  147.     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");
  149.     Window.Left := (X2 - X1 - Window.Width) DIV 2;
  150.     Window.Top  := (Y2 - Y1 - Window.Height) DIV 2;
  151.     DrawWindow;
  152.     WHILE TRUE DO
  153.         CASE SU.WaitForEvent() OF
  154.         |1 : DrawWindow
  155.         |3 : ButtonClick
  156.         END
  157.     END
  158. END Show;
  159.  
  160.  
  161. BEGIN
  162.     ENCODING := 0
  163. END SelEnc.