nmxp_chan.c 19.3 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
 *
10
 * $Id: nmxp_chan.c,v 1.42 2009-01-29 13:10:49 mtheo Exp $
Matteo Quintiliani's avatar
Matteo Quintiliani committed
11
 *
12
13
14
 */

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

#include <string.h>
19
#include <stdlib.h>
20

21
int nmxp_chan_cpy_sta_chan(const char *net_dot_station_dot_channel, char *station_code, char *channel_code, char *network_code) {
22
    int ret = 0;
23
24
25
26
27
28
    int errors = 0;
    int i;
    char *period1 = NULL, *period2 = NULL;
    char *tmp_name = NULL;

    if(net_dot_station_dot_channel || station_code || channel_code || network_code) {
29
30
31

	station_code[0] = 0;
	channel_code[0] = 0;
32
33
	network_code[0] = 0;

34
	tmp_name = NMXP_MEM_STRDUP(net_dot_station_dot_channel);
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
	/* count '.' */
	i=0;
	while(i < strlen(tmp_name)  && !errors) {
	    if(tmp_name[i] == '.') {
		if(!period1) {
		    period1 = tmp_name+i;
		} else if(!period2) {
		    period2 = tmp_name+i;
		} else {
		    errors++;
		}
	    }
	    i++;
	}
	if(!errors && period1) {
	    ret = 1;
	    if(period2) {
		/* NET.STA.CHAN */
		*period1++ = '\0';
		*period2++ = '\0';
		strcpy(network_code, tmp_name);
		strcpy(station_code, period1);
		strcpy(channel_code, period2);
58
	    } else {
59
60
61
62
		/* STA.CHAN */
		*period1++ = '\0';
		strcpy(station_code, tmp_name);
		strcpy(channel_code, period1);
63
	    }
64
	} else {
65
66
	    nmxp_log(NMXP_LOG_ERR, NMXP_LOG_D_CHANNEL, "Name %s is not in NET.STA.CHAN format! (NET. is optional)\n",
		    NMXP_LOG_STR(net_dot_station_dot_channel));
67
68
69
	}

	if(tmp_name) {
70
	    NMXP_MEM_FREE(tmp_name);
71
	    tmp_name = NULL;
72
	}
73

74
    } else {
75
76
	nmxp_log(NMXP_LOG_ERR, NMXP_LOG_D_CHANNEL, "Some parameter is NULL in nmxp_chan_cpy_sta_chan() %s.\n",
		NMXP_LOG_STR(net_dot_station_dot_channel));
77
78
79
80
81
82
    }

    return ret;
}


83

84
85
86
87
88
89
/*
 * 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
 */
90
int nmxp_chan_match(const char *net_dot_station_dot_channel, char *pattern)
91
92
93
94
{
    int ret = 0;
    int i, l;
    char sta_pattern[20];
95
    char cha_pattern[20];
96
    char net_pattern[20];
97
98
99
100
    char sta_sdc[20];
    char *cha_sdc;

    /* validate pattern channel */
101
    if(!nmxp_chan_cpy_sta_chan(pattern, sta_pattern, cha_pattern, net_pattern)) {
102
103
	nmxp_log(NMXP_LOG_ERR, NMXP_LOG_D_CHANNEL, "Channel pattern %s is not in STA.CHAN format!\n",
		NMXP_LOG_STR(pattern));
104
105
106
	return -1;
    }

107
108
109
110
111
112
113
114
115
    l = strlen(net_pattern);
    i = 0;
    while(i < l  &&  ret != -1) {
	if(  !(
		(net_pattern[i] >= 'A'  &&  net_pattern[i] <= 'Z')
		|| (net_pattern[i] >= 'a'  &&  net_pattern[i] <= 'z')
		|| (net_pattern[i] >= '0'  &&  net_pattern[i] <= '9')
		)
	  ) {
116
117
	    nmxp_log(NMXP_LOG_ERR, NMXP_LOG_D_CHANNEL, "Channel pattern %s has not valid NET format!\n",
		    NMXP_LOG_STR(pattern));
118
119
120
121
122
	    return -1;
	}
	i++;
    }
    
123
    l = strlen(sta_pattern);
124
125
126
    if(((l == 1) && sta_pattern[0] == '*')) {
	/* do nothing */
    } else {
127
128
129
130
131
132
133
134
135
    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] == '_' )
	     )
	  ) {
136
137
	    nmxp_log(NMXP_LOG_ERR, NMXP_LOG_D_CHANNEL, "Channel pattern %s has not valid STA format!\n",
		    NMXP_LOG_STR(pattern));
138
139
140
141
	    return -1;
	}
	i++;
    }
