~drizzle-trunk/drizzle/development

1 by brian
clean slate
1
/* Copyright (C) 2000 MySQL AB
2
3
   This program is free software; you can redistribute it and/or modify
4
   it under the terms of the GNU General Public License as published by
5
   the Free Software Foundation; version 2 of the License.
6
7
   This program is distributed in the hope that it will be useful,
8
   but WITHOUT ANY WARRANTY; without even the implied warranty of
9
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10
   GNU General Public License for more details.
11
12
   You should have received a copy of the GNU General Public License
13
   along with this program; if not, write to the Free Software
14
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
15
16
/* Functions for finding files with wildcards */
17
18
/*
19
  The following file-name-test is supported:
20
  - "name [[,] name...]			; Matches any of used filenames.
21
					  Each name can have "*" and/or "?"
22
					  wild-cards.
23
  - [wildspec [,]] !wildspec2		; File that matches wildspec and not
24
					  wildspec2.
25
*/
26
27
#include "mysys_priv.h"
28
#include <m_string.h>
29
30
	/* Store wildcard-string in a easyer format */
31
32
WF_PACK *wf_comp(char * str)
33
{
34
  uint ant;
35
  int not_pos;
36
  register char * pos;
37
  char * buffer;
38
  WF_PACK *ret;
39
  DBUG_ENTER("wf_comp");
40
41
  not_pos= -1;			/* Skip space and '!' in front */
42
  while (*str == ' ')
43
    str++;
44
  if (*str == '!')
45
  {
46
    not_pos=0;
47
    while (*++str == ' ') {};
48
  }
49
  if (*str == 0)				/* Empty == everything */
50
    DBUG_RETURN((WF_PACK *) NULL);
51
52
  ant=1;					/* Count filespecs */
53
  for (pos=str ; *pos ; pos++)
54
    ant+= test(*pos == ' ' || *pos == ',');
55
56
  if ((ret= (WF_PACK*) my_malloc((uint) ant*(sizeof(char **)+2)+
57
				 sizeof(WF_PACK)+ (uint) strlen(str)+1,
58
				 MYF(MY_WME)))
59
	== 0)
60
    DBUG_RETURN((WF_PACK *) NULL);
61
  ret->wild= (char **) (ret+1);
62
  buffer= (char *) (ret->wild+ant);
63
64
  ant=0;
65
  for (pos=str ; *pos ; str= pos)
66
  {
67
    ret->wild[ant++]=buffer;
68
    while (*pos != ' ' && *pos != ',' && *pos != '!' && *pos)
69
      *buffer++ = *pos++;
70
71
    *buffer++ = '\0';
72
    while (*pos == ' ' || *pos == ',' || *pos == '!' )
73
      if (*pos++ == '!' && not_pos <0)
74
	not_pos=(int) ant;
75
  }
76
77
  ret->wilds=ant;
78
  if (not_pos <0)
79
    ret->not_pos=ant;
80
  else
81
    ret->not_pos=(uint) not_pos;
82
83
  DBUG_PRINT("exit",("antal: %d  not_pos: %d",ret->wilds,ret->not_pos));
84
  DBUG_RETURN(ret);
85
} /* wf_comp */
86
87
88
	/* Test if a given filename is matched */
89
90
int wf_test(register WF_PACK *wf_pack, register const char *name)
91
{
92
  register uint i;
93
  register uint not_pos;
94
  DBUG_ENTER("wf_test");
95
96
  if (! wf_pack || wf_pack->wilds == 0)
97
    DBUG_RETURN(0);			/* Everything goes */
98
99
  not_pos=wf_pack->not_pos;
100
  for (i=0 ; i < not_pos; i++)
101
    if (wild_compare(name,wf_pack->wild[i],0) == 0)
102
      goto found;
103
  if (i)
104
    DBUG_RETURN(1);			/* No-match */
105
106
found:
107
/* Test that it isn't in not-list */
108
109
  for (i=not_pos ; i < wf_pack->wilds; i++)
110
    if (wild_compare(name,wf_pack->wild[i],0) == 0)
111
      DBUG_RETURN(1);
112
  DBUG_RETURN(0);
113
} /* wf_test */
114
115
116
	/* We need this because program don't know with malloc we used */
117
118
void wf_end(WF_PACK *buffer)
119
{
120
  DBUG_ENTER("wf_end");
121
  if (buffer)
122
    my_free((uchar*) buffer,MYF(0));
123
  DBUG_VOID_RETURN;
124
} /* wf_end */