00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
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 }
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 }
00095
00096 DRIZZLE_PLUGIN(rot13::init, NULL, NULL);