iostream.pp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. zIOSType : TIOSType;
  23. public
  24. constructor Create(aIOSType : TiosType);
  25. function Read(var Buffer; Count : LongInt) : Longint; override;
  26. function Write(const Buffer; Count : LongInt) : LongInt; override;
  27. procedure SetSize(NewSize: Longint); override;
  28. function Seek(Offset: Longint; Origin: Word): Longint; override;
  29. end;
  30. implementation
  31. const
  32. SReadOnlyStream = 'Cannot write to an input stream.';
  33. SWriteOnlyStream = 'Cannot read from an output stream.';
  34. SInvalidOperation = 'Cannot perform this operation on a IOStream.';
  35. constructor TIOStream.Create(aIOSType : TIOSType);
  36. begin
  37. {$ifdef win32}
  38. case aIOSType of
  39. iosInput : FType := StdInputHandle;
  40. iosOutput : FType := StdOutputHandle;
  41. iosError : FType := StdErrorHandle;
  42. end;
  43. {$else}
  44. FType := Ord(aIOSType);
  45. {$endif}
  46. inherited Create(FType);
  47. zIOSType := aIOSType;
  48. end;
  49. function TIOStream.Read(var Buffer; Count : LongInt) : Longint;
  50. begin
  51. if (zIOSType = iosOutput) then
  52. raise EIOStreamError.Create(SWriteOnlyStream)
  53. else begin
  54. result := inherited Read(Buffer,Count);
  55. inc(FPos,result);
  56. end;
  57. end;
  58. function TIOStream.Write(const Buffer; Count : LongInt) : LongInt;
  59. begin
  60. if (zIOSType = iosInput) then
  61. raise EIOStreamError.Create(SReadOnlyStream)
  62. else 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
  73. BufSize = 100;
  74. var
  75. Buf : array[1..BufSize] of Byte;
  76. begin
  77. If (Origin=soFromCurrent) and (Offset=0) then
  78. result:=FPos;
  79. { Try to fake seek by reading and discarding }
  80. if (zIOSType = iosOutput) or
  81. Not((Origin=soFromCurrent) and (Offset>=0) or
  82. ((Origin=soFrombeginning) and (OffSet>=FPos))) then
  83. Raise EIOStreamError.Create(SInvalidOperation);
  84. if Origin=soFromBeginning then
  85. Dec(Offset,FPos);
  86. While ((Offset Div BufSize)>0)
  87. and (Read(Buf,SizeOf(Buf))=BufSize) do
  88. Dec(Offset,BufSize);
  89. If (Offset>0) then
  90. Read(Buf,BufSize);
  91. Result:=FPos;
  92. end;
  93. end.
  94. {
  95. $Log$
  96. Revision 1.5 2005-02-14 17:13:15 peter
  97. * truncate log
  98. Revision 1.4 2005/02/14 16:39:51 peter
  99. * fixed stdinput reading under win32
  100. }