streamio.pp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. {$IFNDEF FPC_DOTTEDUNITS}
  14. unit StreamIO;
  15. {$ENDIF FPC_DOTTEDUNITS}
  16. interface
  17. {$IFDEF FPC_DOTTEDUNITS}
  18. uses System.Classes,System.SysUtils;
  19. {$ELSE FPC_DOTTEDUNITS}
  20. uses Classes,SysUtils;
  21. {$ENDIF FPC_DOTTEDUNITS}
  22. Procedure AssignStream(var F: Textfile; Stream: TStream);
  23. Function GetStream(var F: TTextRec) : TStream;
  24. implementation
  25. ResourceString
  26. SErrNilStream = 'Can not assign file to Nil stream';
  27. Type
  28. PStream = ^TStream;
  29. { ---------------------------------------------------------------------
  30. Text IO functions
  31. ---------------------------------------------------------------------}
  32. procedure StreamRead(var F: TTextRec);
  33. begin
  34. InOutRes:=0;
  35. With F do
  36. Try
  37. Bufend:=GetStream(F).Read(BufPtr^,BufSize);
  38. BufPos:=0;
  39. except
  40. InOutRes:=100;
  41. end;
  42. end;
  43. procedure StreamWrite(var F: TTextRec );
  44. begin
  45. InOutRes:=0;
  46. with F do
  47. if (BufPos>0) then
  48. try
  49. GetStream(F).WriteBuffer(BufPtr^,BufPos);
  50. BufPos:=0;
  51. except
  52. InOutRes:=101;
  53. end;
  54. end;
  55. {$PUSH}
  56. {$WARN 5024 OFF : Parameter "$1" not used}
  57. Procedure StreamFlush(var F: TTextRec);
  58. begin
  59. InOutRes:=0;
  60. end;
  61. procedure StreamClose(var F: TTextRec);
  62. begin
  63. InOutRes:=0;
  64. end;
  65. {$POP}
  66. Procedure StreamOpen(var F: TTextRec );
  67. begin
  68. InOutRes:=0;
  69. with F do
  70. begin
  71. BufPos:=0;
  72. Bufend:=0;
  73. case Mode of
  74. fmInput:
  75. begin
  76. InOutFunc:=@StreamRead;
  77. FlushFunc:=@StreamFlush;
  78. end;
  79. fmOutput,fmAppend:
  80. begin
  81. InOutFunc:=@StreamWrite;
  82. FlushFunc:=@StreamWrite;
  83. if Mode=fmAppend then
  84. begin
  85. Mode:=fmOutput; // see comments in text.inc
  86. Try
  87. GetStream(F).Seek(0,soFromEnd);
  88. except
  89. InOutRes:=156;
  90. end;
  91. end;
  92. end;
  93. end;
  94. end;
  95. end;
  96. { ---------------------------------------------------------------------
  97. Public functions
  98. ---------------------------------------------------------------------}
  99. Procedure AssignStream(var F: Textfile; Stream : TStream);
  100. Var
  101. E : EInoutError;
  102. begin
  103. if (Stream=Nil) then
  104. begin
  105. E:=EInOutError.Create(SErrNilStream);
  106. E.ErrorCode:=6;
  107. Raise E;
  108. end;
  109. with TTextRec(F) do
  110. begin
  111. OpenFunc:=@StreamOpen;
  112. CloseFunc:=@StreamClose;
  113. Case DefaultTextLineBreakStyle Of
  114. tlbsLF: LineEnd:=#10;
  115. tlbsCRLF: LineEnd:=#13#10;
  116. tlbsCR: LineEnd:=#13;
  117. End;
  118. PStream(@UserData)^:=Stream;
  119. Mode:=fmClosed;
  120. BufSize:=SizeOf(Buffer);
  121. BufPtr:=@Buffer;
  122. Name[0]:=#0;
  123. {$ifdef FPC_HAS_FEATURE_UNICODESTRINGS}
  124. FullName := nil;
  125. {$endif FPC_HAS_FEATURE_UNICODESTRINGS}
  126. end;
  127. SetTextCodePage(F,CP_ACP);
  128. end;
  129. Function GetStream(var F: TTextRec) : TStream;
  130. begin
  131. Result:=PStream(@F.Userdata)^;
  132. end;
  133. end.