LMuadDIb
asked on
change a procedure from loading a TFilename to use a TStringList
this procedure decodes a binary tFilename, and I want to create basically the same procedure but to work on a TStringList instead of saving/loading the file from the hard drive which I have to do now. Im not going to put the whole procedure in here.. but try to place parts of it... parts I think I need to adjust.
AssignFile(InputFile,FInpu tFilename) ;
try
Reset(InputFile,1);
except
raise EyDecoder.Create(Format(De cFileNotFo und,[FInpu tFilename] ));
end;
InputBlockNo:=0;
InputFileSize:=System.File Size(Input File);
if Assigned(FOnProgress) then
begin
Abort:=False;
FOnProgress(Self,0,Abort);
if Abort then
begin
if ifopen then CloseFile(InputFile); ifopen:=false;
FreeMem(InputBuf);
FreeMem(OutputBuf);
Exit;
end;
end;
Escaped:=False;
FindNextKeyword:=True;
Repeat
// read a chunk of data
BlockRead(InputFile,InputB uf^,InputB ufferSize, numRead);
Inc(InputBlockNo);
DebugMessage(Format('read %d bytes, fp@%d',[numRead,FilePos(In putFile)]) );
P:=InputBuf;
if numRead=0 then
begin
DebugMessage(Format('eof detected, bailing',[]));
break; // bail out if EOF
end;
EndBuf:=PByte(Integer(Inpu tBuf)+numR ead-1);
// find the first escape character
// P:=nil;
DebugMessage(Format('enter decode loop',[]));
Repeat
if FindNextKeyword then ....
I can post the whole procedure if needed, or link to the source
any help would be appreciated
AssignFile(InputFile,FInpu
try
Reset(InputFile,1);
except
raise EyDecoder.Create(Format(De
end;
InputBlockNo:=0;
InputFileSize:=System.File
if Assigned(FOnProgress) then
begin
Abort:=False;
FOnProgress(Self,0,Abort);
if Abort then
begin
if ifopen then CloseFile(InputFile); ifopen:=false;
FreeMem(InputBuf);
FreeMem(OutputBuf);
Exit;
end;
end;
Escaped:=False;
FindNextKeyword:=True;
Repeat
// read a chunk of data
BlockRead(InputFile,InputB
Inc(InputBlockNo);
DebugMessage(Format('read %d bytes, fp@%d',[numRead,FilePos(In
P:=InputBuf;
if numRead=0 then
begin
DebugMessage(Format('eof detected, bailing',[]));
break; // bail out if EOF
end;
EndBuf:=PByte(Integer(Inpu
// find the first escape character
// P:=nil;
DebugMessage(Format('enter
Repeat
if FindNextKeyword then ....
I can post the whole procedure if needed, or link to the source
any help would be appreciated
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
something like this should work...
var
a_MemStream: TStream;
begin
a_MemStream := TMemoryStream.Create
try
aStrings.SaveStream(a_MemS tream);
//work with the Stream here...
SaveStream(a_MemStream, MySaveDirectory + MyFileName);
end;
procedure SaveStream(aStream: TStream; aSaveFileName: string);
var
a_SaveStream: TStream;
begin
{aSaveFileName is a Filename which includes the directory}
a_SaveStream := TFileStream.Create('aSaveF ileName');
try
if Assigned(aStream) and Assigned(a_SaveStream) then
a_SaveStream.CopyFrom(aStr eam , 0);
finally
a_SaveStream.Free;
end;
end;
var
a_MemStream: TStream;
begin
a_MemStream := TMemoryStream.Create
try
aStrings.SaveStream(a_MemS
//work with the Stream here...
SaveStream(a_MemStream, MySaveDirectory + MyFileName);
end;
procedure SaveStream(aStream: TStream; aSaveFileName: string);
var
a_SaveStream: TStream;
begin
{aSaveFileName is a Filename which includes the directory}
a_SaveStream := TFileStream.Create('aSaveF
try
if Assigned(aStream) and Assigned(a_SaveStream) then
a_SaveStream.CopyFrom(aStr
finally
a_SaveStream.Free;
end;
end;
ASKER
House_of_Dexter, Im not sure we are on the same page
I need away to change the above procedure to accept a tstringlist as an arg instead of a TFile.
I dont want to save anything to my drive, I want to do everything in memory with a tstringlist, once its decoded I will save it to my drive
I need away to change the above procedure to accept a tstringlist as an arg instead of a TFile.
I dont want to save anything to my drive, I want to do everything in memory with a tstringlist, once its decoded I will save it to my drive
House_of_Dexter is correct. Create a Memorystream, which resides in memory and not on disk. Load the stringlist into the memorystream, go back to the beginning of the memory stream and then read from the memorystream the blocks that you want to read.
You will have to adjust your code to use streams instead of files, though.
But since you already have your data inside a stringlist, why not simply use the stringlist functionality instead? I think you're making things way too complex here. It's like getting a square peg into a round hole. You can do that with plenty of force and a big hammer and other tools, but it's easier to just take a round peg instead. ;-)
You will have to adjust your code to use streams instead of files, though.
But since you already have your data inside a stringlist, why not simply use the stringlist functionality instead? I think you're making things way too complex here. It's like getting a square peg into a round hole. You can do that with plenty of force and a big hammer and other tools, but it's easier to just take a round peg instead. ;-)
Like Alex said...once you move over to a MemoryStream...It will act like your buffer...you can move through it just like an Array of Bytes...its a lot easier to do...no need to derefernce the pointer...just move to the byte and read it...
StringList...has a nice functions that will allow you to move your StringList to a MemoryStream...then TStream which allows you to save the results to disk...in TFileStream...
StringList...has a nice functions that will allow you to move your StringList to a MemoryStream...then TStream which allows you to save the results to disk...in TFileStream...
ASKER
ok, it will be a couple days before I try this out
ASKER
example:
InputFile,OutputFile: File;
so I guess its pure binary files
but I would still like to use a TStringList , because thats how I recieve the data from the internet.