Browse Source

* fcl-db/dbase: cosmetic: use more constants, clarify structure

git-svn-id: trunk@24136 -
reiniero 12 years ago
parent
commit
97e3e6e50e

+ 1 - 1
packages/fcl-db/src/dbase/dbf.pas

@@ -1257,7 +1257,7 @@ begin
 
 
   BindFields(true);
   BindFields(true);
 
 
-  // create array of blobstreams to store memo's in. each field is a possible blob
+  // create array of blobstreams to store memos in. each field is a possible blob
   FBlobStreams := AllocMem(FieldDefs.Count * SizeOf(TDbfBlobStream));
   FBlobStreams := AllocMem(FieldDefs.Count * SizeOf(TDbfBlobStream));
 
 
   // check codepage settings
   // check codepage settings

+ 18 - 10
packages/fcl-db/src/dbase/dbf_dbffile.pas

@@ -203,6 +203,7 @@ uses
 
 
 const
 const
   sDBF_DEC_SEP = '.';
   sDBF_DEC_SEP = '.';
+  FIELD_DESCRIPTOR_ARRAY_TERMINATOR = $0D; // Marker at end of list of fields within header
 
 
 {$I dbf_struct.inc}
 {$I dbf_struct.inc}
 
 
@@ -404,6 +405,7 @@ begin
         // ShowMessage('Invalid Record Count,'+^M+
         // ShowMessage('Invalid Record Count,'+^M+
         //             'RecordCount in Hdr : '+IntToStr(PDbfHdr(Header).RecordCount)+^M+
         //             'RecordCount in Hdr : '+IntToStr(PDbfHdr(Header).RecordCount)+^M+
         //             'expected : '+IntToStr(RecordCount));
         //             'expected : '+IntToStr(RecordCount));
+        // instead, fix up record count without complaint:
         PDbfHdr(Header)^.RecordCount := RecordCount;
         PDbfHdr(Header)^.RecordCount := RecordCount;
         lModified := true;
         lModified := true;
       end;
       end;
@@ -460,7 +462,7 @@ begin
         // open blob file
         // open blob file
         if not FileExists(lMemoFileName) then
         if not FileExists(lMemoFileName) then
           MemoFileClass := TNullMemoFile
           MemoFileClass := TNullMemoFile
-        else if FDbfVersion in [xFoxPro,xVisualFoxPro]  then
+        else if (FDbfVersion in [xFoxPro,xVisualFoxPro]) then
           MemoFileClass := TFoxProMemoFile
           MemoFileClass := TFoxProMemoFile
         else
         else
           MemoFileClass := TDbaseMemoFile;
           MemoFileClass := TDbaseMemoFile;
@@ -602,6 +604,7 @@ begin
     if FDbfVersion = xBaseVII then
     if FDbfVersion = xBaseVII then
     begin
     begin
       // version xBaseVII without memo
       // version xBaseVII without memo
