Browse Source

* Base units of the package now can be compiled also with Delphi, patch from Dariusz Mazur (with some modifications).

git-svn-id: trunk@13627 -
sergei 16 years ago
parent
commit
425170167d

+ 18 - 14
packages/fcl-xml/src/dom.pp

@@ -18,8 +18,8 @@
   This unit provides classes which implement the interfaces defined in the
   This unit provides classes which implement the interfaces defined in the
   DOM (Document Object Model) specification.
   DOM (Document Object Model) specification.
   The current state is:
   The current state is:
-  DOM Level 1  -  Almost completely implemented
-  DOM Level 2  -  Partially implemented
+  DOM Levels 1 and 2 -  Completely implemented
+  DOM Level 3  -  Partially implemented
 
 
   Specification used for this implementation:
   Specification used for this implementation:
 
 
@@ -99,9 +99,13 @@ type
   TDOMProcessingInstruction = class;
   TDOMProcessingInstruction = class;
 
 
   TDOMAttrDef = class;
   TDOMAttrDef = class;
-  PNodePool = ^TNodePool;
   TNodePool = class;
   TNodePool = class;
+  PNodePoolArray = ^TNodePoolArray;
+  TNodePoolArray = array[0..0] of TNodePool;
 
 
+{$ifndef fpc}
+  TFPList = TList;
+{$endif}
 
 
 // -------------------------------------------------------
 // -------------------------------------------------------
 //   DOMString
 //   DOMString
@@ -430,7 +434,7 @@ type
     FEmptyNode: TDOMElement;
     FEmptyNode: TDOMElement;
     FNodeLists: THashTable;
     FNodeLists: THashTable;
     FMaxPoolSize: Integer;
     FMaxPoolSize: Integer;
-    FPools: PNodePool;
+    FPools: PNodePoolArray;
     FDocumentURI: DOMString;
     FDocumentURI: DOMString;
     function GetDocumentElement: TDOMElement;
     function GetDocumentElement: TDOMElement;
     function GetDocType: TDOMDocumentType;
     function GetDocType: TDOMDocumentType;
@@ -2081,7 +2085,7 @@ begin
   FEmptyNode.Free;
   FEmptyNode.Free;
   inherited Destroy;
   inherited Destroy;
   for i := 0 to (FMaxPoolSize div sizeof(TNodePool))-1 do
   for i := 0 to (FMaxPoolSize div sizeof(TNodePool))-1 do
-    FPools[i].Free;
+    FPools^[i].Free;
   FreeMem(FPools);
   FreeMem(FPools);
   FNames.Free;           // free the nametable after inherited has destroyed the children
   FNames.Free;           // free the nametable after inherited has destroyed the children
                          // (because children reference the nametable)
                          // (because children reference the nametable)
@@ -2099,11 +2103,11 @@ begin
     Exit;
     Exit;
   end;
   end;
 
 
-  pp := FPools[size div sizeof(TNodePool)];
+  pp := FPools^[size div sizeof(TNodePool)];
   if pp = nil then
   if pp = nil then
   begin
   begin
     pp := TNodePool.Create(size);
     pp := TNodePool.Create(size);
-    FPools[size div sizeof(TNodePool)] := pp;
+    FPools^[size div sizeof(TNodePool)] := pp;
   end;
   end;
   Result := pp.AllocNode(AClass);
   Result := pp.AllocNode(AClass);
 end;
 end;
@@ -3163,16 +3167,16 @@ end;
 destructor TNodePool.Destroy;
 destructor TNodePool.Destroy;
 var
 var
   ext, next: PExtent;
   ext, next: PExtent;
-  ptr, ptr_end: Pointer;
+  ptr, ptr_end: PAnsiChar;
   sz: Integer;
   sz: Integer;
 begin
 begin
   ext := FCurrExtent;
   ext := FCurrExtent;
-  ptr := Pointer(FCurrBlock) + FElementSize;
+  ptr := PAnsiChar(FCurrBlock) + FElementSize;
   sz := FCurrExtentSize;
   sz := FCurrExtentSize;
   while Assigned(ext) do
   while Assigned(ext) do
   begin
   begin
     // call destructors for everyone still there
     // call destructors for everyone still there
