Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | Download | RSS feed

  1. (*
  2.     Copyright 2016, 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 Ini;
  21.  
  22. IMPORT KOSAPI, sys := SYSTEM, S := Strings, File;
  23.  
  24.  
  25. CONST
  26.  
  27.   IniFileName = "/sys/settings/fb2read.ini";
  28.  
  29.  
  30. VAR
  31.  
  32.         History*, Browser*, Default*, Font*, Files*, Picture* : S.STRING;
  33.         b_pict*: BOOLEAN;
  34.         buffer: ARRAY 5000 OF CHAR;
  35.  
  36.  
  37. PROCEDURE [stdcall, "libini.obj", "ini_enum_keys"] enum_keys (f_name, sec_name: S.STRING; callback: INTEGER); END;
  38. PROCEDURE [stdcall, "libini.obj", "ini_get_color"] get_color (f_name, sec_name, key_name: S.STRING; def_val: INTEGER): INTEGER; END;
  39.  
  40. PROCEDURE Save* (Colors: ARRAY OF INTEGER; b_pict: BOOLEAN);
  41. VAR F: File.FS; pos: INTEGER;
  42.  
  43.   PROCEDURE WriteStr(str: S.STRING; VAR pos: INTEGER);
  44.   BEGIN
  45.     sys.MOVE(sys.ADR(str[0]), pos, LENGTH(str));
  46.     pos := pos + LENGTH(str)
  47.   END WriteStr;
  48.  
  49.   PROCEDURE WriteLn (VAR pos: INTEGER);
  50.   BEGIN
  51.     WriteStr(0DX, pos);
  52.     WriteStr(0AX, pos)
  53.   END WriteLn;
  54.  
  55.   PROCEDURE GetRGB(color: INTEGER; VAR r, g, b: INTEGER);
  56.   BEGIN
  57.     b := ORD(BITS(color) * {0..7});
  58.     g := ORD(BITS(LSR(color, 8)) * {0..7});
  59.     r := ORD(BITS(LSR(color, 16)) * {0..7})
  60.   END GetRGB;
  61.  
  62.   PROCEDURE WriteColor(color: INTEGER; VAR pos: INTEGER);
  63.   VAR r, g, b: INTEGER; s: S.STRING;
  64.   BEGIN
  65.     GetRGB(color, r, g, b);
  66.     S.IntToString(r, s); WriteStr(s, pos); WriteStr(",", pos);
  67.     S.IntToString(g, s); WriteStr(s, pos); WriteStr(",", pos);
  68.     S.IntToString(b, s); WriteStr(s, pos);
  69.   END WriteColor;
  70.  
  71. BEGIN
  72.   pos := sys.ADR(buffer[0]);
  73.   F := File.Create(IniFileName);
  74.   WriteStr("[Paths]", pos);                                  WriteLn(pos);
  75.   WriteStr("history=", pos);    WriteStr(History, pos);      WriteLn(pos);
  76.   WriteStr("browser=", pos);    WriteStr(Browser, pos);      WriteLn(pos);
  77.   WriteStr("default=", pos);    WriteStr(Default, pos);      WriteLn(pos);
  78.   WriteStr("font=", pos);       WriteStr(Font, pos);         WriteLn(pos);
  79.   WriteStr("picture=", pos);    WriteStr(Picture, pos);      WriteLn(pos);
  80.   WriteStr("[Files]", pos);                                  WriteLn(pos);
  81.   WriteStr("files=", pos);      WriteStr(Files, pos);        WriteLn(pos);
  82.   WriteStr("[Flags]", pos);                                  WriteLn(pos);
  83.   WriteStr("picture=", pos);
  84.   IF b_pict THEN
  85.     WriteStr("on", pos)
  86.   ELSE
  87.     WriteStr("off", pos)
  88.   END;
  89.   WriteLn(pos);
  90.   WriteStr("[Colors]", pos);                              WriteLn(pos);
  91.   WriteStr("back=", pos);    WriteColor(Colors[0], pos);  WriteLn(pos);
  92.   WriteStr("text=", pos);    WriteColor(Colors[1], pos);  WriteLn(pos);
  93.   WriteStr("italic=", pos);  WriteColor(Colors[2], pos);  WriteLn(pos);
  94.   WriteStr("link=", pos);    WriteColor(Colors[3], pos);  WriteLn(pos);
  95.   WriteStr("visited=", pos); WriteColor(Colors[4], pos);  WriteLn(pos);
  96.   pos := File.Write(F, sys.ADR(buffer[0]), pos - sys.ADR(buffer[0]));
  97.   File.Close(F)
  98. END Save;
  99.  
  100.  
  101. PROCEDURE [stdcall] callback(f_name, sec_name, key_name, key_value: S.STRING): INTEGER;
  102. BEGIN
  103.   IF    sec_name = "Paths" THEN
  104.     IF    key_name = "history" THEN
  105.       History := key_value
  106.     ELSIF key_name = "browser" THEN
  107.       Browser := key_value
  108.     ELSIF key_name = "default" THEN
  109.       Default := key_value
  110.     ELSIF key_name = "font" THEN
  111.       Font    := key_value
  112.     ELSIF key_name = "picture" THEN
  113.       Picture := key_value
  114.     END
  115.   ELSIF    sec_name = "Files" THEN
  116.     IF key_name = "files" THEN
  117.       Files := key_value
  118.     END
  119.   ELSIF    sec_name = "Flags" THEN
  120.     IF key_name = "picture" THEN
  121.       b_pict := key_value = "on"
  122.     END
  123.   END
  124.   RETURN 1
  125. END callback;
  126.  
  127.  
  128. PROCEDURE GetColor*(key: S.STRING; def: INTEGER): INTEGER;
  129.   RETURN get_color(IniFileName, "Colors", key, def)
  130. END GetColor;
  131.  
  132.  
  133. PROCEDURE SetDefaultPath*(Path: S.STRING);
  134. BEGIN
  135.   Default := Path;
  136. END SetDefaultPath;
  137.  
  138.  
  139. PROCEDURE SetPicturePath*(Path: S.STRING);
  140. BEGIN
  141.   Picture := Path;
  142. END SetPicturePath;
  143.  
  144.  
  145. BEGIN
  146.         enum_keys(IniFileName, "Paths",  sys.ADR(callback));
  147.         enum_keys(IniFileName, "Files",  sys.ADR(callback));
  148.         enum_keys(IniFileName, "Flags",  sys.ADR(callback));
  149. END Ini.
  150.