~ubuntu-branches/ubuntu/trusty/drizzle/trusty

« back to all changes in this revision

Viewing changes to plugin/reverse_function/reverse_function.cc

  • Committer: Bazaar Package Importer
  • Author(s): Monty Taylor
  • Date: 2010-03-18 12:12:31 UTC
  • Revision ID: james.westby@ubuntu.com-20100318121231-k6g1xe6cshbwa0f8
Tags: upstream-2010.03.1347
ImportĀ upstreamĀ versionĀ 2010.03.1347

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
 *  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
 
 
26
using namespace drizzled;
 
27
 
 
28
class ReverseFunction :public Item_str_func
 
29
{
 
30
  String tmp_value;
 
31
public:
 
32
  ReverseFunction() :Item_str_func() {}
 
33
  String *val_str(String *);
 
34
  void fix_length_and_dec();
 
35
  const char *func_name() const { return "reverse"; }
 
36
 
 
37
  bool check_argument_count(int n)
 
38
  {
 
39
    return n == 1;
 
40
  }
 
41
};
 
42
 
 
43
String *ReverseFunction::val_str(String *str)
 
44
{
 
45
  assert(fixed == 1);
 
46
  String *res = args[0]->val_str(str);
 
47
  char *ptr, *end, *tmp;
 
48
 
 
49
  if ((null_value=args[0]->null_value))
 
50
    return 0;
 
51
  /* An empty string is a special case as the string pointer may be null */
 
52
  if (!res->length())
 
53
    return &my_empty_string;
 
54
  if (tmp_value.alloced_length() < res->length() &&
 
55
      tmp_value.realloc(res->length()))
 
56
  {
 
57
    null_value= 1;
 
58
    return 0;
 
59
  }
 
60
  tmp_value.length(res->length());
 
61
  tmp_value.set_charset(res->charset());
 
62
  ptr= (char *) res->ptr();
 
63
  end= ptr + res->length();
 
64
  tmp= (char *) tmp_value.ptr() + tmp_value.length();
 
65
  if (use_mb(res->charset()))
 
66
  {
 
67
    register uint32_t l;
 
68
    while (ptr < end)
 
69
    {
 
70
      if ((l= my_ismbchar(res->charset(),ptr,end)))
 
71
      {
 
72
        tmp-= l;
 
73
        memcpy(tmp,ptr,l);
 
74
        ptr+= l;
 
75
      }
 
76
      else
 
77
        *--tmp= *ptr++;
 
78
    }
 
79
  }
 
80
  else
 
81
  {
 
82
    while (ptr < end)
 
83
      *--tmp= *ptr++;
 
84
  }
 
85
  return &tmp_value;
 
86
}
 
87
 
 
88
void ReverseFunction::fix_length_and_dec()
 
89
{
 
90
  collation.set(args[0]->collation);
 
91
  max_length = args[0]->max_length;
 
92
}
 
93
 
 
94
plugin::Create_function<ReverseFunction> *reverse_function= NULL;
 
95
 
 
96
static int initialize(drizzled::plugin::Registry &registry)
 
97
{
 
98
  reverse_function= new plugin::Create_function<ReverseFunction>("reverse");
 
99
  registry.add(reverse_function);
 
100
  return 0;
 
101
}
 
102
 
 
103
static int finalize(drizzled::plugin::Registry &registry)
 
104
{
 
105
   registry.remove(reverse_function);
 
106
   delete reverse_function;
 
107
   return 0;
 
108
}
 
109
 
 
110
DRIZZLE_DECLARE_PLUGIN
 
111
{
 
112
  DRIZZLE_VERSION_ID,
 
113
  "reverse_function",
 
114
  "1.0",
 
115
  "Stewart Smith",
 
116
  "reverses a string",
 
117
  PLUGIN_LICENSE_GPL,
 
118
  initialize, /* Plugin Init */
 
119
  finalize,   /* Plugin Deinit */
 
120
  NULL,   /* system variables */
 
121
  NULL    /* config options */
 
122
}
 
123
DRIZZLE_DECLARE_PLUGIN_END;