Drizzled Public API Documentation

ipv6_function.cc
00001 /*
00002   Original copyright header listed below. This comes via rsync.
00003   Any additional changes are provided via the same license as the original.
00004 
00005   Copyright (C) 2011 Muhammad Umair
00006 
00007 */
00008 /*
00009  * Copyright (C) 1996-2001  Internet Software Consortium.
00010  *
00011  * Permission to use, copy, modify, and distribute this software for any
00012  * purpose with or without fee is hereby granted, provided that the above
00013  * copyright notice and this permission notice appear in all copies.
00014  *
00015  * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM
00016  * DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
00017  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
00018  * INTERNET SOFTWARE CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
00019  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
00020  * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
00021  * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
00022  * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
00023  */
00024 
00025 #include <config.h>
00026 
00027 #include <drizzled/error.h>
00028 #include <drizzled/charset.h>
00029 #include <drizzled/function/str/strfunc.h>
00030 #include <drizzled/item/func.h>
00031 #include <drizzled/plugin/function.h>
00032 
00033 #include <drizzled/type/ipv6.h>
00034 
00035 namespace plugin {
00036 namespace ipv6 {
00037 
00038 class Generate: public drizzled::Item_str_func
00039 {
00040 public:
00041   Generate(): drizzled::Item_str_func() {}
00042   void fix_length_and_dec()
00043   {
00044     max_length= (drizzled::type::IPv6::IPV6_BUFFER_LENGTH) * drizzled::system_charset_info->mbmaxlen;
00045   }
00046   const char *func_name() const{ return "ipv6"; }
00047   drizzled::String *val_str(drizzled::String *);
00048 };
00049 
00050 
00051 drizzled::String *Generate::val_str(drizzled::String *str)
00052 {
00053   drizzled::String _result;
00054   drizzled::String *result= val_str(&_result);
00055   drizzled::type::IPv6 ptr_address;
00056 
00057   if (not ptr_address.inet_pton(result->c_str()))
00058   {
00059     drizzled::my_error(drizzled::ER_INVALID_IPV6_VALUE, MYF(ME_FATALERROR));
00060      str->length(0);
00061 
00062     return str;
00063   }
00064 
00065   char *ipv6_string;
00066   str->realloc(drizzled::type::IPv6::IPV6_BUFFER_LENGTH);
00067   str->length(drizzled::type::IPv6::IPV6_BUFFER_LENGTH);
00068   str->set_charset(drizzled::system_charset_info);
00069   ipv6_string= (char *) str->ptr();
00070 
00071   if (not ptr_address.inet_ntop(ipv6_string))
00072   {
00073     drizzled::my_error(drizzled::ER_INVALID_IPV6_VALUE, MYF(ME_FATALERROR));
00074     str->length(0);
00075 
00076     return str;
00077   }
00078   str->length(max_length);
00079 
00080   return str;
00081 }
00082 
00083 } // namespace ipv6
00084 } // namespace plugin
00085 
00086 static int initialize(drizzled::module::Context &context)
00087 {
00088   context.add(new drizzled::plugin::Create_function<plugin::ipv6::Generate>("ipv6"));
00089 
00090   return 0;
00091 }
00092 
00093 DRIZZLE_DECLARE_PLUGIN
00094 {
00095   DRIZZLE_VERSION_ID,
00096   "ipv6",
00097   "1.0",
00098   "Muhammad Umair",
00099   "IPV6() function",
00100   drizzled::PLUGIN_LICENSE_GPL,
00101   initialize, /* Plugin Init */
00102   NULL,   /* depends */
00103   NULL    /* config options */
00104 }
00105 DRIZZLE_DECLARE_PLUGIN_END;