2
Copyright (C) 2010 Zimin
4
This program is free software; you can redistribute it and/or
5
modify it under the terms of the GNU General Public License
6
as published by the Free Software Foundation; either version 2
7
of the License, or (at your option) any later version.
9
This program is distributed in the hope that it will be useful,
10
but WITHOUT ANY WARRANTY; without even the implied warranty of
11
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
GNU General Public License for more details.
14
You should have received a copy of the GNU General Public License
15
along with this program; if not, write to the Free Software
16
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20
#include <drizzled/field.h>
22
#include <boost/algorithm/string.hpp>
24
#include "formatinfo.h"
29
static const char* FORMAT_INFO_FILE_PATH= "FILE";
30
static const char* FORMAT_INFO_ROW_SEPARATOR= "ROW_SEPARATOR";
31
static const char* FORMAT_INFO_COL_SEPARATOR= "COL_SEPARATOR";
32
static const char* FORMAT_INFO_FORMAT= "FORMAT";
33
static const char* FORMAT_INFO_SEPARATOR_MODE= "SEPARATOR_MODE";
34
static const char* FORMAT_INFO_SEPARATOR_MODE_STRICT= "STRICT";
35
static const char* FORMAT_INFO_SEPARATOR_MODE_GENERAL= "GENERAL";
36
static const char* FORMAT_INFO_SEPARATOR_MODE_WEAK= "WEAK";
37
static const char* FORMAT_INFO_FORMAT_TAG= "KEY_VALUE";
38
static const char* FORMAT_INFO_FORMAT_NORMAL= "NORMAL";
39
static const char* FORMAT_INFO_ESCAPE= "ESCAPED_BY";
40
enum filesystem_option_separator_mode_type
42
FORMAT_INFO_SEPARATOR_MODE_STRICT_ENUM= 1,
43
FORMAT_INFO_SEPARATOR_MODE_GENERAL_ENUM,
44
FORMAT_INFO_SEPARATOR_MODE_WEAK_ENUM
47
static const char* DEFAULT_ROW_SEPARATOR= "\n";
48
static const char* DEFAULT_COL_SEPARATOR= " \t";
50
FormatInfo::FormatInfo()
51
: row_separator(DEFAULT_ROW_SEPARATOR),
52
col_separator(DEFAULT_COL_SEPARATOR),
53
file_format(FORMAT_INFO_FORMAT_NORMAL),
54
separator_mode(FORMAT_INFO_SEPARATOR_MODE_GENERAL_ENUM)
58
void FormatInfo::parseFromTable(drizzled::message::Table *proto)
63
for (int x= 0; x < proto->engine().options_size(); x++)
65
const drizzled::message::Engine::Option& option= proto->engine().options(x);
67
if (boost::iequals(option.name(), FORMAT_INFO_FILE_PATH))
68
real_file_name= option.state();
69
else if (boost::iequals(option.name(), FORMAT_INFO_ROW_SEPARATOR))
70
row_separator= option.state();
71
else if (boost::iequals(option.name(), FORMAT_INFO_COL_SEPARATOR))
72
col_separator= option.state();
73
else if (boost::iequals(option.name(), FORMAT_INFO_FORMAT))
74
file_format= option.state();
75
else if (boost::iequals(option.name(), FORMAT_INFO_ESCAPE))
76
escape= option.state();
77
else if (boost::iequals(option.name(), FORMAT_INFO_SEPARATOR_MODE))
79
if (boost::iequals(option.state(), FORMAT_INFO_SEPARATOR_MODE_STRICT))
80
separator_mode= FORMAT_INFO_SEPARATOR_MODE_STRICT_ENUM;
81
else if (boost::iequals(option.state(), FORMAT_INFO_SEPARATOR_MODE_GENERAL))
82
separator_mode= FORMAT_INFO_SEPARATOR_MODE_GENERAL_ENUM;
83
else if (boost::iequals(option.state(), FORMAT_INFO_SEPARATOR_MODE_WEAK))
84
separator_mode= FORMAT_INFO_SEPARATOR_MODE_WEAK_ENUM;
89
bool FormatInfo::isFileGiven() const
91
return !real_file_name.empty();
94
string FormatInfo::getFileName() const
96
return real_file_name;
99
bool FormatInfo::isRowSeparator(char ch) const
101
return (row_separator.find(ch) != string::npos);
104
bool FormatInfo::isColSeparator(char ch) const
106
return (col_separator.find(ch) != string::npos);
109
string FormatInfo::getRowSeparatorHead() const
111
return row_separator.substr(0, 1);
114
string FormatInfo::getColSeparatorHead() const
116
return col_separator.substr(0, 1);
119
string FormatInfo::getColSeparator() const
121
return col_separator;
124
bool FormatInfo::validateOption(const std::string &key, const std::string &state)
126
cerr << "validateOption: " << key << " , " << state << endl;
127
if (boost::iequals(key, FORMAT_INFO_FILE_PATH) &&
130
if (boost::iequals(key, FORMAT_INFO_FORMAT) &&
133
if (boost::iequals(key, FORMAT_INFO_ESCAPE) &&
136
if ((boost::iequals(key, FORMAT_INFO_ROW_SEPARATOR) ||
137
boost::iequals(key, FORMAT_INFO_COL_SEPARATOR)) &&
140
if (boost::iequals(key, FORMAT_INFO_SEPARATOR_MODE) &&
141
(boost::iequals(state, FORMAT_INFO_SEPARATOR_MODE_STRICT) ||
142
boost::iequals(state, FORMAT_INFO_SEPARATOR_MODE_GENERAL) ||
143
boost::iequals(state, FORMAT_INFO_SEPARATOR_MODE_WEAK)))
148
bool FormatInfo::isSeparatorModeGeneral() const
150
return (separator_mode >= FORMAT_INFO_SEPARATOR_MODE_GENERAL_ENUM);
153
bool FormatInfo::isSeparatorModeWeak() const
155
return (separator_mode >= FORMAT_INFO_SEPARATOR_MODE_WEAK_ENUM);
158
bool FormatInfo::isTagFormat() const
160
return boost::iequals(file_format, FORMAT_INFO_FORMAT_TAG);
163
bool FormatInfo::isEscapedChar(char ch) const
165
return (!escape.empty() && escape.find(ch) != string::npos);
168
char FormatInfo::getEscapedChar(const char ch)