iostream.pp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. {
  2. $Id$
  3. This file is part of the Free Component Library (FCL)
  4. Copyright (c) 1999-2000 by Michael Van Canneyt and Florian Klaempfl
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  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,
  20. FPos : Longint;
  21. Public
  22. Constructor Create(IOSType : TiosType);
  23. Function Read(var Buffer; Count: Longint): Longint;override;
  24. Function Write(const Buffer; Count: Longint): Longint;override;
  25. Procedure SetSize(NewSize: Longint); override;
  26. Function Seek(Offset: Longint; Origin: Word): Longint; override;
  27. end;
  28. Implementation
  29. Const
  30. SReadOnlyStream = 'Cannot write to an input stream.';
  31. SWriteOnlyStream = 'Cannot read from an output stream.';
  32. SInvalidOperation = 'Cannot perform this operation on a IOStream.';
  33. Constructor TIOStream.Create(IOSType : TiosType);
  34. begin
  35. FType:=Ord(IOSType);
  36. Inherited Create(Ftype);
  37. end;
  38. Function TIOStream.Read(var Buffer; Count: Longint): Longint;
  39. begin
  40. If Ftype>0 then
  41. Raise EIOStreamError.Create(SWriteOnlyStream)
  42. else
  43. begin
  44. Result:=Inherited Read(Buffer,Count);
  45. Inc(FPos,Result);
  46. end;
  47. end;
  48. Function TIOStream.Write(const Buffer; Count: Longint): Longint;
  49. begin
  50. If Ftype=0 then
  51. Raise EIOStreamError.Create(SReadOnlyStream)
  52. else
  53. begin
  54. Result:=Inherited Write(Buffer,Count);
  55. Inc(FPos,Result);
  56. end;
  57. end;
  58. Procedure TIOStream.SetSize(NewSize: Longint);
  59. begin
  60. Raise EIOStreamError.Create(SInvalidOperation);
  61. end;
  62. Function TIOStream.Seek(Offset: Longint; Origin: Word): Longint;
  63. Const BufSize = 100;
  64. Var Buf : array[1..BufSize] of Byte;
  65. begin
  66. If (Origin=soFromCurrent) and (Offset=0) then
  67. result:=FPos;
  68. { Try to fake seek by reading and discarding }
  69. if (Ftype>0) or
  70. Not((Origin=soFromCurrent) and (Offset>=0) or
  71. ((Origin=soFrombeginning) and (OffSet>=FPos))) then
  72. Raise EIOStreamError.Create(SInvalidOperation);
  73. if Origin=soFromBeginning then
  74. Dec(Offset,FPos);
  75. While ((Offset Div BufSize)>0)
  76. and (Read(Buf,SizeOf(Buf))=BufSize) do
  77. Dec(Offset,BufSize);
  78. If (Offset>0) then
  79. Read(Buf,BufSize);
  80. Result:=FPos;
  81. end;
  82. end.