nmxp_chan.c 3.22 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
/*! \file
 *
 * \brief Channels for Nanometrics Protocol Libray
 *
 * Author:
 * 	Matteo Quintiliani
 * 	Istituto Nazionale di Geofisica e Vulcanologia - Italy
 *	quintiliani@ingv.it
 *
 */

#include "nmxp_chan.h"

#include <string.h>
15
#include <stdlib.h>
16

Matteo Quintiliani's avatar
Matteo Quintiliani committed
17
int nmxp_chan_lookupKey(char* name, NMXP_CHAN_LIST *channelList)
18
{
19
20
    int chan_number = channelList->number;
    int i_chan = 0;
21

22
    for (i_chan = 0; i_chan < chan_number; i_chan++)
23
    {
24
25
	if (strcasecmp(name, channelList->channel[i_chan].name) == 0)
	    return channelList->channel[i_chan].key;
26
27
28
29
30
    }

    return -1;
}

Matteo Quintiliani's avatar
Matteo Quintiliani committed
31
char *nmxp_chan_lookupName(uint32_t key, NMXP_CHAN_LIST *channelList)
32
{
33
34
    int chan_number = channelList->number;
    int i_chan = 0;
35

36
    for (i_chan = 0; i_chan < chan_number; i_chan++)
37
    {
38
39
	if ( key == channelList->channel[i_chan].key )
	    return &channelList->channel[i_chan].name[0];
40
41
42
43
44
    }

    return NULL;
}

Matteo Quintiliani's avatar
Matteo Quintiliani committed
45
NMXP_CHAN_LIST *nmxp_chan_getType(NMXP_CHAN_LIST *channelList, NMXP_DATATYPE dataType) {
46
47
48
49
50
51
52
53
54
55
    NMXP_CHAN_LIST *ret_channelList = NULL;

    int chan_number = channelList->number;
    int i_chan = 0;

    ret_channelList = (NMXP_CHAN_LIST *) malloc(sizeof(NMXP_CHAN_LIST));
    ret_channelList->number = 0;

    for (i_chan = 0; i_chan < chan_number; i_chan++)
    {
56
	if ( getDataTypeFromKey(channelList->channel[i_chan].key) == dataType) {
57
58
59
60
61
62
63
64
65
	    ret_channelList->channel[ret_channelList->number].key = channelList->channel[i_chan].key;
	    strcpy(ret_channelList->channel[ret_channelList->number].name, channelList->channel[i_chan].name);
	    ret_channelList->number++;
	}
    }

    return ret_channelList;
}

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
NMXP_CHAN_LIST *nmxp_chan_subset(NMXP_CHAN_LIST *channelList, NMXP_DATATYPE dataType, char *sta_chan_list) {
    NMXP_CHAN_LIST *ret_channelList = NULL;

    int chan_number = channelList->number;
    int i_chan = 0;

    ret_channelList = (NMXP_CHAN_LIST *) malloc(sizeof(NMXP_CHAN_LIST));
    ret_channelList->number = 0;

    for (i_chan = 0; i_chan < chan_number; i_chan++)
    {
	if ( getDataTypeFromKey(channelList->channel[i_chan].key) == dataType) {
	    if(strstr(sta_chan_list, channelList->channel[i_chan].name)) {
		// TODO improve the previous check if needed
		    ret_channelList->channel[ret_channelList->number].key = channelList->channel[i_chan].key;
		    strcpy(ret_channelList->channel[ret_channelList->number].name, channelList->channel[i_chan].name);
		    ret_channelList->number++;
	    }
	}
    }

    return ret_channelList;
}


92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// Comparison Key Function
int chan_key_compare(const void *a, const void *b)
{
    int ret = 0;
    NMXP_CHAN_KEY *pa = (NMXP_CHAN_KEY *) a; 
    NMXP_CHAN_KEY *pb = (NMXP_CHAN_KEY *) b;

    if(pa->key > pb->key) {
	ret = 1;
    } else if(pa->key < pb->key) {
	ret = -1;
    }
    return ret;
}

void nmxp_chan_sortByKey(NMXP_CHAN_LIST *channelList) {
    qsort (channelList->channel, channelList->number, sizeof (NMXP_CHAN_KEY), chan_key_compare);
}

// Comparison Name Function
int chan_name_compare(const void *a, const void *b)
{
    NMXP_CHAN_KEY *pa = (NMXP_CHAN_KEY *) a; 
    NMXP_CHAN_KEY *pb = (NMXP_CHAN_KEY *) b;

    return strcmp(pa->name, pb->name);
}

void nmxp_chan_sortByName(NMXP_CHAN_LIST *channelList) {
    qsort (channelList->channel, channelList->number, sizeof (NMXP_CHAN_KEY), chan_name_compare);
}