problem with splitting a string - Mailing list pgsql-hackers

From Werner Echezuria
Subject problem with splitting a string
Date
Msg-id 2485a25e0908050654s7012ea27sf2106d5de29b1297@mail.gmail.com
Whole thread Raw
Responses Re: problem with splitting a string  (Tom Lane <tgl@sss.pgh.pa.us>)
List pgsql-hackers
Hi,<br /><br />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
splitthe string into words postgres add some weird characters (this works in pure gcc), I write something like "CREATE
FUZZYPREDICATE joven ON 0..120 AS (0,0,35,120);", but postgresql adds a character like  at the end of "joven" and the
otherswords.<br /><br />The code I use to split the string is:<br /><br />void parse_query(char *str,const char
**sqlf){<br/><br />    parse_words(str);<br />    *sqlf=fuzzy_query;<br />}<br />void parse_words(char *str){<br />   
char*word;<br />    int token;<br />     const char semicolon =';';<br />    const char dot='.';<br />    const char
comma=',';<br/>    const char open_bracket='(';<br />    const char close_bracket=')';<br />    struct Token sToken;<br
/><br/>    int i = 0;<br /><br />    void* pParser = ParseAlloc (malloc);<br /><br />    while(str[i] !='\0'){<br />   
   int c=0;<br /><br />        word=(char *)malloc(sizeof(char));<br /><br />        if(isspace(str[i]) ||
str[i]==semicolon){<br/>             i++;<br />            continue;<br />        }<br /><br />        if
(str[i]==open_bracket|| str[i]==close_bracket ||<br />            str[i]==dot || str[i]==comma){<br />               
word[c]= str[i];<br />                i++;<br />                 token=scan(word, strlen(word));<br />               
Parse(pParser,token, sToken);<br />                continue;<br />        }else{<br />           
while(!isspace(str[i])&& str[i]!=semicolon && str[i]!='\0' &&<br />                    
str[i]!=open_bracket&& str[i]!=close_bracket &&<br />                    str[i]!=dot &&
str[i]!=comma){<br/>                        word[c++] = str[i++];<br />            }<br />        }<br /><br />       
token=scan(word,strlen(word));<br /><br />        if (token==PARAMETRO){<br />            //TODO: I don't know why it
needsthe malloc function again, all I know is it's working<br />            const char *param=word;<br />            
word=(char *)malloc(sizeof(char));<br />            sToken.z=param;<br />        }<br /><br />        Parse(pParser,
token,sToken);<br />        free(word);<br />    }<br />  Parse(pParser, 0, sToken);<br />  ParseFree(pParser, free
);<br/><br />}<br /><br />Header:<br /><br />#ifndef SQLF_H_<br />#define SQLF_H_<br /><br />typedef struct Token {<br
/> const char *z;<br />  int value;<br />  unsigned n;<br />} Token;<br />void parse_query(char *str,const char
**sqlf);<br/>void parse_words(char *str);<br /> int scan(char *s, int l);<br /><br />#endif /* SQLF_H_ */<br /><br
/><br/>Screen:<br /><br />postgres=# select * from fuzzy.sqlf('CREATE FUZZY PREDICATE joven ON 0..120 AS
(0,0,35,120);'::text);<br/>ERROR:  syntax error at or near ""<br /> LINE 1: INSERT INTO fuzzydb.pg_fuzzypredicate
VALUES(joven,0�<br/>                                                               �,120<br
/>                                                                    ...<br
/>                                                         ^<br /> QUERY:  INSERT INTO fuzzydb.pg_fuzzypredicate
VALUES(joven,0�<br/>                                                               �,120<br
/>                                                                    �,0�<br
/>                                                                          �,0�<br />
                                                                                �,35<br
/>                                                                                      �,120<br
/>                                                                                            �);<br /><br />Thanks
forany help<br /> 

pgsql-hackers by date:

Previous
From: Heikki Linnakangas
Date:
Subject: Re: md.c should not call files "relations"
Next
From: Michael Meskes
Date:
Subject: Re: ECPG support for struct in INTO list