142
    }
143
144
145
    
    l = strlen(cha_pattern);
    if(l != 3) {
146
147
	nmxp_log(NMXP_LOG_ERR, NMXP_LOG_D_CHANNEL, "Channel pattern %s has not valid CHAN format!\n",
		NMXP_LOG_STR(pattern));
148
149
150
151
152
153
154
155
156
157
158
	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] == '?' )
	      )
	  ) {
159
160
	    nmxp_log(NMXP_LOG_ERR, NMXP_LOG_D_CHANNEL, "Channel pattern %s has not valid CHAN format!\n",
		    NMXP_LOG_STR(pattern));
161
162
163
164
165
	    return -1;
	}
	i++;
    }

166
    strcpy(sta_sdc, net_dot_station_dot_channel);
167
    if( (cha_sdc = strchr(sta_sdc, '.')) == NULL ) {
168
169
	nmxp_log(NMXP_LOG_ERR, NMXP_LOG_D_CHANNEL, "Channel %s is not in STA.CHAN format!\n",
		NMXP_LOG_STR(net_dot_station_dot_channel));
170
171
172
173
174
175
176
	return -2;
    }
    if(cha_sdc) {
	*cha_sdc++ = '\0';
    }
    l = strlen(cha_sdc);
    if(l != 3) {
177
178
	nmxp_log(NMXP_LOG_ERR, NMXP_LOG_D_CHANNEL, "Channel %s has not valid CHAN format!\n",
		NMXP_LOG_STR(pattern));
179
180
181
	return -1;
    }

