Drizzled Public API Documentation

conv.cc
00001 /* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
00002  *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
00003  *
00004  *  Copyright (C) 2008 Sun Microsystems, Inc.
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/function/str/conv.h>
00022 #include <drizzled/internal/m_string.h>
00023 
00024 namespace drizzled
00025 {
00026 
00027 String *Item_func_conv::val_str(String *str)
00028 {
00029   assert(fixed == 1);
00030   String *res= args[0]->val_str(str);
00031   char *endptr,ans[65],*ptr;
00032   int64_t dec;
00033   int from_base= (int) args[1]->val_int();
00034   int to_base= (int) args[2]->val_int();
00035   int err;
00036 
00037   if (args[0]->null_value || args[1]->null_value || args[2]->null_value ||
00038       abs(to_base) > 36 || abs(to_base) < 2 ||
00039       abs(from_base) > 36 || abs(from_base) < 2 || !(res->length()))
00040   {
00041     null_value= 1;
00042     return NULL;
00043   }
00044   null_value= 0;
00045   unsigned_flag= !(from_base < 0);
00046 
00047   if (from_base < 0)
00048     dec= my_strntoll(res->charset(), res->ptr(), res->length(),
00049                      -from_base, &endptr, &err);
00050   else
00051     dec= (int64_t) my_strntoull(res->charset(), res->ptr(), res->length(),
00052                                  from_base, &endptr, &err);
00053 
00054   ptr= internal::int64_t2str(dec, ans, to_base);
00055   if (str->copy(ans, (uint32_t) (ptr-ans), default_charset()))
00056     return &my_empty_string;
00057   return str;
00058 }
00059 
00060 
00061 } /* namespace drizzled */