streamio.pp 2.9 KB

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