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
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/* Copyright (C) 2002 MySQL AB

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; version 2 of the License.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */


#ifdef STANDARD
/* STANDARD is defined, don't use any mysql functions */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#ifdef __WIN__
typedef unsigned __int64 ulonglong;	/* Microsofts 64 bit types */
typedef __int64 longlong;
#else
typedef unsigned long long ulonglong;
typedef long long longlong;
#endif /*__WIN__*/
#else
#include <my_global.h>
#include <my_sys.h>
#if defined(MYSQL_SERVER)
#include <m_string.h>		/* To get strmov() */
#else
/* when compiled as standalone */
#include <string.h>
#define strmov(a,b) stpcpy(a,b)
#define bzero(a,b) memset(a,0,b)
#define memcpy_fixed(a,b,c) memcpy(a,b,c)
#endif
#endif
#include <mysql.h>
#include <ctype.h>

#ifdef HAVE_DLOPEN

#define CHAR_LIM 256

static const char* default_dups = "abcdfglmnoprstvz";

typedef struct 
{
  char dup_map[CHAR_LIM]; 
  my_bool inited;
} Dup_map; 

/* These must be right or mysqld will not find the symbol! */

my_bool remove_dups_init(UDF_INIT *initid, UDF_ARGS *args, char *message);
void remove_dups_deinit(UDF_INIT *initid);
char *remove_dups(UDF_INIT *initid, UDF_ARGS *args, char *result,
	       unsigned long *length, char *is_null, char *error);

void init_dup_map(Dup_map* m, const char* dups, uint dup_len);

void init_dup_map(Dup_map* m, const char* dups, uint dup_len)
{
  const char* dup_end = dups + dup_len;
  bzero(m->dup_map, sizeof(m->dup_map));

  for (;dups < dup_end; dups++)
  {
    m->dup_map[(unsigned char)toupper(*dups)] = 1;
    m->dup_map[(unsigned char)tolower(*dups)] = 1;
  }

  m->inited = 1;
}

my_bool remove_dups_init(UDF_INIT *initid __attribute__((unused)), 
                        UDF_ARGS *args, char *message)
{
  if (args->arg_count < 1 || 
    args->arg_count > 2 || args->arg_type[0] != STRING_RESULT ||
    (args->arg_count == 2 && args->arg_type[1] != STRING_RESULT))
  {
    strcpy(message,"Usage: remove_dups(s[,dup_list])");
    return 1;
  }

  if (!(initid->ptr=(char*) malloc(sizeof(Dup_map))))
  {
    strmov(message,"Couldn't allocate memory");
    return 1;
  }
  ((Dup_map*)initid->ptr)->inited = 0;
  return 0;
}


void remove_dups_deinit(UDF_INIT *initid __attribute__((unused)))
{
  if (initid->ptr)
  {  
    free(initid->ptr);
    initid->ptr = 0;
  }  
}



char *remove_dups(UDF_INIT *initid __attribute__((unused)),
               UDF_ARGS *args, char *result, unsigned long *length,
               char *is_null, char *error __attribute__((unused)))
{
  const char *s=args->args[0], *p;
  char* res_p;
  const char* s_end;
  unsigned long res_len = 0;
  int last_removed = CHAR_LIM;
  Dup_map* m = (Dup_map*)initid->ptr;

  if (!s)
  {
     *is_null = 1;
     return 0;
  }

  if (!m->inited)
  {
    const char* dups;
    uint dup_len;

    if (args->arg_count == 1)
    {
      dups = default_dups;
      dup_len = strlen(default_dups);
    }
    else
    {
      dups = args->args[1];
      dup_len = args->lengths[1];
    }
    init_dup_map(m, dups, dup_len);
  }

  s_end = s + args->lengths[0];
  res_p = result;

  for (p = s;p < s_end; p++)
  {
    int lower_p = tolower(*p);
    if (p > s && tolower(p[-1]) == lower_p && 
      m->dup_map[(unsigned char)*p] && last_removed != lower_p)
    {
      last_removed = lower_p;
      continue;
    }  
    *res_p++ = *p; 
    last_removed = CHAR_LIM;
  }

  *length= (unsigned long)(res_p - result);
  return result;
}



#endif /* HAVE_DLOPEN */