You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

193 lines
4.2 KiB

  1. /**
  2. * xrdp: A Remote Desktop Protocol server.
  3. *
  4. * Copyright (C) Jay Sorg 2004-2014
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. *
  18. * simple list
  19. */
  20. #if defined(HAVE_CONFIG_H)
  21. #include <config_ac.h>
  22. #endif
  23. #include "arch.h"
  24. #include "os_calls.h"
  25. #include "list16.h"
  26. /*****************************************************************************/
  27. struct list16 *
  28. list16_create(void)
  29. {
  30. struct list16 *self;
  31. self = (struct list16 *)g_malloc(sizeof(struct list16), 0);
  32. list16_init(self);
  33. return self;
  34. }
  35. /*****************************************************************************/
  36. void
  37. list16_delete(struct list16 *self)
  38. {
  39. if (self == 0)
  40. {
  41. return;
  42. }
  43. list16_deinit(self);
  44. g_free(self);
  45. }
  46. /*****************************************************************************/
  47. void
  48. list16_init(struct list16* self)
  49. {
  50. g_memset(self, 0, sizeof(struct list16));
  51. self->max_count = 4;
  52. self->items = self->mitems;
  53. }
  54. /*****************************************************************************/
  55. void
  56. list16_deinit(struct list16* self)
  57. {
  58. if (self->items != self->mitems)
  59. {
  60. g_free(self->items);
  61. }
  62. }
  63. /*****************************************************************************/
  64. void
  65. list16_add_item(struct list16 *self, tui16 item)
  66. {
  67. tui16 *p;
  68. int i;
  69. if (self->count >= self->max_count)
  70. {
  71. i = self->max_count;
  72. self->max_count += 4;
  73. p = (tui16 *)g_malloc(sizeof(tui16) * self->max_count, 1);
  74. g_memcpy(p, self->items, sizeof(tui16) * i);
  75. if (self->items != self->mitems)
  76. {
  77. g_free(self->items);
  78. }
  79. self->items = p;
  80. }
  81. self->items[self->count] = item;
  82. self->count++;
  83. }
  84. /*****************************************************************************/
  85. tui16
  86. list16_get_item(struct list16 *self, int index)
  87. {
  88. if (index < 0 || index >= self->count)
  89. {
  90. return 0;
  91. }
  92. return self->items[index];
  93. }
  94. /*****************************************************************************/
  95. void
  96. list16_clear(struct list16 *self)
  97. {
  98. if (self->items != self->mitems)
  99. {
  100. g_free(self->items);
  101. }
  102. self->count = 0;
  103. self->max_count = 4;
  104. self->items = self->mitems;
  105. }
  106. /*****************************************************************************/
  107. int
  108. list16_index_of(struct list16 *self, tui16 item)
  109. {
  110. int i;
  111. for (i = 0; i < self->count; i++)
  112. {
  113. if (self->items[i] == item)
  114. {
  115. return i;
  116. }
  117. }
  118. return -1;
  119. }
  120. /*****************************************************************************/
  121. void
  122. list16_remove_item(struct list16 *self, int index)
  123. {
  124. int i;
  125. if (index >= 0 && index < self->count)
  126. {
  127. for (i = index; i < (self->count - 1); i++)
  128. {
  129. self->items[i] = self->items[i + 1];
  130. }
  131. self->count--;
  132. }
  133. }
  134. /*****************************************************************************/
  135. void
  136. list16_insert_item(struct list16 *self, int index, tui16 item)
  137. {
  138. tui16 *p;
  139. int i;
  140. if (index == self->count)
  141. {
  142. list16_add_item(self, item);
  143. return;
  144. }
  145. if (index >= 0 && index < self->count)
  146. {
  147. self->count++;
  148. if (self->count > self->max_count)
  149. {
  150. i = self->max_count;
  151. self->max_count += 4;
  152. p = (tui16 *)g_malloc(sizeof(tui16) * self->max_count, 1);
  153. g_memcpy(p, self->items, sizeof(tui16) * i);
  154. if (self->items != self->mitems)
  155. {
  156. g_free(self->items);
  157. }
  158. self->items = p;
  159. }
  160. for (i = (self->count - 2); i >= index; i--)
  161. {
  162. self->items[i + 1] = self->items[i];
  163. }
  164. self->items[index] = item;
  165. }
  166. }