list.c 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /*
  2. ****************************************************************************
  3. *
  4. * MODULE: Vector library
  5. *
  6. * AUTHOR(S): Original author CERL, probably Dave Gerdes.
  7. * Update to GRASS 5.7 Radim Blazek.
  8. *
  9. * PURPOSE: Lower level functions for reading/writing/manipulating vectors.
  10. *
  11. * COPYRIGHT: (C) 2001 by the GRASS Development Team
  12. *
  13. * This program is free software under the GNU General Public
  14. * License (>=v2). Read the file COPYING that comes with GRASS
  15. * for details.
  16. *
  17. *****************************************************************************/
  18. #include <stdlib.h>
  19. #include <grass/vector.h>
  20. /* Init int_list */
  21. int dig_init_list(struct ilist *list)
  22. {
  23. list->value = NULL;
  24. list->n_values = 0;
  25. list->alloc_values = 0;
  26. return 1;
  27. }
  28. /* Init box list */
  29. int dig_init_boxlist(struct boxlist *list)
  30. {
  31. list->id = NULL;
  32. list->box = NULL;
  33. list->n_values = 0;
  34. list->alloc_values = 0;
  35. return 1;
  36. }
  37. /* Add item to list, does not check for duplicates */
  38. int dig_list_add(struct ilist *list, int val)
  39. {
  40. if (list->n_values == list->alloc_values) {
  41. size_t size = (list->n_values + 1000) * sizeof(int);
  42. void *p = G_realloc((void *)list->value, size);
  43. if (p == NULL)
  44. return 0;
  45. list->value = (int *)p;
  46. list->alloc_values = list->n_values + 1000;
  47. }
  48. list->value[list->n_values] = val;
  49. list->n_values++;
  50. return 1;
  51. }
  52. /* Add item to box list, does not check for duplicates */
  53. int dig_boxlist_add(struct boxlist *list, int id, struct bound_box box)
  54. {
  55. if (list->n_values == list->alloc_values) {
  56. size_t size = (list->n_values + 1000) * sizeof(int);
  57. void *p = G_realloc((void *)list->id, size);
  58. if (p == NULL)
  59. return 0;
  60. list->id = (int *)p;
  61. size = (list->n_values + 1000) * sizeof(struct bound_box);
  62. p = G_realloc((void *)list->box, size);
  63. if (p == NULL)
  64. return 0;
  65. list->box = (int *)p;
  66. list->alloc_values = list->n_values + 1000;
  67. }
  68. list->id[list->n_values] = id;
  69. list->box[list->n_values] = box;
  70. list->n_values++;
  71. return 1;
  72. }