kolibrios/programs/develop/cedit/SRC/Ini.ob07

206 lines
6.6 KiB
Plaintext
Raw Normal View History

(*
Copyright 2021-2023 Anton Krotov
This file is part of CEdit.
CEdit 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.
CEdit 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 CEdit. If not, see <http://www.gnu.org/licenses/>.
*)
MODULE Ini;
IMPORT
SYSTEM, RW, Text, Utils, File, List, Languages, Lines;
CONST
fileName = "cedit.ini";
libName = "libini.obj";
MAX_LEN = 32;
MAX_SECTIONS* = 10;
TYPE
tString = ARRAY 128 OF CHAR;
tSectionName = ARRAY MAX_LEN OF WCHAR;
tASCIISectionName = ARRAY MAX_LEN OF CHAR;
tSection* = POINTER TO RECORD (List.tItem)
name*: tSectionName
END;
VAR
IniFileName: RW.tFileName;
sections*: List.tList;
curSection*: tASCIISectionName;
curSectionNum*: INTEGER;
blink*, font*, theme*, lineSpacing*: INTEGER;
buildScript*, runScript*, debugScript*: RW.tFileName;
lineNumbers*, autoIndents*, autoBrackets*, trimSpace*: BOOLEAN;
PROCEDURE [stdcall, libName, "ini_get_color"] get_color (f_name: RW.tFileName; sec_name: tASCIISectionName; key_name: tString; def_val: INTEGER): INTEGER; END;
PROCEDURE [stdcall, libName, "ini_get_int"] get_int (f_name: RW.tFileName; sec_name: tASCIISectionName; key_name: tString; def_val: INTEGER): INTEGER; END;
PROCEDURE [stdcall, libName, "ini_get_str"] get_str (f_name, sec_name, key_name, buffer, buf_len, def_val: INTEGER): INTEGER; END;
PROCEDURE [stdcall, libName, "ini_enum_sections"] enum_sections (f_name: RW.tFileName; callback: INTEGER); END;
PROCEDURE [stdcall-, libName, "ini_set_int"] set_int (f_name, sec_name, key_name, val: INTEGER): INTEGER; END;
PROCEDURE getColor (key: tString; def: INTEGER): INTEGER;
RETURN get_color(IniFileName, curSection, key, def)
END getColor;
PROCEDURE getStr* (secName, keyName: ARRAY OF CHAR; VAR s: ARRAY OF CHAR);
BEGIN
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
s[0] := 0X
END
END getStr;
PROCEDURE setInt* (secName, keyName: ARRAY OF CHAR; val: INTEGER);
BEGIN
set_int(SYSTEM.ADR(IniFileName[0]), SYSTEM.ADR(secName[0]), SYSTEM.ADR(keyName[0]), val)
END setInt;
PROCEDURE [stdcall] section_callback (fileName, sectionName: RW.tFileName): INTEGER;
VAR
section: tSection;
name: tSectionName;
i: INTEGER;
BEGIN
IF sections.count < MAX_SECTIONS THEN
i := 0;
WHILE (i < MAX_LEN - 1) & (sectionName[i] # 0X) DO
name[i] := WCHR(ORD(sectionName[i]));
INC(i)
END;
name[i] := 0X
END;
IF Utils.streq(SYSTEM.ADR(name[0]), SYSTEM.WSADR("color_"), 6) THEN
Utils.reverse(name);
name[LENGTH(name) - 6] := 0X;
Utils.reverse(name);
NEW(section);
section.name := name;
List.append(sections, section)
END
RETURN 1
END section_callback;
PROCEDURE selectSection* (idx: INTEGER);
VAR
i: INTEGER;
item: List.tItem;
section: tSection;
text, back, seltext, selback, modified, saved, curline, numtext, numback,
comment, string, escape, num, delim, key1, key2, key3: INTEGER;
BEGIN
IF (0 <= idx) & (idx < sections.count) THEN
curSectionNum := idx;
item := List.getItem(sections, idx);
section := item(tSection);
i := 0;
WHILE section.name[i] # 0X DO
curSection[i] := CHR(ORD(section.name[i]));
INC(i)
END;
curSection[i] := 0X;
Utils.reverse8(curSection);
Utils.append8(curSection, "_roloc");
Utils.reverse8(curSection)
ELSE
curSection := ""
END;
text := getColor("text", 0000000H);
back := getColor("back", 0FFFFFFH);
seltext := getColor("seltext", 0FFFFFFH);
selback := getColor("selback", 00000FFH);
modified := getColor("modified", 0E8E800H);
saved := getColor("saved", 000D000H);
curline := getColor("curline", 0FFFFC8H);
numtext := getColor("numtext", 0000000H);
numback := getColor("numback", 0E6E6E6H);
comment := getColor("comment", 0800080H);
string := getColor("string", 0008000H);
num := getColor("num", 0800000H);
delim := getColor("delim", 0000080H);
key1 := getColor("key1", 0000080H);
key2 := getColor("key2", 0008080H);
key3 := getColor("key3", 0008080H);
escape := getColor("escape", string);
Text.setColors(text, back, seltext, selback, modified, saved, curline, numtext, numback,
comment, string, escape, num, delim, key1, key2, key3);
END selectSection;
PROCEDURE getSettings*;
BEGIN
Lines.setTabs(get_int(IniFileName, "settings", "tab", 4));
blink := get_int(IniFileName, "settings", "blink", 70);
lineNumbers := get_int(IniFileName, "settings", "line_numbers", 1) # 0;
autoIndents := get_int(IniFileName, "settings", "auto_indents", 1) # 0;
autoBrackets := get_int(IniFileName, "settings", "auto_brackets", 0) # 0;
trimSpace := get_int(IniFileName, "settings", "trim_space", 1) # 0;
font := get_int(IniFileName, "settings", "font", 1);
IF (font < 0) OR (font > 2) THEN
font := 1
END;
lineSpacing := get_int(IniFileName, "settings", "line_spacing", 2);
IF (lineSpacing < 0) OR (lineSpacing > Text.MaxLineSpacing) THEN
lineSpacing := 2
END;
theme := get_int(IniFileName, "settings", "theme", 0);
IF (theme < 0) OR (theme >= sections.count) THEN
theme := 0
END;
getStr("settings", "build", buildScript);
getStr("settings", "run", runScript);
getStr("settings", "debug", debugScript);
END getSettings;
PROCEDURE load* (path: RW.tFileName);
BEGIN
sections := List.create(NIL);
Utils.getPath(path, IniFileName);
Utils.append8(IniFileName, Utils.SLASH);
Utils.append8(IniFileName, fileName);
IF ~File.Exists(IniFileName) THEN
IniFileName := "/sys/settings/" + fileName
END;
enum_sections(IniFileName, SYSTEM.ADR(section_callback));
Languages.init(getStr);
selectSection(theme);
END load;
END Ini.