filectrl.inc 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. {
  2. System independent filecontrol interface for linux
  3. $Id$
  4. }
  5. uses
  6. Linux;
  7. function OpenFileStr(FName: PChar; Flags: Longint): TFileHandle; [ alias: 'OpenFile' ];
  8. var
  9. RC : longint;
  10. Todo: TErrorHandlerReturnValue;
  11. begin
  12. repeat
  13. OpenFileStr:=fdOpen(FName, Flags, FilePerms);
  14. RC:=LinuxError;
  15. if (RC > 0) then
  16. Todo := ErrorHandler(RC, FName);
  17. until (RC <= 0) or (Todo <> errRetry);
  18. end;
  19. function CreateFileStr(FName: PChar): TFileHandle; [ alias: 'CreateFile' ];
  20. const
  21. O_RDONLY = 0;
  22. O_WRONLY = 1;
  23. O_RDWR = 2;
  24. O_CREATE = 64;
  25. O_EXCL = 128;
  26. O_NOCTTY = 256;
  27. O_TRUNC = 512;
  28. O_APPEND = 1024;
  29. begin
  30. CreateFileStr := OpenFileStr(FName, O_RDWR+O_CREATE+O_TRUNC);
  31. end;
  32. procedure CloseFile(Handle: TFileHandle);
  33. var
  34. RC: Longint;
  35. Todo: TErrorHandlerReturnValue;
  36. begin
  37. repeat
  38. fdClose(Handle);
  39. RC := LinuxError;
  40. if (RC > 0) then
  41. Todo := ErrorHandler(RC, nil);
  42. until (RC <= 0) or (Todo <> errRetry);
  43. end;
  44. function SeekFile(Handle: TFileHandle; Pos: TFileInt; SeekType: Word): TFileInt;
  45. var
  46. RC: Longint;
  47. Todo: TErrorHandlerReturnValue;
  48. begin
  49. repeat
  50. RC := -fdSeek(Handle, Pos, SeekType);
  51. if (RC > 0) then
  52. Todo := ErrorHandler(RC, nil);
  53. until (RC <= 0) or (Todo <> errRetry);
  54. SeekFile := -RC;
  55. end;
  56. procedure DeleteFileStr(FName: PChar); [ alias: 'DeleteFile' ];
  57. var
  58. RC: Longint;
  59. Todo: TErrorHandlerReturnValue;
  60. begin
  61. repeat
  62. UnLink(FName);
  63. RC:=LinuxError;
  64. if (RC > 0) then
  65. Todo := ErrorHandler(RC, nil);
  66. until (RC <= 0) or (Todo <> errRetry);
  67. end;
  68. function ReadFile(Handle: TFileHandle; var Buff; Count: CPUWord): CPUWord;
  69. var
  70. RC: Longint;
  71. BytesRead: LongInt;
  72. Todo: TErrorHandlerReturnValue;
  73. begin
  74. repeat
  75. BytesRead := fdRead(Handle, Buff, Count);
  76. RC:=LinuxError;
  77. if (RC > 0) then
  78. Todo := ErrorHandler(RC, nil);
  79. until (RC <= 0) or (Todo <> errRetry);
  80. if (RC > 0) then
  81. ReadFile := 0
  82. else
  83. ReadFile := BytesRead;
  84. end;
  85. function WriteFile(Handle: TFileHandle; var Buff; Count: CPUWord): CPUWord;
  86. var
  87. RC: Longint;
  88. BytesWritten: LongInt;
  89. Todo: TErrorHandlerReturnValue;
  90. begin
  91. repeat
  92. BytesWritten := fdWrite(Handle, Buff, Count);
  93. RC:=LinuxError;
  94. if (RC > 0) then
  95. Todo := ErrorHandler(RC, nil);
  96. until (RC <= 0) or (Todo <> errRetry);
  97. if (RC > 0) then
  98. WriteFile := 0
  99. else
  100. WriteFile := BytesWritten;
  101. end;
  102. { The following two routines should go to syscalls... }
  103. procedure FlushFile(Handle: TFileHandle);
  104. var
  105. RC: Longint;
  106. Todo: TErrorHandlerReturnValue;
  107. begin
  108. repeat
  109. fdFlush(Handle);
  110. RC:=LinuxError;
  111. if (RC > 0) then
  112. Todo := ErrorHandler(RC, nil);
  113. until (RC <= 0) or (Todo <> errRetry);
  114. end;
  115. procedure TruncateFile(Handle: TFileHandle);
  116. var
  117. RC: Longint;
  118. Todo: TErrorHandlerReturnValue;
  119. begin
  120. repeat
  121. fdTruncate(Handle,0);
  122. RC:=LinuxError;
  123. if (RC > 0) then
  124. Todo := ErrorHandler(RC, nil);
  125. until (RC <= 0) or (Todo <> errRetry);
  126. end;
  127. function EndOfFile(Handle: TFileHandle): Boolean;
  128. begin
  129. EndOfFile := FilePos(Handle) >= FileSize(Handle);
  130. end;
  131. function FilePos(Handle: TFileHandle): TFileInt;
  132. begin
  133. FilePos := SeekFile(Handle, 0, skCur);
  134. end;
  135. function FileSize(Handle: TFileHandle): TFileInt;
  136. var
  137. L: Longint;
  138. begin
  139. L := FilePos(Handle);
  140. FileSize := SeekFile(Handle, 0, skEnd);
  141. SeekFile(Handle, L, skBeg);
  142. end;
  143. {
  144. $Log$
  145. Revision 1.2 2000-07-13 11:32:24 michael
  146. + removed logs
  147. }