[Lazarus] Updating an app "on the fly"

Mark Morgan Lloyd markMLl.lazarus at telemetry.co.uk
Tue Jun 15 12:06:01 CEST 2010


Hi All,

On Mon, 2010-06-14 at 17:37 +0200, Michael Van Canneyt wrote:
> You may want to look at the TDBF component. I think the package is
> called
> lazdbf or dbflaz.
> 
> Michael.

On Mon Jun 14 18:00:51 CEST 2010, Howard Page-Clark wrote:
> For a general introduction have a look at
> http://wiki.lazarus.freepascal.org/Lazarus_Database_Tutorial

I have looked at this document

> For using Tdbf have a look at
> http://wiki.lazarus.freepascal.org/Lazarus_Tdbf_Tutorial

and printed out and read this one.

and I have studied the Address Example and the other database example.

// need changing 0.50.alpha
//(Sender as Taction).Enabled:=Active;
if (GetFieldByName('x1_pos') = str_x[1]) and

I cannot find what to change XBase1's GetFieldByName(string) function
to. It accepts a field name as a parameter and returns a string.

// #### how do I do this
HexxData_DBF.UpdFieldStr('x' + inttostr(x) + '_left',
option_2_str(pattern1.option[x, 1]));

I cannot find what to change XBase1's UpdFieldStr(string, string)
function. The first parameter is the field name e.g. 'x1_left' and the
second parameter is a string value to assign to the first parameter.

HexxData_DBF.AppendBlank;
Is this how I append a blank record? The documents quoted above don't
mention appending a blank record.

HexxData_DBF_rec_no := HexxData_DBF.RecNo;
I need to get the record number and assign it to a longint

HexxData_DBF.GotoRecord(HexxData_DBF_rec_no); // ### needs changing
Then I need a Function to Goto the Record saved in the above assignment

My code:

{ this procedure nicked from Address Example }
procedure Thxboard.ACloseUpdate(Sender: TObject; DBFName : TDbf);
begin
  (Sender as Taction).Enabled:=Not (DBFName.State in dsEditModes);
end;
{----------------------------------------------------------}

procedure Thxboard.Open_db;
var
  S : String;
  I : Integer;
begin
  S := Application.ExeName;
  // hack for Mac
  I := Pos('.app/Contents/MacOS/', S);
  if (I > 0) then
    S := Copy(S,1, I+3);
  try
    HexxData_DBF.TableLevel := 3; // dBase III+
    HexxData_DBF.TableName := 'hex8.dbf';
    //  if not fileExists(S) then
    //  CopyFile('blank.dbf', S, false);
    //HexxData_DBF := TXBase.create(nil);
    HexxData_DBF.FilePath := ExtractFilePath(S) + 'data' + PathDelim;
    HexxData_DBF.Open;
    //HexxData_DBF.Active := true;
    //HexxData_DBF.GotoBOF;
  //finally
  except
    HexxData_DBF.free; // don't know what goes here.
  end;
end; { Open_db }
{---------------------------------------------------------}

procedure Thxboard.Close_db;
begin
  Caption := 'Saving patterns database...';
  refresh;
  if HexxData_DBF.Exclusive then
    begin
    try
      // HexxData_DBF.FileName := 'hex8.dbf';
      caption := 'One moment please...';
      //  HexxData_DBF.Active := false;
      //  HexxData_DBF.Destroy;
      ACloseUpdate(HexxData_DBF);
      //HexxData_DBF.Close;
    //finally
    except
      HexxData_DBF.Free;
    end;
  end;
end; { Close_db }
{---------------------------------------------------------}

function Thxboard.pattern_is_db_current(pattern1: pattern_type):
boolean;
var
  str_x, str_o: array[1..8] of string;
