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 <boost/foreach.hpp>
00025 #include <drizzled/util/tablename_to_filename.h>
00026 #include <drizzled/internal/my_sys.h>
00027 
00028 namespace drizzled {
00029 namespace util {
00030 
00031 static const char* hexchars= "0123456789abcdef";
00032 
00033 
00034 /*
00035   Translate a table name to a cursor name (WL #1324).
00036 
00037   SYNOPSIS
00038     tablename_to_filename()
00039       from                      The table name
00040       to                OUT     The cursor name
00041 
00042   RETURN
00043     true if errors happen. false on success.
00044 */
00045 std::string tablename_to_filename(const std::string &from)
00046 {
00047   std::string to;
00048   BOOST_FOREACH(char it, from)
00049   {
00050     if (isascii(it))
00051     {
00052       if (isdigit(it) || islower(it) || it == '_' || it == ' ' || it == '-')
00053       {
00054         to.push_back(it);
00055         continue;
00056       }
00057 
00058       if (isupper(it))
00059       {
00060         to.push_back(tolower(it));
00061         continue;
00062       }
00063     }
00064    
00065     /* We need to escape this char in a way that can be reversed */
00066     to.push_back('@');
00067     to.push_back(hexchars[(it >> 4) & 15]);
00068     to.push_back(hexchars[it & 15]);
00069   }
00070 
00071   if (drizzled::internal::check_if_legal_tablename(to.c_str()))
00072   {
00073     to += "@@@";
00074   }
00075   return to;
00076 }
00077 
00078 } /* namespace util */
00079 } /* namespace drizzled */