Subversion Repositories Kolibri OS

Rev

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