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