~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/rot13/rot13.cc

  • Committer: Lee Bieber
  • Date: 2011-04-17 02:05:51 UTC
  • mfrom: (2279.1.3 build)
  • Revision ID: kalebral@gmail.com-20110417020551-59m0hx1p10b0up1x
Merge Joe - 760367: DATA_DICTIONARY.CUMULATIVE_USER_STATS.CONNECTED_TIME_SEC reports bogus results
Merge Olaf - Refactor Catalog Cache
Merge Olaf - Delete unused functions

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
 
using namespace drizzled;
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 plugin::Create_function;
83
 
using module::Context;
84
 
typedef Create_function<Function> PluginFunction;
85
 
PluginFunction *rot13_func= NULL;
86
 
 
87
 
static int init(Context &context)
88
 
{
89
 
  rot13_func= new PluginFunction(rot13::name);
90
 
  context.add(rot13_func);
91
 
  return 0;
92
 
}
93
 
 
94
 
} /* namespace rot13 */
95
 
 
96
 
DRIZZLE_PLUGIN(rot13::init, NULL, NULL);