TPrintWriterUnit.pas 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. {
  2. Copyright 2005-2006 Log4Delphi Project
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. }
  13. {*----------------------------------------------------------------------------
  14. Contains the TPrintWriter class.
  15. @version 0.5
  16. @author <a href="mailto:tcmiller@users.sourceforge.net">Trevor Miller</a>
  17. ----------------------------------------------------------------------------}
  18. unit TPrintWriterUnit;
  19. {$ifdef fpc}
  20. {$mode objfpc}
  21. {$h+}
  22. {$endif}
  23. interface
  24. uses
  25. Classes;
  26. type
  27. {*----------------------------------------------------------------------------
  28. Represents a writer that wraps a stream and makes printing to the stream
  29. easy.
  30. ----------------------------------------------------------------------------}
  31. TPrintWriter = class (TObject)
  32. private
  33. FStream : TStream;
  34. public
  35. constructor Create(AStream : TStream);
  36. procedure Print(AMsg : String);
  37. procedure Println(AMsg : String);
  38. function GetPosition() : Int64;
  39. end;
  40. implementation
  41. {*----------------------------------------------------------------------------
  42. Create an instance and use the given stream object.
  43. ----------------------------------------------------------------------------}
  44. constructor TPrintWriter.Create(AStream : TStream);
  45. begin
  46. inherited Create;
  47. Self.FStream := AStream;
  48. end;
  49. {*----------------------------------------------------------------------------
  50. Print the given message to the stream.
  51. @param AMsg The message to print
  52. ----------------------------------------------------------------------------}
  53. procedure TPrintWriter.Print(AMsg : String);
  54. begin
  55. if (AMsg <> '') then
  56. // See http://sourceforge.net/projects/log4delphi/forums/forum/486124/topic/3270609
  57. {$IFDEF UNICODE}
  58. FStream.Write(PAnsiChar(UTF8String(AMsg))^, Length(UTF8String(AMsg)));
  59. {$ELSE}
  60. FStream.Write(PChar(AMsg)^, Length(AMsg));
  61. {$ENDIF}
  62. end;
  63. {*----------------------------------------------------------------------------
  64. Print the given message to the stream followed by a newline character.
  65. @param AMsg The message to print
  66. ----------------------------------------------------------------------------}
  67. procedure TPrintWriter.Println(AMsg : String);
  68. begin
  69. if (AMsg <> '') then
  70. Print(AMsg+#13#10);
  71. end;
  72. function TPrintWriter.GetPosition() : Int64;
  73. begin
  74. Result := Self.FStream.Position;
  75. end;
  76. end.