Drizzled Public API Documentation

tablename_to_filename.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 Brian Aker
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; either version 2 of the License, or
00009  *  (at your option) any later version.
00010  *
00011  *  This program is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *  GNU General Public License for more details.
00015  *
00016  *  You should have received a copy of the GNU General Public License
00017  *  along with this program; if not, write to the Free Software
00018  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00019  */
00020 
00021 #include <config.h>
00022 #include <string>
00023 
00024 #include <drizzled/util/tablename_to_filename.h>
00025 
00026 #include <drizzled/internal/my_sys.h>
00027 
00028 namespace drizzled
00029 {
00030 namespace util
00031 {
00032 
00033 static const char hexchars[]= "0123456789abcdef";
00034 
00035 
00036 /*
00037   Translate a table name to a cursor name (WL #1324).
00038 
00039   SYNOPSIS
00040     tablename_to_filename()
00041       from                      The table name
00042       to                OUT     The cursor name
00043 
00044   RETURN
00045     true if errors happen. false on success.
00046 */
00047 bool tablename_to_filename(const std::string &from, std::string &to)
00048 {
00049   
00050   std::string::const_iterator iter= from.begin();
00051   for (; iter != from.end(); ++iter)
00052   {
00053     if (isascii(*iter))
00054     {
00055       if ((isdigit(*iter)) ||
00056           (islower(*iter)) ||
00057           (*iter == '_') ||
00058           (*iter == ' ') ||
00059           (*iter == '-'))
00060       {
00061         to.push_back(*iter);
00062         continue;
00063       }
00064 
00065       if (isupper(*iter))
00066       {
00067         to.push_back(tolower(*iter));
00068         continue;
00069       }
00070     }
00071    
00072     /* We need to escape this char in a way that can be reversed */
00073     to.push_back('@');
00074     to.push_back(hexchars[(*iter >> 4) & 15]);
00075     to.push_back(hexchars[(*iter) & 15]);
00076   }
00077 
00078   if (drizzled::internal::check_if_legal_tablename(to.c_str()))
00079   {
00080     to.append("@@@");
00081   }
00082   return false;
00083 }
00084 
00085 } /* namespace util */
00086 } /* namespace drizzled */