Subversion Repositories Kolibri OS

Rev

Rev 8728 | Rev 9050 | 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 Lines;
  21.  
  22. IMPORT
  23.     List, SYSTEM, API, Utils;
  24.  
  25. CONST
  26.     WCHAR_SIZE = 2;
  27.     SPACE = 20X;
  28.  
  29. TYPE
  30.  
  31.     tLine* = POINTER TO RECORD (List.tItem)
  32.         ptr: INTEGER;
  33.         length*: INTEGER;
  34.         modified*, saved*, temp: BOOLEAN;
  35.         cin*, cout*, pos*: INTEGER
  36.     END;
  37.  
  38.     PmovInt = PROCEDURE (VAR v: INTEGER; x: INTEGER);
  39.     PmovBool = PROCEDURE (VAR v: BOOLEAN; x: BOOLEAN);
  40.     PmovPtr = PROCEDURE (VAR v: List.tItem; x: List.tItem);
  41. (*
  42.     PTypedPtr = PROCEDURE (p: List.tItem);
  43.     PUntypedPtr = PROCEDURE (p: INTEGER);
  44. *)
  45.  
  46. VAR
  47.  
  48.     _movInt: PmovInt;
  49.     _movBool: PmovBool;
  50.     _movPtr: PmovPtr;
  51. (*    _typedPtr: PTypedPtr;
  52.     _untypedPtr: PUntypedPtr;*)
  53.  
  54.     maxLength*: INTEGER;
  55.  
  56.  
  57. PROCEDURE movInt (VAR v: INTEGER; x: INTEGER);
  58. BEGIN
  59.     _movInt(v, x)
  60. END movInt;
  61.  
  62.  
  63. PROCEDURE movBool (VAR v: BOOLEAN; x: BOOLEAN);
  64. BEGIN
  65.     _movBool(v, x)
  66. END movBool;
  67.  
  68.  
  69. PROCEDURE movPtr (VAR v: List.tItem; x: List.tItem);
  70. BEGIN
  71.     _movPtr(v, x)
  72. END movPtr;
  73.  
  74.  
  75. PROCEDURE malloc (size: INTEGER): INTEGER;
  76. VAR
  77.     ptr: INTEGER;
  78. BEGIN
  79.     IF size > maxLength THEN
  80.         maxLength := size
  81.     END;
  82.     size := size*WCHAR_SIZE + 4;
  83.     INC(size, (-size) MOD 32);
  84.     ptr := API._NEW(size)
  85.     RETURN ptr
  86. END malloc;
  87.  
  88.  
  89. PROCEDURE free (line: tLine; newPtr: INTEGER);
  90. BEGIN
  91.     IF line.ptr # 0 THEN
  92.         IF line.temp THEN
  93.             line.ptr := API._DISPOSE(line.ptr)
  94.         ELSE
  95.             line.ptr := 0
  96.         END
  97.     END;
  98.     IF ~line.temp THEN
  99.         movInt(line.ptr, newPtr);
  100. (*        IF newPtr # 0 THEN
  101.             _untypedPtr(newPtr)
  102.         END*)
  103.     END;
  104.     line.ptr := newPtr
  105. END free;
  106.  
  107.  
  108. PROCEDURE create* (temp: BOOLEAN): tLine;
  109. VAR
  110.     line: tLine;
  111. BEGIN
  112.     NEW(line);
  113.     ASSERT(line # NIL);
  114. (*    IF ~temp THEN
  115.         _typedPtr(line)
  116.     END;*)
  117.     line.next := NIL;
  118.     line.prev := NIL;
  119.     IF ~temp THEN
  120.         movPtr(line.next, NIL);
  121.         movPtr(line.prev, NIL)
  122.     END;
  123.     line.ptr := malloc(1);
  124.     ASSERT(line.ptr # 0);
  125.     IF ~temp THEN
  126.         (*_untypedPtr(line.ptr);*)
  127.         movInt(line.ptr, line.ptr)
  128.     END;
  129.     SYSTEM.PUT16(line.ptr, 0);
  130.     line.length := 0;
  131.     IF ~temp THEN
  132.         movInt(line.length, 0)
  133.     END;
  134.     line.temp := temp;
  135.     line.modified := FALSE;
  136.     line.saved := FALSE;
  137.     IF ~temp THEN
  138.         movBool(line.modified, FALSE);
  139.         movBool(line.saved, FALSE)
  140.     END;
  141.     line.cin := 0;
  142.     line.cout := 0;
  143.     line.pos := 0
  144.     RETURN line
  145. END create;
  146.  
  147.  
  148. PROCEDURE destroy* (VAR line: tLine);
  149. BEGIN
  150.     IF line.temp THEN
  151.         free(line, 0);
  152.         DISPOSE(line)
  153.     ELSE
  154.         line := NIL
  155.     END
  156. END destroy;
  157.  
  158.  
  159. PROCEDURE modify* (line: tLine);
  160. BEGIN
  161.     IF ~line.temp THEN
  162.         movBool(line.modified, TRUE);
  163.         movBool(line.saved, FALSE)
  164.     END;
  165.     line.modified := TRUE;
  166.     line.saved := FALSE
  167. END modify;
  168.  
  169.  
  170. PROCEDURE save* (line: tLine);
  171. BEGIN
  172.     IF ~line.temp THEN
  173.         movBool(line.saved, TRUE);
  174.         movBool(line.modified, FALSE)
  175.     END;
  176.     line.modified := FALSE;
  177.     line.saved := TRUE
  178. END save;
  179.  
  180.  
  181. PROCEDURE getChar* (line: tLine; i: INTEGER): WCHAR;
  182. VAR
  183.     c: WCHAR;
  184. BEGIN
  185.     SYSTEM.GET(line.ptr + i*WCHAR_SIZE, c)
  186.     RETURN c
  187. END getChar;
  188.  
  189.  
  190. PROCEDURE trimLength* (line: tLine): INTEGER;
  191. VAR
  192.     i: INTEGER;
  193. BEGIN
  194.     i := line.length - 1;
  195.     WHILE (i >= 0) & (getChar(line, i) = SPACE) DO
  196.         DEC(i)
  197.     END
  198.     RETURN i + 1
  199. END trimLength;
  200.  
  201.  
  202. PROCEDURE getPChar* (line: tLine; i: INTEGER): INTEGER;
  203.     RETURN line.ptr + i*WCHAR_SIZE
  204. END getPChar;
  205.  
  206.  
  207. PROCEDURE setChar* (line: tLine; i: INTEGER; c: WCHAR);
  208. BEGIN
  209.     SYSTEM.PUT(line.ptr + i*WCHAR_SIZE, c)
  210. END setChar;
  211.  
  212.  
  213. PROCEDURE move* (src, dst: tLine);
  214. BEGIN
  215.     SYSTEM.MOVE(src.ptr, dst.ptr, (MIN(src.length, dst.length) + 1)*WCHAR_SIZE)
  216. END move;
  217.  
  218.  
  219. PROCEDURE concat* (line: tLine; s: ARRAY OF WCHAR);
  220. VAR
  221.     Len: INTEGER;
  222.     ptr: INTEGER;
  223. BEGIN
  224.     Len := LENGTH(s);
  225.     ptr := malloc(line.length + Len + 1);
  226.     ASSERT(ptr # 0);
  227.     SYSTEM.MOVE(line.ptr, ptr, line.length*WCHAR_SIZE);
  228.     SYSTEM.MOVE(SYSTEM.ADR(s[0]), ptr + line.length*WCHAR_SIZE, Len*WCHAR_SIZE);
  229.     SYSTEM.PUT16(ptr + (line.length + Len)*WCHAR_SIZE, 0);
  230.     IF ~line.temp THEN
  231.         movInt(line.length, line.length + Len)
  232.     END;
  233.     INC(line.length, Len);
  234.     free(line, ptr)
  235. END concat;
  236.  
  237.  
  238. PROCEDURE delChar* (line: tLine; pos: INTEGER);
  239. VAR
  240.     ptr: INTEGER;
  241. BEGIN
  242.     IF pos < line.length THEN
  243.         ptr := malloc(line.length);
  244.         ASSERT(ptr # 0);
  245.         IF ~line.temp THEN
  246.             movInt(line.length, line.length - 1)
  247.         END;
  248.         DEC(line.length);
  249.         SYSTEM.MOVE(line.ptr, ptr, pos*WCHAR_SIZE);
  250.         SYSTEM.MOVE(line.ptr + pos*WCHAR_SIZE + WCHAR_SIZE, ptr + pos*WCHAR_SIZE, (line.length - pos)*WCHAR_SIZE);
  251.         SYSTEM.PUT16(ptr + line.length*WCHAR_SIZE, 0);
  252.         free(line, ptr)
  253.     END
  254. END delChar;
  255.  
  256.  
  257. PROCEDURE insert* (line: tLine; pos: INTEGER; c: WCHAR);
  258. VAR
  259.     ptr: INTEGER;
  260. BEGIN
  261.     ptr := malloc(line.length + 2);
  262.     ASSERT(ptr # 0);
  263.     SYSTEM.MOVE(line.ptr, ptr, pos*WCHAR_SIZE);
  264.     SYSTEM.PUT(ptr + pos*WCHAR_SIZE, c);
  265.     SYSTEM.MOVE(line.ptr + pos*WCHAR_SIZE, ptr + pos*WCHAR_SIZE + WCHAR_SIZE, (line.length - pos)*WCHAR_SIZE);
  266.     IF ~line.temp THEN
  267.         movInt(line.length, line.length + 1)
  268.     END;
  269.     INC(line.length);
  270.     SYSTEM.PUT16(ptr + line.length*WCHAR_SIZE, 0);
  271.     free(line, ptr)
  272. END insert;
  273.  
  274.  
  275. PROCEDURE insert2* (line1: tLine; pos: INTEGER; line2: tLine);
  276. VAR
  277.     ptr: INTEGER;
  278. BEGIN
  279.     IF line2.length > 0 THEN
  280.         ptr := malloc(line1.length + line2.length + 1);
  281.         ASSERT(ptr # 0);
  282.         SYSTEM.MOVE(line1.ptr, ptr, pos*WCHAR_SIZE);
  283.         SYSTEM.MOVE(line2.ptr, ptr + pos*WCHAR_SIZE, line2.length*WCHAR_SIZE);
  284.         SYSTEM.MOVE(line1.ptr + pos*WCHAR_SIZE, ptr + (pos + line2.length)*WCHAR_SIZE, (line1.length - pos)*WCHAR_SIZE);
  285.         SYSTEM.PUT16(ptr + (line1.length + line2.length)*WCHAR_SIZE, 0);
  286.         IF ~line1.temp THEN
  287.             movInt(line1.length, line1.length + line2.length)
  288.         END;
  289.         IF ~line2.temp THEN
  290.             movInt(line2.length, 0)
  291.         END;
  292.         INC(line1.length, line2.length);
  293.         line2.length := 0;
  294.         free(line1, ptr);
  295.         free(line2, 0)
  296.     END
  297. END insert2;
  298.  
  299.  
  300. PROCEDURE insert3* (line: tLine; pos, n: INTEGER);
  301. VAR
  302.     ptr: INTEGER;
  303. BEGIN
  304.     IF n > 0 THEN
  305.         ptr := malloc(line.length + n + 1);
  306.         ASSERT(ptr # 0);
  307.         SYSTEM.MOVE(line.ptr, ptr, pos*WCHAR_SIZE);
  308.         SYSTEM.MOVE(line.ptr + pos*WCHAR_SIZE, ptr + (pos + n)*WCHAR_SIZE, (line.length - pos)*WCHAR_SIZE);
  309.         SYSTEM.PUT16(ptr + (line.length + n)*WCHAR_SIZE, 0);
  310.         IF ~line.temp THEN
  311.             movInt(line.length, line.length + n)
  312.         END;
  313.         INC(line.length, n);
  314.         free(line, ptr)
  315.     END
  316. END insert3;
  317.  
  318.  
  319. PROCEDURE delCharN* (line: tLine; pos, n: INTEGER);
  320. VAR
  321.     ptr: INTEGER;
  322. BEGIN
  323.     IF n > 0 THEN
  324.         ptr := malloc(line.length - n + 1);
  325.         ASSERT(ptr # 0);
  326.         SYSTEM.MOVE(line.ptr, ptr, pos*WCHAR_SIZE);
  327.         SYSTEM.MOVE(line.ptr + (pos + n)*WCHAR_SIZE, ptr + pos*WCHAR_SIZE, (line.length - pos - n)*WCHAR_SIZE);
  328.         SYSTEM.PUT16(ptr + (line.length - n)*WCHAR_SIZE, 0);
  329.         IF ~line.temp THEN
  330.             movInt(line.length, line.length - n)
  331.         END;
  332.         DEC(line.length, n);
  333.         free(line, ptr)
  334.     END
  335. END delCharN;
  336.  
  337.  
  338. PROCEDURE wrap* (line, nextLine: tLine; pos: INTEGER);
  339. VAR
  340.     ptr1, ptr2: INTEGER;
  341.     n: INTEGER;
  342. BEGIN
  343.     ptr1 := malloc(pos + 1);
  344.     ASSERT(ptr1 # 0);
  345.     n := line.length - pos;
  346.     ptr2 := malloc(n + 1);
  347.     ASSERT(ptr2 # 0);
  348.     SYSTEM.MOVE(line.ptr, ptr1, pos*WCHAR_SIZE);
  349.     SYSTEM.PUT16(ptr1 + pos*WCHAR_SIZE, 0);
  350.     SYSTEM.MOVE(line.ptr + pos*WCHAR_SIZE, ptr2, n*WCHAR_SIZE);
  351.     SYSTEM.PUT16(ptr2 + n*WCHAR_SIZE, 0);
  352.     IF ~line.temp THEN
  353.         movInt(line.length, pos)
  354.     END;
  355.     IF ~nextLine.temp THEN
  356.         movInt(nextLine.length, n)
  357.     END;
  358.     line.length := pos;
  359.     nextLine.length := n;
  360.     free(line, ptr1);
  361.     free(nextLine, ptr2)
  362. END wrap;
  363.  
  364.  
  365. PROCEDURE copy* (line: tLine);
  366. VAR
  367.     ptr: INTEGER;
  368. BEGIN
  369.     ptr := malloc(line.length + 1);
  370.     ASSERT(ptr # 0);
  371.     SYSTEM.MOVE(line.ptr, ptr, line.length*WCHAR_SIZE);
  372.     SYSTEM.PUT16(ptr + line.length*WCHAR_SIZE, 0);
  373.     free(line, ptr)
  374. END copy;
  375.  
  376.  
  377. PROCEDURE chCase* (line: tLine; pos1, pos2: INTEGER; upper: BOOLEAN): BOOLEAN;
  378. VAR
  379.     i: INTEGER;
  380.     modified: BOOLEAN;
  381.     c: WCHAR;
  382.     func: PROCEDURE (VAR c: WCHAR): BOOLEAN;
  383. BEGIN
  384.     modified := FALSE;
  385.     IF upper THEN
  386.         func := Utils.cap
  387.     ELSE
  388.         func := Utils.low
  389.     END;
  390.     i := pos2;
  391.     WHILE i >= pos1 DO
  392.         c := getChar(line, i);
  393.         IF func(c) THEN
  394.             modified := TRUE
  395.         END;
  396.         DEC(i)
  397.     END;
  398.  
  399.     IF modified THEN
  400.         copy(line);
  401.         i := pos2;
  402.         WHILE i >= pos1 DO
  403.             c := getChar(line, i);
  404.             IF func(c) THEN
  405.                 setChar(line, i, c)
  406.             END;
  407.             DEC(i)
  408.         END;
  409.         modify(line)
  410.     END
  411.     RETURN modified
  412. END chCase;
  413.  
  414.  
  415. PROCEDURE init* (movInt: PmovInt; movPtr: PmovPtr; movBool: PmovBool(*; typedPtr: PTypedPtr; untypedPtr: PUntypedPtr*));
  416. BEGIN
  417.     _movInt := movInt;
  418.     _movPtr := movPtr;
  419.     _movBool := movBool;
  420. (*    _typedPtr := typedPtr;
  421.     _untypedPtr := untypedPtr;*)
  422. END init;
  423.  
  424.  
  425. BEGIN
  426.     maxLength := 64
  427. END Lines.