Drizzled Public API Documentation

tablename_to_filename.cc
1 /* - mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3  *
4  * Copyright (C) 2010 Brian Aker
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 #include <config.h>
22 #include <string>
23 
24 #include <boost/foreach.hpp>
25 #include <drizzled/util/tablename_to_filename.h>
26 #include <drizzled/internal/my_sys.h>
27 
28 namespace drizzled {
29 namespace util {
30 
31 static const char* hexchars= "0123456789abcdef";
32 
33 
34 /*
35  Translate a table name to a cursor name (WL #1324).
36 
37  SYNOPSIS
38  tablename_to_filename()
39  from The table name
40  to OUT The cursor name
41 
42  RETURN
43  true if errors happen. false on success.
44 */
45 std::string tablename_to_filename(const std::string &from)
46 {
47  std::string to;
48  BOOST_FOREACH(char it, from)
49  {
50  if (isascii(it))
51  {
52  if (isdigit(it) || islower(it) || it == '_' || it == ' ' || it == '-')
53  {
54  to.push_back(it);
55  continue;
56  }
57 
58  if (isupper(it))
59  {
60  to.push_back(tolower(it));
61  continue;
62  }
63  }
64 
65  /* We need to escape this char in a way that can be reversed */
66  to.push_back('@');
67  to.push_back(hexchars[(it >> 4) & 15]);
68  to.push_back(hexchars[it & 15]);
69  }
70 
71  if (drizzled::internal::check_if_legal_tablename(to.c_str()))
72  {
73  to += "@@@";
74  }
75  return to;
76 }
77 
78 } /* namespace util */
79 } /* namespace drizzled */