nmxp_chan.c 12.1 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
 *
 * Author:
 * 	Matteo Quintiliani
 * 	Istituto Nazionale di Geofisica e Vulcanologia - Italy
 *	quintiliani@ingv.it
 *
Matteo Quintiliani's avatar
Matteo Quintiliani committed
10
11
 * $Id: nmxp_chan.c,v 1.21 2007-09-07 07:08:30 mtheo Exp $
 *
12
13
14
 */

#include "nmxp_chan.h"
Matteo Quintiliani's avatar
Matteo Quintiliani committed
15
#include "nmxp_base.h"
16
17

#include <string.h>
18
#include <stdlib.h>
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
int nmxp_chan_cpy_sta_chan(const char *station_dot_channel, char *station_code, char *channel_code) {
    int ret = 0;
    char *sta_code_tmp, *cha_code_tmp;

    if(station_dot_channel || station_code || channel_code) {
	station_code[0] = 0;
	channel_code[0] = 0;

	/* validate pattern channel */
	sta_code_tmp = strdup(station_dot_channel);
	if( (cha_code_tmp = strchr(sta_code_tmp, '.')) == NULL ) {
	    nmxp_log(1, 0, "Name %s is not in STA.CHAN format!\n", station_dot_channel);
	} else {
	    if(cha_code_tmp) {
		*cha_code_tmp++ = '\0';
		strcpy(station_code, sta_code_tmp);
		strcpy(channel_code, cha_code_tmp);
		free(sta_code_tmp);
		ret = 1;
	    } else {
		nmxp_log(1, 0, "Name %s is not in STA.CHAN format! Channel is missing?\n", station_dot_channel);
	    }
	}
    } else {
	nmxp_log(1, 0, "Some parameter is NULL in nmxp_chan_cpy_sta_chan().\n",  station_dot_channel);
    }

    return ret;
}


51

52
53
54
55
56
57
58
59
60
61
62
/*
 * 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];
63
    char cha_pattern[20];
64
65
66
67
    char sta_sdc[20];
    char *cha_sdc;

    /* validate pattern channel */
68
    if(!nmxp_chan_cpy_sta_chan(pattern, sta_pattern, cha_pattern)) {
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
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
134
135
136
137
	nmxp_log(1, 0, "Channel pattern %s is not in STA.CHAN format!\n", pattern);
	return -1;
    }

    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
138
int nmxp_chan_lookupKey(char* name, NMXP_CHAN_LIST *channelList)
139
{
140
141
    int chan_number = channelList->number;
    int i_chan = 0;
142

143
    for (i_chan = 0; i_chan < chan_number; i_chan++)
144
    {
145
146
	if (strcasecmp(name, channelList->channel[i_chan].name) == 0)
	    return channelList->channel[i_chan].key;
147
148
149
150
151
    }

    return -1;
}

152

Matteo Quintiliani's avatar
Matteo Quintiliani committed
153
int nmxp_chan_lookupKeyIndex(int32_t key, NMXP_CHAN_LIST *channelList)
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
{
    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
171
char *nmxp_chan_lookupName(int32_t key, NMXP_CHAN_LIST *channelList)
172
{
173
    int i_chan = 0;
174
    static char ret[12];
175

176
177
178
    ret[0] = 0;

    for (i_chan = 0; i_chan < channelList->number; i_chan++)
179
    {
180
	if ( key == channelList->channel[i_chan].key ) {
181
	    strcpy(ret, channelList->channel[i_chan].name);
182
	}
183
184
    }

185
186
187
188
189
190
    if(ret[0] == 0) {
	nmxp_log(1, 0, "Key %d not found!", key);
	return NULL;
    } else {
	return ret;
    }
191
192
}

193

Matteo Quintiliani's avatar
Matteo Quintiliani committed
194
NMXP_CHAN_LIST *nmxp_chan_getType(NMXP_CHAN_LIST *channelList, NMXP_DATATYPE dataType) {
195
196
197
198
199
200
201
202
203
204
    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++)
    {
205
	if ( getDataTypeFromKey(channelList->channel[i_chan].key) == dataType) {
206
207
208
209
210
211
212
213
214
	    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;
}

215

216
217
NMXP_CHAN_LIST *nmxp_chan_subset(NMXP_CHAN_LIST *channelList, NMXP_DATATYPE dataType, char *sta_chan_list) {
    NMXP_CHAN_LIST *ret_channelList = NULL;
218
    int istalist, ista;
219
220
    char sta_chan_code_pattern[100];
    int i_chan, ret_match;
221
222
223
224

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

225
226
227
228
    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) {
229
	    sta_chan_code_pattern[ista++] = sta_chan_list[istalist++];
230
	}
231
	sta_chan_code_pattern[ista] = 0;
232
233
234
	if(sta_chan_list[istalist] == sep_chan_list) {
	    istalist++;
	}
235
236
237
238
239
240
241
242
243
244
245
246
	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++;
247
248
	}
    }
