nmxp_chan.c 6.62 KB
Newer Older
1
2
/*! \file
 *
Matteo Quintiliani's avatar
Matteo Quintiliani committed
3
 * \brief Channels for Nanometrics Protocol Library
4
5
6
7
8
9
10
11
12
 *
 * 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

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
101
102
103
104
105
106
107
108
/*
 * Match string against the extended regular expression in
 * pattern, treating errors as no match.
 *
 * return 1 for match, 0 for no match, -1 on error for invalid pattern, -2 on error for invalid station_dot_channel
 */
int nmxp_chan_match(const char *station_dot_channel, char *pattern)
{
    int ret = 0;
    int i, l;
    char sta_pattern[20];
    char *cha_pattern;
    char sta_sdc[20];
    char *cha_sdc;

    /* validate pattern channel */
    strcpy(sta_pattern, pattern);
    if( (cha_pattern = strchr(sta_pattern, '.')) == NULL ) {
	nmxp_log(1, 0, "Channel pattern %s is not in STA.CHAN format!\n", pattern);
	return -1;
    }
    if(cha_pattern) {
	*cha_pattern++ = '\0';
    }

    l = strlen(sta_pattern);
    i = 0;
    while(i < l  &&  ret != -1) {
	if(  !(
		(sta_pattern[i] >= 'A'  &&  sta_pattern[i] <= 'Z')
		|| (sta_pattern[i] >= 'a'  &&  sta_pattern[i] <= 'z')
		|| (sta_pattern[i] >= '0'  &&  sta_pattern[i] <= '9')
		|| (sta_pattern[i] == '_' )
	     )
	  ) {
	    nmxp_log(1, 0, "Channel pattern %s has not valid STA format!\n", pattern);
	    return -1;
	}
	i++;
    }
    
    l = strlen(cha_pattern);
    if(l != 3) {
	nmxp_log(1, 0, "Channel pattern %s has not valid CHAN format!\n", pattern);
	return -1;
    }
    i = 0;
    while(i < l  &&  ret != -1) {
	if(  !(
		    (cha_pattern[i] >= 'A'  &&  cha_pattern[i] <= 'Z')
		    || (cha_pattern[i] >= 'a'  &&  cha_pattern[i] <= 'z')
		    || (cha_pattern[i] == '_' )
		    || (cha_pattern[i] == '?' )
	      )
	  ) {
	    nmxp_log(1, 0, "Channel pattern %s has not valid CHAN format!\n", pattern);
	    return -1;
	}
	i++;
    }

    strcpy(sta_sdc, station_dot_channel);
    if( (cha_sdc = strchr(sta_sdc, '.')) == NULL ) {
	nmxp_log(1, 0, "Channel %s is not in STA.CHAN format!\n", station_dot_channel);
	return -2;
    }
    if(cha_sdc) {
	*cha_sdc++ = '\0';
    }
    l = strlen(cha_sdc);
    if(l != 3) {
	nmxp_log(1, 0, "Channel %s has not valid CHAN format!\n", pattern);
	return -1;
    }

    if (strcasecmp(sta_sdc, sta_pattern) == 0) {
	/* matching CHAN */
	ret = 1;
	i = 0;
	while(i < 3  &&  ret != 0) {
	    ret = ((cha_pattern[i] == '?')? 1 : (cha_pattern[i] == cha_sdc[i]));
	    i++;
	}
    }


    return ret;
}


Matteo Quintiliani's avatar
Matteo Quintiliani committed
109
int nmxp_chan_lookupKey(char* name, NMXP_CHAN_LIST *channelList)
110
{
111
112
    int chan_number = channelList->number;
    int i_chan = 0;
113

114
    for (i_chan = 0; i_chan < chan_number; i_chan++)
115
    {
116
117
	if (strcasecmp(name, channelList->channel[i_chan].name) == 0)
	    return channelList->channel[i_chan].key;
118
119
120
121
122
    }

    return -1;
}

123

124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
int nmxp_chan_lookupKeyIndex(uint32_t key, NMXP_CHAN_LIST *channelList)
{
    int i_chan = 0;
    int ret = -1;

    i_chan = 0;
    while(i_chan < channelList->number  &&  ret == -1)
    {
	if ( key == channelList->channel[i_chan].key ) {
	    ret = i_chan;
	}
	i_chan++;
    }

    return ret;
}


Matteo Quintiliani's avatar
Matteo Quintiliani committed
142
char *nmxp_chan_lookupName(uint32_t key, NMXP_CHAN_LIST *channelList)
143
{
144
    int i_chan = 0;
145
    static char ret[12];
146

147
148
149
    ret[0] = 0;

    for (i_chan = 0; i_chan < channelList->number; i_chan++)
150
    {
151
	if ( key == channelList->channel[i_chan].key ) {
152
	    strcpy(ret, channelList->channel[i_chan].name);
153
	}
154
155
    }

156
157
158
159
160
161
    if(ret[0] == 0) {
	nmxp_log(1, 0, "Key %d not found!", key);
	return NULL;
    } else {
	return ret;
    }
162
163
}

164

Matteo Quintiliani's avatar
Matteo Quintiliani committed
165
NMXP_CHAN_LIST *nmxp_chan_getType(NMXP_CHAN_LIST *channelList, NMXP_DATATYPE dataType) {
166
167
168
169
170
171
172
173
174
175
    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++)
    {
176
	if ( getDataTypeFromKey(channelList->channel[i_chan].key) == dataType) {
177
178
179
180
181
182
183
184
185
	    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;
}

186

187
188
NMXP_CHAN_LIST *nmxp_chan_subset(NMXP_CHAN_LIST *channelList, NMXP_DATATYPE dataType, char *sta_chan_list) {
    NMXP_CHAN_LIST *ret_channelList = NULL;
189
    int istalist, ista;
190
191
    char sta_chan_code_pattern[100];
    int i_chan, ret_match;
192
193
194
195

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

196
197
198
199
    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) {
200
	    sta_chan_code_pattern[ista++] = sta_chan_list[istalist++];
201
	}
202
	sta_chan_code_pattern[ista] = 0;
203
204
205
	if(sta_chan_list[istalist] == sep_chan_list) {
	    istalist++;
	}
206
207
208
209
210
211
212
213
214
215
216
217
	ret_match = 1;
	i_chan = 0;
	while(i_chan < channelList->number  &&  ret_match != -1) {
	    ret_match = nmxp_chan_match(channelList->channel[i_chan].name, sta_chan_code_pattern);
	    if(ret_match) {
		    if(i_chan != -1  && getDataTypeFromKey(channelList->channel[i_chan].key) == dataType) {
			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++;
		    }
	    }
	    i_chan++;
218
219
	}
    }
220
    
221
222
223
224
    return ret_channelList;
}


225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
// 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);
}

257
258
259
260
261
262
263
264
265
266
267
268
269
void nmxp_chan_print_channelList(NMXP_CHAN_LIST *channelList) {
    int chan_number = channelList->number;
    int i_chan = 0;

    nmxp_log(0, 0, "%04d channels:\n", chan_number);

    for (i_chan = 0; i_chan < chan_number; i_chan++)
    {
	nmxp_log(0, 0, "%04d %12d %s\n", i_chan+1, channelList->channel[i_chan].key, channelList->channel[i_chan].name);
    }

}