~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/strfunc.cc

  • Committer: Monty Taylor
  • Date: 2008-08-01 22:33:44 UTC
  • mto: (236.1.42 codestyle)
  • mto: This revision was merged to the branch mainline in revision 261.
  • Revision ID: monty@inaugust.com-20080801223344-vzhlflfmtijp1imv
First pass at gettexizing the error messages.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* Copyright (C) 2003 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
 
/* Some useful string utility functions used by the MySQL server */
17
 
#include "config.h"
18
 
 
19
 
#include "drizzled/strfunc.h"
20
 
#include "drizzled/typelib.h"
21
 
#include "drizzled/charset_info.h"
22
 
#include "drizzled/global_charset_info.h"
23
 
 
24
 
/*
25
 
  Return bitmap for strings used in a set
26
 
 
27
 
  SYNOPSIS
28
 
  find_set()
29
 
  lib                   Strings in set
30
 
  str                   Strings of set-strings separated by ','
31
 
  err_pos               If error, set to point to start of wrong set string
32
 
  err_len               If error, set to the length of wrong set string
33
 
  set_warning           Set to 1 if some string in set couldn't be used
34
 
 
35
 
  NOTE
36
 
    We delete all end space from str before comparison
37
 
 
38
 
  RETURN
39
 
    bitmap of all sets found in x.
40
 
    set_warning is set to 1 if there was any sets that couldn't be set
41
 
*/
42
 
 
43
 
static const char field_separator=',';
44
 
 
45
 
uint64_t find_set(TYPELIB *lib, const char *str, uint32_t length,
46
 
                  const CHARSET_INFO * const cs,
47
 
                  char **err_pos, uint32_t *err_len, bool *set_warning)
48
 
{
49
 
  const CHARSET_INFO * const strip= cs ? cs : &my_charset_utf8_general_ci;
50
 
  const char *end= str + strip->cset->lengthsp(strip, str, length);
51
 
  uint64_t found= 0;
52
 
  *err_pos= 0;                  // No error yet
53
 
  if (str != end)
54
 
  {
55
 
    const char *start= str;
56
 
    for (;;)
57
 
    {
58
 
      const char *pos= start;
59
 
      uint32_t var_len;
60
 
      int mblen= 1;
61
 
 
62
 
      for (; pos != end && *pos != field_separator; pos++) 
63
 
      {}
64
 
      var_len= (uint32_t) (pos - start);
65
 
      uint32_t find= cs ? find_type2(lib, start, var_len, cs) :
66
 
                      find_type(lib, start, var_len, (bool) 0);
67
 
      if (!find)
68
 
      {
69
 
        *err_pos= (char*) start;
70
 
        *err_len= var_len;
71
 
        *set_warning= 1;
72
 
      }
73
 
      else
74
 
        found|= ((int64_t) 1 << (find - 1));
75
 
      if (pos >= end)
76
 
        break;
77
 
      start= pos + mblen;
78
 
    }
79
 
  }
80
 
  return found;
81
 
}
82
 
 
83
 
 
84
 
/*
85
 
  Function to find a string in a TYPELIB
86
 
  (Same format as mysys/typelib.c)
87
 
 
88
 
  SYNOPSIS
89
 
   find_type()
90
 
   lib                  TYPELIB (struct of pointer to values + count)
91
 
   find                 String to find
92
 
   length               Length of string to find
93
 
   part_match           Allow part matching of value
94
 
 
95
 
 RETURN
96
 
  0 error
97
 
  > 0 position in TYPELIB->type_names +1
98
 
*/
99
 
 
100
 
uint32_t find_type(const TYPELIB *lib, const char *find, uint32_t length,
101
 
               bool part_match)
102
 
{
103
 
  uint32_t found_count=0, found_pos=0;
104
 
  const char *end= find+length;
105
 
  const char *i;
106
 
  const char *j;
107
 
  for (uint32_t pos=0 ; (j=lib->type_names[pos++]) ; )
108
 
  {
109
 
    for (i=find ; i != end &&
110
 
           my_toupper(system_charset_info,*i) ==
111
 
           my_toupper(system_charset_info,*j) ; i++, j++) ;
112
 
    if (i == end)
113
 
    {
114
 
      if (! *j)
115
 
        return(pos);
116
 
      found_count++;
117
 
      found_pos= pos;
118
 
    }
119
 
  }
120
 
  return(found_count == 1 && part_match ? found_pos : 0);
121
 
}
122
 
 
123
 
 
124
 
/*
125
 
  Find a string in a list of strings according to collation
126
 
 
127
 
  SYNOPSIS
128
 
   find_type2()
129
 
   lib                  TYPELIB (struct of pointer to values + count)
130
 
   x                    String to find
131
 
   length               String length
132
 
   cs                   Character set + collation to use for comparison
133
 
 
134
 
  NOTES
135
 
 
136
 
  RETURN
137
 
    0   No matching value
138
 
    >0  Offset+1 in typelib for matched string
139
 
*/
140
 
 
141
 
uint32_t find_type2(const TYPELIB *typelib, const char *x, uint32_t length,
142
 
                const CHARSET_INFO * const cs)
143
 
{
144
 
  int pos;
145
 
  const char *j;
146
 
 
147
 
  if (!typelib->count)
148
 
  {
149
 
    return(0);
150
 
  }
151
 
 
152
 
  for (pos=0 ; (j=typelib->type_names[pos]) ; pos++)
153
 
  {
154
 
    if (!my_strnncoll(cs, (const unsigned char*) x, length,
155
 
                          (const unsigned char*) j, typelib->type_lengths[pos]))
156
 
      return(pos+1);
157
 
  }
158
 
  return(0);
159
 
} /* find_type */
160