bits.inc 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. {
  2. $Id$
  3. This file is part of the Free Component Library (FCL)
  4. Copyright (c) 1998 by the Free Pascal development team
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. {****************************************************************************}
  12. {* TBits *}
  13. {****************************************************************************}
  14. procedure TBits.Error;
  15. begin
  16. {$ifdef NoExceptions}
  17. ;
  18. {$else}
  19. Raise(EBitsError);
  20. {$endif}
  21. end;
  22. procedure TBits.SetSize(Value: Integer);
  23. var
  24. hp : pointer;
  25. cvalue,csize : Integer;
  26. begin
  27. { ajust value to n*8 }
  28. cvalue:=Value;
  29. if cvalue mod 8<>0 then
  30. cvalue:=cvalue+(8-(cvalue mod 8));
  31. { store pointer to release it later }
  32. hp:=FBits;
  33. { ajust size to n*8 }
  34. csize:=FSize;
  35. if csize mod 8<>0 then
  36. csize:=csize+(8-(csize mod 8));
  37. if FSize>0 then
  38. begin
  39. { get new memory }
  40. GetMem(FBits,cvalue div 8);
  41. { clear the whole array }
  42. FillChar(FBits^,cvalue div 8,0);
  43. { copy old data }
  44. Move(hp^,FBits^,csize div 8);
  45. end
  46. else
  47. FBits:=nil;
  48. if assigned(hp) then
  49. FreeMem(hp,csize div 8);
  50. FSize:=Value;
  51. end;
  52. procedure TBits.SetBit(Index: Integer; Value: Boolean);
  53. type
  54. pbyte = ^byte;
  55. begin
  56. if (Index>=FSize) or (Index<0) then
  57. Error
  58. else
  59. begin
  60. if Value then
  61. pbyte(FBits)[Index div 8]:=pbyte(FBits)[Index div 8] or
  62. (1 shl (Index mod 8))
  63. else
  64. pbyte(FBits)[Index div 8]:=pbyte(FBits)[Index div 8] and
  65. not(1 shl (Index mod 8));
  66. end;
  67. end;
  68. function TBits.GetBit(Index: Integer): Boolean;
  69. type
  70. pbyte = ^byte;
  71. begin
  72. if (Index>=FSize) or (Index<0) then
  73. Error
  74. else
  75. GetBit:=(pbyte(FBits)[Index div 8] and (1 shl (Index mod 8)))<>0;
  76. end;
  77. destructor TBits.Destroy;
  78. var
  79. csize : Integer;
  80. begin
  81. { ajust size to n*8 }
  82. csize:=FSize;
  83. if csize mod 8<>0 then
  84. csize:=csize+(8-(csize mod 8));
  85. if assigned(FBits) then
  86. FreeMem(FBits,csize);
  87. inherited Destroy;
  88. end;
  89. function TBits.OpenBit: Integer;
  90. type
  91. pbyte = ^byte;
  92. var
  93. i : Integer;
  94. begin
  95. for i:=0 to FSize-1 do
  96. if (pbyte(FBits)[i div 8] and (1 shl (i mod 8)))=0 then
  97. begin
  98. OpenBit:=i;
  99. exit;
  100. end;
  101. SetSize(FSize+1);
  102. OpenBit:=FSize-1;
  103. end;
  104. {
  105. $Log$
  106. Revision 1.1 1998-05-04 14:30:11 michael
  107. * Split file according to Class; implemented dummys for all methods, so unit compiles.
  108. }