drizzle
C++ MySQL-protocol-compatible database server
Loading...
Searching...
No Matches
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 <drizzled/util/tablename_to_filename.h>
25#include <drizzled/internal/my_sys.h>
26
27namespace drizzled {
28namespace util {
29
30static const char* hexchars= "0123456789abcdef";
31
32
33/*
34 Translate a table name to a cursor name (WL #1324).
35
36 SYNOPSIS
37 tablename_to_filename()
38 from The table name
39 to OUT The cursor name
40
41 RETURN
42 true if errors happen. false on success.
43*/
44std::string tablename_to_filename(const std::string &from)
45{
46 std::string to;
47 for (char it : from)
48 {
49 if (isascii(it))
50 {
51 if (isdigit(it) || islower(it) || it == '_' || it == ' ' || it == '-')
52 {
53 to.push_back(it);
54 continue;
55 }
56
57 if (isupper(it))
58 {
59 to.push_back(tolower(it));
60 continue;
61 }
62 }
63
64 /* We need to escape this char in a way that can be reversed */
65 to.push_back('@');
66 to.push_back(hexchars[(it >> 4) & 15]);
67 to.push_back(hexchars[it & 15]);
68 }
69
70 if (drizzled::internal::check_if_legal_tablename(to.c_str()))
71 {
72 to += "@@@";
73 }
74 return to;
75}
76
77} /* namespace util */
78} /* namespace drizzled */
TODO: Rename this file - func.h is stupid.