File: sort.c

package info (click to toggle)
libiio 0.26-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,832 kB
  • sloc: ansic: 21,880; python: 1,844; cs: 1,232; sh: 1,062; cpp: 688; yacc: 441; xml: 192; lex: 172; makefile: 40
file content (100 lines) | stat: -rw-r--r-- 3,096 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// SPDX-License-Identifier: LGPL-2.1-or-later
/*
 * libiio - Library for interfacing industrial I/O (IIO) devices
 *
 * Copyright (C) 2018 Analog Devices, Inc.
 * Author: Robin Getz <robin.getz@analog.com>
 */

#include "iio-private.h"
#include <string.h>

/* These are a few functions to do sorting via qsort for various
 * iio structures. For more info, see the qsort(3) man page.
 *
 * The qsort comparison function must return an integer less than, equal to,
 * or greater than zero if the first argument is considered to be
 * respectively less than, equal to, or greater than the second. If two
 * members compare as equal, their order in the sort order is undefined.
 *
 * If the structures are updated, the compare functions may
 * need to be updated.
 *
 * The actual arguments to these function are "pointers to
 * pointers to char", but strcmp(3) arguments are "pointers
 * to char", hence the cast plus dereference
 */

int iio_channel_compare(const void *p1, const void *p2)
{
	const struct iio_channel *tmp1 = *(struct iio_channel **)p1;
	const struct iio_channel *tmp2 = *(struct iio_channel **)p2;

	/* make sure buffer enabled channels are first */
	if (iio_channel_is_scan_element(tmp1) && !iio_channel_is_scan_element(tmp2))
		return -1;
	if (!iio_channel_is_scan_element(tmp1) && iio_channel_is_scan_element(tmp2))
		return 1;
	/* and sort them by index */
	if (iio_channel_is_scan_element(tmp1) && iio_channel_is_scan_element(tmp2)){
		if (iio_channel_get_index(tmp1) > iio_channel_get_index(tmp2))
			return 1;
		return -1;
	}
	/* otherwise, if the ID is the same, input channels first */
	if (strcmp(tmp1->id, tmp2->id) == 0)
		return !iio_channel_is_output(tmp1);

	/* finally by ID */
	return strcmp(tmp1->id, tmp2->id);
}

int iio_channel_attr_compare(const void *p1, const void *p2)
{
	const struct iio_channel_attr *tmp1 = (struct iio_channel_attr *)p1;
	const struct iio_channel_attr *tmp2 = (struct iio_channel_attr *)p2;
	/* qsort channel attributes by name */
	return strcmp(tmp1->name, tmp2->name);
}

int iio_device_compare(const void *p1, const void *p2)
{
	const struct iio_device *tmp1 = *(struct iio_device **)p1;
	const struct iio_device *tmp2 = *(struct iio_device **)p2;
	/* qsort devices by ID */
	return strcmp(tmp1->id, tmp2->id);
}

int iio_device_attr_compare(const void *p1, const void *p2)
{
	const char *tmp1 = *(const char **)p1;
	const char *tmp2 = *(const char **)p2;
	/* qsort device attributes by name */
	return strcmp(tmp1, tmp2);
}

int iio_buffer_attr_compare(const void *p1, const void *p2)
{
	const char *tmp1 = *(const char **)p1;
	const char *tmp2 = *(const char **)p2;
	/* qsort buffer attributes by name */
	return strcmp(tmp1, tmp2);
}

int iio_context_info_compare(const void *p1, const void *p2)
{
	int ret;
	const struct iio_context_info *tmp1 = *(struct iio_context_info **)p1;
	const struct iio_context_info *tmp2 = *(struct iio_context_info **)p2;

	if(!tmp1->uri)
		return 1;
	if (!tmp2->uri)
		return 0;

	ret = strcmp(tmp1->uri, tmp2->uri);
	if (ret)
		return ret;

	return strcmp(tmp1->description, tmp2->description);
}