-    ptr_end := Pointer(ext) + sizeof(TExtent) + (sz - 1) * FElementSize;
+    ptr_end := PAnsiChar(ext) + sizeof(TExtent) + (sz - 1) * FElementSize;
     while ptr <= ptr_end do
     while ptr <= ptr_end do
     begin
     begin
       if TDOMNode(ptr).FPool = Self then
       if TDOMNode(ptr).FPool = Self then
@@ -3184,7 +3188,7 @@ begin
     FreeMem(ext);
     FreeMem(ext);
     ext := next;
     ext := next;
     sz := sz div 2;
     sz := sz div 2;
-    ptr := Pointer(ext) + sizeof(TExtent);
+    ptr := PAnsiChar(ext) + sizeof(TExtent);
   end;
   end;
   inherited Destroy;
   inherited Destroy;
 end;
 end;
@@ -3194,13 +3198,13 @@ var
   ext: PExtent;
   ext: PExtent;
 begin
 begin
   Assert((FCurrExtent = nil) or
   Assert((FCurrExtent = nil) or
-    (Pointer(FCurrBlock) = Pointer(FCurrExtent) + sizeof(TExtent)));
+    (PAnsiChar(FCurrBlock) = PAnsiChar(FCurrExtent) + sizeof(TExtent)));
   Assert(AElemCount > 0);
   Assert(AElemCount > 0);
 
 
   GetMem(ext, sizeof(TExtent) + AElemCount * FElementSize);
   GetMem(ext, sizeof(TExtent) + AElemCount * FElementSize);
   ext^.Next := FCurrExtent;
   ext^.Next := FCurrExtent;
   // point to the beginning of the last block of extent
   // point to the beginning of the last block of extent
-  FCurrBlock := TDOMNode(Pointer(ext) + sizeof(TExtent) + (AElemCount - 1) * FElementSize);
+  FCurrBlock := TDOMNode(PAnsiChar(ext) + sizeof(TExtent) + (AElemCount - 1) * FElementSize);
   FCurrExtent := ext;
   FCurrExtent := ext;
   FCurrExtentSize := AElemCount;
   FCurrExtentSize := AElemCount;
 end;
 end;
@@ -3214,7 +3218,7 @@ begin
   end
   end
   else
   else
   begin
   begin
-    if Pointer(FCurrBlock) = Pointer(FCurrExtent) + sizeof(TExtent) then
+    if PAnsiChar(FCurrBlock) = PAnsiChar(FCurrExtent) + sizeof(TExtent) then
       AddExtent(FCurrExtentSize * 2);
       AddExtent(FCurrExtentSize * 2);
     Result := FCurrBlock;
     Result := FCurrBlock;
     Dec(PAnsiChar(FCurrBlock), FElementSize);
     Dec(PAnsiChar(FCurrBlock), FElementSize);

+ 15 - 15
packages/fcl-xml/src/xmlcfg.pp

@@ -18,7 +18,7 @@
   configuration data
   configuration data
 }
 }
 
 
-{$MODE objfpc}
+{$ifdef fpc}{$MODE objfpc}{$endif}
 {$H+}
 {$H+}
 
 
 unit XMLCfg;
 unit XMLCfg;
@@ -51,7 +51,7 @@ type
     FStartEmpty: Boolean;
     FStartEmpty: Boolean;
     FUseEscaping: Boolean;
     FUseEscaping: Boolean;
     FRootName: DOMString;
     FRootName: DOMString;
-    procedure SetFilename(const AFilename: String; ForceReload: Boolean);
+    procedure SetFilenameForce(const AFilename: String; ForceReload: Boolean);
     procedure SetFilename(const AFilename: String);
     procedure SetFilename(const AFilename: String);
     procedure SetStartEmpty(AValue: Boolean);
     procedure SetStartEmpty(AValue: Boolean);
     procedure SetRootName(const AValue: DOMString);
     procedure SetRootName(const AValue: DOMString);
@@ -66,15 +66,15 @@ type
     destructor Destroy; override;
     destructor Destroy; override;
     procedure Clear;
     procedure Clear;
     procedure Flush;    // Writes the XML file
     procedure Flush;    // Writes the XML file