+      // todo: add support for foxpro writing codepage to codepage slot; use FoxLangId_Intl_850 etc
       HeaderSize := SizeOf(rDbfHdr) + SizeOf(rAfterHdrVII);
       HeaderSize := SizeOf(rDbfHdr) + SizeOf(rAfterHdrVII);
       RecordSize := SizeOf(rFieldDescVII);
       RecordSize := SizeOf(rFieldDescVII);
       FillChar(Header^, HeaderSize, #0);
       FillChar(Header^, HeaderSize, #0);
@@ -617,9 +620,11 @@ begin
       HeaderSize := SizeOf(rDbfHdr) + SizeOf(rAfterHdrIII);
       HeaderSize := SizeOf(rDbfHdr) + SizeOf(rAfterHdrIII);
       RecordSize := SizeOf(rFieldDescIII);
       RecordSize := SizeOf(rFieldDescIII);
       FillChar(Header^, HeaderSize, #0);
       FillChar(Header^, HeaderSize, #0);
+      // Note: VerDBF may be changed later on depending on what features/fields are used
+      // (autoincrement etc)
       case FDbfVersion of
       case FDbfVersion of
         xFoxPro: PDbfHdr(Header)^.VerDBF := $02; {FoxBASE}
         xFoxPro: PDbfHdr(Header)^.VerDBF := $02; {FoxBASE}
-        xVisualFoxPro: PDbfHdr(Header)^.VerDBF := $30; {Visual FoxPro no autoincrement,no varchar} //todo: check autoincrement, Varchar, Varbinary, or Blob-enabled
+        xVisualFoxPro: PDbfHdr(Header)^.VerDBF := $30; {Visual FoxPro no autoincrement,no varchar}
         else PDbfHdr(Header)^.VerDBF := $03; {FoxBASE+/dBASE III PLUS, no memo!?}
         else PDbfHdr(Header)^.VerDBF := $03; {FoxBASE+/dBASE III PLUS, no memo!?}
       end;
       end;
       // standard language WE, dBase III no language support
       // standard language WE, dBase III no language support
@@ -681,7 +686,7 @@ begin
         lFieldDescIII.FieldType := lFieldDef.NativeFieldType;
         lFieldDescIII.FieldType := lFieldDef.NativeFieldType;
         lFieldDescIII.FieldSize := lSize;
         lFieldDescIII.FieldSize := lSize;
         lFieldDescIII.FieldPrecision := lPrec;
         lFieldDescIII.FieldPrecision := lPrec;
-        if FDbfVersion in [xFoxPro,xVisualFoxPro] then
+        if (FDbfVersion in [xFoxPro,xVisualFoxPro]) then
           lFieldDescIII.FieldOffset := SwapIntLE(lFieldOffset);
           lFieldDescIII.FieldOffset := SwapIntLE(lFieldOffset);
         if (PDbfHdr(Header)^.VerDBF = $02) and (lFieldDef.NativeFieldType in ['0', 'Y', 'T', 'O', '+']) then
         if (PDbfHdr(Header)^.VerDBF = $02) and (lFieldDef.NativeFieldType in ['0', 'Y', 'T', 'O', '+']) then
           PDbfHdr(Header)^.VerDBF := $30; {Visual FoxPro}
           PDbfHdr(Header)^.VerDBF := $30; {Visual FoxPro}
@@ -701,8 +706,10 @@ begin
       WriteRecord(I, lFieldDescPtr);
       WriteRecord(I, lFieldDescPtr);
       Inc(lFieldOffset, lFieldDef.Size);
       Inc(lFieldOffset, lFieldDef.Size);
     end;
     end;
-    // end of header
-    WriteChar($0D);
+    // end of field descriptor; ussually end of header -
+    // Visual Foxpro backlink info is part of the header but comes after the
+    // terminator
+    WriteChar(FIELD_DESCRIPTOR_ARRAY_TERMINATOR);
 
 
     // write memo bit
     // write memo bit
     if lHasBlob then
     if lHasBlob then
@@ -725,7 +732,7 @@ begin
       an associated database (.dbc) file, information. If the first byte is 0x00, 
       an associated database (.dbc) file, information. If the first byte is 0x00, 
       the file is not associated with a database. Therefore, database files always 
       the file is not associated with a database. Therefore, database files always 
       contain 0x00. }
       contain 0x00. }
-    if FDbfVersion = xVisualFoxPro then
+    if (FDbfVersion = xVisualFoxPro) then
       Inc(PDbfHdr(Header)^.FullHdrSize, 263);
       Inc(PDbfHdr(Header)^.FullHdrSize, 263);
 
 
     // write dbf header to disk
     // write dbf header to disk
@@ -741,7 +748,7 @@ begin
   if HasBlob and (FMemoFile=nil) then
   if HasBlob and (FMemoFile=nil) then
   begin
   begin
     lMemoFileName := ChangeFileExt(FileName, GetMemoExt);
     lMemoFileName := ChangeFileExt(FileName, GetMemoExt);
-    if FDbfVersion in [xFoxPro,xVisualFoxPro] then
+    if (FDbfVersion in [xFoxPro,xVisualFoxPro]) then
       FMemoFile := TFoxProMemoFile.Create(Self)
       FMemoFile := TFoxProMemoFile.Create(Self)
     else
     else
       FMemoFile := TDbaseMemoFile.Create(Self);
       FMemoFile := TDbaseMemoFile.Create(Self);
@@ -802,6 +809,7 @@ begin
 //  lDataHdr.RecordCount := RecordCount;
 //  lDataHdr.RecordCount := RecordCount;
   inherited WriteHeader;
   inherited WriteHeader;
 
 
+  // Write terminator at the end of the file, after the records:
   EofTerminator := $1A;
   EofTerminator := $1A;
   WriteBlock(@EofTerminator, 1, CalcPageOffset(RecordCount+1));
   WriteBlock(@EofTerminator, 1, CalcPageOffset(RecordCount+1));
 end;
 end;
@@ -923,7 +931,7 @@ begin
 
 
       // continue until header termination character found
       // continue until header termination character found
       // or end of header reached
       // or end of header reached
-    until (I > lColumnCount) or (ReadChar = $0D);
+    until (I > lColumnCount) or (ReadChar = FIELD_DESCRIPTOR_ARRAY_TERMINATOR);
 
 
     // test if not too many fields
     // test if not too many fields
     if FFieldDefs.Count >= 4096 then
     if FFieldDefs.Count >= 4096 then
@@ -983,7 +991,7 @@ begin
         end;
         end;
       end;
       end;
       // read custom properties...not implemented
       // read custom properties...not implemented
-      // read RI properties...not implemented
+      // read RI/referential integrity properties...not implemented
     end;
     end;
   finally
   finally
     HeaderSize := PDbfHdr(Header)^.FullHdrSize;
     HeaderSize := PDbfHdr(Header)^.FullHdrSize;
@@ -1571,7 +1579,7 @@ begin
       end;
       end;
     'B':    // Foxpro double
     'B':    // Foxpro double
       begin
       begin
-        if FDbfVersion in [xFoxPro,xVisualFoxPro] then
+        if (FDbfVersion in [xFoxPro,xVisualFoxPro]) then
         begin
         begin
           Result := true;
           Result := true;
           if Dst <> nil then
           if Dst <> nil then

+ 3 - 1
packages/fcl-db/src/dbase/dbf_struct.inc

@@ -50,7 +50,9 @@ type
   rFieldDescIII = packed record
   rFieldDescIII = packed record
     FieldName       : array[0..10] of Char;
     FieldName       : array[0..10] of Char;
     FieldType       : Char;     // 11
     FieldType       : Char;     // 11
-    FieldOffset     : Integer;  // 12..15   only applicable to foxpro databases
+    FieldOffset     : Integer;  // 12..15
+    //FieldOffset only applicable to foxpro databases
+    //DBase III: address in memory
     FieldSize       : Byte;     // 16
     FieldSize       : Byte;     // 16
     FieldPrecision  : Byte;     // 17
     FieldPrecision  : Byte;     // 17
     FoxProFlags	    : Byte;	// 18
     FoxProFlags	    : Byte;	// 18