Subversion Repositories Kolibri OS

Rev

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