streamio.pp 2.7 KB

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