summaryrefslogtreecommitdiff
path: root/storage/mroonga/lib/mrn_parameters_parser.cpp
blob: 9a05097e548738db0f946b6e98592eeb20fc1c48 (plain)
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
/* -*- c-basic-offset: 2 -*- */
/*
  Copyright(C) 2011-2013 Kentoku SHIBA
  Copyright(C) 2011-2013 Kouhei Sutou <kou@clear-code.com>

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

  This library 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
  Lesser General Public License for more details.

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

#include "mrn_parameters_parser.hpp"

#include <mrn_mysql_compat.h>

namespace mrn {
  class Parameter {
  public:
    char *key_;
    char *value_;

    Parameter(const char *key, unsigned int key_length,
              const char *value, unsigned int value_length)
      : key_(my_strndup(key, key_length, MYF(0))),
        value_(my_strndup(value, value_length, MYF(0))) {
    };
    ~Parameter() {
      if (key_) {
        my_free(key_, MYF(0));
      }
      if (value_) {
        my_free(value_, MYF(0));
      }
    };
  };

  ParametersParser::ParametersParser(const char *input,
                                     unsigned int input_length)
    : input_(input),
      input_length_(input_length),
      parameters_(NULL) {
  }

  ParametersParser::~ParametersParser() {
    for (LIST *next = parameters_; next; next = next->next) {
      Parameter *parameter = static_cast<Parameter *>(next->data);
      delete parameter;
    }
    list_free(parameters_, false);
  }

  void ParametersParser::parse() {
    const char *current = input_;
    const char *end = input_ + input_length_;
    for (; current < end; ++current) {
      if (is_white_space(current[0])) {
        continue;
      }

      const char *key = current;
      unsigned int key_length = 0;
      while (current < end &&
             !is_white_space(current[0]) &&
             current[0] != '\'' && current[0] != '"' && current[0] != ',') {
        ++current;
        ++key_length;
      }
      if (current == end) {
        break;
      }

      while (current < end && is_white_space(current[0])) {
        ++current;
      }
      if (current == end) {
        break;
      }
      current = parse_value(current, end, key, key_length);
      if (!current) {
        break;
      }

      while (current < end && is_white_space(current[0])) {
        ++current;
      }
      if (current == end) {
        break;
      }
      if (current[0] != ',') {
        // TODO: report error
        break;
      }
    }
  }

  const char *ParametersParser::parse_value(const char *current,
                                            const char *end,
                                            const char *key,
                                            unsigned int key_length) {
    char quote = current[0];
    if (quote != '\'' && quote != '"') {
      // TODO: report error
      return NULL;
    }
    ++current;

    bool found = false;
    static const unsigned int max_value_length = 4096;
    char value[max_value_length];
    unsigned int value_length = 0;
    for (; current < end && value_length < max_value_length; ++current) {
      if (current[0] == quote) {
        Parameter *parameter = new Parameter(key, key_length,
                                             value, value_length);
        list_push(parameters_, parameter);
        found = true;
        ++current;
        break;
      }

      switch (current[0]) {
      case '\\':
        if (current + 1 == end) {
          break;
        }
        switch (current[1]) {
        case 'b':
          value[value_length] = '\b';
          break;
        case 'n':
          value[value_length] = '\n';
          break;
        case 'r':
          value[value_length] = '\r';
          break;
        case 't':
          value[value_length] = '\t';
          break;
        default:
          value[value_length] = current[1];
          break;
        }
        break;
      default:
        value[value_length] = current[0];
        break;
      }
      ++value_length;
    }

    if (!found) {
      // TODO: report error
    }

    return current;
  }

  const char *ParametersParser::operator[](const char *key) {
    for (LIST *next = parameters_; next; next = next->next) {
      Parameter *parameter = static_cast<Parameter *>(next->data);
      if (strcasecmp(parameter->key_, key) == 0) {
        return parameter->value_;
      }
    }
    return NULL;
  }
}