-    function  GetValue(const APath, ADefault: String): String;
-    function  GetValue(const APath: String; ADefault: Integer): Integer;
-    function  GetValue(const APath: String; ADefault: Boolean): Boolean;
-    procedure SetValue(const APath, AValue: String);
-    procedure SetDeleteValue(const APath, AValue, DefValue: String);
-    procedure SetValue(const APath: String; AValue: Integer);
-    procedure SetDeleteValue(const APath: String; AValue, DefValue: Integer);
-    procedure SetValue(const APath: String; AValue: Boolean);
-    procedure SetDeleteValue(const APath: String; AValue, DefValue: Boolean);
+    function  GetValue(const APath, ADefault: String): String; overload;
+    function  GetValue(const APath: String; ADefault: Integer): Integer; overload;
+    function  GetValue(const APath: String; ADefault: Boolean): Boolean; overload;
+    procedure SetValue(const APath, AValue: String); overload;
+    procedure SetDeleteValue(const APath, AValue, DefValue: String); overload;
+    procedure SetValue(const APath: String; AValue: Integer); overload;
+    procedure SetDeleteValue(const APath: String; AValue, DefValue: Integer); overload;
+    procedure SetValue(const APath: String; AValue: Boolean); overload;
+    procedure SetDeleteValue(const APath: String; AValue, DefValue: Boolean); overload;
     procedure DeletePath(const APath: string);
     procedure DeletePath(const APath: string);
     procedure DeleteValue(const APath: string);
     procedure DeleteValue(const APath: string);
     property Modified: Boolean read FModified;
     property Modified: Boolean read FModified;
@@ -300,7 +300,7 @@ procedure TXMLConfig.Loaded;
 begin
 begin
   inherited Loaded;
   inherited Loaded;
   if Length(Filename) > 0 then
   if Length(Filename) > 0 then
-    SetFilename(Filename, true);              // Load the XML config file
+    SetFilenameForce(Filename, true);              // Load the XML config file
 end;
 end;
 
 
 function TXMLConfig.FindNode(const APath: String;
 function TXMLConfig.FindNode(const APath: String;
@@ -370,7 +370,7 @@ begin
     Result := s;
     Result := s;
 end;
 end;
 
 
-procedure TXMLConfig.SetFilename(const AFilename: String; ForceReload: Boolean);
+procedure TXMLConfig.SetFilenameForce(const AFilename: String; ForceReload: Boolean);
 begin
 begin
   {$IFDEF MEM_CHECK}CheckHeapWrtMemCnt('TXMLConfig.SetFilename A '+AFilename);{$ENDIF}
   {$IFDEF MEM_CHECK}CheckHeapWrtMemCnt('TXMLConfig.SetFilename A '+AFilename);{$ENDIF}
   if (not ForceReload) and (FFilename = AFilename) then
   if (not ForceReload) and (FFilename = AFilename) then
@@ -400,7 +400,7 @@ end;
 
 
 procedure TXMLConfig.SetFilename(const AFilename: String);
 procedure TXMLConfig.SetFilename(const AFilename: String);
 begin
 begin
-  SetFilename(AFilename, False);
+  SetFilenameForce(AFilename, False);
 end;
 end;
 
 
 procedure TXMLConfig.SetRootName(const AValue: DOMString);
 procedure TXMLConfig.SetRootName(const AValue: DOMString);
@@ -424,7 +424,7 @@ begin
   begin
   begin
     FStartEmpty := AValue;
     FStartEmpty := AValue;
     if (not AValue) and not Modified then
     if (not AValue) and not Modified then
-      SetFilename(Filename, True);
+      SetFilenameForce(Filename, True);
   end;
   end;
 end;
 end;
 
 

+ 29 - 17
packages/fcl-xml/src/xmlutils.pp

@@ -14,7 +14,7 @@
  **********************************************************************}
  **********************************************************************}
 unit xmlutils;
 unit xmlutils;
 
 
-{$mode objfpc}
+{$ifdef fpc}{$mode objfpc}{$endif}
 {$H+}
 {$H+}
 
 
 interface
 interface
@@ -38,6 +38,10 @@ function WStrLIComp(S1, S2: PWideChar; Len: Integer): Integer;
 { a simple hash table with WideString keys }
 { a simple hash table with WideString keys }
 
 
 type
 type
+{$ifndef fpc}
+  PtrInt = LongInt;
+{$endif}  
+
   PPHashItem = ^PHashItem;
   PPHashItem = ^PHashItem;
   PHashItem = ^THashItem;
   PHashItem = ^THashItem;
   THashItem = record
   THashItem = record
@@ -46,6 +50,8 @@ type
     Next: PHashItem;
     Next: PHashItem;
     Data: TObject;
     Data: TObject;
   end;
   end;