182
183
184
    l = strlen(sta_pattern);
    if ( (strcasecmp(sta_sdc, sta_pattern) == 0) 
	    || ((l == 1) && sta_pattern[0] == '*')) {
185
186
187
188
189
190
191
192
193
194
195
196
197
198
	/* 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
199
int nmxp_chan_lookupKey(char* name, NMXP_CHAN_LIST *channelList)
200
{
201
202
    int chan_number = channelList->number;
    int i_chan = 0;
203

204
    for (i_chan = 0; i_chan < chan_number; i_chan++)
205
    {
206
207
	if (strcasecmp(name, channelList->channel[i_chan].name) == 0)
	    return channelList->channel[i_chan].key;
208
209
210
211
212
    }

    return -1;
}

213

214
int nmxp_chan_lookupKeyIndex(int32_t key, NMXP_CHAN_LIST_NET *channelList)
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
{
    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;
}


232
char *nmxp_chan_lookupName(int32_t key, NMXP_CHAN_LIST_NET *channelList)
233
{
234
    int i_chan = 0;
235
    static char ret[15];
236

237
238
239
    ret[0] = 0;

    for (i_chan = 0; i_chan < channelList->number; i_chan++)
240
    {
241
	if ( key == channelList->channel[i_chan].key ) {
242
	    strcpy(ret, channelList->channel[i_chan].name);
243
	}
244
245
    }

246
247
248
249
250
    if(ret[0] == 0) {
	return NULL;
    } else {
	return ret;
    }
251
252
}

253

Matteo Quintiliani's avatar
Matteo Quintiliani committed
254
NMXP_CHAN_LIST *nmxp_chan_getType(NMXP_CHAN_LIST *channelList, NMXP_DATATYPE dataType) {
255
256
257
258
259
    NMXP_CHAN_LIST *ret_channelList = NULL;

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

260
    ret_channelList = (NMXP_CHAN_LIST *) NMXP_MEM_MALLOC(sizeof(NMXP_CHAN_LIST));
261
262
263
264
    ret_channelList->number = 0;

    for (i_chan = 0; i_chan < chan_number; i_chan++)
    {
265
	if ( getDataTypeFromKey(channelList->channel[i_chan].key) == dataType) {
266
267
268
269
270
271
272
273
274
	    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;
}

275

276
277
NMXP_CHAN_LIST_NET *nmxp_chan_subset(NMXP_CHAN_LIST *channelList, NMXP_DATATYPE dataType, char *sta_chan_list, const char *network_code_default) {
    NMXP_CHAN_LIST_NET *ret_channelList = NULL;
278
    int istalist, ista;
279
280
    char sta_chan_code_pattern[100];
    int i_chan, ret_match;
281
282
283
    char network_code[20];
    char station_code[20];
    char channel_code[20];
284
285
286
287
    int i_chan_found = -1;
    int i_chan_duplicated = -1;
    char *nmxp_channel_name = NULL;
    char nmxp_channel_name_duplicated[50];
288

289
    ret_channelList = (NMXP_CHAN_LIST_NET *) NMXP_MEM_MALLOC(sizeof(NMXP_CHAN_LIST_NET));
290
291
    ret_channelList->number = 0;

292
293
    istalist = 0;
    while(sta_chan_list[istalist] != sep_chan_list  &&  sta_chan_list[istalist] != 0) {
294
295
	
	/* Build sta_chan_code_pattern from sta_chan_list */
296
297
	ista = 0;
	while(sta_chan_list[istalist] != sep_chan_list  &&  sta_chan_list[istalist] != 0) {
298
	    sta_chan_code_pattern[ista++] = sta_chan_list[istalist++];
299
	}
300
	sta_chan_code_pattern[ista] = 0;
301
302
303
	if(sta_chan_list[istalist] == sep_chan_list) {
	    istalist++;
	}
304
305
306
307
308
309

	/* Match name to sta_chan_code_pattern and set i_chan_found */
	nmxp_channel_name = NULL;
	nmxp_channel_name_duplicated[0] = 0;
	i_chan_found = -1;
	i_chan_duplicated = -1;
310
311
	ret_match = 1;
	i_chan = 0;
312
	while(i_chan < channelList->number && ret_match != -1) {
313
	    ret_match = nmxp_chan_match(channelList->channel[i_chan].name, sta_chan_code_pattern);
314
	    if(ret_match == 1) {
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
		    if(getDataTypeFromKey(channelList->channel[i_chan].key) == dataType) {
			/* Check for channel duplication */
			nmxp_channel_name = nmxp_chan_lookupName(channelList->channel[i_chan].key, ret_channelList);
			if(nmxp_channel_name == NULL) {
			    /* Add channel */
			    i_chan_found = i_chan;
			    ret_channelList->channel[ret_channelList->number].key =        channelList->channel[i_chan_found].key;
			    strcpy(ret_channelList->channel[ret_channelList->number].name, channelList->channel[i_chan_found].name);
			    nmxp_chan_cpy_sta_chan(sta_chan_code_pattern, station_code, channel_code, network_code);
			    sprintf(ret_channelList->channel[ret_channelList->number].name, "%s.%s",
				    (network_code[0] != 0)? network_code : network_code_default, channelList->channel[i_chan_found].name);
			    nmxp_log(NMXP_LOG_NORM, NMXP_LOG_D_CHANNEL, "Added %s for %s.\n",
				    ret_channelList->channel[ret_channelList->number].name, sta_chan_code_pattern);
			    ret_channelList->number++;
			} else {
			    strncpy(nmxp_channel_name_duplicated, nmxp_channel_name, 50);
			    i_chan_duplicated = i_chan;
332
333
334
335
336
337
338
339
340
341

			    if(i_chan_duplicated != -1) {
				/* Warning message for duplication */
				nmxp_log(NMXP_LOG_WARN, NMXP_LOG_D_ANY, "Pattern %s duplicates %s. Kept %s. (%d, Key %d).\n",
					sta_chan_code_pattern,
					channelList->channel[i_chan_duplicated].name,
					NMXP_LOG_STR(nmxp_channel_name_duplicated),
					i_chan_duplicated, channelList->channel[i_chan_duplicated].key);
			    }

342
			}
343
344
345
		    }
	    }
	    i_chan++;
346
	}
347
348
349

	if(i_chan_found == -1  &&  i_chan_duplicated == -1) {
	    /* Error message for channel not found of channel is not dataType */
350
	    nmxp_log(NMXP_LOG_ERR, NMXP_LOG_D_ANY, "Pattern %s does not match to any key.\n",
351
352
353
		    sta_chan_code_pattern);
	}

354
    }
