MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
string_service.cc
1 /* Copyright © 2012, Oracle and/or its affiliates. All rights reserved.
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
15 
16 
17 /*
18  This file provide mysql_string service to plugins.
19  operations on mysql_string can be performed by plugins via these service
20  functions.
21 */
22 
23 #include <my_sys.h>
24 #include "string_service.h"
25 /*
26  This service function converts the mysql_string to the character set
27  specified by charset_name parameter.
28 */
29 extern "C"
30 int mysql_string_convert_to_char_ptr(mysql_string_handle string_handle,
31  const char *charset_name,
32  char *buffer,
33  unsigned int buffer_size,
34  int *error)
35 {
36  String *str= (String *) string_handle;
37  int len= (int)my_convert(buffer, buffer_size - 1, &my_charset_utf8_general_ci,
38  str->ptr(), str->length(), str->charset(),
39  (uint*) error);
40  buffer[len]= '\0';
41  return (len);
42 }
43 
44 /*
45  This service function deallocates the mysql_string_handle allocated on
46  server and used in plugins.
47 */
48 extern "C"
49 void mysql_string_free(mysql_string_handle string_handle)
50 {
51  my_free((String *) string_handle);
52 }
53 
54 /*
55  This service function deallocates the mysql_string_iterator_handle
56  allocated on server and used in plugins.
57 */
58 extern "C"
59 void mysql_string_iterator_free(mysql_string_iterator_handle iterator_handle)
60 {
61  my_free((string_iterator *) iterator_handle);
62 }
63 
64 /* This service function allocate mysql_string_iterator_handle and return it */
65 extern "C"
66 mysql_string_iterator_handle mysql_string_get_iterator(mysql_string_handle
67  string_handle)
68 {
69  String *str= (String *) string_handle;
70  string_iterator *iterator= (string_iterator *) my_malloc(sizeof
71  (struct st_string_iterator), MYF(0));
72  iterator->iterator_str= str;
73  iterator->iterator_ptr= str->ptr();
74  iterator->ctype= 0;
75  return (iterator);
76 }
77 
78 /* Provide service which returns the next mysql_string_iterator_handle */
79 extern "C"
80 int mysql_string_iterator_next(mysql_string_iterator_handle iterator_handle)
81 {
82  int char_len, char_type;
83  string_iterator *iterator= (string_iterator *) iterator_handle;
84  String *str= iterator->iterator_str;
85  const CHARSET_INFO *cs= str->charset();
86  char *end= (char*) str->ptr() + str->length();
87  if (iterator->iterator_ptr == (const char*) end)
88  return (0);
89  char_len= (cs->cset->ctype(cs, &char_type, (uchar*) iterator->iterator_ptr,
90  (uchar*) end));
91  iterator->ctype= char_type;
92  iterator->iterator_ptr+= (char_len > 0 ? char_len : (char_len < 0
93  ? -char_len : 1));
94  return (1);
95 }
96 
97 /*
98  Provide service which calculate weather the current iterator_ptr points to
99  upper case character or not
100 */
101 extern "C"
102 int mysql_string_iterator_isupper(mysql_string_iterator_handle iterator_handle)
103 {
104  string_iterator *iterator= (string_iterator *) iterator_handle;
105  return (iterator->ctype & _MY_U);
106 }
107 
108 /*
109  Provide service which calculate weather the current iterator_ptr points to
110  lower case character or not
111 */
112 extern "C"
113 int mysql_string_iterator_islower(mysql_string_iterator_handle iterator_handle)
114 {
115  string_iterator *iterator= (string_iterator *) iterator_handle;
116  return (iterator->ctype & _MY_L);
117 }
118 
119 /*
120  Provide service which calculate weather the current iterator_ptr points to
121  digit or not
122 */
123 extern "C"
124 int mysql_string_iterator_isdigit(mysql_string_iterator_handle iterator_handle)
125 {
126  string_iterator *iterator= (string_iterator *) iterator_handle;
127  return (iterator->ctype & _MY_NMR);
128 }
129 
130 /*
131  This function provide plugin service to convert a String pointed by handle to
132  lower case. Conversion depends on the client character set info
133 */
134 extern "C"
135 mysql_string_handle mysql_string_to_lowercase(mysql_string_handle string_handle)
136 {
137  String *str= (String *) string_handle;
138  String *res;
139  const CHARSET_INFO *cs= str->charset();
140 
141  if (cs->casedn_multiply == 1)
142  {
143  uint len;
144  len= cs->cset->casedn(cs, (char*) str->ptr(), str->length(),
145  (char*) str->ptr(), str->length());
146  str->length(len);
147  res= str;
148  }
149  else
150  {
151  uint len= str->length() * cs->casedn_multiply;
152  temp_str.alloc(len);
153  temp_str.set_charset(cs);
154  len= cs->cset->casedn(cs, (char*) str->ptr(), str->length(),
155  (char*) temp_str.ptr(), len);
156  temp_str.length(len);
157  res= &temp_str;
158  }
159  return (res);
160 }