Changeset 37 for trunk/Packages/Common


Ignore:
Timestamp:
May 9, 2018, 1:22:44 PM (7 years ago)
Author:
chronos
Message:
  • Added: Multi-lingual support.
Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk

    • Property svn:ignore
      •  

        old new  
        33lib
        44backup
         5LibrePaint.exe
         6*.lrj
  • trunk/Packages/Common/UMemory.pas

    r28 r37  
    1010type
    1111
    12   { TMemory }
    13 
    14   TMemory = class
     12  { TBlock }
     13
     14  TBlock = class
    1515  private
    1616    FData: PByte;
    1717    FSize: Integer;
    18     function GetItem(Index: Integer): Byte;
    19     procedure SetItem(Index: Integer; AValue: Byte);
    20     procedure SetSize(AValue: Integer); virtual;
    21   public
    22     procedure Clear(Value: Byte = 0);
    23     procedure Assign(Source: TMemory);
     18    function GetItem(Index: Integer): Byte; virtual; abstract;
     19    procedure SetItem(Index: Integer; AValue: Byte); virtual; abstract;
     20    procedure SetSize(AValue: Integer); virtual; abstract;
     21  public
     22    procedure ReadBlock(Block: TBlock; Position: Integer); virtual;
     23    procedure WriteBlock(Block: TBlock; Position: Integer); virtual;
     24    procedure Clear(Value: Byte = 0); virtual;
     25    procedure Assign(Source: TBlock); virtual;
     26    property Size: Integer read FSize write SetSize;
     27    property Items[Index: Integer]: Byte read GetItem write SetItem; default;
     28  end;
     29
     30  { TMemory }
     31
     32  TMemory = class(TBlock)
     33  private
     34    FData: PByte;
     35    FSize: Integer;
     36    function GetItem(Index: Integer): Byte; override;
     37    procedure SetItem(Index: Integer; AValue: Byte); override;
     38    procedure SetSize(AValue: Integer); override;
     39  public
     40    procedure Clear(Value: Byte = 0); override;
     41    procedure Assign(Source: TBlock); override;
    2442    constructor Create;
    2543    destructor Destroy; override;
    2644    property Data: PByte read FData;
    27     property Size: Integer read FSize write SetSize;
    28     property Items[Index: Integer]: Byte read GetItem write SetItem; default;
    2945  end;
    3046
     
    4258  end;
    4359
     60  { TBitBlock }
     61
     62  TBitBlock = class
     63  private
     64    function GetItem(Index: Integer): Byte; virtual;
     65    function GetSize: Integer; virtual;
     66    procedure SetItem(Index: Integer; AValue: Byte); virtual;
     67    procedure SetSize(AValue: Integer); virtual;
     68  public
     69    procedure Invert; virtual;
     70    function GetInteger: Integer; virtual;
     71    procedure SetInteger(Value: Integer); virtual;
     72    procedure ReadBlock(Block: TBitBlock; Position: Integer); virtual;
     73    procedure WriteBlock(Block: TBitBlock; Position: Integer); virtual;
     74    procedure Clear(Value: Byte = 0); virtual;
     75    procedure Assign(Source: TBlock); virtual;
     76    property Size: Integer read GetSize write SetSize;
     77    property Items[Index: Integer]: Byte read GetItem write SetItem; default;
     78  end;
     79
     80  { TBitMemory }
     81
     82  TBitMemory = class(TBitBlock)
     83  private
     84    FData: PByte;
     85    FSize: Integer;
     86    function GetSize: Integer; override;
     87    procedure SetSize(AValue: Integer); override;
     88    function GetItem(Index: Integer): Byte; override;
     89    procedure SetItem(Index: Integer; AValue: Byte); override;
     90  public
     91    constructor Create;
     92    destructor Destroy; override;
     93    function GetInteger: Integer; override;
     94    procedure SetInteger(Value: Integer); override;
     95    procedure Clear(Value: Byte = 0); override;
     96    procedure ReadBlock(Block: TBitBlock; Position: Integer); override;
     97    procedure WriteBlock(Block: TBitBlock; Position: Integer); override;
     98    property Data: PByte read FData;
     99    procedure Invert; override;
     100  end;
     101
     102
    44103implementation
     104
     105{ TBitMemory }
     106
     107procedure TBitMemory.Clear(Value: Byte);
     108begin
     109  if (Size and 7) = 0 then begin
     110    if Value = 0 then FillChar(FData^, Size shr 3, 0)
     111      else FillChar(FData^, Size shr 3, $ff);
     112  end else inherited;
     113end;
     114
     115procedure TBitMemory.ReadBlock(Block: TBitBlock; Position: Integer);
     116begin
     117  if Block is TBitMemory then begin
     118    if (Position and 7) = 0 then begin
     119      if (Block.Size and 7) = 0 then
     120        Move(PByte(FData + Position shr 3)^, TBitMemory(Block).Data^, Block.Size shr 3)
     121        else inherited;
     122    end else inherited;
     123  end else inherited;
     124end;
     125
     126procedure TBitMemory.WriteBlock(Block: TBitBlock; Position: Integer);
     127begin
     128  if Block is TBitMemory then begin
     129    if (Position and 7) = 0 then begin
     130      if (Block.Size and 7) = 0 then
     131        Move(TBitMemory(Block).Data^, PByte(FData + Position shr 3)^, Block.Size shr 3)
     132        else inherited;
     133    end else inherited;
     134  end else inherited;
     135end;
     136
     137procedure TBitMemory.Invert;
     138var
     139  I: Integer;
     140begin
     141  if (Size and 7) = 0 then begin
     142    for I := 0 to (Size shr 3) - 1 do
     143      PByte(FData + I)^ := PByte(FData + I)^ xor $ff;
     144  end
     145  else inherited;
     146
     147end;
     148
     149function TBitMemory.GetInteger: Integer;
     150var
     151  I: Integer;
     152  V: Integer;
     153begin
     154  Result := 0;
     155  I := 0;
     156  while (I < 32) and (I < Size) do begin
     157    V := FData[I shr 3];
     158    V := V shl I;
     159    Result := Result or V;
     160//    Result := Result or (FData[I shr 3] shl I);
     161    Inc(I, 8);
     162  end;
     163  if Size < 32 then
     164    Result := Result and ((1 shl Size) - 1);
     165end;
     166
     167procedure TBitMemory.SetInteger(Value: Integer);
     168var
     169  I: Integer;
     170begin
     171  I := 0;
     172  while (I < 32) and (I < Size) do begin
     173    FData[I shr 3] := (Value shr I) and $ff;
     174    Inc(I, 8);
     175  end;
     176end;
     177
     178function TBitMemory.GetSize: Integer;
     179begin
     180  Result := FSize;
     181end;
     182
     183procedure TBitMemory.SetSize(AValue: Integer);
     184var
     185  ByteSize: Integer;
     186begin
     187  if AValue = FSize then Exit;
     188  FSize := AValue;
     189  ByteSize := FSize shr 3;
     190  if (FSize and 7) > 0 then Inc(ByteSize);
     191  FData := ReAllocMem(FData, ByteSize);
     192end;
     193
     194function TBitMemory.GetItem(Index: Integer): Byte;
     195begin
     196  if Index >= Size then raise Exception.Create('Out of range');
     197  Result := (PByte(FData + (Index shr 3))^ shr (Index and 7)) and 1;
     198end;
     199
     200procedure TBitMemory.SetItem(Index: Integer; AValue: Byte);
     201begin
     202  if Index >= Size then raise Exception.Create('Out of range, Size:' + IntToStr(Size) + ', Index:' + IntToStr(Index));
     203  PByte(FData + (Index shr 3))^ := PByte(FData + (Index shr 3))^ and not (1 shl (Index and 7))
     204    or ((AValue and 1) shl (Index and 7));
     205end;
     206
     207constructor TBitMemory.Create;
     208begin
     209  FData := nil;
     210end;
     211
     212destructor TBitMemory.Destroy;
     213begin
     214  FreeMem(FData);
     215  inherited Destroy;
     216end;
     217
     218{ TBitBlock }
     219
     220function TBitBlock.GetItem(Index: Integer): Byte;
     221begin
     222  Result := 0;
     223end;
     224
     225function TBitBlock.GetSize: Integer;
     226begin
     227  Result := 0;
     228end;
     229
     230procedure TBitBlock.SetItem(Index: Integer; AValue: Byte);
     231begin
     232
     233end;
     234
     235procedure TBitBlock.SetSize(AValue: Integer);
     236begin
     237end;
     238
     239procedure TBitBlock.Invert;
     240var
     241  I: Integer;
     242begin
     243  for I := 0 to Size - 1 do
     244    Items[I] := not Items[I];
     245end;
     246
     247function TBitBlock.GetInteger: Integer;
     248begin
     249  Result := 0;
     250end;
     251
     252procedure TBitBlock.SetInteger(Value: Integer);
     253begin
     254
     255end;
     256
     257procedure TBitBlock.ReadBlock(Block: TBitBlock; Position: Integer);
     258var
     259  I: Integer;
     260begin
     261  for I := 0 to Block.Size - 1 do
     262    Block.Items[I] := Items[Position + I];
     263end;
     264
     265procedure TBitBlock.WriteBlock(Block: TBitBlock; Position: Integer);
     266var
     267  I: Integer;
     268begin
     269  for I := 0 to Block.Size - 1 do
     270    Items[Position + I] := Block.Items[I];
     271end;
     272
     273procedure TBitBlock.Clear(Value: Byte);
     274var
     275  I: Integer;
     276begin
     277  for I := 0 to Size - 1 do
     278    Items[I] := Value;
     279end;
     280
     281procedure TBitBlock.Assign(Source: TBlock);
     282var
     283  I: Integer;
     284begin
     285  Size := Source.Size;
     286  for I := 0 to Size - 1 do
     287    Items[I] := Source.Items[I];
     288end;
     289
     290{ TBlock }
     291
     292procedure TBlock.ReadBlock(Block: TBlock; Position: Integer);
     293var
     294  I: Integer;
     295begin
     296  if Position + Block.Size > Size then raise Exception.Create('');
     297  for I := 0 to Block.Size - 1 do
     298    Items[I] := Items[Position + I];
     299end;
     300
     301procedure TBlock.WriteBlock(Block: TBlock; Position: Integer);
     302var
     303  I: Integer;
     304begin
     305  if Position + Block.Size > Size then raise Exception.Create('');
     306  for I := 0 to Block.Size - 1 do
     307    Items[Position + I] := Items[I];
     308end;
     309
     310procedure TBlock.Clear(Value: Byte);
     311var
     312  I: Integer;
     313begin
     314  for I := 0 to Size - 1 do
     315    Items[I] := Value;
     316end;
     317
     318procedure TBlock.Assign(Source: TBlock);
     319var
     320  I: Integer;
     321begin
     322  Size := Source.Size;
     323  for I := 0 to Size - 1 do
     324    Items[I] := Source.Items[I];
     325end;
    45326
    46327{ TPositionMemory }
     
    90371end;
    91372
    92 procedure TMemory.Assign(Source: TMemory);
    93 begin
    94   Size := Source.Size;
    95   Move(Source.Data^, FData^, Size);
     373procedure TMemory.Assign(Source: TBlock);
     374begin
     375  if Source is TMemory then begin
     376    Size := Source.Size;
     377    Move(TMemory(Source).Data^, FData^, Size);
     378  end else inherited;
    96379end;
    97380
Note: See TracChangeset for help on using the changeset viewer.