355
    
356
357
358
359
    return ret_channelList;
}


360
/* Comparison Key Function*/
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
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);
}

379
/* Comparison Name Function*/
380
381
382
383
384
385
386
387
388
389
390
391
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);
}

392
393

void nmxp_chan_print_channelList(NMXP_CHAN_LIST *channelList) {
394
    int chan_number = 0;
395
396
    int i_chan = 0;

397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
    if(channelList) {
	chan_number = channelList->number;
	nmxp_log(NMXP_LOG_NORM_NO, NMXP_LOG_D_CHANNEL, "%04d channels:\n", chan_number);

	for (i_chan = 0; i_chan < chan_number; i_chan++)
	{
	    nmxp_log(NMXP_LOG_NORM_NO, NMXP_LOG_D_ANY, "%04d %12d %6s%c%-11s\n",
		    i_chan+1,
		    channelList->channel[i_chan].key,
		    "    ",
		    ' ',
		    NMXP_LOG_STR(channelList->channel[i_chan].name));
	}
    } else {
	nmxp_log(NMXP_LOG_ERR, NMXP_LOG_D_CHANNEL, "Channel list is NULL.\n");
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
464
465
466
467

void nmxp_chan_print_channelList_with_match(NMXP_CHAN_LIST *channelList, char *sta_chan_list) {
    int chan_number = 0;
    int i_chan = 0;
    int ret_match = 0;
    int istalist, ista;
    char sta_chan_code_pattern[100];

    if(channelList) {
	chan_number = channelList->number;
	nmxp_log(NMXP_LOG_NORM_NO, NMXP_LOG_D_CHANNEL, "%04d channels:\n", chan_number);

	for (i_chan = 0; i_chan < chan_number; i_chan++)
	{
	    if(sta_chan_list) {

		ret_match = 0;
		istalist = 0;
		while(sta_chan_list[istalist] != sep_chan_list  &&  sta_chan_list[istalist] != 0  &&  ret_match == 0) {

		    /* Build sta_chan_code_pattern from sta_chan_list */
		    ista = 0;
		    while(sta_chan_list[istalist] != sep_chan_list  &&  sta_chan_list[istalist] != 0) {
			sta_chan_code_pattern[ista++] = sta_chan_list[istalist++];
		    }
		    sta_chan_code_pattern[ista] = 0;
		    if(sta_chan_list[istalist] == sep_chan_list) {
			istalist++;
		    }

		    ret_match = nmxp_chan_match(channelList->channel[i_chan].name, sta_chan_code_pattern);
		}

	    } else {
		ret_match = 1;
	    }
	    if(ret_match == 1) {
		nmxp_log(NMXP_LOG_NORM_NO, NMXP_LOG_D_ANY, "%04d %12d %6s%c%-11s\n",
			i_chan+1,
			channelList->channel[i_chan].key,
			"    ",
			' ',
			NMXP_LOG_STR(channelList->channel[i_chan].name));
	    }
	}
    } else {
	nmxp_log(NMXP_LOG_ERR, NMXP_LOG_D_CHANNEL, "Channel list is NULL.\n");
    }

}


468
void nmxp_chan_print_netchannelList(NMXP_CHAN_LIST_NET *channelList) {
469
470
471
    int chan_number = channelList->number;
    int i_chan = 0;

472
    nmxp_log(NMXP_LOG_NORM, NMXP_LOG_D_CHANNEL, "%04d channels:\n", chan_number);
473
474
475

    for (i_chan = 0; i_chan < chan_number; i_chan++)
    {
476
477
478
	nmxp_log(NMXP_LOG_NORM, NMXP_LOG_D_CHANNEL, "%04d %12d %s\n",
		i_chan+1, channelList->channel[i_chan].key,
		NMXP_LOG_STR(channelList->channel[i_chan].name));
479
480
481
482
    }

}

Matteo Quintiliani's avatar
Matteo Quintiliani committed
483
484
485
486
487

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;

488
    nmxp_log(NMXP_LOG_NORM, NMXP_LOG_D_CHANNEL, "nmxp_meta_chan_free()\n");
Matteo Quintiliani's avatar
Matteo Quintiliani committed
489
490
491
492

    if(iter) {
	iter_next = iter->next;
	while(iter) {
493
	    NMXP_MEM_FREE(iter);
Matteo Quintiliani's avatar
Matteo Quintiliani committed
494
495
496
497
498
499
500
501
	    iter = iter_next;
	    iter_next = iter->next;
	}
	*chan_list = NULL;
    }

}

502
int nmxp_meta_chan_compare(NMXP_META_CHAN_LIST *item1, NMXP_META_CHAN_LIST *item2, NMXP_META_CHAN_LIST_SORT_TYPE sorttype) {
503
    int ret = 0;
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
    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:
530
	    nmxp_log(NMXP_LOG_ERR, NMXP_LOG_D_CHANNEL, "Sort type %d not defined!\n", sorttype);
531
	    break;
532
533
534
535
    }
    return ret;
}

