streamio.pp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. {
  2. $Id$
  3. This file is part of the Free Component Library (FCL)
  4. Copyright (c) 1999-2000 by the Free Pascal development team
  5. This unit converts a stream to a regular text file.
  6. See the file COPYING.FPC, included in this distribution,
  7. for details about the copyright.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. **********************************************************************}
  12. unit StreamIO;
  13. interface
  14. uses Classes,SysUtils;
  15. Procedure AssignStream(var F: Textfile; Stream: TStream);
  16. Function GetStream(var F: TTextRec) : TStream;
  17. implementation
  18. ResourceString
  19. SErrNilStream = 'Can not assign file to Nil stream';
  20. Type
  21. PStream = ^TStream;
  22. { ---------------------------------------------------------------------
  23. Text IO functions
  24. ---------------------------------------------------------------------}
  25. Function StreamRead(var F: TTextRec) : longint;
  26. begin
  27. Result:=0;
  28. With F do
  29. Try
  30. Bufend:=GetStream(F).Read(BufPtr^,BufSize);
  31. BufPos:=0;
  32. except
  33. Result:=100;
  34. end;
  35. end;
  36. Function StreamWrite(var F: TTextRec ): longint;
  37. begin
  38. Result:=0;
  39. with F do
  40. if (BufPos>0) then
  41. try
  42. GetStream(F).WriteBuffer(BufPtr^,BufPos);
  43. BufPos:=0;
  44. except
  45. Result:=101;
  46. end;
  47. end;
  48. Function StreamFlush(var F: TTextRec): longint;
  49. begin
  50. Result:=0;
  51. end;
  52. Function StreamClose(var F: TTextRec): longint;
  53. begin
  54. Result:=0;
  55. end;
  56. Function StreamOpen(var F: TTextRec ): longint;
  57. begin
  58. Result := 0;
  59. with F do
  60. begin
  61. BufPos:=0;
  62. Bufend:=0;
  63. case Mode of
  64. fmInput:
  65. begin
  66. InOutFunc:=@StreamRead;
  67. FlushFunc:=@StreamFlush;
  68. end;
  69. fmOutput,fmAppend:
  70. begin
  71. InOutFunc:=@StreamWrite;
  72. FlushFunc:=@StreamWrite;
  73. if mode=fmAppend then
  74. Try
  75. GetStream(F).Seek(0,soFromEnd);
  76. except
  77. Result:=156;
  78. end;
  79. end;
  80. end;
  81. end;
  82. end;
  83. { ---------------------------------------------------------------------
  84. Public functions
  85. ---------------------------------------------------------------------}
  86. Procedure AssignStream(var F: Textfile; Stream : TStream);
  87. Var
  88. E : EInoutError;
  89. begin
  90. if (Stream=Nil) then
  91. begin
  92. E:=EInOutError.Create(SErrNilStream);
  93. E.ErrorCode:=6;
  94. Raise E;
  95. end;
  96. with TTextRec(F) do
  97. begin
  98. OpenFunc:=@StreamOpen;
  99. CloseFunc:=@StreamClose;
  100. PStream(@UserData)^:=Stream;
  101. Mode:=fmClosed;
  102. BufSize:=SizeOf(Buffer);
  103. BufPtr:=@Buffer;
  104. Name[0]:=#0;
  105. end;
  106. end;
  107. Function GetStream(var F: TTextRec) : TStream;
  108. begin
  109. Result:=PStream(@F.Userdata)^;
  110. end;
  111. end.