i'm trying to work with the synapse mime library but am running into a bit of a problem... i'm an old procedural type coder from the TP3 to TP6 days... it has been a long time since i've done much of anything with objects and i've forgotten a lot more than i remember... i've not done any delphi coding at all...

so, i'm starting with source\demo\FreePascal\testmime.pas because it reads an email message from a text file, processes it a bit and then saves a new copy to another text file... what i'm trying to add to this, so as to understand how the library works and can be used, is how to step thru the parts of a multipart message and work with each individual part...

currently i have something that determines that we are working with a multipart message and branches to another routine... in this other routine, we create another object for the part, process it just like in the main part, then destroy the object as we loop back for the next part... i'm able to see what each part is (text, html, etc) and display some info on them BUT when we drop back out of the multipart routine and get back to the main section, we get an access violation on destroying the initial object at the end of the program execution... if i do not destroy the secondary objects in the multipart routine, this access violation does not occur...

attached is the mess i'm working with... yes, it is peppered with writeln's so as to try to see what's going on where and when since there is no debugger support in the fpc i'm working with so i cannot see the contents of the variables like i'm so extremely used to doing in the TP/BP IDEs...


{$MODE DELPHI}

Program testmime;

uses
  mimepart, synachar, classes;

type
  Tc = class(TObject)
  public
    class procedure ph(const Sender: TMimePart);
  end;

class procedure Tc.ph(const Sender: TMimePart);
begin
  Sender.DecodePart;
  Sender.EncodePart;
end;

Procedure ProcessParts(const Sender: TMimePart; ThePartCount: integer);

var
  m1: tmimepart;
  MyPart: integer;
  MyPartCount: integer;
//
begin
  writeln;
  writeln(' entering ProcessParts...');
  for MyPart := 1 to ThePartCount do
    begin
      if MyPart > 1 then
        writeln;
      writeln('  allocate m1-',MyPart);
      m1 := tmimepart.create;
      try
        writeln('   assign OnWalkPart-',MyPart);
        m1.OnWalkPart:=tc.ph;
        writeln('   pulling part ',MyPart,' into m1-',MyPart);
        m1:= Sender.GetSubPart(MyPart - 1);
        writeln('   fire DecomposeParts-',MyPart);
        m1.DecomposeParts;
        writeln('   return from DecomposeParts-',MyPart);
        writeln('   fire WalkPart-',MyPart);
        m1.WalkPart;
        writeln('   return from WalkPart-',MyPart);
        MyPartCount := m1.GetSubPartCount;
        writeln('    WP-',MyPart,' Target Charset  : ',m1.TargetCharset);
        writeln('    WP-',MyPart,' Default Charset : ',m1.DefaultCharset);
        writeln('    WP-',MyPart,' GetSubPartCount : ',MyPartCount);
        writeln('    WP-',MyPart,' SubLevel        : ',m1.SubLevel);
        writeln('    WP-',MyPart,' MaxSubLevel     : ',m1.MaxSubLevel);
        writeln('    WP-',MyPart,' Primary         : ',m1.Primary);
        writeln('    WP-',MyPart,' Secondary       : ',m1.Secondary);
        writeln('    WP-',MyPart,' Encoding        : ',m1.Encoding);
        writeln('    WP-',MyPart,' Boundary        : ',m1.Boundary);
        writeln('    WP-',MyPart,' ContentID       : ',m1.ContentID);

      finally
        writeln('   finally');
        writeln('    free m1-',MyPart);
//        m1.free;
        writeln('  done m1-',MyPart);
      end;
    end;
  writeln(' vacating ProcessParts...');
end;

var
  l: tstringlist;
  m: tmimepart;
  PartCount: integer;

begin
  l := TStringList.create;
  m := tmimepart.create;
  try
    m.OnWalkPart:=tc.ph;
    m.Lines.LoadFromFile(paramstr(1));
    writeln('Target Charset     : ',m.TargetCharset);
    m.TargetCharset := GetCPFromID('ISO_8859_1');
    writeln('Target Charset     : ',m.TargetCharset);
    writeln('Default Charset    : ',m.DefaultCharset);
    m.DefaultCharset := 'ISO_8859_1';
    writeln('Default Charset    : ',m.DefaultCharset);
    m.DecomposeParts;
    writeln('DP Target Charset  : ',m.TargetCharset);
    m.TargetCharset := GetCPFromID('ISO_8859_1');
    writeln('DP Target Charset  : ',m.TargetCharset);
    writeln('DP Default Charset : ',m.DefaultCharset);
    m.WalkPart;
    PartCount := m.GetSubPartCount;
    writeln('WP Target Charset  : ',m.TargetCharset);
    writeln('WP Target Charset  : ',m.TargetCharset);
    writeln('WP Default Charset : ',m.DefaultCharset);
    writeln('WP GetSubPartCount : ',PartCount);
    writeln('WP SubLevel        : ',m.SubLevel);
    writeln('WP MaxSubLevel     : ',m.MaxSubLevel);
    writeln('WP Primary         : ',m.Primary);
    writeln('WP Secondary       : ',m.Secondary);
    writeln('WP Encoding        : ',m.Encoding);
    writeln('WP Boundary        : ',m.Boundary);
    writeln('WP ContentID       : ',m.ContentID);

    if m.Primary = 'MULTIPART' then
      ProcessParts(m, PartCount);

    writeln;
    writeln(' fire ComposeParts...');
    m.ComposeParts;
    writeln(' return from ComposeParts...');
    writeln('CP Target Charset  : ',m.TargetCharset);
    writeln('CP Target Charset  : ',m.TargetCharset);
    writeln('CP Default Charset : ',m.DefaultCharset);
    writeln(' fire SaveToFile...');
    m.Lines.SaveToFile(paramstr(1) + '.repack');
    writeln(' return from SaveTofile...');
  finally
    writeln;
    writeln('finally');
    writeln(' free m');
    m.free;
    writeln(' done m');
    writeln(' free l');
    l.free;
    writeln(' done l');
    writeln('done...');
  end;
end.
_______________________________________________
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal

Reply via email to