MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
gstream.cc
1 /* Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
2 
3  This program is free software; you can redistribute it and/or modify
4  it under the terms of the GNU General Public License as published by
5  the Free Software Foundation; version 2 of the License.
6 
7  This program is distributed in the hope that it will be useful,
8  but WITHOUT ANY WARRANTY; without even the implied warranty of
9  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  GNU General Public License for more details.
11 
12  You should have received a copy of the GNU General Public License
13  along with this program; if not, write to the Free Software Foundation,
14  51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA */
15 
16 /*
17  Functions to read and parse geometrical data.
18  NOTE: These functions assumes that the string is end \0 terminated!
19 */
20 
21 #include "sql_priv.h"
22 #include "gstream.h"
23 #include "m_string.h" // LEX_STRING
24 
25 enum Gis_read_stream::enum_tok_types Gis_read_stream::get_next_toc_type()
26 {
27  skip_space();
28  if (m_cur >= m_limit)
29  return eostream;
30  if (my_isvar_start(&my_charset_bin, *m_cur))
31  return word;
32  if ((*m_cur >= '0' && *m_cur <= '9') || *m_cur == '-' || *m_cur == '+')
33  return numeric;
34  if (*m_cur == '(')
35  return l_bra;
36  if (*m_cur == ')')
37  return r_bra;
38  if (*m_cur == ',')
39  return comma;
40  return unknown;
41 }
42 
43 
44 bool Gis_read_stream::get_next_word(LEX_STRING *res)
45 {
46  skip_space();
47  res->str= (char*) m_cur;
48  /* The following will also test for \0 */
49  if ((m_cur >= m_limit) || !my_isvar_start(&my_charset_bin, *m_cur))
50  return 1;
51 
52  /*
53  We can't combine the following increment with my_isvar() because
54  my_isvar() is a macro that would cause side effects
55  */
56  m_cur++;
57  while ((m_cur < m_limit) && my_isvar(&my_charset_bin, *m_cur))
58  m_cur++;
59 
60  res->length= (uint32) (m_cur - res->str);
61  return 0;
62 }
63 
64 
65 /*
66  Read a floating point number
67 
68  NOTE: Number must start with a digit or sign. It can't start with a decimal
69  point
70 */
71 
72 bool Gis_read_stream::get_next_number(double *d)
73 {
74  char *endptr;
75  int err;
76 
77  skip_space();
78 
79  if ((m_cur >= m_limit) ||
80  ((*m_cur < '0' || *m_cur > '9') && *m_cur != '-' && *m_cur != '+'))
81  {
82  set_error_msg("Numeric constant expected");
83  return 1;
84  }
85 
86  *d = my_strntod(m_charset, (char *)m_cur,
87  (uint) (m_limit-m_cur), &endptr, &err);
88  if (err)
89  return 1;
90  if (endptr)
91  m_cur = endptr;
92  return 0;
93 }
94 
95 
96 bool Gis_read_stream::check_next_symbol(char symbol)
97 {
98  skip_space();
99  if ((m_cur >= m_limit) || (*m_cur != symbol))
100  {
101  char buff[32];
102  strmov(buff, "'?' expected");
103  buff[2]= symbol;
104  set_error_msg(buff);
105  return 1;
106  }
107  m_cur++;
108  return 0;
109 }
110 
111 
112 /*
113  Remember error message.
114 */
115 
116 void Gis_read_stream::set_error_msg(const char *msg)
117 {
118  size_t len= strlen(msg); // ok in this context
119  m_err_msg= (char *) my_realloc(m_err_msg, (uint) len + 1, MYF(MY_ALLOW_ZERO_PTR));
120  memcpy(m_err_msg, msg, len + 1);
121 }