cutils.pas 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. unit cutils;
  2. {$mode objfpc}{$H+}
  3. interface
  4. uses
  5. {$IFDEF MSWINDOWS}
  6. Windows, WinSock2,
  7. {$ELSE}
  8. BaseUnix,
  9. {$ENDIF}
  10. ctypes;
  11. const
  12. LIB_NAME = {$IFDEF MSWINDOWS}'msvcrt'{$ELSE}'c'{$ENDIF};
  13. {$IFDEF UNIX}
  14. UINT16_MAX = 65535;
  15. {$ENDIF}
  16. SEEK_SET = 0;
  17. SEEK_CUR = 1;
  18. SEEK_END = 2;
  19. {$IFDEF MSWINDOWS}
  20. DELTA_EPOCH_IN_MICROSECS: culonglong = 11644473600000000;
  21. SIGINT = 2;
  22. SIGILL = 4;
  23. SIGFPE = 8;
  24. SIGSEGV = 11;
  25. SIGTERM = 15;
  26. SIGBREAK = 21;
  27. SIGABRT = 22;
  28. {$ENDIF}
  29. type
  30. {$IFDEF UNIX}
  31. __off_t = longint;
  32. {$ENDIF}
  33. Pcchar = PAnsiChar;
  34. Ppcchar = ^Pcchar;
  35. FILEptr = ^File;
  36. seek_mode = longint;
  37. open_mode = (fopenread, fopenwrite, fappendwrite);
  38. signal_func = procedure(sig: cint); cdecl;
  39. {$IFDEF MSWINDOWS}
  40. function fpgettimeofday(tv: PTimeVal; tz: PTimeZone): cint;
  41. procedure _tzset; cdecl; external LIB_NAME name '_tzset';
  42. function _timezone: cint; cdecl; external LIB_NAME name '_timezone';
  43. function _daylight: clong; cdecl; external LIB_NAME name '__daylight';
  44. {$ENDIF}
  45. {$IFDEF UNIX}
  46. function sscanf(s: Pcchar; format: Pcchar): cint; cdecl; varargs; external LIB_NAME name 'sscanf';
  47. function lseek(fd: cint; offset: __off_t; whence: cint): __off_t; cdecl; external LIB_NAME name 'lseek';
  48. function isprint(p: Char): cint; cdecl; external LIB_NAME name 'isprint';
  49. function strdup(para1: Pcchar): Pcchar; cdecl; external LIB_NAME name 'strdup';
  50. function strchr(para1: Pcchar; para2: cint): Pcchar; cdecl; external LIB_NAME name 'strchr';
  51. function strstr(haystack: Pcchar; needle: Pcchar): Pcchar; cdecl; external LIB_NAME name 'strstr';
  52. function sprintf(s: Pcchar; format: Pcchar): cint; cdecl; varargs; external LIB_NAME name 'sprintf';
  53. function asprintf(resultp: Ppcchar; format: Pcchar): cint; cdecl; varargs; external LIB_NAME name 'asprintf';
  54. function errno: PInteger; cdecl; external LIB_NAME name '__errno_location';
  55. {$ENDIF}
  56. function memset(s: pointer; c: longint; n: size_t): pointer; cdecl; external LIB_NAME name 'memset';
  57. function snprintf(str: Pcchar; size: size_t; format: Pcchar): cint; cdecl; varargs; external LIB_NAME Name {$IFDEF MSWINDOWS}'_snprintf'{$ELSE}'snprintf'{$ENDIF};
  58. function rand: cint; cdecl; external LIB_NAME name 'rand';
  59. function strerror(errnum: cint): Pchar; cdecl; external LIB_NAME name 'strerror';
  60. function strncat(a, b: Pcchar; sz: size_t): Pchar; cdecl; external LIB_NAME name 'strncat';
  61. function strcpy(a, b: Pcchar): Pchar; cdecl; external LIB_NAME name 'strcpy';
  62. function strncmp(a, b: Pcchar; sz: size_t): cint; cdecl; external LIB_NAME name 'strncmp';
  63. function signal(sig: cint; func: signal_func): signal_func; cdecl; external LIB_NAME Name 'signal';
  64. function fopen(filename: PAnsiChar; mode: open_mode): FILEptr;
  65. procedure fclose(fp: FILEptr);
  66. function fseek(fp: FILEptr; recPos: longint; mode: seek_mode): longint;
  67. function fread(buf: pointer; recSize: longint; recCount: longint; fp: FILEptr): longint;
  68. function fwrite(buf: pointer; recSize: longint; recCount: longint; fp: FILEptr): longint;
  69. function ftell(fp: FILEptr): LongInt;
  70. function feof(fp: FILEptr): LongInt;
  71. implementation
  72. {$IFDEF MSWINDOWS}
  73. function fpgettimeofday(tv: PTimeVal; tz: PTimeZone): cint;
  74. const
  75. tzflag: cint = 0;
  76. var
  77. ft: FILETIME;
  78. tmpres: QWord = 0;
  79. begin
  80. if nil <> tv then
  81. begin
  82. GetSystemTimeAsFileTime(@ft);
  83. tmpres := tmpres or ft.dwHighDateTime;
  84. tmpres := tmpres shl 32;
  85. tmpres := tmpres or ft.dwLowDateTime;
  86. tmpres := tmpres div 10;
  87. tmpres -= DELTA_EPOCH_IN_MICROSECS;
  88. tv^.tv_sec := clong(tmpres div culong(1000000));
  89. tv^.tv_usec := clong(tmpres mod culong(1000000));
  90. end;
  91. if nil <> tz then
  92. begin
  93. if tzflag <> 1 then
  94. begin
  95. _tzset;
  96. Inc(tzflag);
  97. end;
  98. tz^.tz_minuteswest := _timezone div 60;
  99. tz^.tz_dsttime := _daylight;
  100. end;
  101. Result := 0;
  102. end;
  103. {$ENDIF}
  104. function fopen(filename: PAnsiChar; mode: open_mode): FILEptr;
  105. var
  106. fp: FILEptr;
  107. OldFileMode: Byte;
  108. begin
  109. fp := nil;
  110. OldFileMode := FileMode;
  111. GetMem(fp, SizeOf(File));
  112. Assign(fp^, StrPas(filename));
  113. {$PUSH}{$I-}
  114. case mode of
  115. fopenread:
  116. begin
  117. FileMode := 0;
  118. Reset(fp^, 1);
  119. end;
  120. fopenwrite:
  121. begin
  122. FileMode := 1;
  123. ReWrite(fp^, 1);
  124. end;
  125. fappendwrite:
  126. begin
  127. FileMode := 2;
  128. Reset(fp^, 1);
  129. if IOResult = 2 then
  130. ReWrite(fp^, 1);
  131. Seek(fp^, FileSize(fp^));
  132. end;
  133. end;
  134. FileMode := OldFileMode;
  135. {$POP}
  136. if IOResult <> 0 then
  137. begin
  138. FreeMem(fp, SizeOf(File));
  139. fp := nil;
  140. end;
  141. fopen := fp;
  142. end;
  143. procedure fclose(fp : FILEptr);
  144. begin
  145. if Assigned(fp) then
  146. begin
  147. {$PUSH}{$I-}
  148. Close(fp^);
  149. {$POP}
  150. if IOresult = 0 then
  151. FreeMem(fp, SizeOf(File));
  152. end;
  153. end;
  154. function fread(buf: Pointer; recSize: LongInt; recCount: LongInt;
  155. fp : FILEptr): LongInt;
  156. var
  157. totalSize, readcount : LongInt;
  158. begin
  159. if Assigned(buf) then
  160. begin
  161. totalSize := recCount * LongInt(recSize);
  162. {$PUSH}{$I-}{$HINTS OFF}
  163. BlockRead(fp^, buf^, totalSize, readcount);
  164. if readcount <> totalSize then
  165. fread := readcount div recSize
  166. else
  167. fread := recCount;
  168. {$POP}
  169. end
  170. else
  171. fread := 0;
  172. end;
  173. function fwrite(buf: Pointer; recSize: LongInt; recCount: LongInt;
  174. fp: FILEptr) : LongInt;
  175. var
  176. totalSize, written: LongInt;
  177. begin
  178. if Assigned(buf) then
  179. begin
  180. totalSize := recCount * LongInt(recSize);
  181. {$PUSH}{$I-}{$HINTS OFF}
  182. BlockWrite(fp^, buf^, totalSize, written);
  183. if written <> totalSize then
  184. fwrite := written div recSize
  185. else
  186. fwrite := recCount;
  187. {$POP}
  188. end
  189. else
  190. fwrite := 0;
  191. end;
  192. function fseek(fp: FILEptr; recPos: LongInt; mode: seek_mode): LongInt;
  193. begin
  194. {$PUSH}{$I-}
  195. case mode of
  196. SEEK_SET: Seek(fp^, recPos);
  197. SEEK_CUR: Seek(fp^, FilePos(fp^) + recPos);
  198. SEEK_END: Seek(fp^, FileSize(fp^) - 1 - recPos);
  199. end;
  200. {$POP}
  201. fseek := IOResult;
  202. end;
  203. function ftell(fp: FILEptr): LongInt;
  204. begin
  205. ftell := FilePos(fp^);
  206. end;
  207. function feof(fp: FILEptr): LongInt;
  208. begin
  209. feof := 0;
  210. if Assigned(fp) then
  211. if eof(fp^) then
  212. feof := 1
  213. else
  214. feof := 0;
  215. end;
  216. end.