begin
  pattern_is_db_current := false;
  str(pattern1.x_piece[1], str_x[1]);
  str(pattern1.x_piece[2], str_x[2]);
  str(pattern1.x_piece[3], str_x[3]);
  str(pattern1.x_piece[4], str_x[4]);
  str(pattern1.x_piece[5], str_x[5]);
  str(pattern1.x_piece[6], str_x[6]);
  str(pattern1.x_piece[7], str_x[7]);
  str(pattern1.x_piece[8], str_x[8]);
  str(pattern1.o_piece[1], str_o[1]);
  str(pattern1.o_piece[2], str_o[2]);
  str(pattern1.o_piece[3], str_o[3]);
  str(pattern1.o_piece[4], str_o[4]);
  str(pattern1.o_piece[5], str_o[5]);
  str(pattern1.o_piece[6], str_o[6]);
  str(pattern1.o_piece[7], str_o[7]);
  str(pattern1.o_piece[8], str_o[8]);

  with HexxData_DBF do
  begin
    try
      HexxData_DBF.TableLevel := 3; // dBase III+
      Exclusive := True;
      with HexxData_DBF.FieldDefs do
      begin
        // need changing 0.50.alpha
        //(Sender as Taction).Enabled:=Active;
        if (GetFieldByName('x1_pos') = str_x[1]) and
           (GetFieldByName('x2_pos') = str_x[2]) and
           (GetFieldByName('x3_pos') = str_x[3]) and
           (GetFieldByName('x4_pos') = str_x[4]) and
           (GetFieldByName('x5_pos') = str_x[5]) and
           (GetFieldByName('x6_pos') = str_x[6]) and
           (GetFieldByName('x7_pos') = str_x[7]) and
           (GetFieldByName('x8_pos') = str_x[8]) then
             if (GetFieldByName('o1_pos') = str_o[1]) and
                (GetFieldByName('o2_pos') = str_o[2]) and
                (GetFieldByName('o3_pos') = str_o[3]) and
                (GetFieldByName('o4_pos') = str_o[4]) and
                (GetFieldByName('o5_pos') = str_o[5]) and
                (GetFieldByName('o6_pos') = str_o[6]) and
                (GetFieldByName('o7_pos') = str_o[7]) and
                (GetFieldByName('o8_pos') = str_o[8]) then

                pattern_is_db_current := true;
        end
      HexxData_DBF.Exclusive := False; // added 0.50.alpha
      // HexxData_DBF.Close; // added 0.50.alpha
    except
//    finally
      HexxData_DBF.Free; // added 0.50.alpha
    end;
  end;
end; { pattern_is_db_current }
{---------------------------------------------------------}

procedure Thxboard.save_options(pattern1: pattern_type);
var
  x: integer;
begin
  for x := 1 to 8 do
    begin
      // #### how do I do this
      HexxData_DBF.UpdFieldStr('x' + inttostr(x) + '_left',
option_2_str(pattern1.option[x, 1]));
      HexxData_DBF.UpdFieldStr('x' + inttostr(x) + '_forward',
option_2_str(pattern1.option[x, 2]));
      HexxData_DBF.UpdFieldStr('x' + inttostr(x) + '_right',
option_2_str(pattern1.option[x, 3]));
    end;
end; { save_options }
{---------------------------------------------------------}

procedure Thxboard.write_updated_pattern(var pattern1: pattern_type);
var
  found: boolean;
