ilist.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. ****************************************************************************
  3. *
  4. * MODULE: gis 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 and manipulating integer list
  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/gis.h>
  20. /**
  21. * \brief Free allocated memory of an integer list
  22. *
  23. * \param list The pointer to an integer list
  24. *
  25. * */
  26. void G_free_ilist(struct ilist *list)
  27. {
  28. if(list->value)
  29. G_free(list->value);
  30. G_free(list);
  31. }
  32. /**
  33. * \brief Return a new integer list.
  34. *
  35. * G_fatal_error() will be invoked by the
  36. * allocation function.
  37. *
  38. * \return list The pointer to a new allocated integer list
  39. *
  40. * */
  41. struct ilist * G_new_ilist()
  42. {
  43. struct ilist *l = G_malloc(sizeof(struct ilist));
  44. l->value = NULL;
  45. G_init_ilist(l);
  46. return l;
  47. }
  48. /**
  49. * \brief Init an integer list and free allocated memory
  50. *
  51. * \param list The pointer to an integer list
  52. *
  53. * */
  54. void G_init_ilist(struct ilist *list)
  55. {
  56. if(list->value)
  57. G_free(list->value);
  58. list->value = NULL;
  59. list->n_values = 0;
  60. list->alloc_values = 0;
  61. }
  62. /**
  63. * \brief Add item to ilist
  64. *
  65. * This function adds an integer to the list but does not check for duplicates.
  66. * In case reallocation fails, G_fatal_error() will be invoked by the
  67. * allocation function.
  68. *
  69. * \param list The ilist pointer
  70. * \param val The value to attach
  71. *
  72. * */
  73. void G_ilist_add(struct ilist *list, int val)
  74. {
  75. if (list->n_values == list->alloc_values) {
  76. size_t size = (list->n_values + 1000) * sizeof(int);
  77. void *p = G_realloc((void *)list->value, size);
  78. list->value = (int *)p;
  79. list->alloc_values = list->n_values + 1000;
  80. }
  81. list->value[list->n_values] = val;
  82. list->n_values++;
  83. }