bits.inc 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. Raise EBitsError.Create('');
  17. end;
  18. procedure TBits.SetSize(Value: Integer);
  19. var
  20. hp : pointer;
  21. cvalue,csize : Integer;
  22. begin
  23. { ajust value to n*8 }
  24. cvalue:=Value;
  25. if cvalue mod 8<>0 then
  26. cvalue:=cvalue+(8-(cvalue mod 8));
  27. { store pointer to release it later }
  28. hp:=FBits;
  29. { ajust size to n*8 }
  30. csize:=FSize;
  31. if csize mod 8<>0 then
  32. csize:=csize+(8-(csize mod 8));
  33. if FSize>0 then
  34. begin
  35. { get new memory }
  36. GetMem(FBits,cvalue div 8);
  37. { clear the whole array }
  38. FillChar(FBits^,cvalue div 8,0);
  39. { copy old data }
  40. Move(hp^,FBits^,csize div 8);
  41. end
  42. else
  43. FBits:=nil;
  44. if assigned(hp) then
  45. FreeMem(hp,csize div 8);
  46. FSize:=Value;
  47. end;
  48. procedure TBits.SetBit(Index: Integer; Value: Boolean);
  49. type
  50. pbyte = ^byte;
  51. begin
  52. if (Index>=FSize) or (Index<0) then
  53. Error
  54. else
  55. begin
  56. if Value then
  57. pbyte(FBits)[Index div 8]:=pbyte(FBits)[Index div 8] or
  58. (1 shl (Index mod 8))
  59. else
  60. pbyte(FBits)[Index div 8]:=pbyte(FBits)[Index div 8] and
  61. not(1 shl (Index mod 8));
  62. end;
  63. end;
  64. function TBits.GetBit(Index: Integer): Boolean;
  65. type
  66. pbyte = ^byte;
  67. begin
  68. if (Index>=FSize) or (Index<0) then
  69. Error
  70. else
  71. GetBit:=(pbyte(FBits)[Index div 8] and (1 shl (Index mod 8)))<>0;
  72. end;
  73. destructor TBits.Destroy;
  74. var
  75. csize : Integer;
  76. begin
  77. { ajust size to n*8 }
  78. csize:=FSize;
  79. if csize mod 8<>0 then
  80. csize:=csize+(8-(csize mod 8));
  81. if assigned(FBits) then
  82. FreeMem(FBits,csize);
  83. inherited Destroy;
  84. end;
  85. function TBits.OpenBit: Integer;
  86. type
  87. pbyte = ^byte;
  88. var
  89. i : Integer;
  90. begin
  91. for i:=0 to FSize-1 do
  92. if (pbyte(FBits)[i div 8] and (1 shl (i mod 8)))=0 then
  93. begin
  94. OpenBit:=i;
  95. exit;
  96. end;
  97. SetSize(FSize+1);
  98. OpenBit:=FSize-1;
  99. end;
  100. {
  101. $Log$
  102. Revision 1.3 1998-11-04 14:36:29 michael
  103. Error handling always with exceptions
  104. Revision 1.2 1998/11/04 10:46:42 peter
  105. * exceptions work
  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. }