Subversion Repositories Kolibri OS

Rev

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

  1. (*
  2.     Copyright 2021 Anton Krotov
  3.  
  4.     This file is part of CEdit.
  5.  
  6.     CEdit 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.     CEdit 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 CEdit. If not, see <http://www.gnu.org/licenses/>.
  18. *)
  19.  
  20. MODULE Ini;
  21.  
  22. IMPORT
  23.  
  24.     KOSAPI, SYSTEM, RW, Text, Utils, File, List, Languages, KolibriOS, Lines;
  25.  
  26.  
  27. CONST
  28.  
  29.     fileName = "cedit.ini";
  30.  
  31.     MAX_LEN = 32;
  32.     MAX_SECTIONS* = 10;
  33.  
  34.  
  35. TYPE
  36.  
  37.     tString = ARRAY 128 OF CHAR;
  38.  
  39.     tSectionName = ARRAY MAX_LEN OF WCHAR;
  40.     tASCIISectionName = ARRAY MAX_LEN OF CHAR;
  41.  
  42.     tSection* = POINTER TO RECORD (List.tItem)
  43.         name*: tSectionName
  44.     END;
  45.  
  46.  
  47. VAR
  48.  
  49.     get_color, get_int: PROCEDURE [stdcall] (f_name: RW.tFileName; sec_name: tASCIISectionName; key_name: tString; def_val: INTEGER): INTEGER;
  50.     get_str: PROCEDURE [stdcall] (f_name, sec_name, key_name, buffer, buf_len, def_val: INTEGER): INTEGER;
  51.     enum_sections: PROCEDURE [stdcall] (f_name: RW.tFileName; callback: INTEGER);
  52.  
  53.     IniFileName: RW.tFileName;
  54.     sections*: List.tList;
  55.  
  56.     curSection*: tASCIISectionName;
  57.     curSectionNum*: INTEGER;
  58.     blink*: INTEGER;
  59.  
  60.  
  61. PROCEDURE getColor (key: tString; def: INTEGER): INTEGER;
  62.     RETURN get_color(IniFileName, curSection, key, def)
  63. END getColor;
  64.  
  65.  
  66. PROCEDURE getStr* (secName, keyName: ARRAY OF CHAR; VAR s: ARRAY OF CHAR);
  67. BEGIN
  68.     IF get_str(SYSTEM.ADR(IniFileName[0]), SYSTEM.ADR(secName[0]), SYSTEM.ADR(keyName[0]), SYSTEM.ADR(s[0]), LEN(s) - 1, SYSTEM.SADR("")) = -1 THEN
  69.         s[0] := 0X
  70.     END
  71. END getStr;
  72.  
  73.  
  74. PROCEDURE [stdcall] section_callback (fileName, sectionName: RW.tFileName): INTEGER;
  75. VAR
  76.     section: tSection;
  77.     name: tSectionName;
  78.     i: INTEGER;
  79. BEGIN
  80.     IF sections.count < MAX_SECTIONS THEN
  81.         i := 0;
  82.         WHILE (i < MAX_LEN - 1) & (sectionName[i] # 0X) DO
  83.             name[i] := WCHR(ORD(sectionName[i]));
  84.             INC(i)
  85.         END;
  86.         name[i] := 0X
  87.     END;
  88.     IF Utils.streq(SYSTEM.ADR(name[0]), SYSTEM.WSADR("color_"), 6) THEN
  89.         Utils.reverse(name);
  90.         name[LENGTH(name) - 6] := 0X;
  91.         Utils.reverse(name);
  92.         NEW(section);
  93.         section.name := name;
  94.         List.append(sections, section)
  95.     END
  96.     RETURN 1
  97. END section_callback;
  98.  
  99.  
  100. PROCEDURE selectSection* (idx: INTEGER);
  101. VAR
  102.     i: INTEGER;
  103.     item: List.tItem;
  104.     section: tSection;
  105.  
  106.     text, back, seltext, selback, modified, saved, curline, numtext, numback,
  107.     comment, string, num, delim, key1, key2, key3: INTEGER;
  108. BEGIN
  109.     IF (0 <= idx) & (idx < sections.count) THEN
  110.         curSectionNum := idx;
  111.         item := List.getItem(sections, idx);
  112.         section := item(tSection);
  113.         i := 0;
  114.         WHILE section.name[i] # 0X DO
  115.             curSection[i] := CHR(ORD(section.name[i]));
  116.             INC(i)
  117.         END;
  118.         curSection[i] := 0X;
  119.         Utils.reverse8(curSection);
  120.         Utils.append8(curSection, "_roloc");
  121.         Utils.reverse8(curSection)
  122.     ELSE
  123.         curSection := ""
  124.     END;
  125.  
  126.     text     := getColor("text",     0000000H);
  127.     back     := getColor("back",     0FFFFFFH);
  128.     seltext  := getColor("seltext",  0FFFFFFH);
  129.     selback  := getColor("selback",  00000FFH);
  130.     modified := getColor("modified", 0E8E800H);
  131.     saved    := getColor("saved",    000D000H);
  132.     curline  := getColor("curline",  0FFFFC8H);
  133.     numtext  := getColor("numtext",  0000000H);
  134.     numback  := getColor("numback",  0E6E6E6H);
  135.  
  136.     comment  := getColor("comment",  0800080H);
  137.     string   := getColor("string",   0008000H);
  138.     num      := getColor("num",      0800000H);
  139.     delim    := getColor("delim",    0000080H);
  140.     key1     := getColor("key1",     0000080H);
  141.     key2     := getColor("key2",     0008080H);
  142.     key3     := getColor("key3",     0008080H);
  143.  
  144.     Text.setColors(text, back, seltext, selback, modified, saved, curline, numtext, numback,
  145.         comment, string, num, delim, key1, key2, key3);
  146. END selectSection;
  147.  
  148.  
  149. PROCEDURE getSettings* (VAR build, run, debug: RW.tFileName);
  150. BEGIN
  151.         Lines.setTabs(get_int(IniFileName, "settings", "tab", 4));
  152.         blink := get_int(IniFileName, "settings", "blink", 70);
  153.         IF blink = 0 THEN
  154.                 blink := -1
  155.         ELSE
  156.                 blink := MIN(MAX(blink, 1), 1000)
  157.         END;
  158.     getStr("settings", "build", build);
  159.     getStr("settings", "run",   run);
  160.     getStr("settings", "debug", debug)
  161. END getSettings;
  162.  
  163.  
  164. PROCEDURE load* (path: RW.tFileName);
  165. VAR
  166.     Lib: INTEGER;
  167.  
  168.     PROCEDURE GetProc(Lib, v: INTEGER; name: ARRAY OF CHAR);
  169.     VAR
  170.         a: INTEGER;
  171.     BEGIN
  172.         a := KOSAPI.GetProcAdr(name, Lib);
  173.         ASSERT(a # 0);
  174.         SYSTEM.PUT(v, a)
  175.     END GetProc;
  176.  
  177. BEGIN
  178.     sections := List.create(NIL);
  179.  
  180.     Utils.getPath(path, IniFileName);
  181.     Utils.append8(IniFileName, Utils.SLASH);
  182.     Utils.append8(IniFileName, fileName);
  183.  
  184.     IF ~File.Exists(IniFileName) THEN
  185.         IniFileName := "/rd/1/settings/cedit.ini"
  186.     END;
  187.  
  188.     Lib := KOSAPI.LoadLib("/rd/1/Lib/Libini.obj");
  189.     GetProc(Lib, SYSTEM.ADR(get_color), "ini_get_color");
  190.     GetProc(Lib, SYSTEM.ADR(get_int), "ini_get_int");
  191.     GetProc(Lib, SYSTEM.ADR(get_str), "ini_get_str");
  192.     GetProc(Lib, SYSTEM.ADR(enum_sections), "ini_enum_sections");
  193.  
  194.     enum_sections(IniFileName, SYSTEM.ADR(section_callback));
  195.     Languages.init(getStr);
  196.     selectSection(0);
  197. END load;
  198.  
  199.  
  200. END Ini.