File: str_match.c

package info (click to toggle)
fis-gtm 7.1-006-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 32,908 kB
  • sloc: ansic: 344,906; asm: 5,184; csh: 4,859; sh: 2,000; awk: 294; makefile: 73; sed: 13
file content (106 lines) | stat: -rwxr-xr-x 2,666 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
/****************************************************************
 *								*
 *	Copyright 2001, 2007 Fidelity Information Services, Inc	*
 *								*
 *	This source code contains the intellectual property	*
 *	of its copyright holder(s), and is made available	*
 *	under a license.  If you do not know the terms of	*
 *	the license, please stop and do not read further.	*
 *								*
 ****************************************************************/

/* str_match.c */

#include "mdef.h"

#include "str_match.h"

char *mystrstr(char *o, unsigned short olen, char *t, unsigned short tlen);

char *mystrstr(char *o, unsigned short olen, char *t, unsigned short tlen)
{
        char            *ptr;
        unsigned short  length;
        bool            matched = FALSE;

        if (olen < tlen)
                return NULL;

        for (ptr = o; ptr <= o + olen - tlen; ptr++)
        {
                length = 0;
                matched = TRUE;
                for (length = 0; length < tlen; length++)
                {
                        if (('?' != *(t + length)) && (*(ptr + length) != *(t + length)))
                        {
                                matched = FALSE;
                                break;
                        }
                }
                if (TRUE == matched)
                        return ptr;
        }

        return NULL;
}

bool str_match(char *ori, unsigned short orilen, char *template, unsigned short template_len)
{
	char 		*c, *c_top, *start, *c_prev;
	template_struct	temps;
	unsigned short	counter = 0, i, len, prev_counter;
	bool		wild = FALSE;

	/* ================== Analyze the template string ============================== */

	for (c = template, c_top = template + template_len; c < c_top;)
	{
		c_prev = c;
		while (c < c_top && '*' == *c)
			c++;
		if (c > c_prev)
		{
			temps.sub[counter].addr = NULL;
			temps.sub[counter].len = 0;
			counter++;
			c_prev = c;
		}
		while (c < c_top && '*' != *c)
			c++;
		if (c > c_prev)
		{
			temps.sub[counter].addr = c_prev;
			temps.sub[counter].len = INTCAST(c - c_prev);
			counter++;
		}
	}
	temps.n_subs = counter;

	/* ================== Match the original string ================================ */

	c_prev = ori;
	c_top = ori + orilen;

	for(i = 0; i < temps.n_subs; i++)
	{
		if (0 == temps.sub[i].len)
		{
			wild = TRUE;
			continue;
		}
		else
		{
			if ((NULL == (c = mystrstr(c_prev, c_top - c_prev, temps.sub[i].addr, temps.sub[i].len)))
				|| ((c_prev != c) && (TRUE != wild)))
				return FALSE;
			c_prev = c + temps.sub[i].len;
			wild = FALSE;
		}
	}

	if ((c_prev != c_top) && (TRUE != wild))
		return FALSE;

	return TRUE;
}