gstream.cc 2.81 KB
Newer Older
1 2 3 4
/* Copyright (C) 2004 MySQL AB

   This program is free software; you can redistribute it and/or modify
   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 8 9 10 11 12 13 14 15 16 17 18 19 20

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

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */

/*
  Functions to read and parse geometrical data.
  NOTE: These functions assumes that the string is end \0 terminated!
*/

21 22
#include "mysql_priv.h"

23
enum Gis_read_stream::enum_tok_types Gis_read_stream::get_next_toc_type()
24
{
25
  skip_space();
hf@deer.(none)'s avatar
hf@deer.(none) committed
26
  if (m_cur >= m_limit)
27
    return eostream;
28
  if (my_isvar_start(&my_charset_bin, *m_cur))
29
    return word;
30
  if ((*m_cur >= '0' && *m_cur <= '9') || *m_cur == '-' || *m_cur == '+')
31
    return numeric;
32
  if (*m_cur == '(')
33
    return l_bra;
34
  if (*m_cur == ')')
35
    return r_bra;
36
  if (*m_cur == ',')
37 38 39 40 41
    return comma;
  return unknown;
}


42 43 44 45 46
bool Gis_read_stream::get_next_word(LEX_STRING *res)
{
  skip_space();
  res->str= (char*) m_cur;
  /* The following will also test for \0 */
47
  if ((m_cur >= m_limit) || !my_isvar_start(&my_charset_bin, *m_cur))
48
    return 1;
49

50 51 52 53 54
  /*
    We can't combine the following increment with my_isvar() because
    my_isvar() is a macro that would cause side effects
  */
  m_cur++;
hf@deer.(none)'s avatar
hf@deer.(none) committed
55
  while ((m_cur < m_limit) && my_isvar(&my_charset_bin, *m_cur))
56
    m_cur++;
57

58 59 60
  res->length= (uint32) (m_cur - res->str);
  return 0;
}
61 62


63 64
/*
  Read a floating point number
65

66 67 68
  NOTE: Number must start with a digit or sign. It can't start with a decimal
  point
*/
69

70
bool Gis_read_stream::get_next_number(double *d)
71
{
72
  char *endptr;
hf@deer.(none)'s avatar
hf@deer.(none) committed
73
  int err;
74

75
  skip_space();
hf@deer.(none)'s avatar
hf@deer.(none) committed
76 77 78

  if ((m_cur >= m_limit) ||
      (*m_cur < '0' || *m_cur > '9') && *m_cur != '-' && *m_cur != '+')
79 80 81 82 83
  {
    set_error_msg("Numeric constant expected");
    return 1;
  }

hf@deer.(none)'s avatar
hf@deer.(none) committed
84
  *d = my_strntod(m_charset, (char *)m_cur,
85
		  (uint) (m_limit-m_cur), &endptr, &err);
hf@deer.(none)'s avatar
hf@deer.(none) committed
86 87
  if (err)
    return 1;
88
  if (endptr)
89 90 91 92
    m_cur = endptr;
  return 0;
}

93 94

bool Gis_read_stream::check_next_symbol(char symbol)
95
{
96
  skip_space();
hf@deer.(none)'s avatar
hf@deer.(none) committed
97
  if ((m_cur >= m_limit) || (*m_cur != symbol))
98
  {
99 100 101 102 103
    char buff[32];
    strmov(buff, "'?' expected");
    buff[2]= symbol;
    set_error_msg(buff);
    return 1;
104
  }
105 106 107
  m_cur++;
  return 0;
}
108 109


110 111 112
/*
  Remember error message.
*/
113

114
void Gis_read_stream::set_error_msg(const char *msg)
115
{
116
  size_t len= strlen(msg);			// ok in this context
117
  m_err_msg= (char *) my_realloc(m_err_msg, (uint) len + 1, MYF(MY_ALLOW_ZERO_PTR));
118 119
  memcpy(m_err_msg, msg, len + 1);
}