Commit 24d51b14 authored by Matteo Quintiliani's avatar Matteo Quintiliani

Added function nmxp_chan_match()


git-svn-id: file:///home/quintiliani/svncopy/nmxptool/trunk@120 3cd66e75-5955-46cb-a940-c26e5fc5497d
parent 4d32e54d
......@@ -46,6 +46,22 @@ typedef enum {
#define getDataTypeFromKey(key) ((key >> 8) & 0xff)
/*! \brief Match station_dot_channel against pattern, treating errors as no match.
*
* \param station_dot_channel STA.CHAN format
* \param pattern STA.?HZ or STA.H?Z or STA.HH? or STA.?H? or ....
*
* \retval 1 for match
* \retval 0 for no match
* \retval -1 on error for invalid pattern
* \retval -2 on error for invalid station_dot_channel
*
*/
int nmxp_chan_match(const char *station_dot_channel, char *pattern);
/*! \brief Looks up a channel key in the list using the name
*
* \param name Channel name.
......
......@@ -16,6 +16,96 @@
#include <stdlib.h>
/*
* 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;
}
int nmxp_chan_lookupKey(char* name, NMXP_CHAN_LIST *channelList)
{
int chan_number = channelList->number;
......@@ -30,6 +120,7 @@ int nmxp_chan_lookupKey(char* name, NMXP_CHAN_LIST *channelList)
return -1;
}
char *nmxp_chan_lookupName(uint32_t key, NMXP_CHAN_LIST *channelList)
{
int i_chan = 0;
......@@ -77,8 +168,8 @@ NMXP_CHAN_LIST *nmxp_chan_getType(NMXP_CHAN_LIST *channelList, NMXP_DATATYPE dat
NMXP_CHAN_LIST *nmxp_chan_subset(NMXP_CHAN_LIST *channelList, NMXP_DATATYPE dataType, char *sta_chan_list) {
NMXP_CHAN_LIST *ret_channelList = NULL;
int istalist, ista;
char sta_chan_code[100];
uint32_t sta_chan_key;
char sta_chan_code_pattern[100];
int i_chan, ret_match;
ret_channelList = (NMXP_CHAN_LIST *) malloc(sizeof(NMXP_CHAN_LIST));
ret_channelList->number = 0;
......@@ -87,23 +178,27 @@ NMXP_CHAN_LIST *nmxp_chan_subset(NMXP_CHAN_LIST *channelList, NMXP_DATATYPE data
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_pattern[ista++] = sta_chan_list[istalist++];
}
sta_chan_code[ista] = 0;
sta_chan_code_pattern[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);
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++;
}
}
return ret_channelList;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment