[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

change a procedure from loading a TFilename to use a TStringList

Posted on 2006-07-11
7
Medium Priority
?
470 Views
Last Modified: 2010-08-05
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,FInputFilename);
      try
        Reset(InputFile,1);
      except
        raise EyDecoder.Create(Format(DecFileNotFound,[FInputFilename]));
      end;

      InputBlockNo:=0;
      InputFileSize:=System.FileSize(InputFile);

      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,InputBuf^,InputBufferSize,numRead);
        Inc(InputBlockNo);

        DebugMessage(Format('read %d bytes, fp@%d',[numRead,FilePos(InputFile)]));

        P:=InputBuf;
        if numRead=0 then
          begin
            DebugMessage(Format('eof detected, bailing',[]));
            break; // bail out if EOF
          end;

        EndBuf:=PByte(Integer(InputBuf)+numRead-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
0
Comment
Question by:LMuadDIb
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
7 Comments
 
LVL 4

Author Comment

by:LMuadDIb
ID: 17083685
actually not a TFilename but it uses File
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.
0
 
LVL 6

Accepted Solution

by:
House_of_Dexter earned 2000 total points
ID: 17084102
Why use AssignFile...work with a TFileStream..  


function TrpCopy.SetDir(const Value: string; aMode: word): TStream;
var
  a_FileName: string;
begin
{Warning this is basically a FileSteam Constructor...you are responisble for freeing
 the TStream}
  Result := nil;
  try
    FDir.Name := Value;
    a_FileName := Value + FileName;

    Result := TFileStream.Create(a_FileName, aMode);
  except
     On EFOpenError do
        //swallow this and pass nil pointer
     else Raise;
  end;
end;

procedure TrpCopy.ShellCopy(aFromFileName, aToFileName: string);
var
  a_shellinfo: TSHFileOpStructA;
begin
  a_shellinfo.wFunc:=FO_COPY;
  a_shellinfo.pFrom:=PChar(aFromFileName);
  a_shellinfo.pTo:=PChar(aToFileName);
  a_shellinfo.fFlags:=FOF_NOCONFIRMATION or FOF_SILENT;
  SHFileOperation(a_shellinfo);
end;

procedure TrpCopy.SetDirectory(const Value: string);
begin
  FDir.Name := Value;
  FDirectory := FDir.Name;
  Check.IsTrue(FDir.Exist, 'Directory does not exist', 'TrpCopy.SetDirectory');
end;

procedure TrpCopy.SetFileName(const Value: string);
var
  a_DirStream: TStream;
  a_DestStream: TStream;
begin
  Check.IsNotEmpty(Directory, 'Directory', 'TrpCopy.SetFileName');
  Check.IsNotEmpty(Directory, 'DestDirectory', 'TrpCopy.SetFileName');
  a_DirStream := nil;
  a_DestStream := nil;

  FFileName := Value;
  try
  a_DirStream := SetDir(Directory, fmOpenRead);
  a_DestStream := SetDir(DestDirectory, fmCreate);

// write you code here to manipulate the stream...

  if Assigned(a_DirStream) and Assigned(a_DestStream) then
    a_DestStream.CopyFrom(a_DirStream , 0);
  if Assigned(FOnCopied) then
    FOnCopied(Self);  
  finally
    a_DirStream.Free;
    a_DestStream.Free;
  end;

end;

0
 
LVL 6

Expert Comment

by:House_of_Dexter
ID: 17085226
something like this should work...


var
  a_MemStream: TStream;

begin
  a_MemStream := TMemoryStream.Create  
  try
     aStrings.SaveStream(a_MemStream);
    //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('aSaveFileName');
   try
     if Assigned(aStream) and Assigned(a_SaveStream) then
        a_SaveStream.CopyFrom(aStream , 0);
  finally
    a_SaveStream.Free;
  end;

end;

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 4

Author Comment

by:LMuadDIb
ID: 17086926
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

0
 
LVL 17

Expert Comment

by:Wim ten Brink
ID: 17089159
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. ;-)
0
 
LVL 6

Expert Comment

by:House_of_Dexter
ID: 17090751
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...
0
 
LVL 4

Author Comment

by:LMuadDIb
ID: 17094896
ok, it will be a couple days before I try this out
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Suggested Courses

656 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question