Subversion Repositories Kolibri OS

Rev

Rev 9010 | 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 concat* (line: tLine; s: ARRAY OF WCHAR);
  214. VAR
  215.     Len: INTEGER;
  216.     ptr: INTEGER;
  217. BEGIN
  218.     Len := LENGTH(s);
  219.     ptr := malloc(line.length + Len + 1);
  220.     ASSERT(ptr # 0);
  221.     SYSTEM.MOVE(line.ptr, ptr, line.length*WCHAR_SIZE);
  222.     SYSTEM.MOVE(SYSTEM.ADR(s[0]), ptr + line.length*WCHAR_SIZE, Len*WCHAR_SIZE);
  223.     SYSTEM.PUT16(ptr + (line.length + Len)*WCHAR_SIZE, 0);
  224.     IF ~line.temp THEN
  225.         movInt(line.length, line.length + Len)
  226.     END;
  227.     INC(line.length, Len);
  228.     free(line, ptr)
  229. END concat;
  230.  
  231.  
  232. PROCEDURE delChar* (line: tLine; pos: INTEGER);
  233. VAR
  234.     ptr: INTEGER;
  235. BEGIN
  236.     IF pos < line.length THEN
  237.         ptr := malloc(line.length);
  238.         ASSERT(ptr # 0);
  239.         IF ~line.temp THEN
  240.             movInt(line.length, line.length - 1)
  241.         END;
  242.         DEC(line.length);
  243.         SYSTEM.MOVE(line.ptr, ptr, pos*WCHAR_SIZE);
  244.         SYSTEM.MOVE(line.ptr + pos*WCHAR_SIZE + WCHAR_SIZE, ptr + pos*WCHAR_SIZE, (line.length - pos)*WCHAR_SIZE);
  245.         SYSTEM.PUT16(ptr + line.length*WCHAR_SIZE, 0);
  246.         free(line, ptr)
  247.     END
  248. END delChar;
  249.  
  250.  
  251. PROCEDURE insert* (line: tLine; pos: INTEGER; c: WCHAR);
  252. VAR
  253.     ptr: INTEGER;
  254. BEGIN
  255.     ptr := malloc(line.length + 2);
  256.     ASSERT(ptr # 0);
  257.     SYSTEM.MOVE(line.ptr, ptr, pos*WCHAR_SIZE);
  258.     SYSTEM.PUT(ptr + pos*WCHAR_SIZE, c);
  259.     SYSTEM.MOVE(line.ptr + pos*WCHAR_SIZE, ptr + pos*WCHAR_SIZE + WCHAR_SIZE, (line.length - pos)*WCHAR_SIZE);
  260.     IF ~line.temp THEN
  261.         movInt(line.length, line.length + 1)
  262.     END;
  263.     INC(line.length);
  264.     SYSTEM.PUT16(ptr + line.length*WCHAR_SIZE, 0);
  265.     free(line, ptr)
  266. END insert;
  267.  
  268.  
  269. PROCEDURE insert2* (line1: tLine; pos: INTEGER; line2: tLine);
  270. VAR
  271.     ptr: INTEGER;
  272. BEGIN
  273.     IF line2.length > 0 THEN
  274.         ptr := malloc(line1.length + line2.length + 1);
  275.         ASSERT(ptr # 0);
  276.         SYSTEM.MOVE(line1.ptr, ptr, pos*WCHAR_SIZE);
  277.         SYSTEM.MOVE(line2.ptr, ptr + pos*WCHAR_SIZE, line2.length*WCHAR_SIZE);
  278.         SYSTEM.MOVE(line1.ptr + pos*WCHAR_SIZE, ptr + (pos + line2.length)*WCHAR_SIZE, (line1.length - pos)*WCHAR_SIZE);
  279.         SYSTEM.PUT16(ptr + (line1.length + line2.length)*WCHAR_SIZE, 0);
  280.         IF ~line1.temp THEN
  281.             movInt(line1.length, line1.length + line2.length)
  282.         END;
  283.         IF ~line2.temp THEN
  284.             movInt(line2.length, 0)
  285.         END;
  286.         INC(line1.length, line2.length);
  287.         line2.length := 0;
  288.         free(line1, ptr);
  289.         free(line2, 0)
  290.     END
  291. END insert2;
  292.  
  293.  
  294. PROCEDURE insert3* (line: tLine; pos, n: INTEGER);
  295. VAR
  296.     ptr: INTEGER;
  297. BEGIN
  298.     IF n > 0 THEN
  299.         ptr := malloc(line.length + n + 1);
  300.         ASSERT(ptr # 0);
  301.         SYSTEM.MOVE(line.ptr, ptr, pos*WCHAR_SIZE);
  302.         SYSTEM.MOVE(line.ptr + pos*WCHAR_SIZE, ptr + (pos + n)*WCHAR_SIZE, (line.length - pos)*WCHAR_SIZE);
  303.         SYSTEM.PUT16(ptr + (line.length + n)*WCHAR_SIZE, 0);
  304.         IF ~line.temp THEN
  305.             movInt(line.length, line.length + n)
  306.         END;
  307.         INC(line.length, n);
  308.         free(line, ptr)
  309.     END
  310. END insert3;
  311.  
  312.  
  313. PROCEDURE delCharN* (line: tLine; pos, n: INTEGER);
  314. VAR
  315.     ptr: INTEGER;
  316. BEGIN
  317.     IF n > 0 THEN
  318.         ptr := malloc(line.length - n + 1);
  319.         ASSERT(ptr # 0);
  320.         SYSTEM.MOVE(line.ptr, ptr, pos*WCHAR_SIZE);
  321.         SYSTEM.MOVE(line.ptr + (pos + n)*WCHAR_SIZE, ptr + pos*WCHAR_SIZE, (line.length - pos - n)*WCHAR_SIZE);
  322.         SYSTEM.PUT16(ptr + (line.length - n)*WCHAR_SIZE, 0);
  323.         IF ~line.temp THEN
  324.             movInt(line.length, line.length - n)
  325.         END;
  326.         DEC(line.length, n);
  327.         free(line, ptr)
  328.     END
  329. END delCharN;
  330.  
  331.  
  332. PROCEDURE wrap* (line, nextLine: tLine; pos: INTEGER);
  333. VAR
  334.     ptr1, ptr2: INTEGER;
  335.     n: INTEGER;
  336. BEGIN
  337.     ptr1 := malloc(pos + 1);
  338.     ASSERT(ptr1 # 0);
  339.     n := line.length - pos;
  340.     ptr2 := malloc(n + 1);
  341.     ASSERT(ptr2 # 0);
  342.     SYSTEM.MOVE(line.ptr, ptr1, pos*WCHAR_SIZE);
  343.     SYSTEM.PUT16(ptr1 + pos*WCHAR_SIZE, 0);
  344.     SYSTEM.MOVE(line.ptr + pos*WCHAR_SIZE, ptr2, n*WCHAR_SIZE);
  345.     SYSTEM.PUT16(ptr2 + n*WCHAR_SIZE, 0);
  346.     IF ~line.temp THEN
  347.         movInt(line.length, pos)
  348.     END;
  349.     IF ~nextLine.temp THEN
  350.         movInt(nextLine.length, n)
  351.     END;
  352.     line.length := pos;
  353.     nextLine.length := n;
  354.     free(line, ptr1);
  355.     free(nextLine, ptr2)
  356. END wrap;
  357.  
  358.  
  359. PROCEDURE copy* (line: tLine);
  360. VAR
  361.     ptr: INTEGER;
  362. BEGIN
  363.     ptr := malloc(line.length + 1);
  364.     ASSERT(ptr # 0);
  365.     SYSTEM.MOVE(line.ptr, ptr, line.length*WCHAR_SIZE);
  366.     SYSTEM.PUT16(ptr + line.length*WCHAR_SIZE, 0);
  367.     free(line, ptr)
  368. END copy;
  369.  
  370.  
  371. PROCEDURE chCase* (line: tLine; pos1, pos2: INTEGER; upper: BOOLEAN): BOOLEAN;
  372. VAR
  373.     i: INTEGER;
  374.     modified: BOOLEAN;
  375.     c: WCHAR;
  376.     func: PROCEDURE (VAR c: WCHAR): BOOLEAN;
  377. BEGIN
  378.     modified := FALSE;
  379.     IF upper THEN
  380.         func := Utils.cap
  381.     ELSE
  382.         func := Utils.low
  383.     END;
  384.     i := pos2;
  385.     WHILE i >= pos1 DO
  386.         c := getChar(line, i);
  387.         IF func(c) THEN
  388.             modified := TRUE
  389.         END;
  390.         DEC(i)
  391.     END;
  392.  
  393.     IF modified THEN
  394.         copy(line);
  395.         i := pos2;
  396.         WHILE i >= pos1 DO
  397.             c := getChar(line, i);
  398.             IF func(c) THEN
  399.                 setChar(line, i, c)
  400.             END;
  401.             DEC(i)
  402.         END;
  403.         modify(line)
  404.     END
  405.     RETURN modified
  406. END chCase;
  407.  
  408.  
  409. PROCEDURE init* (movInt: PmovInt; movPtr: PmovPtr; movBool: PmovBool(*; typedPtr: PTypedPtr; untypedPtr: PUntypedPtr*));
  410. BEGIN
  411.     _movInt := movInt;
  412.     _movPtr := movPtr;
  413.     _movBool := movBool;
  414. (*    _typedPtr := typedPtr;
  415.     _untypedPtr := untypedPtr;*)
  416. END init;
  417.  
  418.  
  419. BEGIN
  420.     maxLength := 64
  421. END Lines.