~drizzle-trunk/drizzle/development

1271.2.2 by Tim Penhey
Add in a rot 13 function.
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
 *
4
 *  Copyright (C) 2010 Tim Penhey <tim@penhey.net>
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
#include <drizzled/plugin/function.h>
22
#include <drizzled/item/func.h>
23
#include <drizzled/function/str/strfunc.h>
24
25
#include <string>
1271.2.5 by Tim Penhey
Make it actually rot-13.
26
#include <sstream>
1271.2.6 by Tim Penhey
OMG it works \o/
27
#include <iostream>
1271.2.2 by Tim Penhey
Add in a rot 13 function.
28
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
29
using namespace drizzled;
30
1271.2.15 by Tim Penhey
More standard indentation.
31
namespace rot13
32
{
33
34
char const* name= "rot13";
35
36
namespace
37
{
38
39
std::string rot13(std::string const& s)
40
{
41
  std::ostringstream sout;
42
  for (std::size_t i= 0, max= s.length(); i < max; ++i)
43
  {
44
    const char& c= s[i];
45
    if ((c >= 'a' && c <= 'm') || (c >= 'A' && c <= 'M'))
46
      sout << char(c + 13);
47
    else if ((c >= 'n' && c <= 'z') || (c >= 'N' && c <= 'Z'))
48
      sout << char(c - 13);
49
    else
50
      sout << c;
51
  }
52
  return sout.str();
53
}
54
55
} // anon namespace
56
57
class Function : public Item_str_func
58
{
59
public:
60
  Function() : Item_str_func() {}
61
  const char *func_name() const { return rot13::name; }
62
63
  String *val_str(String *s)
64
  {
65
    String val;
66
    String *other= args[0]->val_str(&val);
67
    std::string to_rot= String_to_std_string(*other);
68
    return set_String_from_std_string(s, rot13(to_rot));
69
  };
70
71
  void fix_length_and_dec()
72
  {
73
    max_length= args[0]->max_length;
74
  }
75
76
  bool check_argument_count(int n)
77
  {
78
    return (n == 1);
79
  }
80
};
81
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
82
using plugin::Create_function;
1324.2.2 by Monty Taylor
Use the plugin::Context everywhere.
83
using plugin::Context;
1271.2.15 by Tim Penhey
More standard indentation.
84
typedef Create_function<Function> PluginFunction;
85
PluginFunction *rot13_func= NULL;
86
1324.2.2 by Monty Taylor
Use the plugin::Context everywhere.
87
static int init(Context &context)
1271.2.15 by Tim Penhey
More standard indentation.
88
{
89
  rot13_func= new PluginFunction(rot13::name);
1324.2.2 by Monty Taylor
Use the plugin::Context everywhere.
90
  context.add(rot13_func);
1271.2.15 by Tim Penhey
More standard indentation.
91
  return 0;
92
}
93
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
94
} /* namespace rot13 */
1271.2.9 by Tim Penhey
Namespaces are a honking good idea, lets do more of those.
95
1324.2.3 by Monty Taylor
Remove plugin deinit.
96
DRIZZLE_PLUGIN(rot13::init, NULL);