536
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
537
538
539
    NMXP_META_CHAN_LIST *iter = NULL;
    NMXP_META_CHAN_LIST *new_item = NULL;

540
    if(sorttype != NMXP_META_SORT_KEY  &&  sorttype != NMXP_META_SORT_NAME) {
541
542
	nmxp_log(NMXP_LOG_ERR, NMXP_LOG_D_CHANNEL,
		"nmxp_meta_chan_add() can only accept NMXP_META_SORT_KEY or NMXP_META_SORT_NAME. Fixed NMXP_META_SORT_KEY!\n");
543
544
	sorttype = NMXP_META_SORT_KEY;
    }
Matteo Quintiliani's avatar
Matteo Quintiliani committed
545

546
    new_item = (NMXP_META_CHAN_LIST *) NMXP_MEM_MALLOC(sizeof(NMXP_META_CHAN_LIST));
Matteo Quintiliani's avatar
Matteo Quintiliani committed
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
    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 {
567
	if(nmxp_meta_chan_compare(new_item, *chan_list, sorttype) < 0) {
Matteo Quintiliani's avatar
Matteo Quintiliani committed
568
569
570
	    new_item->next = *chan_list;
	    *chan_list = new_item;
	} else {
571
	    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
572
573
574
575
576
577
578
579
580
581
582
583
584
	    }
	    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;

Matteo Quintiliani's avatar
Matteo Quintiliani committed
585
    /* nmxp_log(NMXP_LOG_NORM, NMXP_LOG_D_CHANNEL, "nmxp_meta_chan_search_key()\n"); */
Matteo Quintiliani's avatar
Matteo Quintiliani committed
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600

    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;

601
    nmxp_log(NMXP_LOG_NORM_NO, NMXP_LOG_D_CHANNEL, "nmxp_meta_chan_set_name()\n");
Matteo Quintiliani's avatar
Matteo Quintiliani committed
602
603
604
605
606
607
608
609
610
611
612

    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;

Matteo Quintiliani's avatar
Matteo Quintiliani committed
613
    /* nmxp_log(NMXP_LOG_NORM, NMXP_LOG_D_CHANNEL, "nmxp_meta_chan_set_times()\n"); */
Matteo Quintiliani's avatar
Matteo Quintiliani committed
614
615
616
617
618
619
620
621
622
623
624
625

    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;

626
    nmxp_log(NMXP_LOG_NORM, NMXP_LOG_D_CHANNEL, "nmxp_meta_chan_set_network()\n");
Matteo Quintiliani's avatar
Matteo Quintiliani committed
627
628
629
630
631
632
633
634
635
636
637

    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];
