1548.4.104
by ziminq
formatinfo refactored |
1 |
/*
|
2 |
Copyright (C) 2010 Zimin
|
|
3 |
||
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.
|
|
8 |
||
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.
|
|
13 |
||
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.
|
|
17 |
*/
|
|
18 |
||
1548.4.105
by Zimin
make FormatInfo refactoring work ok. |
19 |
#include "config.h" |
20 |
#include <drizzled/field.h> |
|
1548.4.104
by ziminq
formatinfo refactored |
21 |
#include <string> |
1548.4.105
by Zimin
make FormatInfo refactoring work ok. |
22 |
#include <boost/algorithm/string.hpp> |
1548.4.104
by ziminq
formatinfo refactored |
23 |
|
1548.4.103
by Zimin
extract and make options as FormatInfo |
24 |
#include "formatinfo.h" |
25 |
||
1548.4.116
by Zimin
add escaped_by option to filesystem storage engine. |
26 |
#include <iostream> |
1548.4.104
by ziminq
formatinfo refactored |
27 |
using namespace std; |
28 |
||
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"; |
|
1548.4.106
by Zimin
add support for files such as /proc/meminfo, test cases included. |
32 |
static const char* FORMAT_INFO_FORMAT= "FORMAT"; |
1548.4.104
by ziminq
formatinfo refactored |
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"; |
|
1548.4.106
by Zimin
add support for files such as /proc/meminfo, test cases included. |
37 |
static const char* FORMAT_INFO_FORMAT_TAG= "KEY_VALUE"; |
38 |
static const char* FORMAT_INFO_FORMAT_NORMAL= "NORMAL"; |
|
1548.4.116
by Zimin
add escaped_by option to filesystem storage engine. |
39 |
static const char* FORMAT_INFO_ESCAPE= "ESCAPED_BY"; |
1548.4.104
by ziminq
formatinfo refactored |
40 |
enum filesystem_option_separator_mode_type |
41 |
{
|
|
42 |
FORMAT_INFO_SEPARATOR_MODE_STRICT_ENUM= 1, |
|
43 |
FORMAT_INFO_SEPARATOR_MODE_GENERAL_ENUM, |
|
44 |
FORMAT_INFO_SEPARATOR_MODE_WEAK_ENUM
|
|
45 |
};
|
|
46 |
||
47 |
static const char* DEFAULT_ROW_SEPARATOR= "\n"; |
|
48 |
static const char* DEFAULT_COL_SEPARATOR= " \t"; |
|
49 |
||
1548.4.103
by Zimin
extract and make options as FormatInfo |
50 |
FormatInfo::FormatInfo() |
51 |
: row_separator(DEFAULT_ROW_SEPARATOR), |
|
52 |
col_separator(DEFAULT_COL_SEPARATOR), |
|
1548.4.106
by Zimin
add support for files such as /proc/meminfo, test cases included. |
53 |
file_format(FORMAT_INFO_FORMAT_NORMAL), |
1548.4.104
by ziminq
formatinfo refactored |
54 |
separator_mode(FORMAT_INFO_SEPARATOR_MODE_GENERAL_ENUM) |
1548.4.103
by Zimin
extract and make options as FormatInfo |
55 |
{
|
56 |
}
|
|
57 |
||
58 |
void FormatInfo::parseFromTable(drizzled::message::Table *proto) |
|
59 |
{
|
|
60 |
if (!proto) |
|
61 |
return; |
|
62 |
||
63 |
for (int x= 0; x < proto->engine().options_size(); x++) |
|
64 |
{
|
|
1548.4.105
by Zimin
make FormatInfo refactoring work ok. |
65 |
const drizzled::message::Engine::Option& option= proto->engine().options(x); |
1548.4.103
by Zimin
extract and make options as FormatInfo |
66 |
|
1548.4.105
by Zimin
make FormatInfo refactoring work ok. |
67 |
if (boost::iequals(option.name(), FORMAT_INFO_FILE_PATH)) |
1548.4.103
by Zimin
extract and make options as FormatInfo |
68 |
real_file_name= option.state(); |
1548.4.105
by Zimin
make FormatInfo refactoring work ok. |
69 |
else if (boost::iequals(option.name(), FORMAT_INFO_ROW_SEPARATOR)) |
1548.4.103
by Zimin
extract and make options as FormatInfo |
70 |
row_separator= option.state(); |
1548.4.105
by Zimin
make FormatInfo refactoring work ok. |
71 |
else if (boost::iequals(option.name(), FORMAT_INFO_COL_SEPARATOR)) |
1548.4.103
by Zimin
extract and make options as FormatInfo |
72 |
col_separator= option.state(); |
1548.4.106
by Zimin
add support for files such as /proc/meminfo, test cases included. |
73 |
else if (boost::iequals(option.name(), FORMAT_INFO_FORMAT)) |
74 |
file_format= option.state(); |
|
1548.4.116
by Zimin
add escaped_by option to filesystem storage engine. |
75 |
else if (boost::iequals(option.name(), FORMAT_INFO_ESCAPE)) |
76 |
escape= option.state(); |
|
1548.4.105
by Zimin
make FormatInfo refactoring work ok. |
77 |
else if (boost::iequals(option.name(), FORMAT_INFO_SEPARATOR_MODE)) |
1548.4.103
by Zimin
extract and make options as FormatInfo |
78 |
{
|
1548.4.105
by Zimin
make FormatInfo refactoring work ok. |
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; |
|
1548.4.103
by Zimin
extract and make options as FormatInfo |
85 |
}
|
86 |
}
|
|
87 |
}
|
|
88 |
||
89 |
bool FormatInfo::isFileGiven() const |
|
90 |
{
|
|
91 |
return !real_file_name.empty(); |
|
92 |
}
|
|
93 |
||
1548.4.105
by Zimin
make FormatInfo refactoring work ok. |
94 |
string FormatInfo::getFileName() const |
95 |
{
|
|
96 |
return real_file_name; |
|
97 |
}
|
|
98 |
||
1548.4.103
by Zimin
extract and make options as FormatInfo |
99 |
bool FormatInfo::isRowSeparator(char ch) const |
100 |
{
|
|
1548.4.104
by ziminq
formatinfo refactored |
101 |
return (row_separator.find(ch) != string::npos); |
102 |
}
|
|
103 |
||
104 |
bool FormatInfo::isColSeparator(char ch) const |
|
105 |
{
|
|
106 |
return (col_separator.find(ch) != string::npos); |
|
107 |
}
|
|
108 |
||
109 |
string FormatInfo::getRowSeparatorHead() const |
|
110 |
{
|
|
111 |
return row_separator.substr(0, 1); |
|
112 |
}
|
|
113 |
||
114 |
string FormatInfo::getColSeparatorHead() const |
|
115 |
{
|
|
116 |
return col_separator.substr(0, 1); |
|
1548.4.103
by Zimin
extract and make options as FormatInfo |
117 |
}
|
1548.4.105
by Zimin
make FormatInfo refactoring work ok. |
118 |
|
1548.4.106
by Zimin
add support for files such as /proc/meminfo, test cases included. |
119 |
string FormatInfo::getColSeparator() const |
120 |
{
|
|
121 |
return col_separator; |
|
122 |
}
|
|
123 |
||
1548.4.105
by Zimin
make FormatInfo refactoring work ok. |
124 |
bool FormatInfo::validateOption(const std::string &key, const std::string &state) |
125 |
{
|
|
1548.4.116
by Zimin
add escaped_by option to filesystem storage engine. |
126 |
cerr << "validateOption: " << key << " , " << state << endl; |
1548.4.105
by Zimin
make FormatInfo refactoring work ok. |
127 |
if (boost::iequals(key, FORMAT_INFO_FILE_PATH) && |
128 |
! state.empty()) |
|
129 |
return true; |
|
1548.4.106
by Zimin
add support for files such as /proc/meminfo, test cases included. |
130 |
if (boost::iequals(key, FORMAT_INFO_FORMAT) && |
131 |
! state.empty()) |
|
132 |
return true; |
|
1548.4.116
by Zimin
add escaped_by option to filesystem storage engine. |
133 |
if (boost::iequals(key, FORMAT_INFO_ESCAPE) && |
134 |
! state.empty()) |
|
135 |
return true; |
|
1548.4.105
by Zimin
make FormatInfo refactoring work ok. |
136 |
if ((boost::iequals(key, FORMAT_INFO_ROW_SEPARATOR) || |
137 |
boost::iequals(key, FORMAT_INFO_COL_SEPARATOR)) && |
|
138 |
! state.empty()) |
|
139 |
return true; |
|
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))) |
|
144 |
return true; |
|
145 |
return false; |
|
146 |
}
|
|
147 |
||
148 |
bool FormatInfo::isSeparatorModeGeneral() const |
|
149 |
{
|
|
150 |
return (separator_mode >= FORMAT_INFO_SEPARATOR_MODE_GENERAL_ENUM); |
|
151 |
}
|
|
152 |
||
153 |
bool FormatInfo::isSeparatorModeWeak() const |
|
154 |
{
|
|
155 |
return (separator_mode >= FORMAT_INFO_SEPARATOR_MODE_WEAK_ENUM); |
|
156 |
}
|
|
1548.4.106
by Zimin
add support for files such as /proc/meminfo, test cases included. |
157 |
|
158 |
bool FormatInfo::isTagFormat() const |
|
159 |
{
|
|
160 |
return boost::iequals(file_format, FORMAT_INFO_FORMAT_TAG); |
|
161 |
}
|
|
1548.4.116
by Zimin
add escaped_by option to filesystem storage engine. |
162 |
|
163 |
bool FormatInfo::isEscapedChar(char ch) const |
|
164 |
{
|
|
165 |
return (!escape.empty() && escape.find(ch) != string::npos); |
|
166 |
}
|
|
167 |
||
168 |
char FormatInfo::getEscapedChar(const char ch) |
|
169 |
{
|
|
170 |
char escaped= ch; |
|
171 |
switch (ch) |
|
172 |
{
|
|
173 |
case 't': |
|
174 |
escaped= '\t'; |
|
175 |
break; |
|
176 |
case 'b': |
|
177 |
escaped= '\b'; |
|
178 |
break; |
|
179 |
case 'r': |
|
180 |
escaped= '\r'; |
|
181 |
break; |
|
182 |
case 'n': |
|
183 |
escaped= '\n'; |
|
184 |
break; |
|
185 |
}
|
|
186 |
return escaped; |
|
187 |
}
|