OVQUEUE.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * Seven Kingdoms: Ancient Adversaries
  3. *
  4. * Copyright 1997,1998 Enlight Software Ltd.
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. *
  19. */
  20. // Filename : OVQUEUE.CPP
  21. // Description : variable length queue
  22. #include <OVQUEUE.h>
  23. #include <ALL.h>
  24. #define QUEUE_SIZE_INC 0x1000
  25. // -------- begin of function VLenQueue::VLenQueue ----------//
  26. VLenQueue::VLenQueue() : queue_buf(NULL), queue_buf_size(0), queued_size(0), queue_ptr(NULL)
  27. {
  28. }
  29. VLenQueue::VLenQueue(int s) : queue_buf(mem_add(s)), queue_buf_size(s)
  30. {
  31. queued_size = 0;
  32. queue_ptr = NULL;
  33. }
  34. VLenQueue::VLenQueue(VLenQueue &q) : queue_buf(mem_add(q.queue_buf_size)),
  35. queue_buf_size(q.queue_buf_size)
  36. {
  37. memcpy( queue_buf, q.queue_buf, q.queued_size);
  38. queued_size = q.queued_size;
  39. queue_ptr = queue_buf + (q.queue_ptr - q.queue_buf);
  40. }
  41. // -------- end of function VLenQueue::VLenQueue ----------//
  42. // -------- begin of function VLenQueue::~VLenQueue ----------//
  43. VLenQueue::~VLenQueue()
  44. {
  45. if( queue_buf )
  46. mem_del(queue_buf);
  47. }
  48. // -------- end of function VLenQueue::~VLenQueue ----------//
  49. // -------- begin of function VLenQueue::operator= ----------//
  50. VLenQueue& VLenQueue::operator= (VLenQueue &q)
  51. {
  52. expand( q.queued_size );
  53. memcpy( queue_buf, q.queue_buf, queued_size = q.queued_size);
  54. queue_ptr = queue_buf + (q.queue_ptr - q.queue_buf);
  55. return *this;
  56. }
  57. // -------- end of function VLenQueue::operator= ----------//
  58. // -------- begin of function VLenQueue::clear ----------//
  59. void VLenQueue::clear()
  60. {
  61. queued_size = 0;
  62. queue_ptr = queue_buf;
  63. }
  64. // -------- end of function VLenQueue::clear ----------//
  65. // -------- begin of function VLenQueue::reserve ----------//
  66. char* VLenQueue::reserve( int s )
  67. {
  68. expand( queued_size + s);
  69. char *ret= queue_buf + queued_size;
  70. queued_size += s;
  71. return ret;
  72. }
  73. // -------- end of function VLenQueue::reserve ----------//
  74. // -------- begin of function VLenQueue::append_queue ----------//
  75. void VLenQueue::append_queue( VLenQueue &q )
  76. {
  77. memcpy( reserve(q.queued_size), q.queue_buf, q.queued_size) ;
  78. }
  79. // -------- end of function VLenQueue::append_queue ----------//
  80. // -------- begin of function VLenQueue::swap ----------//
  81. void VLenQueue::swap( VLenQueue &q)
  82. {
  83. char *tempPtr;
  84. int tempInt;
  85. tempPtr = queue_buf;
  86. queue_buf = q.queue_buf;
  87. q.queue_buf = tempPtr;
  88. tempInt = queue_buf_size;
  89. queue_buf_size = q.queue_buf_size;
  90. q.queue_buf_size = tempInt;
  91. tempInt = queued_size;
  92. queued_size = q.queued_size;
  93. q.queued_size = tempInt;
  94. tempPtr = queue_ptr;
  95. queue_ptr = q.queue_ptr;
  96. q.queue_ptr = tempPtr;
  97. }
  98. // -------- end of function VLenQueue::swap ----------//
  99. // -------- begin of function VLenQueue::length ----------//
  100. int VLenQueue::length()
  101. {
  102. return queued_size;
  103. }
  104. // -------- end of function VLenQueue::length ----------//
  105. // -------- begin of function VLenQueue::expand ----------//
  106. void VLenQueue::expand( int newSize)
  107. {
  108. if( newSize > queued_size )
  109. {
  110. char *oldBuf = queue_buf;
  111. queue_buf_size = newSize + (QUEUE_SIZE_INC - newSize % QUEUE_SIZE_INC);
  112. queue_buf = mem_resize( queue_buf, queue_buf_size);
  113. queue_ptr = queue_buf + (queue_ptr - oldBuf);
  114. }
  115. }
  116. // -------- end of function VLenQueue::expand ----------//