+  THashItemArray = array[0..0] of PHashItem;
+  PHashItemArray = ^THashItemArray;
 
 
   THashForEach = function(Entry: PHashItem; arg: Pointer): Boolean;
   THashForEach = function(Entry: PHashItem; arg: Pointer): Boolean;
 
 
@@ -53,7 +59,7 @@ type
   private
   private
     FCount: LongWord;
     FCount: LongWord;
     FBucketCount: LongWord;
     FBucketCount: LongWord;
-    FBucket: PPHashItem;
+    FBucket: PHashItemArray;
     FOwnsObjects: Boolean;
     FOwnsObjects: Boolean;
     function Lookup(Key: PWideChar; KeyLength: Integer; out Found: Boolean; CanCreate: Boolean): PHashItem;
     function Lookup(Key: PWideChar; KeyLength: Integer; out Found: Boolean; CanCreate: Boolean): PHashItem;
     procedure Resize(NewCapacity: LongWord);
     procedure Resize(NewCapacity: LongWord);
@@ -73,7 +79,6 @@ type
 
 
 { another hash, for detecting duplicate namespaced attributes without memory allocations }
 { another hash, for detecting duplicate namespaced attributes without memory allocations }
 
 
-  PExpHashEntry = ^TExpHashEntry;
   TExpHashEntry = record
   TExpHashEntry = record
     rev: LongWord;
     rev: LongWord;
     hash: LongWord;
     hash: LongWord;
@@ -81,12 +86,14 @@ type
     lname: PWideChar;
     lname: PWideChar;
     lnameLen: Integer;
     lnameLen: Integer;
   end;
   end;
+  TExpHashEntryArray = array[0..0] of TExpHashEntry;
+  PExpHashEntryArray = ^TExpHashEntryArray;
 
 
   TDblHashArray = class(TObject)
   TDblHashArray = class(TObject)
   private
   private
     FSizeLog: Integer;
     FSizeLog: Integer;
     FRevision: LongWord;
     FRevision: LongWord;
-    FData: PExpHashEntry;
+    FData: PExpHashEntryArray;
   public  
   public  
     procedure Init(NumSlots: Integer);
     procedure Init(NumSlots: Integer);
     function Locate(uri: PWideString; localName: PWideChar; localLength: Integer): Boolean;
     function Locate(uri: PWideString; localName: PWideChar; localLength: Integer): Boolean;
@@ -128,7 +135,7 @@ begin
   Result := Xml11Pg;
   Result := Xml11Pg;
 end;
 end;
 
 