249
    
250
251
252
253
    return ret_channelList;
}


254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
// 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);
}

286
287
288
289
290
291
292
293
294
295
296
297
298
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);
    }

}

Matteo Quintiliani's avatar
Matteo Quintiliani committed
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317

void nmxp_meta_chan_free(NMXP_META_CHAN_LIST **chan_list) {
    NMXP_META_CHAN_LIST *iter = *chan_list;
    NMXP_META_CHAN_LIST *iter_next = NULL;

    nmxp_log(0, 1, "nmxp_meta_chan_free()\n");

    if(iter) {
	iter_next = iter->next;
	while(iter) {
	    free(iter);
	    iter = iter_next;
	    iter_next = iter->next;
	}
	*chan_list = NULL;
    }

}

318
int nmxp_meta_chan_compare(NMXP_META_CHAN_LIST *item1, NMXP_META_CHAN_LIST *item2, NMXP_META_CHAN_LIST_SORT_TYPE sorttype) {
319
    int ret = 0;
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
    switch(sorttype) {
	case NMXP_META_SORT_KEY:
	    if(item1->key > item2->key) {
		ret = 1;
	    } else if(item1->key < item2->key) {
		ret = -1;
	    }
	    break;
	case NMXP_META_SORT_NAME:
	    ret = strcmp(item1->name, item2->name);
	    break;
	case NMXP_META_SORT_START_TIME:
	    if(item1->start_time > item2->start_time) {
		ret = 1;
	    } else if(item1->start_time < item2->start_time) {
		ret = -1;
	    }
	    break;
	case NMXP_META_SORT_END_TIME:
	    if(item1->end_time > item2->end_time) {
		ret = 1;
	    } else if(item1->end_time < item2->end_time) {
		ret = -1;
	    }
	    break;
	default:
	    nmxp_log(1, 0, "Sort type %d not defined!\n", sorttype);
	    break;
348
349
350
351
    }
    return ret;
}

352
NMXP_META_CHAN_LIST *nmxp_meta_chan_add(NMXP_META_CHAN_LIST **chan_list, int32_t key, char *name, int32_t start_time, int32_t end_time, char *network, NMXP_META_CHAN_LIST_SORT_TYPE sorttype) {
Matteo Quintiliani's avatar
Matteo Quintiliani committed
353
354
355
    NMXP_META_CHAN_LIST *iter = NULL;
    NMXP_META_CHAN_LIST *new_item = NULL;

356
357
358
359
360
361
    nmxp_log(0, 1, "nmxp_meta_chan_add(%d, %d, %s, %d, %d, %s, %d)\n", *chan_list, key, name, start_time, end_time, network, sorttype);

    if(sorttype != NMXP_META_SORT_KEY  &&  sorttype != NMXP_META_SORT_NAME) {
	nmxp_log(1, 0, "nmxp_meta_chan_add() can only accept NMXP_META_SORT_KEY or NMXP_META_SORT_NAME. Fixed NMXP_META_SORT_KEY!\n");
	sorttype = NMXP_META_SORT_KEY;
    }
Matteo Quintiliani's avatar
Matteo Quintiliani committed
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383

    new_item = (NMXP_META_CHAN_LIST *) malloc(sizeof(NMXP_META_CHAN_LIST));
    new_item->key = 0;
    new_item->name[0] = 0;
    new_item->start_time = 0;
    new_item->end_time = 0;
    new_item->network[0] = 0;
    new_item->next = NULL;
    new_item->key = key;
    if(name) {
	strncpy(new_item->name, name, 12);
    }
    new_item->start_time = start_time;
    new_item->end_time = end_time;
    if(network) {
	strncpy(new_item->network, network, 12);
    }


    if(*chan_list == NULL) {
	*chan_list = new_item;
    } else {
384
	if(nmxp_meta_chan_compare(new_item, *chan_list, sorttype) < 0) {
Matteo Quintiliani's avatar
Matteo Quintiliani committed
385
386
387
	    new_item->next = *chan_list;
	    *chan_list = new_item;
	} else {
388
	    for(iter = *chan_list; iter->next != NULL  && nmxp_meta_chan_compare(new_item, iter->next, sorttype) > 0; iter = iter->next) {
Matteo Quintiliani's avatar
Matteo Quintiliani committed
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
	    }
	    new_item->next = iter->next;
	    iter->next = new_item;
	}
    }

    return new_item;
}

