problem with splitting a string

From: Werner Echezuria <wercool(at)gmail(dot)com>
To: pgsql-hackers(at)postgresql(dot)org
Subject: problem with splitting a string
Date: 2009-08-05 13:54:14
Message-ID: 2485a25e0908050654s7012ea27sf2106d5de29b1297@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi,

I'm trying to develop a contrib module in order to parse sqlf queries, I'm
using lemon as a LALR parser generator (because I think it's easier than
bison) and re2c (because I think it's easier than flex) but when I try to
split the string into words postgres add some weird characters (this works
in pure gcc), I write something like "CREATE FUZZY PREDICATE joven ON 0..120
AS (0,0,35,120);", but postgresql adds a character like  at the end of
"joven" and the others words.

The code I use to split the string is:

void parse_query(char *str,const char **sqlf){

parse_words(str);
*sqlf=fuzzy_query;
}
void parse_words(char *str){
char *word;
int token;
const char semicolon =';';
const char dot='.';
const char comma=',';
const char open_bracket='(';
const char close_bracket=')';
struct Token sToken;

int i = 0;

void* pParser = ParseAlloc (malloc);

while(str[i] !='\0'){
int c=0;

word=(char *)malloc(sizeof(char));

if(isspace(str[i]) || str[i]==semicolon){
i++;
continue;
}

if (str[i]==open_bracket || str[i]==close_bracket ||
str[i]==dot || str[i]==comma){
word[c] = str[i];
i++;
token=scan(word, strlen(word));
Parse(pParser, token, sToken);
continue;
}else{
while(!isspace(str[i]) && str[i]!=semicolon && str[i]!='\0' &&
str[i]!=open_bracket && str[i]!=close_bracket &&
str[i]!=dot && str[i]!=comma){
word[c++] = str[i++];
}
}

token=scan(word, strlen(word));

if (token==PARAMETRO){
//TODO: I don't know why it needs the malloc function again, all
I know is it's working
const char *param=word;
word= (char *)malloc(sizeof(char));
sToken.z=param;
}

Parse(pParser, token, sToken);
free(word);
}
Parse(pParser, 0, sToken);
ParseFree(pParser, free );

}

Header:

#ifndef SQLF_H_
#define SQLF_H_

typedef struct Token {
const char *z;
int value;
unsigned n;
} Token;
void parse_query(char *str,const char **sqlf);
void parse_words(char *str);
int scan(char *s, int l);

#endif /* SQLF_H_ */

Screen:

postgres=# select * from fuzzy.sqlf('CREATE FUZZY PREDICATE joven ON 0..120
AS (0,0,35,120);'::text);
ERROR: syntax error at or near ""
LINE 1: INSERT INTO fuzzydb.pg_fuzzypredicate VALUES(joven,0�
�,120
...
^
QUERY: INSERT INTO fuzzydb.pg_fuzzypredicate VALUES(joven,0�
�,120
�,0�

�,0�

�,35

�,120

�);

Thanks for any help

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Tom Lane 2009-08-05 14:04:00 Re: Adding error message "source"
Previous Message Heikki Linnakangas 2009-08-05 13:42:32 Re: md.c should not call files "relations"