iostream.pp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. {$mode objfpc}
  12. unit iostream;
  13. Interface
  14. Uses Classes;
  15. Type
  16. TiosType = (iosInput,iosOutPut,iosError);
  17. EIOStreamError = Class(EStreamError);
  18. TIOStream = Class(THandleStream)
  19. Private
  20. FType,
  21. FPos : Longint;
  22. Public
  23. Constructor Create(IOSType : 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(IOSType : TiosType);
  35. begin
  36. {$ifdef win32}
  37. Case IOSType of
  38. iosOutput : FType:=Stdoutputhandle;
  39. iosInput : FType:=Stdinputhandle;
  40. iosError : FType:=StdErrorHandle;
  41. end;
  42. {$else}
  43. FType:=Ord(IOSType);
  44. {$endif}
  45. Inherited Create(Ftype);
  46. end;
  47. Function TIOStream.Read(var Buffer; Count: Longint): Longint;
  48. begin
  49. If Ftype>0 then
  50. Raise EIOStreamError.Create(SWriteOnlyStream)
  51. else
  52. 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 Ftype=0 then
  60. Raise EIOStreamError.Create(SReadOnlyStream)
  61. else
  62. begin
  63. Result:=Inherited Write(Buffer,Count);
  64. Inc(FPos,Result);
  65. end;
  66. end;
  67. Procedure TIOStream.SetSize(NewSize: Longint);
  68. begin
  69. Raise EIOStreamError.Create(SInvalidOperation);
  70. end;
  71. Function TIOStream.Seek(Offset: Longint; Origin: Word): Longint;
  72. Const BufSize = 100;
  73. Var Buf : array[1..BufSize] of Byte;
  74. begin
  75. If (Origin=soFromCurrent) and (Offset=0) then
  76. result:=FPos;
  77. { Try to fake seek by reading and discarding }
  78. if (Ftype>0) or
  79. Not((Origin=soFromCurrent) and (Offset>=0) or
  80. ((Origin=soFrombeginning) and (OffSet>=FPos))) then
  81. Raise EIOStreamError.Create(SInvalidOperation);
  82. if Origin=soFromBeginning then
  83. Dec(Offset,FPos);
  84. While ((Offset Div BufSize)>0)
  85. and (Read(Buf,SizeOf(Buf))=BufSize) do
  86. Dec(Offset,BufSize);
  87. If (Offset>0) then
  88. Read(Buf,BufSize);
  89. Result:=FPos;
  90. end;
  91. end.
  92. {
  93. $Log$
  94. Revision 1.3 2002-09-07 15:15:24 peter
  95. * old logs removed and tabs fixed
  96. }