{*************************************************************************}
{                                                                         }
{ Stream_Ex.pas - convinient stream processing classes                    }
{                                                                         }
{ © Parfenov Alexey Andreevich, 2007                                      }
{                                                                         }
{ e-mail: AlkatrazStudio@yandex.ru                                        }
{                                                                         }
{ This library is free software; you can redistribute it and/or           }
{ modify it under the terms of the GNU General Public License             }
{ as published by the Free Software Foundation; either version 2.1 of     }
{ the License, or (at your option) any later version.                     }
{                                                                         }
{ This library is distributed in the hope that it will be useful,         }
{ but WITHOUT ANY WARRANTY; without even the implied warranty of          }
{ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU        }
{ General Public License for more details.                                }
{                                                                         }
{ You may read GNU General Public License at:                             }
{   http://www.gnu.org/copyleft/gpl.html                                  }
{                                                                         }
{ last modified: 25.04.2009                                               }
{*************************************************************************}
unit Stream_Ex;
interface
uses classes,sysutils;

type TFileStreamEx=class(TFileStream)
  public
    procedure WriteInteger(i:Integer);
    procedure WriteByte(b:Byte);
    procedure WriteBoolean(b:Boolean);
    procedure WriteCardinal(c:Cardinal);
    procedure WriteSingle(s:Single);
    procedure WriteSmallint(i:Smallint);
    procedure WriteShortInt(i:Shortint);
    procedure WriteWord(w:Word);
    procedure WriteChar(c:Char);
    procedure WriteString(s:String);
    procedure WriteStringList(s:String);
    procedure WriteInt64(i:Int64);
    function ReadInteger:Integer;
    function ReadByte:Byte;
    function ReadBoolean:Boolean;
    function ReadCardinal:Cardinal;
    function ReadSingle:Single;
    function ReadSmallint:Smallint;
    function ReadShortint:Shortint;
    function ReadWord:Word;
    function ReadChar:Char;
    function ReadString:String;
    function ReadStringList:String;
    function ReadInt64:Int64;
end;

type TStringStreamEx=class(TStringStream)
  public
    procedure WriteInteger(i:Integer);
    procedure WriteByte(b:Byte);
    procedure WriteBoolean(b:Boolean);
    procedure WriteCardinal(c:Cardinal);
    procedure WriteSingle(s:Single);
    procedure WriteSmallint(i:Smallint);
    procedure WriteShortInt(i:Shortint);
    procedure WriteWord(w:Word);
    procedure WriteChar(c:Char);
    procedure WriteString(s:String);
    procedure WriteStringList(s:String);
    procedure WriteInt64(i:Int64);
    function ReadInteger:Integer;
    function ReadByte:Byte;
    function ReadBoolean:Boolean;
    function ReadCardinal:Cardinal;
    function ReadSingle:Single;
    function ReadSmallint:Smallint;
    function ReadShortint:Shortint;
    function ReadWord:Word;
    function ReadChar:Char;
    function ReadString:String;
    function ReadStringList:String;
    function ReadInt64:Int64;
end;

type TMemoryStreamEx=class(TMemoryStream)
  public
    procedure WriteInteger(i:Integer);
    procedure WriteByte(b:Byte);
    procedure WriteBoolean(b:Boolean);
    procedure WriteCardinal(c:Cardinal);
    procedure WriteSingle(s:Single);
    procedure WriteSmallint(i:Smallint);
    procedure WriteShortInt(i:Shortint);
    procedure WriteWord(w:Word);
    procedure WriteChar(c:Char);
    procedure WriteString(s:String);
    procedure WriteStringList(s:String);
    procedure WriteInt64(i:Int64);
    function ReadInteger:Integer;
    function ReadByte:Byte;
    function ReadBoolean:Boolean;
    function ReadCardinal:Cardinal;
    function ReadSingle:Single;
    function ReadSmallint:Smallint;
    function ReadShortint:Shortint;
    function ReadWord:Word;
    function ReadChar:Char;
    function ReadString:String;
    function ReadStringList:String;
    function ReadInt64:Int64;
end;

implementation

{ TFileStreamEx }

function TFileStreamEx.ReadBoolean:Boolean;begin ReadBuffer(Result,1);end;
function TFileStreamEx.ReadByte:Byte;begin ReadBuffer(Result,1);end;
function TFileStreamEx.ReadCardinal:Cardinal;begin ReadBuffer(Result,4);end;
function TFileStreamEx.ReadChar:Char;begin ReadBuffer(Result,1);end;
function TFileStreamEx.ReadInteger:Integer;begin ReadBuffer(Result,4);end;
function TFileStreamEx.ReadSingle:Single;begin ReadBuffer(Result,4);end;
function TFileStreamEx.ReadSmallint:Smallint;begin ReadBuffer(Result,2);end;
function TFileStreamEx.ReadShortint:Shortint;begin ReadBuffer(Result,1);end;
function TFileStreamEx.ReadWord:Word;begin ReadBuffer(Result,2);end;
function TFileStreamEx.ReadInt64:Int64;begin ReadBuffer(Result,8);end;
procedure TFileStreamEx.WriteBoolean(b:Boolean);begin WriteBuffer(b,1);end;
procedure TFileStreamEx.WriteByte(b:Byte);begin WriteBuffer(b,1);end;
procedure TFileStreamEx.WriteCardinal(c:Cardinal);begin WriteBuffer(c,4);end;
procedure TFileStreamEx.WriteChar(c:Char);begin WriteBuffer(c,1);end;
procedure TFileStreamEx.WriteInteger(i:Integer);begin WriteBuffer(i,4);end;
procedure TFileStreamEx.WriteSingle(s:Single);begin WriteBuffer(s,4);end;
procedure TFileStreamEx.WriteSmallint(i:Smallint);begin WriteBuffer(i,2);end;
procedure TFileStreamEx.WriteShortInt(i:Shortint);begin WriteBuffer(i,1);end;
procedure TFileStreamEx.WriteWord(w:Word);begin WriteBuffer(w,2);end;
procedure TFileStreamEx.WriteInt64(i:Int64);begin WriteBuffer(i,8);end;

function TFileStreamEx.ReadString:String;
var c:char;
begin
  result:='';repeat ReadBuffer(c,1);if c<>#0 then result:=result+c;until c=#0;
end;

function TFileStreamEx.ReadStringList: String;
begin
  result:=StringReplace(ReadString,#13,#13#10,[rfReplaceAll]);
end;

procedure TFileStreamEx.WriteString(s:String);
var
  a,n:integer;e:byte;
begin
  e:=0;n:=length(s);if n>0 then for a:=1 to Length(s) do WriteBuffer(s[a],1);WriteBuffer(e,1);
end;

procedure TFileStreamEx.WriteStringList(s: String);
begin
  WriteString(StringReplace(s,#13#10,#13,[rfReplaceAll]));
end;

{ TStringStream }

function TStringStreamEx.ReadBoolean:Boolean;begin ReadBuffer(Result,1);end;
function TStringStreamEx.ReadByte:Byte;begin ReadBuffer(Result,1);end;
function TStringStreamEx.ReadCardinal:Cardinal;begin ReadBuffer(Result,4);end;
function TStringStreamEx.ReadChar:Char;begin ReadBuffer(Result,1);end;
function TStringStreamEx.ReadInteger:Integer;begin ReadBuffer(Result,4);end;
function TStringStreamEx.ReadSingle:Single;begin ReadBuffer(Result,4);end;
function TStringStreamEx.ReadSmallint:Smallint;begin ReadBuffer(Result,2);end;
function TStringStreamEx.ReadShortint:Shortint;begin ReadBuffer(Result,1);end;
function TStringStreamEx.ReadWord:Word;begin ReadBuffer(Result,2);end;
function TStringStreamEx.ReadInt64:Int64;begin ReadBuffer(Result,8);end;
procedure TStringStreamEx.WriteBoolean(b:Boolean);begin WriteBuffer(b,1);end;
procedure TStringStreamEx.WriteByte(b:Byte);begin WriteBuffer(b,1);end;
procedure TStringStreamEx.WriteCardinal(c:Cardinal);begin WriteBuffer(c,4);end;
procedure TStringStreamEx.WriteChar(c:Char);begin WriteBuffer(c,1);end;
procedure TStringStreamEx.WriteInteger(i:Integer);begin WriteBuffer(i,4);end;
procedure TStringStreamEx.WriteSingle(s:Single);begin WriteBuffer(s,4);end;
procedure TStringStreamEx.WriteSmallint(i:Smallint);begin WriteBuffer(i,2);end;
procedure TStringStreamEx.WriteShortInt(i:Shortint);begin WriteBuffer(i,1);end;
procedure TStringStreamEx.WriteWord(w:Word);begin WriteBuffer(w,2);end;
procedure TStringStreamEx.WriteInt64(i:Int64);begin WriteBuffer(i,8);end;

function TStringStreamEx.ReadString:String;
var c:char;
begin
  result:='';repeat ReadBuffer(c,1);if c<>#0 then result:=result+c;until c=#0;
end;

function TStringStreamEx.ReadStringList: String;
begin
  result:=StringReplace(ReadString,#13,#13#10,[rfReplaceAll]);
end;

procedure TStringStreamEx.WriteString(s:String);
var
  a:integer;e:byte;
begin
  e:=0;for a:=1 to Length(s) do WriteBuffer(s[a],1);WriteBuffer(e,1);
end;

procedure TStringStreamEx.WriteStringList(s: String);
begin
  WriteString(StringReplace(s,#13#10,#13,[rfReplaceAll]));
end;

{ TMemoryStreamEx }

function TMemoryStreamEx.ReadBoolean:Boolean;begin ReadBuffer(Result,1);end;
function TMemoryStreamEx.ReadByte:Byte;begin ReadBuffer(Result,1);end;
function TMemoryStreamEx.ReadCardinal:Cardinal;begin ReadBuffer(Result,4);end;
function TMemoryStreamEx.ReadChar:Char;begin ReadBuffer(Result,1);end;
function TMemoryStreamEx.ReadInteger:Integer;begin ReadBuffer(Result,4);end;
function TMemoryStreamEx.ReadSingle:Single;begin ReadBuffer(Result,4);end;
function TMemoryStreamEx.ReadSmallint:Smallint;begin ReadBuffer(Result,2);end;
function TMemoryStreamEx.ReadShortint:Shortint;begin ReadBuffer(Result,1);end;
function TMemoryStreamEx.ReadWord:Word;begin ReadBuffer(Result,2);end;
function TMemoryStreamEx.ReadInt64:Int64;begin ReadBuffer(Result,8);end;
procedure TMemoryStreamEx.WriteBoolean(b:Boolean);begin WriteBuffer(b,1);end;
procedure TMemoryStreamEx.WriteByte(b:Byte);begin WriteBuffer(b,1);end;
procedure TMemoryStreamEx.WriteCardinal(c:Cardinal);begin WriteBuffer(c,4);end;
procedure TMemoryStreamEx.WriteChar(c:Char);begin WriteBuffer(c,1);end;
procedure TMemoryStreamEx.WriteInteger(i:Integer);begin WriteBuffer(i,4);end;
procedure TMemoryStreamEx.WriteSingle(s:Single);begin WriteBuffer(s,4);end;
procedure TMemoryStreamEx.WriteSmallint(i:Smallint);begin WriteBuffer(i,2);end;
procedure TMemoryStreamEx.WriteShortInt(i:Shortint);begin WriteBuffer(i,1);end;
procedure TMemoryStreamEx.WriteWord(w:Word);begin WriteBuffer(w,2);end;
procedure TMemoryStreamEx.WriteInt64(i:Int64);begin WriteBuffer(i,8);end;

function TMemoryStreamEx.ReadString:String;
var c:char;
begin
  result:='';repeat ReadBuffer(c,1);if c<>#0 then result:=result+c;until c=#0;
end;

function TMemoryStreamEx.ReadStringList: String;
begin
  result:=StringReplace(ReadString,#13,#13#10,[rfReplaceAll]);
end;

procedure TMemoryStreamEx.WriteString(s:String);
var
  a,n:integer;e:byte;
begin
  e:=0;n:=length(s);if n>0 then for a:=1 to Length(s) do WriteBuffer(s[a],1);WriteBuffer(e,1);
end;

procedure TMemoryStreamEx.WriteStringList(s: String);
begin
  WriteString(StringReplace(s,#13#10,#13,[rfReplaceAll]));
end;

end.