Matteo Quintiliani's avatar
Matteo Quintiliani committed
638
639
    int i_chan = 0;

Matteo Quintiliani's avatar
Matteo Quintiliani committed
640
641
642
    str_start_time[0] = 0;
    str_end_time[0] = 0;

643
    nmxp_log(NMXP_LOG_NORM, NMXP_LOG_D_CHANNEL, "nmxp_meta_chan_print()\n");
Matteo Quintiliani's avatar
Matteo Quintiliani committed
644
645
646
647
648

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

649
650
	nmxp_log(NMXP_LOG_NORM_NO, NMXP_LOG_D_ANY, "%04d %12d %6s%c%-11s (%s  -  %s)\n",
		i_chan+1,
Matteo Quintiliani's avatar
Matteo Quintiliani committed
651
		iter->key,
652
		NMXP_LOG_STR(iter->network),
653
		(strcmp(iter->network, "")==0)? ' ' : '.',
654
655
656
		NMXP_LOG_STR(iter->name),
		NMXP_LOG_STR(str_start_time),
		NMXP_LOG_STR(str_end_time)
Matteo Quintiliani's avatar
Matteo Quintiliani committed
657
658
		);
	iter = iter->next;
659
	i_chan++;
Matteo Quintiliani's avatar
Matteo Quintiliani committed
660
661
662
663
    }
}


664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
void nmxp_meta_chan_print_with_match(NMXP_META_CHAN_LIST *chan_list, char *sta_chan_list) {
    NMXP_META_CHAN_LIST *iter = chan_list;
    char str_start_time[200], str_end_time[200];
    int i_chan = 0;
    int ret_match = 0;
    int istalist, ista;
    char sta_chan_code_pattern[100];

    str_start_time[0] = 0;
    str_end_time[0] = 0;

    nmxp_log(NMXP_LOG_NORM, NMXP_LOG_D_CHANNEL, "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);

	if(sta_chan_list) {

	    ret_match = 0;
	    istalist = 0;
	    while(sta_chan_list[istalist] != sep_chan_list  &&  sta_chan_list[istalist] != 0  &&  ret_match == 0) {

		/* Build sta_chan_code_pattern from sta_chan_list */
		ista = 0;
		while(sta_chan_list[istalist] != sep_chan_list  &&  sta_chan_list[istalist] != 0) {
		    sta_chan_code_pattern[ista++] = sta_chan_list[istalist++];
		}
		sta_chan_code_pattern[ista] = 0;
		if(sta_chan_list[istalist] == sep_chan_list) {
		    istalist++;
		}

		ret_match = nmxp_chan_match(iter->name, sta_chan_code_pattern);
	    }

	} else {
	    ret_match = 1;
	}
	if(ret_match == 1) {
	    nmxp_log(NMXP_LOG_NORM_NO, NMXP_LOG_D_ANY, "%04d %12d %6s%c%-11s (%s  -  %s)\n",
		    i_chan+1,
		    iter->key,
		    NMXP_LOG_STR(iter->network),
		    (strcmp(iter->network, "")==0)? ' ' : '.',
		    NMXP_LOG_STR(iter->name),
		    NMXP_LOG_STR(str_start_time),
		    NMXP_LOG_STR(str_end_time)
		    );
	}
	iter = iter->next;
	i_chan++;
    }
}



Matteo Quintiliani's avatar
Matteo Quintiliani committed
721