~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/function/str/pad.cc

Cleanup around SAFEMALLOC

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
 
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
 
 *
4
 
 *  Copyright (C) 2008 Sun Microsystems
5
 
 *
6
 
 *  This program is free software; you can redistribute it and/or modify
7
 
 *  it under the terms of the GNU General Public License as published by
8
 
 *  the Free Software Foundation; version 2 of the License.
9
 
 *
10
 
 *  This program is distributed in the hope that it will be useful,
11
 
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 
 *  GNU General Public License for more details.
14
 
 *
15
 
 *  You should have received a copy of the GNU General Public License
16
 
 *  along with this program; if not, write to the Free Software
17
 
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
 
 */
19
 
 
20
 
#include "config.h"
21
 
 
22
 
#include <drizzled/function/str/pad.h>
23
 
#include <drizzled/error.h>
24
 
#include <drizzled/function/str/alloc_buffer.h>
25
 
#include <drizzled/session.h>
26
 
 
27
 
namespace drizzled
28
 
{
29
 
 
30
 
void Item_func_rpad::fix_length_and_dec()
31
 
{
32
 
  // Handle character set for args[0] and args[2].
33
 
  if (agg_arg_charsets(collation, &args[0], 2, MY_COLL_ALLOW_CONV, 2))
34
 
    return;
35
 
  if (args[1]->const_item())
36
 
  {
37
 
    uint64_t length= 0;
38
 
 
39
 
    if (collation.collation->mbmaxlen > 0)
40
 
    {
41
 
      uint64_t temp= (uint64_t) args[1]->val_int();
42
 
 
43
 
      /* Assumes that the maximum length of a String is < INT32_MAX. */
44
 
      /* Set here so that rest of code sees out-of-bound value as such. */
45
 
      if (temp > INT32_MAX)
46
 
        temp = INT32_MAX;
47
 
 
48
 
      length= temp * collation.collation->mbmaxlen;
49
 
    }
50
 
 
51
 
    if (length >= MAX_BLOB_WIDTH)
52
 
    {
53
 
      length= MAX_BLOB_WIDTH;
54
 
      maybe_null= 1;
55
 
    }
56
 
    max_length= (ulong) length;
57
 
  }
58
 
  else
59
 
  {
60
 
    max_length= MAX_BLOB_WIDTH;
61
 
    maybe_null= 1;
62
 
  }
63
 
}
64
 
 
65
 
 
66
 
String *Item_func_rpad::val_str(String *str)
67
 
{
68
 
  assert(fixed == 1);
69
 
  uint32_t res_byte_length,res_char_length,pad_char_length,pad_byte_length;
70
 
  char *to;
71
 
  const char *ptr_pad;
72
 
  /* must be int64_t to avoid truncation */
73
 
  int64_t count= args[1]->val_int();
74
 
  int64_t byte_count;
75
 
  String *res= args[0]->val_str(str);
76
 
  String *rpad= args[2]->val_str(&rpad_str);
77
 
 
78
 
  if (!res || args[1]->null_value || !rpad ||
79
 
      ((count < 0) && !args[1]->unsigned_flag))
80
 
    goto err;
81
 
  null_value=0;
82
 
  /* Assumes that the maximum length of a String is < INT32_MAX. */
83
 
  /* Set here so that rest of code sees out-of-bound value as such. */
84
 
  if ((uint64_t) count > INT32_MAX)
85
 
    count= INT32_MAX;
86
 
  if (count <= (res_char_length= res->numchars()))
87
 
  {                                             // String to pad is big enough
88
 
    res->length(res->charpos((int) count));     // Shorten result if longer
89
 
    return (res);
90
 
  }
91
 
  pad_char_length= rpad->numchars();
92
 
 
93
 
  byte_count= count * collation.collation->mbmaxlen;
94
 
  if ((uint64_t) byte_count > current_session->variables.max_allowed_packet)
95
 
  {
96
 
    push_warning_printf(current_session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
97
 
                        ER_WARN_ALLOWED_PACKET_OVERFLOWED,
98
 
                        ER(ER_WARN_ALLOWED_PACKET_OVERFLOWED),
99
 
                        func_name(), current_session->variables.max_allowed_packet);
100
 
    goto err;
101
 
  }
102
 
  if (args[2]->null_value || !pad_char_length)
103
 
    goto err;
104
 
  res_byte_length= res->length();       /* Must be done before alloc_buffer */
105
 
  if (!(res= alloc_buffer(res,str,&tmp_value, (ulong) byte_count)))
106
 
    goto err;
107
 
 
108
 
  to= (char*) res->ptr()+res_byte_length;
109
 
  ptr_pad=rpad->ptr();
110
 
  pad_byte_length= rpad->length();
111
 
  count-= res_char_length;
112
 
  for ( ; (uint32_t) count > pad_char_length; count-= pad_char_length)
113
 
  {
114
 
    memcpy(to,ptr_pad,pad_byte_length);
115
 
    to+= pad_byte_length;
116
 
  }
117
 
  if (count)
118
 
  {
119
 
    pad_byte_length= rpad->charpos((int) count);
120
 
    memcpy(to,ptr_pad,(size_t) pad_byte_length);
121
 
    to+= pad_byte_length;
122
 
  }
123
 
  res->length(to- (char*) res->ptr());
124
 
  return (res);
125
 
 
126
 
 err:
127
 
  null_value=1;
128
 
  return 0;
129
 
}
130
 
 
131
 
 
132
 
void Item_func_lpad::fix_length_and_dec()
133
 
{
134
 
  // Handle character set for args[0] and args[2].
135
 
  if (agg_arg_charsets(collation, &args[0], 2, MY_COLL_ALLOW_CONV, 2))
136
 
    return;
137
 
 
138
 
  if (args[1]->const_item())
139
 
  {
140
 
    uint64_t length= 0;
141
 
 
142
 
    if (collation.collation->mbmaxlen > 0)
143
 
    {
144
 
      uint64_t temp= (uint64_t) args[1]->val_int();
145
 
 
146
 
      /* Assumes that the maximum length of a String is < INT32_MAX. */
147
 
      /* Set here so that rest of code sees out-of-bound value as such. */
148
 
      if (temp > INT32_MAX)
149
 
        temp= INT32_MAX;
150
 
 
151
 
      length= temp * collation.collation->mbmaxlen;
152
 
    }
153
 
 
154
 
    if (length >= MAX_BLOB_WIDTH)
155
 
    {
156
 
      length= MAX_BLOB_WIDTH;
157
 
      maybe_null= 1;
158
 
    }
159
 
    max_length= (ulong) length;
160
 
  }
161
 
  else
162
 
  {
163
 
    max_length= MAX_BLOB_WIDTH;
164
 
    maybe_null= 1;
165
 
  }
166
 
}
167
 
 
168
 
 
169
 
String *Item_func_lpad::val_str(String *str)
170
 
{
171
 
  assert(fixed == 1);
172
 
  uint32_t res_char_length,pad_char_length;
173
 
  /* must be int64_t to avoid truncation */
174
 
  int64_t count= args[1]->val_int();
175
 
  int64_t byte_count;
176
 
  String *res= args[0]->val_str(&tmp_value);
177
 
  String *pad= args[2]->val_str(&lpad_str);
178
 
 
179
 
  if (!res || args[1]->null_value || !pad ||
180
 
      ((count < 0) && !args[1]->unsigned_flag))
181
 
    goto err;
182
 
  null_value=0;
183
 
  /* Assumes that the maximum length of a String is < INT32_MAX. */
184
 
  /* Set here so that rest of code sees out-of-bound value as such. */
185
 
  if ((uint64_t) count > INT32_MAX)
186
 
    count= INT32_MAX;
187
 
 
188
 
  res_char_length= res->numchars();
189
 
 
190
 
  if (count <= res_char_length)
191
 
  {
192
 
    res->length(res->charpos((int) count));
193
 
    return res;
194
 
  }
195
 
 
196
 
  pad_char_length= pad->numchars();
197
 
  byte_count= count * collation.collation->mbmaxlen;
198
 
 
199
 
  if ((uint64_t) byte_count > current_session->variables.max_allowed_packet)
200
 
  {
201
 
    push_warning_printf(current_session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
202
 
                        ER_WARN_ALLOWED_PACKET_OVERFLOWED,
203
 
                        ER(ER_WARN_ALLOWED_PACKET_OVERFLOWED),
204
 
                        func_name(), current_session->variables.max_allowed_packet);
205
 
    goto err;
206
 
  }
207
 
 
208
 
  if (args[2]->null_value || !pad_char_length ||
209
 
      str->alloc((uint32_t) byte_count))
210
 
    goto err;
211
 
 
212
 
  str->length(0);
213
 
  str->set_charset(collation.collation);
214
 
  count-= res_char_length;
215
 
  while (count >= pad_char_length)
216
 
  {
217
 
    str->append(*pad);
218
 
    count-= pad_char_length;
219
 
  }
220
 
  if (count > 0)
221
 
    str->append(pad->ptr(), pad->charpos((int) count), collation.collation);
222
 
 
223
 
  str->append(*res);
224
 
  null_value= 0;
225
 
  return str;
226
 
 
227
 
err:
228
 
  null_value= 1;
229
 
  return 0;
230
 
}
231
 
 
232
 
} /* namespace drizzled */