iostream.pp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 }
  18. TIOStream = class(THandleStream)
  19. private
  20. FType : longint;
  21. FPos : Int64;
  22. zIOSType : TIOSType;
  23. protected
  24. procedure SetSize(const NewSize: Int64); override;
  25. function GetPosition: Int64; override;
  26. procedure InvalidSeek; override;
  27. public
  28. constructor Create(aIOSType : TiosType);
  29. function Read(var Buffer; Count : LongInt) : Longint; override;
  30. function Write(const Buffer; Count : LongInt) : LongInt; override;
  31. function Seek(const Offset: int64; Origin: TSeekOrigin): int64; override;
  32. end;
  33. implementation
  34. const
  35. SReadOnlyStream = 'Cannot write to an input stream.';
  36. SWriteOnlyStream = 'Cannot read from an output stream.';
  37. SInvalidOperation = 'Cannot perform this operation on a IOStream.';
  38. procedure TIOStream.SetSize(const NewSize: Int64);
  39. begin
  40. raise EIOStreamError.Create(SInvalidOperation);
  41. end;
  42. function TIOStream.GetPosition: Int64;
  43. begin
  44. Result:=FPos;
  45. end;
  46. procedure TIOStream.InvalidSeek;
  47. begin
  48. raise EIOStreamError.Create(SInvalidOperation);
  49. end;
  50. constructor TIOStream.Create(aIOSType : TIOSType);
  51. begin
  52. {$ifdef windows}
  53. case aIOSType of
  54. iosInput : FType := StdInputHandle;
  55. iosOutput : FType := StdOutputHandle;
  56. iosError : FType := StdErrorHandle;
  57. end;
  58. {$else}
  59. FType := Ord(aIOSType);
  60. {$endif}
  61. inherited Create(FType);
  62. zIOSType := aIOSType;
  63. end;
  64. function TIOStream.Read(var Buffer; Count : LongInt) : Longint;
  65. begin
  66. if (zIOSType <> iosInput) then
  67. raise EIOStreamError.Create(SWriteOnlyStream)
  68. else begin
  69. result := inherited Read(Buffer,Count);
  70. inc(FPos,result);
  71. end;
  72. end;
  73. function TIOStream.Write(const Buffer; Count : LongInt) : LongInt;
  74. begin
  75. if (zIOSType = iosInput) then
  76. raise EIOStreamError.Create(SReadOnlyStream)
  77. else begin
  78. result := inherited Write(Buffer,Count);
  79. inc(FPos,result);
  80. end;
  81. end;
  82. function TIOStream.Seek(const Offset: int64; Origin: TSeekOrigin): int64;
  83. begin
  84. if (Origin=soCurrent) and (Offset=0) then
  85. Result:=FPos
  86. else
  87. begin
  88. if zIOSType in [iosOutput,iosError] then
  89. InvalidSeek;
  90. FakeSeekForward(Offset,Origin,FPos);
  91. Result:=FPos;
  92. end;
  93. end;
  94. end.