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.
 
 
 
 
 
 

231 lines
5.1 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 "list.h"
  26. /*****************************************************************************/
  27. struct list *
  28. list_create(void)
  29. {
  30. struct list *self;
  31. self = (struct list *)g_malloc(sizeof(struct list), 1);
  32. self->grow_by = 10;
  33. self->alloc_size = 10;
  34. self->items = (tbus *)g_malloc(sizeof(tbus) * 10, 1);
  35. return self;
  36. }
  37. /*****************************************************************************/
  38. void
  39. list_delete(struct list *self)
  40. {
  41. int i;
  42. if (self == 0)
  43. {
  44. return;
  45. }
  46. if (self->auto_free)
  47. {
  48. for (i = 0; i < self->count; i++)
  49. {
  50. g_free((void *)self->items[i]);
  51. self->items[i] = 0;
  52. }
  53. }
  54. g_free(self->items);
  55. g_free(self);
  56. }
  57. /*****************************************************************************/
  58. void
  59. list_add_item(struct list *self, tbus item)
  60. {
  61. tbus *p;
  62. int i;
  63. if (self->count >= self->alloc_size)
  64. {
  65. i = self->alloc_size;
  66. self->alloc_size += self->grow_by;
  67. p = (tbus *)g_malloc(sizeof(tbus) * self->alloc_size, 1);
  68. g_memcpy(p, self->items, sizeof(tbus) * i);
  69. g_free(self->items);
  70. self->items = p;
  71. }
  72. self->items[self->count] = item;
  73. self->count++;
  74. }
  75. /*****************************************************************************/
  76. tbus
  77. list_get_item(const struct list *self, int index)
  78. {
  79. if (index < 0 || index >= self->count)
  80. {
  81. return 0;
  82. }
  83. return self->items[index];
  84. }
  85. /*****************************************************************************/
  86. void
  87. list_clear(struct list *self)
  88. {
  89. int i;
  90. if (self->auto_free)
  91. {
  92. for (i = 0; i < self->count; i++)
  93. {
  94. g_free((void *)self->items[i]);
  95. self->items[i] = 0;
  96. }
  97. }
  98. g_free(self->items);
  99. self->count = 0;
  100. self->grow_by = 10;
  101. self->alloc_size = 10;
  102. self->items = (tbus *)g_malloc(sizeof(tbus) * 10, 1);
  103. }
  104. /*****************************************************************************/
  105. int
  106. list_index_of(struct list *self, tbus item)
  107. {
  108. int i;
  109. for (i = 0; i < self->count; i++)
  110. {
  111. if (self->items[i] == item)
  112. {
  113. return i;
  114. }
  115. }
  116. return -1;
  117. }
  118. /*****************************************************************************/
  119. void
  120. list_remove_item(struct list *self, int index)
  121. {
  122. int i;
  123. if (index >= 0 && index < self->count)
  124. {
  125. if (self->auto_free)
  126. {
  127. g_free((void *)self->items[index]);
  128. self->items[index] = 0;
  129. }
  130. for (i = index; i < (self->count - 1); i++)
  131. {
  132. self->items[i] = self->items[i + 1];
  133. }
  134. self->count--;
  135. }
  136. }
  137. /*****************************************************************************/
  138. void
  139. list_insert_item(struct list *self, int index, tbus item)
  140. {
  141. tbus *p;
  142. int i;
  143. if (index == self->count)
  144. {
  145. list_add_item(self, item);
  146. return;
  147. }
  148. if (index >= 0 && index < self->count)
  149. {
  150. self->count++;
  151. if (self->count > self->alloc_size)
  152. {
  153. i = self->alloc_size;
  154. self->alloc_size += self->grow_by;
  155. p = (tbus *)g_malloc(sizeof(tbus) * self->alloc_size, 1);
  156. g_memcpy(p, self->items, sizeof(tbus) * i);
  157. g_free(self->items);
  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. }
  167. /*****************************************************************************/
  168. /* append one list to another using strdup for each item in the list */
  169. /* begins copy at start_index, a zero based index on the source list */
  170. void
  171. list_append_list_strdup(struct list *self, struct list *dest, int start_index)
  172. {
  173. int index;
  174. tbus item;
  175. char *dup;
  176. for (index = start_index; index < self->count; index++)
  177. {
  178. item = list_get_item(self, index);
  179. dup = g_strdup((char *)item);
  180. list_add_item(dest, (tbus)dup);
  181. }
  182. }
  183. /*****************************************************************************/
  184. void
  185. list_dump_items(struct list *self)
  186. {
  187. int index;
  188. if (self->count == 0)
  189. {
  190. g_writeln("List is empty");
  191. }
  192. for (index = 0; index < self->count; index++)
  193. {
  194. g_writeln("%d: %p", index, (void *) list_get_item(self, index));
  195. }
  196. }