Example #1
0
static gint
item_compare_tricky(gconstpointer a,
                    gconstpointer b)
{
    if (sort_by_name)
        return item_compare(a, b);

    const GwyItemTest *itemtesta = (const GwyItemTest*)a;
    const GwyItemTest *itemtestb = (const GwyItemTest*)b;
    return itemtesta->value - itemtestb->value;
}
Example #2
0
static int list_read(const char *file_name,
                     ipv4_rangelist_item_t **list,
                     size_t *list_items,
                     size_t *list_alloc)
{
	FILE *f;
	size_t i, j, lineno;
	char line[256], *hlp;
	int ooo = 0;

	FUN("list_read");

#ifndef IPLIST_IGNORE_DESCRIPTION_FIELD
	for (i = 0; i < *list_items; i++) {
		if ((*list)[i].description != NULL) {
			free((*list)[i].description);
			(*list)[i].description = NULL;
		}
	}
#endif /* ! IPLIST_IGNORE_DESCRIPTION_FIELD */
	*list_items = 0;

	f = fopen(file_name, "r");
	if (f == NULL) {
		LOGS(L_PEER,L_ERROR,
			("Can't open ip range file \"%s\".\n", file_name));
		return -1;
	}

	i = 0;
	lineno = 0;
	while(fgets(line, sizeof (line), f) != NULL) {
		lineno++;
		while (((hlp = strchr(line, '\n')) != NULL) ||
			((hlp = strchr(line, '\r')) != NULL)) {
			*hlp = '\0';
		}
		if (*line == '#') {
			/* ignore comment lines */
			continue;
		}
		if (i >= *list_alloc) {
			ipv4_rangelist_item_t *nl;

			nl = (ipv4_rangelist_item_t *)
				xcalloc(*list_alloc + IPLIST_ITEM_ALLOC_STEP,
					sizeof (*nl));
			if (*list != NULL) {
				for (j = 0; j < i; j++) {
					nl[j] = (*list)[j];
				}
				xfree(*list);
			}
			*list = nl;
			*list_alloc += IPLIST_ITEM_ALLOC_STEP;
		}
		if (parse_list_line(line, &(*list)[i])) {
			if (((*list)[i]).range_start > ((*list)[i]).range_end) {
				LOGS(L_PEER,L_ERROR,
					("Ignored empty range \"%s\" on line %d in iplist.\n",
					line, lineno));
			} else if ((i > 0) &&
				(item_compare(&((*list)[i - 1]), &((*list)[i])) > 0)) {
				/* Range is out of order and the array needs to be
				 * sorted afterwards
				 */
				ooo = 1;
				i++;
			} else {
				i++;
			}
		} else {
			LOGS(L_PEER,L_ERROR,
				("Invalid line \"%s\" on line %d in iplist.\n",
				line, lineno));
		}
	}
	fclose(f);

	if (ooo) {
		LOGS(L_PEER,L_MINOR, ("Range file is out of order, sorting data.\n"));
		qsort(*list, i, sizeof (ipv4_rangelist_item_t), item_compare);
	}

	*list_items = i;

	/* give some feedback */
	info("%d entries ", i);

	return *list_items;
}