Drizzled Public API Documentation

rot13.cc

00001 /* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
00002  *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
00003  *
00004  *  Copyright (C) 2010 Tim Penhey <tim@penhey.net>
00005  *
00006  *  This program is free software; you can redistribute it and/or modify
00007  *  it under the terms of the GNU General Public License as published by
00008  *  the Free Software Foundation; version 2 of the License.
00009  *
00010  *  This program is distributed in the hope that it will be useful,
00011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  *  GNU General Public License for more details.
00014  *
00015  *  You should have received a copy of the GNU General Public License
00016  *  along with this program; if not, write to the Free Software
00017  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00018  */
00019 
00020 #include <config.h>
00021 #include <drizzled/plugin/function.h>
00022 #include <drizzled/item/func.h>
00023 #include <drizzled/function/str/strfunc.h>
00024 
00025 #include <string>
00026 #include <sstream>
00027 #include <iostream>
00028 
00029 using namespace drizzled;
00030 
00031 namespace rot13
00032 {
00033 
00034 char const* name= "rot13";
00035 
00036 namespace
00037 {
00038 
00039 std::string rot13(std::string const& s)
00040 {
00041   std::ostringstream sout;
00042   for (std::size_t i= 0, max= s.length(); i < max; ++i)
00043   {
00044     const char& c= s[i];
00045     if ((c >= 'a' && c <= 'm') || (c >= 'A' && c <= 'M'))
00046       sout << char(c + 13);
00047     else if ((c >= 'n' && c <= 'z') || (c >= 'N' && c <= 'Z'))
00048       sout << char(c - 13);
00049     else
00050       sout << c;
00051   }
00052   return sout.str();
00053 }
00054 
00055 } // anon namespace
00056 
00057 class Function : public Item_str_func
00058 {
00059 public:
00060   Function() : Item_str_func() {}
00061   const char *func_name() const { return rot13::name; }
00062 
00063   String *val_str(String *s)
00064   {
00065     String val;
00066     String *other= args[0]->val_str(&val);
00067     std::string to_rot= String_to_std_string(*other);
00068     return set_String_from_std_string(s, rot13(to_rot));
00069   };
00070 
00071   void fix_length_and_dec()
00072   {
00073     max_length= args[0]->max_length;
00074   }
00075 
00076   bool check_argument_count(int n)
00077   {
00078     return (n == 1);
00079   }
00080 };
00081 
00082 using plugin::Create_function;
00083 using module::Context;
00084 typedef Create_function<Function> PluginFunction;
00085 PluginFunction *rot13_func= NULL;
00086 
00087 static int init(Context &context)
00088 {
00089   rot13_func= new PluginFunction(rot13::name);
00090   context.add(rot13_func);
00091   return 0;
00092 }
00093 
00094 } /* namespace rot13 */
00095 
00096 DRIZZLE_PLUGIN(rot13::init, NULL, NULL);