lmimestreams.pp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. { MIME Streams
  2. CopyRight (C) 2006-2008 Micha Nelissen
  3. This library is Free software; you can rediStribute it and/or modify it
  4. under the terms of the GNU Library General Public License as published by
  5. the Free Software Foundation; either version 2 of the License, or (at your
  6. option) any later version.
  7. This program is diStributed in the hope that it will be useful, but WITHOUT
  8. ANY WARRANTY; withOut even the implied warranty of MERCHANTABILITY or
  9. FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License
  10. for more details.
  11. You should have received a Copy of the GNU Library General Public License
  12. along with This library; if not, Write to the Free Software Foundation,
  13. Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  14. This license has been modified. See File LICENSE.ADDON for more inFormation.
  15. Should you find these sources without a LICENSE File, please contact
  16. me at [email protected]
  17. }
  18. unit lMimeStreams;
  19. {$mode objfpc}{$H+}
  20. interface
  21. uses
  22. Classes;
  23. const
  24. CRLF = #13#10;
  25. type
  26. TStreamNotificationEvent = procedure(const aSize: Integer) of object;
  27. { TMimeOutputStream }
  28. TMimeOutputStream = class(TStream)
  29. protected
  30. FInputData: string;
  31. FNotificationEvent: TStreamNotificationEvent;
  32. function GetSize: Int64; override;
  33. procedure AddInputData(const s: string);
  34. public
  35. constructor Create(aNotificationEvent: TStreamNotificationEvent);
  36. function Read(var Buffer; Count: Longint): Longint; override;
  37. function Write(const Buffer; Count: Longint): Longint; override;
  38. function Seek(Offset: Longint; Origin: Word): Longint; overload; override;
  39. function Seek(const Offset: Int64; Origin: TSeekOrigin): Int64; overload; override;
  40. procedure Reset;
  41. end;
  42. { TBogusStream }
  43. TBogusStream = class(TStream)
  44. protected
  45. FData: string;
  46. function GetSize: Int64; override;
  47. public
  48. function Read(var Buffer; Count: Longint): Longint; override;
  49. function Write(const Buffer; Count: Longint): Longint; override;
  50. function Seek(Offset: Longint; Origin: Word): Longint; overload; override;
  51. function Seek(const Offset: Int64; Origin: TSeekOrigin): Int64; overload; override;
  52. procedure Reset;
  53. end;
  54. function EncodeMimeHeaderText(const s: string): string;
  55. implementation
  56. uses
  57. Math;
  58. type
  59. TByteArray = array of Byte;
  60. function EncodeMimeHeaderText(const s: string): string;
  61. begin
  62. Result := s;
  63. end;
  64. { TMimeOutputStream }
  65. function TMimeOutputStream.GetSize: Int64;
  66. begin
  67. Result := Length(FInputData);
  68. end;
  69. procedure TMimeOutputStream.AddInputData(const s: string);
  70. { function RightPos(const What, Where: string): Integer;
  71. var
  72. i, j: Integer;
  73. begin
  74. Result := 0;
  75. j := Length(What);
  76. for i := Length(Where) downto 1 do
  77. if Where[i] = What[j] then begin
  78. Dec(j);
  79. if j = 0 then Exit(i);
  80. end else
  81. j := Length(What);
  82. end;
  83. var
  84. n: Integer;}
  85. begin
  86. { n := RightPos(CRLF, s);
  87. if n > 0 then
  88. Inc(FLastCRLF, (Length(FInputData) - FLastCRLF) + n);}
  89. FInputData := FInputData + s;
  90. { while Length(FInputData) - FLastCRLF >= 74 do begin
  91. Insert(CRLF, FInputData, FLastCRLF + 75);
  92. Inc(FLastCRLF, 77);
  93. end;}
  94. end;
  95. constructor TMimeOutputStream.Create(aNotificationEvent: TStreamNotificationEvent);
  96. begin
  97. inherited Create;
  98. FNotificationEvent := aNotificationEvent;
  99. end;
  100. function TMimeOutputStream.Read(var Buffer; Count: Longint): Longint;
  101. begin
  102. if Assigned(FNotificationEvent) then
  103. FNotificationEvent(Count);
  104. Result := Min(Count, Length(FInputData));
  105. if Result <= 0 then
  106. Exit(0);
  107. Move(FInputData[1], Buffer, Result);
  108. Delete(FInputData, 1, Result);
  109. end;
  110. function TMimeOutputStream.Write(const Buffer; Count: Longint): Longint;
  111. var
  112. s: string;
  113. begin
  114. SetLength(s, Count);
  115. Move(Buffer, s[1], Count);
  116. AddInputData(s);
  117. Result := Count;
  118. end;
  119. function TMimeOutputStream.Seek(Offset: Longint; Origin: Word): Longint;
  120. begin
  121. Result := Offset;
  122. end;
  123. function TMimeOutputStream.Seek(const Offset: Int64; Origin: TSeekOrigin
  124. ): Int64;
  125. begin
  126. Result := Offset;
  127. end;
  128. procedure TMimeOutputStream.Reset;
  129. begin
  130. FInputData := '';
  131. end;
  132. { TBogusStream }
  133. function TBogusStream.GetSize: Int64;
  134. begin
  135. Result := Length(FData);
  136. end;
  137. function TBogusStream.Read(var Buffer; Count: Longint): Longint;
  138. begin
  139. Result := Min(Count, Length(FData));
  140. Move(FData[1], Buffer, Result);
  141. Delete(FData, 1, Result);
  142. end;
  143. function TBogusStream.Write(const Buffer; Count: Longint): Longint;
  144. var
  145. l: Integer;
  146. begin
  147. l := Length(FData);
  148. Result := Count;
  149. SetLength(FData, l + Count);
  150. Inc(l);
  151. Move(Buffer, FData[l], Count);
  152. end;
  153. function TBogusStream.Seek(Offset: Longint; Origin: Word): Longint;
  154. begin
  155. Result := Offset;
  156. end;
  157. function TBogusStream.Seek(const Offset: Int64; Origin: TSeekOrigin): Int64;
  158. begin
  159. Result := Offset;
  160. end;
  161. procedure TBogusStream.Reset;
  162. begin
  163. FData := '';
  164. end;
  165. end.