~drizzle-trunk/drizzle/development

2136.3.2 by Brian Aker
Merge in boolean convert function so we use the same code for conversion.
1
/* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
 *
4
 *  Copyright (C) 2011 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
2173.2.1 by Monty Taylor
Fixes incorrect usage of include
21
#include <config.h>
2136.3.2 by Brian Aker
Merge in boolean convert function so we use the same code for conversion.
22
2173.2.1 by Monty Taylor
Fixes incorrect usage of include
23
#include <drizzled/sql_string.h>
24
#include <drizzled/type/boolean.h>
2281.5.1 by Muhammad Umair
Merged charset declarations of global_charset_info.h and charset_info.h into charset.h header file.
25
#include <drizzled/charset.h>
2136.3.2 by Brian Aker
Merge in boolean convert function so we use the same code for conversion.
26
2318.6.14 by Olaf van der Spek
Refactor
27
namespace drizzled {
28
namespace type {
29
2318.7.7 by Olaf van der Spek
Refactor type::Boolean::convert
30
const char* convert(bool source, bool ansi_display)
2318.6.14 by Olaf van der Spek
Refactor
31
{
2136.3.3 by Brian Aker
Move over the convert to be in the type class.
32
  if (source)
2318.7.7 by Olaf van der Spek
Refactor type::Boolean::convert
33
    return ansi_display ? "YES" : "TRUE";
34
  return ansi_display ? "NO" : "FALSE";
35
}
36
37
void convert(String& destination, bool source, bool ansi_display)
38
{
39
  const char* v= convert(source, ansi_display);
40
  destination.alloc(strlen(v));
41
  strcpy(destination.c_ptr(), v);
42
  destination.length(strlen(v));
2136.3.3 by Brian Aker
Move over the convert to be in the type class.
43
}
44
45
bool convert(bool &destination, const char *source, const size_t source_length)
2136.3.2 by Brian Aker
Merge in boolean convert function so we use the same code for conversion.
46
{
47
  switch (source_length)
48
  {
49
  case 1:
50
    {
51
      switch (source[0])
52
      {
53
      case 'y': case 'Y':
54
      case 't': case 'T': // PG compatibility
55
        destination= true;
56
        return true;
57
58
      case 'n': case 'N':
59
      case 'f': case 'F': // PG compatibility
60
        destination= false;
61
        return true;
62
      }
63
    }
64
    break;
65
66
  case 5:
67
    if (not (my_strcasecmp(system_charset_info, source, "FALSE")))
68
    {
69
      destination= false;
70
      return true;
71
    }
72
    break;
73
74
  case 4:
75
    if (not (my_strcasecmp(system_charset_info, source, "TRUE")))
76
    {
77
      destination= true;
78
      return true;
79
    }
80
    break;
81
82
  case 3:
83
    if (not (my_strcasecmp(system_charset_info, source, "YES")))
84
    {
85
      destination= true;
86
      return true;
87
    }
88
    break;
89
90
  case 2:
91
    if (not (my_strcasecmp(system_charset_info, source, "NO")))
92
    {
93
      destination= false;
94
      return true;
95
    }
96
    break;
97
  }
98
99
  // Failure to match
100
  destination= false;
101
  return false;
102
}
103
104
bool convert(bool &destination, String &source)
105
{
106
  return convert(destination, source.c_ptr(), source.length());
107
}
108
109
} /* namespace type */
110
} /* namespace drizzled */