Subversion Repositories Kolibri OS

Rev

Rev 7597 | Rev 7696 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. (*
  2.     BSD 2-Clause License
  3.  
  4.     Copyright (c) 2018, 2019, Anton Krotov
  5.     All rights reserved.
  6. *)
  7.  
  8. MODULE UTILS;
  9.  
  10. IMPORT HOST, UNIXTIME;
  11.  
  12.  
  13. CONST
  14.  
  15.     slash* = HOST.slash;
  16.  
  17.     bit_depth* = HOST.bit_depth;
  18.     maxint* = HOST.maxint;
  19.     minint* = HOST.minint;
  20.  
  21.     OS = HOST.OS;
  22.  
  23.     min32* = -2147483647-1;
  24.     max32* =  2147483647;
  25.  
  26.  
  27. VAR
  28.  
  29.     time*: INTEGER;
  30.  
  31.     eol*:  ARRAY 3 OF CHAR;
  32.  
  33.     maxreal*: REAL;
  34.  
  35.     target*:
  36.  
  37.       RECORD
  38.  
  39.           bit_depth*,
  40.           maxInt*,
  41.           minInt*,
  42.           maxSet*,
  43.           maxHex*:    INTEGER;
  44.  
  45.           maxReal*:   REAL
  46.  
  47.       END;
  48.  
  49.     bit_diff*: INTEGER;
  50.  
  51.  
  52. PROCEDURE FileRead* (F: INTEGER; VAR Buffer: ARRAY OF BYTE; bytes: INTEGER): INTEGER;
  53.     RETURN HOST.FileRead(F, Buffer, bytes)
  54. END FileRead;
  55.  
  56.  
  57. PROCEDURE FileWrite* (F: INTEGER; Buffer: ARRAY OF BYTE; bytes: INTEGER): INTEGER;
  58.     RETURN HOST.FileWrite(F, Buffer, bytes)
  59. END FileWrite;
  60.  
  61.  
  62. PROCEDURE FileCreate* (FName: ARRAY OF CHAR): INTEGER;
  63.     RETURN HOST.FileCreate(FName)
  64. END FileCreate;
  65.  
  66.  
  67. PROCEDURE FileClose* (F: INTEGER);
  68. BEGIN
  69.     HOST.FileClose(F)
  70. END FileClose;
  71.  
  72.  
  73. PROCEDURE FileOpen* (FName: ARRAY OF CHAR): INTEGER;
  74.     RETURN HOST.FileOpen(FName)
  75. END FileOpen;
  76.  
  77.  
  78. PROCEDURE GetArg* (i: INTEGER; VAR str: ARRAY OF CHAR);
  79. BEGIN
  80.     HOST.GetArg(i, str)
  81. END GetArg;
  82.  
  83.  
  84. PROCEDURE Exit* (code: INTEGER);
  85. BEGIN
  86.     HOST.ExitProcess(code)
  87. END Exit;
  88.  
  89.  
  90. PROCEDURE GetTickCount* (): INTEGER;
  91.     RETURN HOST.GetTickCount()
  92. END GetTickCount;
  93.  
  94.  
  95. PROCEDURE OutChar* (c: CHAR);
  96. BEGIN
  97.     HOST.OutChar(c)
  98. END OutChar;
  99.  
  100.  
  101. PROCEDURE splitf* (x: REAL; VAR a, b: INTEGER): INTEGER;
  102.     RETURN HOST.splitf(x, a, b)
  103. END splitf;
  104.  
  105.  
  106. PROCEDURE isRelative* (path: ARRAY OF CHAR): BOOLEAN;
  107.     RETURN HOST.isRelative(path)
  108. END isRelative;
  109.  
  110.  
  111. PROCEDURE GetCurrentDirectory* (VAR path: ARRAY OF CHAR);
  112. BEGIN
  113.     HOST.GetCurrentDirectory(path)
  114. END GetCurrentDirectory;
  115.  
  116.  
  117. PROCEDURE UnixTime* (): INTEGER;
  118. VAR
  119.     year, month, day, hour, min, sec: INTEGER;
  120.     res: INTEGER;
  121.  
  122. BEGIN
  123.     IF OS = "LINUX" THEN
  124.         res := HOST.UnixTime()
  125.     ELSE
  126.         HOST.now(year, month, day, hour, min, sec);
  127.         res := UNIXTIME.time(year, month, day, hour, min, sec)
  128.     END
  129.  
  130.     RETURN res
  131. END UnixTime;
  132.  
  133.  
  134. PROCEDURE SetBitDepth* (BitDepth: INTEGER);
  135. BEGIN
  136.     ASSERT((BitDepth = 16) OR (BitDepth = 32) OR (BitDepth = 64));
  137.     bit_diff := bit_depth - BitDepth;
  138.     ASSERT(bit_diff >= 0);
  139.  
  140.     target.bit_depth := BitDepth;
  141.     target.maxSet    := BitDepth - 1;
  142.     target.maxHex    := BitDepth DIV 4;
  143.     target.minInt    := ASR(minint, bit_diff);
  144.     target.maxInt    := ASR(maxint, bit_diff);
  145.     target.maxReal   := 1.9;
  146.     PACK(target.maxReal, 1023);
  147. END SetBitDepth;
  148.  
  149.  
  150. PROCEDURE Byte* (n: INTEGER; idx: INTEGER): BYTE;
  151.     RETURN ASR(n, 8 * idx) MOD 256
  152. END Byte;
  153.  
  154.  
  155. PROCEDURE Align* (VAR bytes: INTEGER; align: INTEGER): BOOLEAN;
  156. VAR
  157.     res: BOOLEAN;
  158.  
  159. BEGIN
  160.     IF bytes MOD align # 0 THEN
  161.         res := maxint - bytes >= align - (bytes MOD align);
  162.         IF res THEN
  163.             bytes := bytes + align - (bytes MOD align)
  164.         END
  165.     ELSE
  166.         res := TRUE
  167.     END
  168.  
  169.     RETURN res
  170. END Align;
  171.  
  172.  
  173. PROCEDURE Long* (value: INTEGER): INTEGER;
  174.     RETURN ASR(LSL(value, bit_diff), bit_diff)
  175. END Long;
  176.  
  177.  
  178. PROCEDURE Short* (value: INTEGER): INTEGER;
  179.     RETURN LSR(LSL(value, bit_diff), bit_diff)
  180. END Short;
  181.  
  182.  
  183. PROCEDURE Log2* (x: INTEGER): INTEGER;
  184. VAR
  185.     n: INTEGER;
  186.  
  187. BEGIN
  188.     ASSERT(x > 0);
  189.  
  190.     n := 0;
  191.     WHILE ~ODD(x) DO
  192.         x := x DIV 2;
  193.         INC(n)
  194.     END;
  195.  
  196.     IF x # 1 THEN
  197.         n := -1
  198.     END
  199.  
  200.     RETURN n
  201. END Log2;
  202.  
  203.  
  204. BEGIN
  205.     time := GetTickCount();
  206.     COPY(HOST.eol, eol);
  207.     maxreal := 1.9;
  208.     PACK(maxreal, 1023)
  209. END UTILS.