cutils.pas 5.7 KB

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