~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/filesystem_engine/formatinfo.cc

  • Committer: Monty Taylor
  • Date: 2011-03-09 20:59:40 UTC
  • mfrom: (2226.1.14 build)
  • Revision ID: mordred@inaugust.com-20110309205940-7f5mk6zba2u7bawa
Merged Dave - Filtered Replication docs
Merged Olaf - Refactoring work
Removed archive, blackhole, filesystem_engine, blitzdb, csv and pbxt from
the tree pre-GA as we have no interest in supporting them moving forward.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
 
 
19
 
#include <config.h>
20
 
#include <drizzled/field.h>
21
 
#include <string>
22
 
#include <boost/algorithm/string.hpp>
23
 
 
24
 
#include "formatinfo.h"
25
 
 
26
 
#include <iostream>
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";
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
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
 
 
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)
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
 
  {
65
 
    const drizzled::message::Engine::Option& option= proto->engine().options(x);
66
 
 
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))
78
 
    {
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;
85
 
    }
86
 
  }
87
 
}
88
 
 
89
 
bool FormatInfo::isFileGiven() const
90
 
{
91
 
  return !real_file_name.empty();
92
 
}
93
 
 
94
 
string FormatInfo::getFileName() const
95
 
{
96
 
  return real_file_name;
97
 
}
98
 
 
99
 
bool FormatInfo::isRowSeparator(char ch) const
100
 
{
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);
117
 
}
118
 
 
119
 
string FormatInfo::getColSeparator() const
120
 
{
121
 
  return col_separator;
122
 
}
123
 
 
124
 
bool FormatInfo::validateOption(const std::string &key, const std::string &state)
125
 
{
126
 
  cerr << "validateOption: " << key << " , " << state << endl;
127
 
  if (boost::iequals(key, FORMAT_INFO_FILE_PATH) &&
128
 
      ! state.empty())
129
 
    return true;
130
 
  if (boost::iequals(key, FORMAT_INFO_FORMAT) &&
131
 
      ! state.empty())
132
 
    return true;
133
 
  if (boost::iequals(key, FORMAT_INFO_ESCAPE) &&
134
 
      ! state.empty())
135
 
    return true;
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
 
}
157
 
 
158
 
bool FormatInfo::isTagFormat() const
159
 
{
160
 
  return boost::iequals(file_format, FORMAT_INFO_FORMAT_TAG);
161
 
}
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
 
}