[S]The CallHistory outload generates inconsistant results to the xml file...

<< < (2/2)

Robert.Thompson:
Quote from: bbarker on March 03, 2012, 05:40:45 pm

This looks interesting.  Any chance of getting the source code?  Or how about a 32-bit EXE?


I am not a 'programmer' but here is the source code for the pascal unit that does the work:

unit Unit1;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls;

type

  { TForm1 }

  TForm1 = class(TForm)
    Button1: TButton;
    Edit1: TEdit;
    Edit2: TEdit;
    procedure Button1Click(Sender: TObject);
    procedure Edit2Change(Sender: TObject);
  private
    { private declarations }
  public
    { public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.lfm}

var
  chFile, tmpfile ,csvFile : TextFile;
  text,newtext,outline, field9   : string;
   cnt, count, test : integer;


{ TForm1 }

procedure TForm1.Button1Click(Sender: TObject);
begin
  close;
end;

procedure TForm1.Edit2Change(Sender: TObject);
begin

end;


begin
  // open the callhistory.xml file for reading
  AssignFile(chFile, 'callhistory.xml');
  ReSet(chFile);
  // open or create the callhistorytmp.txt file for writing
  AssignFile(tmpFile, 'callhistorytmp.txt');
  ReWrite(tmpFile);

   // Initialize the variables that will be used
   newtext := '';
   outline := '';
   cnt := 0;

   // *************  Build the callhistorytmp.txt file ****************
      begin
          while not Eof(chFile) do
          begin
            ReadLn(chFile, text);
            cnt := cnt + 1;
            // skip the first 2 lines of the file
             if cnt < 3 then text := '';
            //replace any existing commas with spaces
             StringReplace(trim(text),',',' ', [rfReplaceAll, rfIgnoreCase]);
             newtext := text;
            // continue processing: remove literals & add commas as field separators
            newtext := StringReplace(trim(newtext),'<CallHistory date="','', [rfReplaceAll, rfIgnoreCase]);
            newtext := StringReplace(trim(newtext),'" time="' ,',', [rfReplaceAll, rfIgnoreCase]);
            newtext := StringReplace(trim(newtext),'">' ,',', [rfReplaceAll, rfIgnoreCase]);
            newtext := StringReplace(trim(newtext),'<Terminal id="' ,'', [rfReplaceAll, rfIgnoreCase]);
            newtext := StringReplace(trim(newtext),'" dir="' ,',', [rfReplaceAll, rfIgnoreCase]);
            newtext := StringReplace(trim(newtext),'<Peer name="' ,'', [rfReplaceAll, rfIgnoreCase]);
            newtext := StringReplace(trim(newtext),'" number="' ,',', [rfReplaceAll, rfIgnoreCase]);
            newtext := StringReplace(trim(newtext),'"/>' ,',', [rfReplaceAll, rfIgnoreCase]);
            newtext := StringReplace(trim(newtext),'<Event time="' ,'', [rfReplaceAll, rfIgnoreCase]);
            newtext := StringReplace(trim(newtext),'</Event>' ,',', [rfReplaceAll, rfIgnoreCase]);
            newtext := StringReplace(trim(newtext),'</Terminal>' ,'', [rfReplaceAll, rfIgnoreCase]);
            newtext := StringReplace(trim(newtext),'<?xml' ,'', [rfReplaceAll, rfIgnoreCase]);
            newtext := StringReplace(trim(newtext),'<CallHistoryFile>' ,'', [rfReplaceAll, rfIgnoreCase]);
            newtext := StringReplace(trim(newtext),'</CallHistoryFile>' ,'', [rfReplaceAll, rfIgnoreCase]);
            // Write the processed line to the callhistorytmp.txt file
            if newtext <> '' then writeln(tmpfile,newtext);
           end;
      CloseFile(chFile);
      CloseFile(tmpFile);
      end;


 // *************  Build the callhistory.csv file ****************
 Begin
  //Open the callhistory.csv file for writing
   AssignFile(csvFile, 'callhistory.txt');
   ReWrite(csvFile);
   //Open the callhistorytmp.txt file for reading
   AssignFile(tmpFile, 'callhistorytmp.txt');
   ReSet(tmpFile);
   text := '';
   outline := '';
   // Build column headings for callhistory.csv file (row # 1)
   outline := 'CallDate,'+'CallTime,'+'TerminalID,'+'Direction,'+'PeerName,'+
      'Number,'+'EventTime,'+'Event,'+'EventTime2,'+'Event2,'+'TerminalID2,'+
      'Direction2,'+'PeerName2,'+'Number2,'+'EventTime3,'+'Event3,'+'EventTime4,'+
      'Event4';
   // Write colum headings to callhistory.csv file
   writeln(csvfile,outline);
   // Initialize variables
   outline :='';
   count := 0;
   test := 0;
   field9:='';

  // Read through callhistorytmp.txt file
  while not Eof(tmpFile) do
  begin
    ReadLn(tmpFile, text);
    // build the record
    outline := outline + text;
    // Determine if processing the last line of a callhistorytmp record
    if text = '</CallHistory>' then
    begin
    // now, get rid of last line
    outline := StringReplace(trim(outline),'</CallHistory>' ,'', [rfReplaceAll, rfIgnoreCase]);
          begin
           for count := 1 to length(outline) do
              begin
                 //count the comma deliminaters to find the 8th comma
                 if copy(outline, count, 1) = ',' then test := test +1;
                 if test = 8 then
                 //test to see if some fields are missing
                    begin
                       // look at the data following the 8th comma
                       field9 := copy(outline,count+3,1);
                       //  ******** not sure about this test  *******
                       if (field9 <> ':') and  (field9 <> '') then insert(',,',outline,count);
                       test := 0;
                       break;
                    end;
              end;
          end;
       writeln(csvfile,outline);
       outline := '';
    end;
  end;
    closefile(csvfile);
    closefile(tmpfile);
end;
end.
                                       

Navigation

[0] Message Index

[*] Previous page