iostream.pp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. {
  2. This file is part of the Free Component Library (FCL)
  3. Copyright (c) 1999-2000 by Michael Van Canneyt and Florian Klaempfl
  4. See the file COPYING.FPC, included in this distribution,
  5. for details about the copyright.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  9. **********************************************************************}
  10. {$mode objfpc}
  11. unit iostream;
  12. interface
  13. uses Classes;
  14. type
  15. TIOSType = (iosInput,iosOutPut,iosError);
  16. EIOStreamError = class(EStreamError);
  17. TIOStream = class(THandleStream)
  18. private
  19. FType : longint;
  20. FPos : Int64;
  21. zIOSType : TIOSType;
  22. public
  23. constructor Create(aIOSType : TiosType);
  24. function Read(var Buffer; Count : LongInt) : Longint; override;
  25. function Write(const Buffer; Count : LongInt) : LongInt; override;
  26. procedure SetSize(NewSize: Longint); override;
  27. function Seek(Offset: Longint; Origin: Word): Longint; override;
  28. end;
  29. implementation
  30. const
  31. SReadOnlyStream = 'Cannot write to an input stream.';
  32. SWriteOnlyStream = 'Cannot read from an output stream.';
  33. SInvalidOperation = 'Cannot perform this operation on a IOStream.';
  34. constructor TIOStream.Create(aIOSType : TIOSType);
  35. begin
  36. {$ifdef win32}
  37. case aIOSType of
  38. iosInput : FType := StdInputHandle;
  39. iosOutput : FType := StdOutputHandle;
  40. iosError : FType := StdErrorHandle;
  41. end;
  42. {$else}
  43. FType := Ord(aIOSType);
  44. {$endif}
  45. inherited Create(FType);
  46. zIOSType := aIOSType;
  47. end;
  48. function TIOStream.Read(var Buffer; Count : LongInt) : Longint;
  49. begin
  50. if (zIOSType <> iosInput) then
  51. raise EIOStreamError.Create(SWriteOnlyStream)
  52. else begin
  53. result := inherited Read(Buffer,Count);
  54. inc(FPos,result);
  55. end;
  56. end;
  57. function TIOStream.Write(const Buffer; Count : LongInt) : LongInt;
  58. begin
  59. if (zIOSType = iosInput) then
  60. raise EIOStreamError.Create(SReadOnlyStream)
  61. else begin
  62. result := inherited Write(Buffer,Count);
  63. inc(FPos,result);
  64. end;
  65. end;
  66. procedure TIOStream.SetSize(NewSize: Longint);
  67. begin
  68. raise EIOStreamError.Create(SInvalidOperation);
  69. end;
  70. function TIOStream.Seek(Offset: Longint; Origin: Word): Longint;
  71. const
  72. BufSize = 1024;
  73. var
  74. Buf : array[1..BufSize] of Byte;
  75. begin
  76. If (Origin=soFromCurrent) and (Offset=0) then
  77. result:=FPos;
  78. { Try to fake seek by reading and discarding }
  79. if (zIOSType = iosOutput) or
  80. Not((Origin=soFromCurrent) and (Offset>=0) or
  81. ((Origin=soFrombeginning) and (OffSet>=FPos))) then
  82. Raise EIOStreamError.Create(SInvalidOperation);
  83. if Origin=soFromBeginning then
  84. Dec(Offset,FPos);
  85. While ((Offset Div BufSize)>0)
  86. and (Read(Buf,SizeOf(Buf))=BufSize) do
  87. Dec(Offset,BufSize);
  88. If (Offset>0) then
  89. Read(Buf,BufSize);
  90. Result:=FPos;
  91. end;
  92. end.