~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/reverse_function/reverse_function.cc

  • Committer: Padraig O'Sullivan
  • Date: 2009-03-07 20:12:20 UTC
  • mto: (934.3.2 mordred)
  • mto: This revision was merged to the branch mainline in revision 938.
  • Revision ID: osullivan.padraig@gmail.com-20090307201220-u9r93y0knyyb8ggy
Cleaning up my function object a little bit.

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, Inc.
5
 
 *  Copyright (C) 2010 Stewart Smith
6
 
 *
7
 
 *  This program is free software; you can redistribute it and/or modify
8
 
 *  it under the terms of the GNU General Public License as published by
9
 
 *  the Free Software Foundation; version 2 of the License.
10
 
 *
11
 
 *  This program is distributed in the hope that it will be useful,
12
 
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 
 *  GNU General Public License for more details.
15
 
 *
16
 
 *  You should have received a copy of the GNU General Public License
17
 
 *  along with this program; if not, write to the Free Software
18
 
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19
 
 */
20
 
 
21
 
#include "config.h"
22
 
 
23
 
#include <drizzled/plugin/function.h>
24
 
#include <drizzled/function/str/strfunc.h>
25
 
#include <drizzled/charset_info.h>
26
 
 
27
 
using namespace drizzled;
28
 
 
29
 
class ReverseFunction :public Item_str_func
30
 
{
31
 
  String tmp_value;
32
 
public:
33
 
  ReverseFunction() :Item_str_func() {}
34
 
  String *val_str(String *);
35
 
  void fix_length_and_dec();
36
 
  const char *func_name() const { return "reverse"; }
37
 
 
38
 
  bool check_argument_count(int n)
39
 
  {
40
 
    return n == 1;
41
 
  }
42
 
};
43
 
 
44
 
String *ReverseFunction::val_str(String *str)
45
 
{
46
 
  assert(fixed == 1);
47
 
  String *res = args[0]->val_str(str);
48
 
  char *ptr, *end, *tmp;
49
 
 
50
 
  if ((null_value=args[0]->null_value))
51
 
    return 0;
52
 
  /* An empty string is a special case as the string pointer may be null */
53
 
  if (!res->length())
54
 
    return &my_empty_string;
55
 
  if (tmp_value.alloced_length() < res->length() &&
56
 
      tmp_value.realloc(res->length()))
57
 
  {
58
 
    null_value= 1;
59
 
    return 0;
60
 
  }
61
 
  tmp_value.length(res->length());
62
 
  tmp_value.set_charset(res->charset());
63
 
  ptr= (char *) res->ptr();
64
 
  end= ptr + res->length();
65
 
  tmp= (char *) tmp_value.ptr() + tmp_value.length();
66
 
  if (use_mb(res->charset()))
67
 
  {
68
 
    register uint32_t l;
69
 
    while (ptr < end)
70
 
    {
71
 
      if ((l= my_ismbchar(res->charset(),ptr,end)))
72
 
      {
73
 
        tmp-= l;
74
 
        memcpy(tmp,ptr,l);
75
 
        ptr+= l;
76
 
      }
77
 
      else
78
 
        *--tmp= *ptr++;
79
 
    }
80
 
  }
81
 
  else
82
 
  {
83
 
    while (ptr < end)
84
 
      *--tmp= *ptr++;
85
 
  }
86
 
  return &tmp_value;
87
 
}
88
 
 
89
 
void ReverseFunction::fix_length_and_dec()
90
 
{
91
 
  collation.set(args[0]->collation);
92
 
  max_length = args[0]->max_length;
93
 
}
94
 
 
95
 
plugin::Create_function<ReverseFunction> *reverse_function= NULL;
96
 
 
97
 
static int initialize(drizzled::module::Context &context)
98
 
{
99
 
  reverse_function= new plugin::Create_function<ReverseFunction>("reverse");
100
 
  context.add(reverse_function);
101
 
  return 0;
102
 
}
103
 
 
104
 
DRIZZLE_DECLARE_PLUGIN
105
 
{
106
 
  DRIZZLE_VERSION_ID,
107
 
  "reverse_function",
108
 
  "1.0",
109
 
  "Stewart Smith",
110
 
  "reverses a string",
111
 
  PLUGIN_LICENSE_GPL,
112
 
  initialize, /* Plugin Init */
113
 
  NULL,   /* depends */
114
 
  NULL    /* config options */
115
 
}
116
 
DRIZZLE_DECLARE_PLUGIN_END;