#ifndef MACROS_H #define MACROS_H #define ASSERT(x) { \ if (!(x)) { \ putlog(MMLOG_ERROR, "ASSERTION: %s:%d in function %s", __FILE__, __LINE__, __FUNCTION__); \ abort(); \ } \ } #define ALIGN2(x, y) ( ((x) + (y) - 1) & ~((y) - 1) ) #define STRIP_CRLF(x) { \ char *crlf_ptr; \ for (crlf_ptr = x; *crlf_ptr; crlf_ptr++) { \ if (*crlf_ptr == '\r' || *crlf_ptr == '\n') { \ *crlf_ptr = '\0'; \ break; \ } \ } \ }; #define FREE_AND_STRDUP(x, a) { \ if ((x) != NULL) xfree(x); \ (x) = ((a) != NULL) ? xstrdup(a) : NULL; \ }; #define FREE_AND_NULL(x) { \ if ((x) != NULL) { \ xfree((x)); \ (x) = NULL; \ } \ }; #define XML_LIST_LOOP(x, y) \ while (x && (x = x->next) && x->item && strcasecmp(&x->item[2], &y[1])) #define XML_LIST_CMP(x, y) \ if (x && x->item && !strcasecmp(x->item, y)) #define PUSH(x, y) { \ do { \ if (x == NULL) { \ x = xmalloc(sizeof(STACK)); \ x->prev = NULL; \ } else { \ x->next = xmalloc(sizeof(STACK)); \ x->next->prev = x; \ x = x->next; \ } \ x->next = NULL; \ x->data = xstrdup(y); \ } while(0); \ }; #define POP(x) { \ do { \ if (x != NULL) { \ xfree(x->data); \ if (x->prev != NULL) { \ x = x->prev; \ xfree(x->next); \ x->next = NULL; \ } else { \ xfree(x); \ x = NULL; \ } \ } \ } while(0); \ }; /* this macro will move a node from anywhere in one list to the end of another */ #define MOVENODE(t, a, x, y) { \ do { \ if (a->next != NULL) \ a->next->prev = a->prev; \ if (a->prev != NULL) \ a->prev->next = a->next; \ if (a == x) x = a->next; \ a->next = NULL; \ if (y == NULL) { \ y = a; \ y->prev = NULL; \ } else { \ t tmpnode; \ for (tmpnode = y; tmpnode->next; tmpnode = tmpnode->next); \ if (tmpnode->next != NULL) { \ tmpnode->next->next = a; \ tmpnode->next->next->prev = tmpnode->next; \ } else { \ tmpnode->next = a; \ tmpnode->next->prev = tmpnode; \ } \ } \ } while(0); \ }; /* this macro will shift a node in a linked list up or down one */ #define SHIFTNODE(t, x, a, d) { \ do { \ t tmp_node = NULL; \ if (d == UP && a != (x)) { \ if ((x) == a->prev) (x) = a; \ if (a->prev->prev != NULL) a->prev->prev->next = a; \ tmp_node = a->prev->prev; \ a->prev->next = a->next; \ a->prev->prev = a; \ if (a->next != NULL) a->next->prev = a->prev; \ a->next = a->prev; \ a->prev = tmp_node; \ } else if (d == DOWN && a->next != NULL) { \ if ((x) == a) (x) = a->next; \ if (a->next->next != NULL) a->next->next->prev = a; \ tmp_node = a->next->next; \ a->next->prev = a->prev; \ a->next->next = a; \ if (a->prev != NULL) a->prev->next = a->next; \ a->prev = a->next; \ a->next = tmp_node; \ } \ } while(0); \ }; /* this macro moves a list node to top or bottom of list */ #define SETNODE(t, x, a, d) { \ do { \ t tmp_node = NULL; \ if (d == TOP && a != (x)) { \ if (a->next != NULL) a->next->prev = a->prev; \ a->prev->next = a->next; \ a->prev = NULL; \ (x)->prev = a; \ a->next = (x); \ (x) = a; \ } else if (d == DOWN && a->next != NULL) { \ tmp_node = a; \ while (tmp_node->next != NULL) tmp_node = tmp_node->next; \ if (tmp_node != a) { \ a->next->prev = a->prev; \ if (a->prev != NULL) \ a->prev->next = a->next; \ else \ (x) = a->next; \ a->prev = tmp_node; \ a->next = NULL; \ tmp_node->next = a; \ } \ } \ } while (0); \ }; #endif /* MACROS_H */