nmxp_chan.c 3.5 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

17

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

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

    return -1;
}

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

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

    return NULL;
}

Matteo Quintiliani's avatar
Matteo Quintiliani committed
46
NMXP_CHAN_LIST *nmxp_chan_getType(NMXP_CHAN_LIST *channelList, NMXP_DATATYPE dataType) {
47
48
49
50
51
52
53
54
55
56
    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++)
    {
57
	if ( getDataTypeFromKey(channelList->channel[i_chan].key) == dataType) {
58
59
60
61
62
63
64
65
66
	    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;
}

67

68
69
NMXP_CHAN_LIST *nmxp_chan_subset(NMXP_CHAN_LIST *channelList, NMXP_DATATYPE dataType, char *sta_chan_list) {
    NMXP_CHAN_LIST *ret_channelList = NULL;
70
71
72
    int istalist, ista;
    char sta_chan_code[100];
    uint32_t sta_chan_key;
73
74
75
76

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

77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
    istalist = 0;
    while(sta_chan_list[istalist] != sep_chan_list  &&  sta_chan_list[istalist] != 0) {
	ista = 0;
	while(sta_chan_list[istalist] != sep_chan_list  &&  sta_chan_list[istalist] != 0) {
	    sta_chan_code[ista++] = sta_chan_list[istalist++];
	}
	sta_chan_code[ista] = 0;
	if(sta_chan_list[istalist] == sep_chan_list) {
	    istalist++;
	}
	sta_chan_key = nmxp_chan_lookupKey(sta_chan_code, channelList);
	if(sta_chan_key != -1  && getDataTypeFromKey(sta_chan_key) == dataType) {
	    ret_channelList->channel[ret_channelList->number].key = sta_chan_key;
	    strcpy(ret_channelList->channel[ret_channelList->number].name, sta_chan_code);
	    ret_channelList->number++;
	} else {
	    nmxp_log(1, 0, "Channel %s not found!\n", sta_chan_code);
94
95
96
	}
    }

97

98
99
100
101
    return ret_channelList;
}


102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// 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);
}