list.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /**
  2. * \file list.c
  3. *
  4. * \brief Implementation of a FIFO list of messages
  5. *
  6. * \author Porta Claudio
  7. *
  8. * This program is free software under the GPL (>=v2)
  9. * Read the COPYING file that comes with GRASS for details.
  10. *
  11. * \version 1.0
  12. *
  13. *
  14. */
  15. #include <stdlib.h>
  16. #include <grass/gis.h>
  17. #include <grass/glocale.h>
  18. #include "daemon.h"
  19. /**
  20. * \brief insert a node in list
  21. * \param l list where to insert
  22. * \param mess the message to insert
  23. */
  24. void insertNode(struct list *l, msg mess)
  25. {
  26. struct node *new;
  27. new = G_malloc(sizeof(struct node));
  28. new->m = G_malloc(sizeof(msg));
  29. if (new != NULL) {
  30. memcpy(new->m, &mess, sizeof(msg));
  31. new->next = new->prev = NULL;
  32. if (l->head == NULL) {
  33. l->head = l->tail = new;
  34. }
  35. else {
  36. l->tail->next = new;
  37. new->prev = l->tail;
  38. l->tail = new;
  39. }
  40. }
  41. else
  42. G_message(_("Out of memory"));
  43. l->size++;
  44. }
  45. /**
  46. *\brief remove a node from list
  47. * \param list where to remove
  48. */
  49. void removeNode(struct list *l)
  50. {
  51. if (l->head == NULL) {
  52. return;
  53. }
  54. if (l->head->next == NULL) {
  55. struct node *tmp = l->head;
  56. l->head = NULL;
  57. free(tmp);
  58. l->size--;
  59. }
  60. else {
  61. struct node *tmp = l->head;
  62. l->head = l->head->next;
  63. l->head->prev = NULL;
  64. free(tmp);
  65. l->size--;
  66. }
  67. }
  68. /**
  69. * \brief runtime area generation
  70. * \param gen area generator to use
  71. * \param msg next area message
  72. */
  73. int next(struct g_area *gen, msg *toReturn)
  74. {
  75. if (gen->cl > gen->cols)
  76. return 0;
  77. if (gen->rl > gen->rows)
  78. return 0;
  79. if (gen->maskname == NULL) {
  80. /* area */
  81. (*toReturn).type = AREA;
  82. if (gen->cols - gen->x + gen->sf_x < gen->add_col) {
  83. gen->x = gen->sf_x + gen->dist;
  84. gen->y = gen->y + gen->add_row;
  85. }
  86. if (gen->rows - gen->y + gen->sf_y >= gen->add_row) {
  87. (*toReturn).f.f_a.aid = gen->count;
  88. (gen->count)++;
  89. (*toReturn).f.f_a.x = gen->x;
  90. gen->x = gen->x + gen->add_col;
  91. (*toReturn).f.f_a.y = gen->y;
  92. (*toReturn).f.f_a.rl = gen->rl;
  93. (*toReturn).f.f_a.cl = gen->cl;
  94. return 1;
  95. }
  96. else
  97. return 0;
  98. }
  99. else {
  100. /* maskedarea */
  101. (*toReturn).type = MASKEDAREA;
  102. if (gen->cols - gen->x + gen->sf_x < gen->add_col) {
  103. gen->x = gen->sf_x + gen->dist;
  104. gen->y = gen->y + gen->add_row;
  105. }
  106. if (gen->rows - gen->y + gen->sf_y > gen->add_row) {
  107. (*toReturn).f.f_ma.aid = gen->count;
  108. (gen->count)++;
  109. (*toReturn).f.f_ma.x = gen->x;
  110. gen->x = gen->x + gen->add_col;
  111. (*toReturn).f.f_ma.y = gen->y;
  112. (*toReturn).f.f_ma.rl = gen->rl;
  113. (*toReturn).f.f_ma.cl = gen->cl;
  114. strcpy((*toReturn).f.f_ma.mask, gen->maskname);
  115. return 1;
  116. }
  117. else
  118. return 0;
  119. }
  120. }