jshell.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. ** Command & Conquer Generals Zero Hour(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /***********************************************************************************************
  19. *** C O N F I D E N T I A L --- W E S T W O O D S T U D I O S ***
  20. ***********************************************************************************************
  21. * *
  22. * Project Name : Command & Conquer *
  23. * *
  24. * $Archive:: /Commando/Code/Tools/pluglib/jshell.cpp $*
  25. * *
  26. * $Author:: Greg_h $*
  27. * *
  28. * $Modtime:: 11/07/00 2:32p $*
  29. * *
  30. * $Revision:: 28 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * Fatal -- General purpose fatal error handler. *
  35. * Set_Bit -- Set bit in a bit array. *
  36. * Get_Bit -- Fetch the bit value from a bit array. *
  37. * First_True_Bit -- Return with the first true bit index. *
  38. * First_False_Bit -- Find the first false bit in the bit array. *
  39. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  40. #include "always.h"
  41. /***********************************************************************************************
  42. * Set_Bit -- Set bit in a bit array. *
  43. * *
  44. * This routine is used to set (or clear) a bit in a bit array. *
  45. * *
  46. * INPUT: array -- Pointer to the bit array. *
  47. * *
  48. * bit -- The bit number to set. I.e., bit 32 is the first bit in the second *
  49. * long word of memory. *
  50. * *
  51. * value -- The value to set the bit. The only values supported are 0 and 1. *
  52. * *
  53. * OUTPUT: none *
  54. * *
  55. * WARNINGS: Be sure that the array is big enough to set the specified bit. *
  56. * *
  57. * HISTORY: *
  58. * 10/06/1997 JLB : Created. *
  59. *=============================================================================================*/
  60. void Set_Bit(void * array, int bit, int value)
  61. {
  62. unsigned char mask = (unsigned char)(1 << (bit % 8));
  63. if (value != 0) {
  64. *((unsigned char *)array + (bit/8)) |= mask;
  65. } else {
  66. *((unsigned char *)array + (bit/8)) &= (unsigned char)~mask;
  67. }
  68. }
  69. /***********************************************************************************************
  70. * Get_Bit -- Fetch the bit value from a bit array. *
  71. * *
  72. * This routine will fetch the specified bit value from the bit array. This is the *
  73. * counterpart function to the Set_Bit() function. *
  74. * *
  75. * INPUT: array -- Pointer to the bit array to fetch the bit value from. *
  76. * *
  77. * bit -- The bit number to fetch. *
  78. * *
  79. * OUTPUT: Returns with the value of the bit. This return value will be either 1 or 0. *
  80. * *
  81. * WARNINGS: none *
  82. * *
  83. * HISTORY: *
  84. * 10/06/1997 JLB : Created. *
  85. *=============================================================================================*/
  86. int Get_Bit(void const * array, int bit)
  87. {
  88. unsigned char mask = (unsigned char)(1 << (bit % 8));
  89. return((*((unsigned char *)array + (bit/8)) & mask) != 0);
  90. }
  91. /***********************************************************************************************
  92. * First_True_Bit -- Return with the first true bit index. *
  93. * *
  94. * This routine will scan the bit array and return with the index for the first true bit *
  95. * in the array. *
  96. * *
  97. * INPUT: array -- Pointer to the bit array to scan. *
  98. * *
  99. * OUTPUT: Returns with the index of the first true (set to 1) bit in the array. *
  100. * *
  101. * WARNINGS: This routine does not stop at the end of the array (it doesn't know where the *
  102. * end is) so there must be at least one true bit in the array or else it will *
  103. * end up scanning past the end (undefined results in that case). *
  104. * *
  105. * HISTORY: *
  106. * 10/06/1997 JLB : Created. *
  107. *=============================================================================================*/
  108. int First_True_Bit(void const * array)
  109. {
  110. int index = 0;
  111. while (*((unsigned char *)array) == 0) {
  112. index++;
  113. array = ((char*)array) + 1;
  114. }
  115. int subindex;
  116. for (subindex = 0; subindex < 8; subindex++) {
  117. if (Get_Bit(array, subindex)) break;
  118. }
  119. return(index * 8 + subindex);
  120. }
  121. /***********************************************************************************************
  122. * First_False_Bit -- Find the first false bit in the bit array. *
  123. * *
  124. * This routine will scan the bit array and return with the index of the first false (set *
  125. * to 0) bit found. *
  126. * *
  127. * INPUT: array -- Pointer to the bit array to scan. *
  128. * *
  129. * OUTPUT: Returns with the index of the first false bit found in the array. *
  130. * *
  131. * WARNINGS: This routine will not stop scanning until a false bit was found. This means *
  132. * that there must be at least one false bit in the array or else it will scan *
  133. * past the end of the array. *
  134. * *
  135. * HISTORY: *
  136. * 10/06/1997 JLB : Created. *
  137. *=============================================================================================*/
  138. int First_False_Bit(void const * array)
  139. {
  140. int index = 0;
  141. while (*((unsigned char *)array) == 0xFF) {
  142. index++;
  143. array = ((char*)array) + 1;
  144. }
  145. int subindex;
  146. for (subindex = 0; subindex < 8; subindex++) {
  147. if (!Get_Bit(array, subindex)) break;
  148. }
  149. return(index * 8 + subindex);
  150. }