begin
  found := false;
  if pattern_is_db_current(pattern1) then
    { save pattern options to db }
    begin
      save_options(pattern1);

      // #### how do I do this
      HexxData_DBF_rec_no := HexxData_DBF.RecNo;
      close_db;
      if trace_moves then
        begin
          open_trace_file;
          With TraceFile_DBF do
          try
            // #### needs changing
            TraceFile_DBF.GotoRecord(trace_rec_no);
            TraceFile_DBF.UpdFieldStr('record_no',
inttostr(HexxData_DBF_rec_no));
          //finally
          except
            close_trace_file;
          end;
        end;
      open_db;
      // #### how do I do this
      HexxData_DBF.GotoRecord(HexxData_DBF_rec_no);
    end
  else
  { save entire pattern to db };
  begin

    while (not HexxData_DBF.EOF) and not found do
      begin
        if (pattern_is_db_current(pattern1)) then
          begin
            save_options(pattern1);
            found := true;
            HexxData_DBF_rec_no := HexxData_DBF.RecNo;
            close_db;
            if trace_moves then
              begin
                open_trace_file;
                With TraceFile_DBF do
                try
                  // ### needs changing
                  TraceFile_DBF.GotoRecord(trace_rec_no);
                  TraceFile_DBF.UpdFieldStr('record_no',
inttostr(HexxData_DBF_rec_no));
                //finally
                except
                  close_trace_file;
                end;
              end;
            open_db;
            // ### needs changing
            HexxData_DBF.GotoRecord(HexxData_DBF_rec_no);
          end;
        // ### needs changing
        HexxData_DBF.Next;
      end;
    if not found then
      begin
        with HexxData_DBF
        try
          HexxData_DBF.AppendBlank;
          With HexxData_DBF.FieldDefs do
          begin
               { --- add a new record --- }
               save_options(pattern1);
               // ### needs changing
               HexxData_DBF.UpdFieldStr('this_game',
inttostr(pattern.this_game));
               HexxData_DBF.UpdFieldStr('x1_pos',
inttostr(pattern1.x_piece[1]));
               HexxData_DBF.UpdFieldStr('x2_pos',
inttostr(pattern1.x_piece[2]));
               HexxData_DBF.UpdFieldStr('x3_pos',
inttostr(pattern1.x_piece[3]));
               HexxData_DBF.UpdFieldStr('x4_pos',
inttostr(pattern1.x_piece[4]));
               HexxData_DBF.UpdFieldStr('x5_pos',
inttostr(pattern1.x_piece[5]));
               HexxData_DBF.UpdFieldStr('x6_pos',
inttostr(pattern1.x_piece[6]));
               HexxData_DBF.UpdFieldStr('x7_pos',
inttostr(pattern1.x_piece[7]));
               HexxData_DBF.UpdFieldStr('x8_pos',
inttostr(pattern1.x_piece[8]));
               HexxData_DBF.UpdFieldStr('o1_pos',
inttostr(pattern1.o_piece[1]));
               HexxData_DBF.UpdFieldStr('o2_pos',
inttostr(pattern1.o_piece[2]));
               HexxData_DBF.UpdFieldStr('o3_pos',
inttostr(pattern1.o_piece[3]));
               HexxData_DBF.UpdFieldStr('o4_pos',
inttostr(pattern1.o_piece[4]));
               HexxData_DBF.UpdFieldStr('o5_pos',
inttostr(pattern1.o_piece[5]));
               HexxData_DBF.UpdFieldStr('o6_pos',
inttostr(pattern1.o_piece[6]));
               HexxData_DBF.UpdFieldStr('o7_pos',
inttostr(pattern1.o_piece[7]));
               HexxData_DBF.UpdFieldStr('o8_pos',
inttostr(pattern1.o_piece[8]));
               HexxData_DBF_rec_no := HexxData_DBF.RecNo;
          end;
        //finally
        except
          close_db;
        end;
        if trace_moves then
          begin
            open_trace_file;
            with TraceFile_DBF
            try
              With TraceFile_DBF.FieldDefs do
              begin
                // ### needs changing
                TraceFile_DBF.GotoRecord(trace_rec_no);
                TraceFile_DBF.UpdFieldStr('record_no',
inttostr(HexxData_DBF_rec_no));
              end;
            //finally
            except
              close_trace_file;
            end;
          end;
        open_db;
        // ### needs changing
        HexxData_DBF.GotoRecord(HexxData_DBF_rec_no);
      end; // if found

  end; // else
end; { write_updated_pattern  }
{---------------------------------------------------------}

Best Regards,
	Peter

> 
> On Tue, 15 Jun 2010, Peter E Williams wrote:
> 
> > Hi All,
> >
> > I really need to know what is the Dbase3/4 unit for Lazarus/Free
> Pascal
> > and where is the sample project for manipulating them. I found it
> > somewhere on the lazarus website but don't know where. I hope that
> it is
> > okay for me to post some of my code which uses the XBase1 unit in
> Delphi
> > 5 Enterprise which I am porting to my Hexxpawn8 game (which is cross
> > platform open source).
> >
> > I use Dbase3/4+ files to store board patterns for my game which is
> sort
> > of a cross between the pawns in Chess and checkers, using AI logic
> > against the computer. (I chose Dbase format because it was the
> single
> > most common database file format at the time.)
> >
> > What is the replacement unit for XBase1 for using Dbase3/4 files,
> and
> > the functions/procedures etc to replace these calls to XBase1 ?
> >
> > How do I do the following:
> >
> > // declaring a Dbase file -- TXBase (what is the equivalent in
> Lazarus?)
> > // how do I copy a blank.dfg file to hex8.bdf (Copyfile) ???
> > // I need to Goto to the Bottom of File (HexxData.GotoBOF;)
> > // I need to call a function GetFieldByName passing a string as the
> > field name and comparing it with a character
> > // I need to Goto the Next record (HexxData.GotoNext;)
> > // I need to Get the Record # (HexxData.RecNo;)
> > // and test the Dbase.EOF (not HexxData.EOF)
> > // and Append a Blank record (HexxData.AppendBlank;)
> > // save an integer to a Field (HexxData.UpdFieldStr('this_game',
> > inttostr(pattern.this_game)); )
> > // and Goto to the Record number
> (HexxData.GotoRecord(hexxdata_rec_no);)
> >

-- 
Proudly developing Quality Cross Platform Open Source Games
Since 1970 with a Commodore PET 4016 with 16 KRAM
http://pews-freeware-games.org (<--- brand new)





More information about the Lazarus mailing list