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

#include "nmxp_chan.h"
Matteo Quintiliani's avatar
Matteo Quintiliani committed
13
#include "nmxp_base.h"
14
15

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

18

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

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

    return -1;
}

Matteo Quintiliani's avatar
Matteo Quintiliani committed
33
char *nmxp_chan_lookupName(uint32_t key, NMXP_CHAN_LIST *channelList)
34
{
35
    int i_chan = 0;
36
    static char ret[12];
37

38
39
40
    ret[0] = 0;

    for (i_chan = 0; i_chan < channelList->number; i_chan++)
41
    {
42
	if ( key == channelList->channel[i_chan].key ) {
43
	    strcpy(ret, channelList->channel[i_chan].name);
44
	}
45
46
    }

47
48
49
50
51
52
    if(ret[0] == 0) {
	nmxp_log(1, 0, "Key %d not found!", key);
	return NULL;
    } else {
	return ret;
    }
53
54
}

Matteo Quintiliani's avatar
Matteo Quintiliani committed
55
NMXP_CHAN_LIST *nmxp_chan_getType(NMXP_CHAN_LIST *channelList, NMXP_DATATYPE dataType) {
56
57
58
59
60
61
62
63
64
65
    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++)
    {
66
	if ( getDataTypeFromKey(channelList->channel[i_chan].key) == dataType) {
67
68
69
70
71
72
73
74
75
	    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;
}

76

77
78
NMXP_CHAN_LIST *nmxp_chan_subset(NMXP_CHAN_LIST *channelList, NMXP_DATATYPE dataType, char *sta_chan_list) {
    NMXP_CHAN_LIST *ret_channelList = NULL;
79
80
81
    int istalist, ista;
    char sta_chan_code[100];
    uint32_t sta_chan_key;
82
83
84
85

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

86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
    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);
103
104
105
	}
    }

106

107
108
109
110
    return ret_channelList;
}


111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// 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);
}