NMXP_META_CHAN_LIST *nmxp_meta_chan_search_key(NMXP_META_CHAN_LIST *chan_list, int32_t key) {
    NMXP_META_CHAN_LIST *iter = chan_list;
    int found = 0;

    nmxp_log(0, 1, "nmxp_meta_chan_search_key()\n");

    while(iter != NULL  &&  !found) {
	if(iter->key == key) {
	    found = 1;
	} else {
	    iter = iter->next;
	}
    }

    return iter;
}

NMXP_META_CHAN_LIST *nmxp_meta_chan_set_name(NMXP_META_CHAN_LIST *chan_list, int32_t key, char *name) {
    NMXP_META_CHAN_LIST *ret = NULL;

    nmxp_log(0, 1, "nmxp_meta_chan_set_name()\n");

    if( (ret = nmxp_meta_chan_search_key(chan_list, key)) ) {
	strncpy(ret->name, name, 12);
    }

    return ret;
}

NMXP_META_CHAN_LIST *nmxp_meta_chan_set_times(NMXP_META_CHAN_LIST *chan_list, int32_t key, int32_t start_time, int32_t end_time) {
    NMXP_META_CHAN_LIST *ret = NULL;

    nmxp_log(0, 1, "nmxp_meta_chan_set_times()\n");

    if( (ret = nmxp_meta_chan_search_key(chan_list, key)) ) {
	ret->start_time = start_time;
	ret->end_time = end_time;
    }

    return ret;
}

NMXP_META_CHAN_LIST *nmxp_meta_chan_set_network(NMXP_META_CHAN_LIST *chan_list, int32_t key, char *network) {
    NMXP_META_CHAN_LIST *ret = NULL;

    nmxp_log(0, 1, "nmxp_meta_chan_set_network()\n");

    if( (ret = nmxp_meta_chan_search_key(chan_list, key)) ) {
	strncpy(ret->network, network, 12);
    }

    return ret;
}

void nmxp_meta_chan_print(NMXP_META_CHAN_LIST *chan_list) {
    NMXP_META_CHAN_LIST *iter = chan_list;
    char str_start_time[200], str_end_time[200];
    str_start_time[0] = 0;
    str_end_time[0] = 0;

    nmxp_log(0, 1, "nmxp_meta_chan_print()\n");

    while(iter != NULL) {
	nmxp_data_to_str(str_start_time, iter->start_time);
	nmxp_data_to_str(str_end_time,   iter->end_time);

Matteo Quintiliani's avatar
Matteo Quintiliani committed
464
	nmxp_log(0, 0, "%10d %11s.%-5s (%s  -  %s)\n",
Matteo Quintiliani's avatar
Matteo Quintiliani committed
465
466
467
468
469
470
471
472
473
474
475
476
		iter->key,
		iter->name,
		iter->network,
		str_start_time,
		str_end_time
		);
	iter = iter->next;
    }
}