~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/rot13/rot13.cc

  • Committer: Brian Aker
  • Date: 2010-01-27 19:09:05 UTC
  • mfrom: (1271.2.15 rot-13)
  • Revision ID: brian@gaz-20100127190905-a60d1px4bzuz4q50
Merge rot13()

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) 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>
 
26
#include <sstream>
 
27
#include <iostream>
 
28
 
 
29
namespace plugin
 
30
{
 
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
 
 
82
using drizzled::plugin::Create_function;
 
83
using drizzled::plugin::Registry;
 
84
typedef Create_function<Function> PluginFunction;
 
85
PluginFunction *rot13_func= NULL;
 
86
 
 
87
static int init(Registry &registry)
 
88
{
 
89
  rot13_func= new PluginFunction(rot13::name);
 
90
  registry.add(rot13_func);
 
91
  return 0;
 
92
}
 
93
 
 
94
static int deinit(Registry &registry)
 
95
{
 
96
  registry.remove(rot13_func);
 
97
  delete rot13_func;
 
98
  return 0;
 
99
}
 
100
 
 
101
} // rot13 namespace
 
102
} // plugin namespace
 
103
 
 
104
DRIZZLE_PLUGIN(plugin::rot13::init, plugin::rot13::deinit, NULL, NULL);