-function IsXml11Char(Value: PWideChar; var Index: Integer): Boolean;
+function IsXml11Char(Value: PWideChar; var Index: Integer): Boolean; overload;
 begin
 begin
   if (Value[Index] >= #$D800) and (Value[Index] <= #$DB7F) then
   if (Value[Index] >= #$D800) and (Value[Index] <= #$DB7F) then
   begin
   begin
@@ -139,7 +146,7 @@ begin
     Result := False;
     Result := False;
 end;
 end;
 
 
-function IsXml11Char(const Value: WideString; var Index: Integer): Boolean;
+function IsXml11Char(const Value: WideString; var Index: Integer): Boolean; overload;
 begin
 begin
   if (Value[Index] >= #$D800) and (Value[Index] <= #$DB7F) then
   if (Value[Index] >= #$D800) and (Value[Index] <= #$DB7F) then
   begin
   begin
@@ -346,7 +353,11 @@ end;
 
 
 function KeyCompare(const Key1: WideString; Key2: Pointer; Key2Len: Integer): Boolean;
 function KeyCompare(const Key1: WideString; Key2: Pointer; Key2Len: Integer): Boolean;
 begin
 begin
+{$IFDEF FPC}
   Result := (Length(Key1)=Key2Len) and (CompareWord(Pointer(Key1)^, Key2^, Key2Len) = 0);
   Result := (Length(Key1)=Key2Len) and (CompareWord(Pointer(Key1)^, Key2^, Key2Len) = 0);
+{$ELSE}
+  Result := (Length(Key1)=Key2Len) and CompareMem(Pointer(Key1), Key2, Key2Len*2);
+{$ENDIF}
 end;
 end;
 
 
 { THashTable }
 { THashTable }
@@ -377,7 +388,7 @@ var
 begin
 begin
   for I := 0 to FBucketCount-1 do
   for I := 0 to FBucketCount-1 do
   begin
   begin
-    item := FBucket[I];
+    item := FBucket^[I];
     while Assigned(item) do
     while Assigned(item) do
     begin
     begin
       next := item^.Next;
       next := item^.Next;
@@ -386,8 +397,8 @@ begin
       Dispose(item);
       Dispose(item);
       item := next;
       item := next;
     end;
     end;
+    FBucket^[I] := nil;
   end;
   end;
-  FillChar(FBucket^, FBucketCount * sizeof(PHashItem), 0);
 end;
 end;
 
 
 function THashTable.Find(Key: PWideChar; KeyLen: Integer): PHashItem;
 function THashTable.Find(Key: PWideChar; KeyLen: Integer): PHashItem;
@@ -460,14 +471,15 @@ end;
 
 
 procedure THashTable.Resize(NewCapacity: LongWord);
 procedure THashTable.Resize(NewCapacity: LongWord);
 var
 var
-  p, chain: PPHashItem;
+  p: PHashItemArray;
+  chain: PPHashItem;
   i: Integer;
   i: Integer;
   e, n: PHashItem;
   e, n: PHashItem;
 begin
 begin
   p := AllocMem(NewCapacity * sizeof(PHashItem));
   p := AllocMem(NewCapacity * sizeof(PHashItem));
   for i := 0 to FBucketCount-1 do
   for i := 0 to FBucketCount-1 do
   begin
   begin
-    e := FBucket[i];
+    e := FBucket^[i];
     while Assigned(e) do
     while Assigned(e) do
     begin
     begin
       chain := @p[e^.HashValue mod NewCapacity];
       chain := @p[e^.HashValue mod NewCapacity];
@@ -538,7 +550,7 @@ var
 begin
 begin
   for i := 0 to FBucketCount-1 do
   for i := 0 to FBucketCount-1 do
   begin
   begin
-    e := FBucket[i];
+    e := FBucket^[i];
     while Assigned(e) do
     while Assigned(e) do
     begin
     begin
       if not proc(e, arg) then
       if not proc(e, arg) then
@@ -572,7 +584,7 @@ begin
   begin
   begin
     FRevision := $FFFFFFFF;
     FRevision := $FFFFFFFF;
     for i := (1 shl FSizeLog)-1 downto 0 do
     for i := (1 shl FSizeLog)-1 downto 0 do
-      FData[i].rev := FRevision;
+      FData^[i].rev := FRevision;
   end;
   end;
   Dec(FRevision);
   Dec(FRevision);
 end;
 end;
@@ -591,18 +603,18 @@ begin
   step := (HashValue and (not mask)) shr (FSizeLog-1) and (mask shr 2) or 1;
   step := (HashValue and (not mask)) shr (FSizeLog-1) and (mask shr 2) or 1;
   idx := HashValue and mask;
   idx := HashValue and mask;
   result := True;
   result := True;
-  while FData[idx].rev = FRevision do
+  while FData^[idx].rev = FRevision do
   begin
   begin
-    if (HashValue = FData[idx].hash) and (FData[idx].uriPtr^ = uri^) and
-      (FData[idx].lnameLen = localLength) and
-       CompareMem(FData[idx].lname, localName, localLength * sizeof(WideChar)) then
+    if (HashValue = FData^[idx].hash) and (FData^[idx].uriPtr^ = uri^) and
+      (FData^[idx].lnameLen = localLength) and
+       CompareMem(FData^[idx].lname, localName, localLength * sizeof(WideChar)) then
       Exit;
       Exit;
     if idx < step then
     if idx < step then
       Inc(idx, (1 shl FSizeLog) - step)
       Inc(idx, (1 shl FSizeLog) - step)
     else
     else
       Dec(idx, step);
       Dec(idx, step);
   end;
   end;
-  with FData[idx] do
+  with FData^[idx] do
   begin
   begin
     rev := FRevision;
     rev := FRevision;
     hash := HashValue;
     hash := HashValue;

+ 1 - 1
packages/fcl-xml/src/xmlwrite.pp

@@ -17,7 +17,7 @@
 
 
 unit XMLWrite;
 unit XMLWrite;
 
 
-{$MODE objfpc}
+{$ifdef fpc}{$MODE objfpc}{$endif}
 {$H+}
 {$H+}
 
 
 interface
 interface