home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1998 April A / Pcwk4a98.iso / PROGRAM / DELPHI16 / Calmira / Src / UTILS / STREAMER.PAS < prev    next >
Pascal/Delphi Source File  |  1997-02-15  |  2KB  |  82 lines

  1. {*********************************************************}
  2. {                                                         }
  3. {    Calmira System Library 1.0                           }
  4. {    by Li-Hsin Huang,                                    }
  5. {    released into the public domain January 1997         }
  6. {                                                         }
  7. {*********************************************************}
  8.  
  9. unit Streamer;
  10.  
  11. { Combines Delphi's new stream system with some of Turbo Vision's
  12.   useful stream facilities.  TStreamer avoids the need to use
  13.   a TFiler object, and stores data directly to a binary file.
  14.  
  15.   Of course, it is unbuffered, but for small files, this is
  16.   unecessary.  A more serious issue might be the lack of error
  17.   checking, but then TStreamer can interpret any binary data file
  18.   whereas TReader needs to find markers left by TWriter }
  19.  
  20. interface
  21.  
  22. uses Classes;
  23.  
  24. type
  25.   TStreamer = class(TFileStream)
  26.   public
  27.     procedure WriteInteger(i: Integer);
  28.     procedure WriteBoolean(b: Boolean);
  29.     procedure WriteString(const s : string);
  30.     procedure WriteChar(c: Char);
  31.     function ReadInteger: Integer;
  32.     function ReadBoolean: Boolean;
  33.     function ReadString: string;
  34.     function ReadChar: Char;
  35.   end;
  36.  
  37. implementation
  38.  
  39. procedure TStreamer.WriteInteger(i: Integer);
  40. begin
  41.   Write(i, SizeOf(i));
  42. end;
  43.  
  44. procedure TStreamer.WriteBoolean(b: Boolean);
  45. begin
  46.   Write(b, SizeOf(b));
  47. end;
  48.  
  49. procedure TStreamer.WriteString(const s: string);
  50. begin
  51.   Write(s[0], SizeOf(s[0]));
  52.   Write(s[1], Ord(s[0]));
  53. end;
  54.  
  55. procedure TStreamer.WriteChar(c: Char);
  56. begin
  57.   Write(c, SizeOf(c));
  58. end;
  59.  
  60. function TStreamer.ReadInteger: Integer;
  61. begin
  62.   Read(Result, SizeOf(Result));
  63. end;
  64.  
  65. function TStreamer.ReadBoolean: Boolean;
  66. begin
  67.   Read(Result, SizeOf(Result));
  68. end;
  69.  
  70. function TStreamer.ReadString: string;
  71. begin
  72.   Read(Result[0], SizeOf(Result[0]));
  73.   Read(Result[1], Ord(Result[0]));
  74. end;
  75.  
  76. function TStreamer.ReadChar: Char;
  77. begin
  78.   Read(Result, SizeOf(Result));
  79. end;
  80.  
  81. end.
  82.