Code:
unit uBuilder;
{
~ Anhängen von Strings an Dateien leicht gemacht ~
~uBuilder Unit von Hamtaro - Kontakt ICQ: 383065218~
~Release Datum: 21.01.2008 - Going Down, Down, Down~
}
interface
uses sysutils, classes;
function AppendInfo (Exename, Name, Info : String) : Boolean;
function AppendOrReplace(Exename, Name, Info : String) : Boolean;
function ReadInfo (Exename, Name : String) : String;
function DeleteAllInfo (Exename : String) :Boolean;
/////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////
var Delimiter1 : String = '<in';
Delimiter2 : String = 'f¤>';
InnerDelimiter : String = '¦';
type TStringArray = Array of String;
type TByteArray = Array of Byte;
implementation
//Die Explode funktion hab ich da her: http://www.delphipraxis.net/topic3816_ein+dynamisches+array+aus+einem+string+erzeugen.html
function Explode(const Separator, S: string; Limit: Integer = 0): TStringArray;
var
SepLen: Integer;
F, P: PChar;
ALen, Index: Integer;
begin
SetLength(Result, 0);
if (S = '') or (Limit < 0) then Exit;
if Separator = '' then
begin
SetLength(Result, 1);
Result[0] := S;
Exit;
end;
SepLen := Length(Separator);
ALen := Limit;
SetLength(Result, ALen);
Index := 0;
P := PChar(S);
while P^ <> #0 do
begin
F := P;
P := AnsiStrPos(P, PChar(Separator));
if (P = nil) or ((Limit > 0) and (Index = Limit - 1)) then P := StrEnd(F);
if Index >= ALen then
begin
Inc(ALen, 5);
SetLength(Result, ALen);
end;
SetString(Result[Index], F, P - F);
Inc(Index);
if P^ <> #0 then Inc(P, SepLen);
end;
if Index < ALen then SetLength(Result, Index);
end;
function ReadWholeFile(exename : String) : String;
var
Datei : TFileStream;
begin
Datei := TFilestream.Create(Exename, fmShareDenyNone or fmOpenRead);
Datei.Seek(0,soFromBeginning);
SetLength(Result,Datei.Size);
Datei.Read(Result[1], Datei.Size);
Datei.Free;
end;
function ReadAllInfo(Exename : string) : String;
begin
Result := Copy(ReadWholeFile(Exename),Pos(Delimiter1 + Delimiter2,ReadWholeFile(Exename)),Length(ReadWholeFile(Exename)));
end;
function AppendInfo(Exename, Name, Info : String) : Boolean;
var
Datei : TFilestream;
infoString : String;
begin
Result := False;
If ReadInfo(Exename, Name) <> '' Then Exit; //wir wollen keine doppelten
Result := True;
try
InfoString := Delimiter1 + Delimiter2 + Name + InnerDelimiter + Info;
Datei := TFilestream.Create(Exename, fmOpenReadWrite or fmShareDenyWrite);
Datei.Seek(0, soFromEnd);
Datei.Write(Pointer(InfoString)^, Length(InfoString));
except
Result := False;
end;
Datei.Free;
end;
function AppendOrReplace(Exename, Name, Info : String) : Boolean;
var
Datei : TFilestream;
alleInfos : String;
neueInfos : String;
begin
result := True;
try
If ReadInfo(Exename,Name) = '' Then begin
AppendInfo(Exename,Name,Info);
Exit;
end;
AlleInfos := ReadAllInfo(Exename);
neueInfos := StringReplace(AlleInfos, '<inf¤>' + Name + '¦' + ReadInfo(Exename,Name),'<inf¤>' + Name + '¦' + Info, []);
DeleteAllInfo(Exename);
Datei := TFilestream.Create(Exename,fmOpenWrite Or fmShareDenyNone);
Datei.Seek(0,soFromEnd);
Datei.Write(Pointer(neueInfos)^,Length(neueInfos));
Datei.Free;
except
Result := false;
end;
end;
function DeleteAllInfo(Exename : String) : Boolean;
var
Datei : TFileStream;
ganzeDatei : String;
ohneInfos : String;
begin
Result := True;
try
ganzeDatei := ReadWholeFile(Exename);
ohneInfos := Copy(ReadWholeFIle(Exename),0,Pos(Delimiter1 + Delimiter2,ganzeDatei) - 1);
DeleteFile(Exename);
Datei := TFileStream.Create(Exename,fmCreate Or fmShareExclusive Or fmOpenWrite);
Datei.Write(Pointer(ohneInfos)^,Length(ohneInfos));
Datei.Free;
except
Result := False;
end;
end;
function ReadInfo (Exename, Name : String) : String;
var
alleinfos : String;
aInfos : TStringArray;
i : Integer;
begin
alleInfos := ReadAllInfo(Exename);
aInfos := Explode(Delimiter1 + Delimiter2, alleInfos);
For i := 0 To High(aInfos) do begin
If Copy(aInfos[i],0, Pos(Innerdelimiter,aInfos[i]) -1)= name Then
begin Result := Copy(aInfos[i],Pos(Innerdelimiter,aInfos[i]) + 1, Length(aInfos[i]));
exit;
end;
end;
end;
end.
Hat aber den Nachteil, dass die classes unit genutzt wird, unter 300kb wirst du deine exe kaum bekommen