Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

  1. (*
  2.     Copyright 2013, 2017, 2018, 2020 Anton Krotov
  3.  
  4.     This program is free software: you can redistribute it and/or modify
  5.     it under the terms of the GNU Lesser General Public License as published by
  6.     the Free Software Foundation, either version 3 of the License, or
  7.     (at your option) any later version.
  8.  
  9.     This program is distributed in the hope that it will be useful,
  10.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.     GNU Lesser General Public License for more details.
  13.  
  14.     You should have received a copy of the GNU Lesser General Public License
  15.     along with this program.  If not, see <http://www.gnu.org/licenses/>.
  16. *)
  17.  
  18. MODULE Utils;
  19.  
  20. IMPORT WINAPI;
  21.  
  22. PROCEDURE PutSeed*(seed: INTEGER);
  23. BEGIN
  24.   WINAPI.srand(seed)
  25. END PutSeed;
  26.  
  27. PROCEDURE Rnd*(range : INTEGER): INTEGER;
  28.   RETURN WINAPI.rand() MOD range
  29. END Rnd;
  30.  
  31. PROCEDURE Utf8To16*(source: ARRAY OF CHAR; VAR dest: ARRAY OF CHAR): INTEGER;
  32. VAR i, j, L, u, N: INTEGER;
  33. BEGIN
  34.   L := LEN(source);
  35.   N := LEN(dest);
  36.   N := N - N MOD 2 - 1;
  37.   i := 0;
  38.   j := 0;
  39.   WHILE (i < L) & (j < N) & (source[i] # 0X) DO
  40.     CASE source[i] OF
  41.     |00X..7FX: u := ORD(source[i]);
  42.     |0C1X..0DFX:
  43.       u := LSL(ORD(source[i]) - 0C0H, 6);
  44.       IF i + 1 < L THEN
  45.         u := u + ROR(LSL(ORD(source[i + 1]), 26), 26);
  46.         INC(i)
  47.       END
  48.     |0E1X..0EFX:
  49.       u := LSL(ORD(source[i]) - 0E0H, 12);
  50.       IF i + 1 < L THEN
  51.         u := u + ROR(LSL(ORD(source[i + 1]), 26), 20);
  52.         INC(i)
  53.       END;
  54.       IF i + 1 < L THEN
  55.         u := u + ROR(LSL(ORD(source[i + 1]), 26), 26);
  56.         INC(i)
  57.       END
  58. (*    |0F1X..0F7X:
  59.     |0F9X..0FBX:
  60.     |0FDX:*)
  61.     ELSE
  62.     END;
  63.     INC(i);
  64.     dest[j] := CHR(u MOD 256);
  65.     INC(j);
  66.     dest[j] := CHR(u DIV 256);
  67.     INC(j);
  68.   END;
  69.   IF j < N THEN
  70.     dest[j] := 0X;
  71.     dest[j + 1] := 0X
  72.   END
  73.   RETURN j DIV 2
  74. END Utf8To16;
  75.  
  76. END Utils.