~drizzle-trunk/drizzle/development

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*
  Copyright (C) 2010 Zimin

  This program is free software; you can redistribute it and/or
  modify it under the terms of the GNU General Public License
  as published by the Free Software Foundation; either version 2
  of the License, or (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

#include <config.h>
#include <drizzled/field.h>
#include <string>
#include <boost/algorithm/string.hpp>

#include "formatinfo.h"

#include <iostream>
using namespace std;

static const char* FORMAT_INFO_FILE_PATH= "FILE";
static const char* FORMAT_INFO_ROW_SEPARATOR= "ROW_SEPARATOR";
static const char* FORMAT_INFO_COL_SEPARATOR= "COL_SEPARATOR";
static const char* FORMAT_INFO_FORMAT= "FORMAT";
static const char* FORMAT_INFO_SEPARATOR_MODE= "SEPARATOR_MODE";
static const char* FORMAT_INFO_SEPARATOR_MODE_STRICT= "STRICT";
static const char* FORMAT_INFO_SEPARATOR_MODE_GENERAL= "GENERAL";
static const char* FORMAT_INFO_SEPARATOR_MODE_WEAK= "WEAK";
static const char* FORMAT_INFO_FORMAT_TAG= "KEY_VALUE";
static const char* FORMAT_INFO_FORMAT_NORMAL= "NORMAL";
static const char* FORMAT_INFO_ESCAPE= "ESCAPED_BY";
enum filesystem_option_separator_mode_type
{
  FORMAT_INFO_SEPARATOR_MODE_STRICT_ENUM= 1,
  FORMAT_INFO_SEPARATOR_MODE_GENERAL_ENUM,
  FORMAT_INFO_SEPARATOR_MODE_WEAK_ENUM
};

static const char* DEFAULT_ROW_SEPARATOR= "\n";
static const char* DEFAULT_COL_SEPARATOR= " \t";

FormatInfo::FormatInfo()
  : row_separator(DEFAULT_ROW_SEPARATOR),
  col_separator(DEFAULT_COL_SEPARATOR),
  file_format(FORMAT_INFO_FORMAT_NORMAL),
  separator_mode(FORMAT_INFO_SEPARATOR_MODE_GENERAL_ENUM)
{
}

void FormatInfo::parseFromTable(drizzled::message::Table *proto)
{
  if (!proto)
    return;

  for (int x= 0; x < proto->engine().options_size(); x++)
  {
    const drizzled::message::Engine::Option& option= proto->engine().options(x);

    if (boost::iequals(option.name(), FORMAT_INFO_FILE_PATH))
      real_file_name= option.state();
    else if (boost::iequals(option.name(), FORMAT_INFO_ROW_SEPARATOR))
      row_separator= option.state();
    else if (boost::iequals(option.name(), FORMAT_INFO_COL_SEPARATOR))
      col_separator= option.state();
    else if (boost::iequals(option.name(), FORMAT_INFO_FORMAT))
      file_format= option.state();
    else if (boost::iequals(option.name(), FORMAT_INFO_ESCAPE))
      escape= option.state();
    else if (boost::iequals(option.name(), FORMAT_INFO_SEPARATOR_MODE))
    {
      if (boost::iequals(option.state(), FORMAT_INFO_SEPARATOR_MODE_STRICT))
        separator_mode= FORMAT_INFO_SEPARATOR_MODE_STRICT_ENUM;
      else if (boost::iequals(option.state(), FORMAT_INFO_SEPARATOR_MODE_GENERAL))
        separator_mode= FORMAT_INFO_SEPARATOR_MODE_GENERAL_ENUM;
      else if (boost::iequals(option.state(), FORMAT_INFO_SEPARATOR_MODE_WEAK))
        separator_mode= FORMAT_INFO_SEPARATOR_MODE_WEAK_ENUM;
    }
  }
}

bool FormatInfo::isFileGiven() const
{
  return !real_file_name.empty();
}

string FormatInfo::getFileName() const
{
  return real_file_name;
}

bool FormatInfo::isRowSeparator(char ch) const
{
  return (row_separator.find(ch) != string::npos);
}

bool FormatInfo::isColSeparator(char ch) const
{
  return (col_separator.find(ch) != string::npos);
}

string FormatInfo::getRowSeparatorHead() const
{
  return row_separator.substr(0, 1);
}

string FormatInfo::getColSeparatorHead() const
{
  return col_separator.substr(0, 1);
}

string FormatInfo::getColSeparator() const
{
  return col_separator;
}

bool FormatInfo::validateOption(const std::string &key, const std::string &state)
{
  cerr << "validateOption: " << key << " , " << state << endl;
  if (boost::iequals(key, FORMAT_INFO_FILE_PATH) &&
      ! state.empty())
    return true;
  if (boost::iequals(key, FORMAT_INFO_FORMAT) &&
      ! state.empty())
    return true;
  if (boost::iequals(key, FORMAT_INFO_ESCAPE) &&
      ! state.empty())
    return true;
  if ((boost::iequals(key, FORMAT_INFO_ROW_SEPARATOR) ||
       boost::iequals(key, FORMAT_INFO_COL_SEPARATOR)) &&
      ! state.empty())
    return true;
  if (boost::iequals(key, FORMAT_INFO_SEPARATOR_MODE) &&
      (boost::iequals(state, FORMAT_INFO_SEPARATOR_MODE_STRICT) ||
       boost::iequals(state, FORMAT_INFO_SEPARATOR_MODE_GENERAL) ||
       boost::iequals(state, FORMAT_INFO_SEPARATOR_MODE_WEAK)))
    return true;
  return false;
}

bool FormatInfo::isSeparatorModeGeneral() const
{
  return (separator_mode >= FORMAT_INFO_SEPARATOR_MODE_GENERAL_ENUM);
}

bool FormatInfo::isSeparatorModeWeak() const
{
  return (separator_mode >= FORMAT_INFO_SEPARATOR_MODE_WEAK_ENUM);
}

bool FormatInfo::isTagFormat() const
{
  return boost::iequals(file_format, FORMAT_INFO_FORMAT_TAG);
}

bool FormatInfo::isEscapedChar(char ch) const
{
  return (!escape.empty() && escape.find(ch) != string::npos);
}

char FormatInfo::getEscapedChar(const char ch)
{
  char escaped= ch;
  switch (ch)
  {
    case 't':
      escaped= '\t';
      break;
    case 'b':
      escaped= '\b';
      break;
    case 'r':
      escaped= '\r';
      break;
    case 'n':
      escaped= '\n';
      break;
  }
  return escaped;
}