2018-03-12 11:22:06 +01:00
|
|
|
#include <ctype.h>
|
|
|
|
|
|
|
|
#include "ktv.h"
|
|
|
|
|
|
|
|
struct parser {
|
|
|
|
int line;
|
|
|
|
int pos;
|
|
|
|
int prevpos;
|
|
|
|
char error[256];
|
2018-03-30 11:12:50 +02:00
|
|
|
int quote;
|
|
|
|
FILE *f;
|
2018-03-12 11:22:06 +01:00
|
|
|
};
|
|
|
|
|
2018-03-30 11:12:50 +02:00
|
|
|
static int get_char(struct parser *p)
|
2018-03-12 11:22:06 +01:00
|
|
|
{
|
|
|
|
int c;
|
2018-03-30 11:12:50 +02:00
|
|
|
c = fgetc (p->f);
|
2018-03-12 11:22:06 +01:00
|
|
|
p->pos++;
|
|
|
|
if (c=='\n'){
|
|
|
|
p->prevpos=p->pos;
|
|
|
|
p->line ++;
|
|
|
|
p->pos=0;
|
|
|
|
}
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
2018-03-30 11:12:50 +02:00
|
|
|
|
|
|
|
static void unget_char(struct parser *p,int c){
|
|
|
|
ungetc(c,p->f);
|
2018-03-12 11:22:06 +01:00
|
|
|
if (c=='\n'){
|
|
|
|
p->line--;
|
|
|
|
p->pos=p->prevpos;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
p->pos--;
|
|
|
|
}
|
|
|
|
|
2018-03-30 11:12:50 +02:00
|
|
|
|
|
|
|
|
|
|
|
static int get_char_q(struct parser *p)
|
2018-03-12 11:22:06 +01:00
|
|
|
{
|
|
|
|
int c;
|
2018-03-30 11:12:50 +02:00
|
|
|
|
|
|
|
while(1) {
|
|
|
|
c = get_char(p);
|
|
|
|
if (c==EOF || c=='\n')
|
|
|
|
return c;
|
|
|
|
|
|
|
|
if(c=='"' && !p->quote){
|
|
|
|
p->quote=1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if(c=='"' && p->quote){
|
|
|
|
p->quote=0;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2018-03-12 11:22:06 +01:00
|
|
|
|
2018-03-30 11:12:50 +02:00
|
|
|
if (!p->quote)
|
|
|
|
return c;
|
|
|
|
|
|
|
|
if (c!='\\')
|
|
|
|
return c;
|
|
|
|
|
|
|
|
c = get_char(p);
|
|
|
|
switch(c){
|
|
|
|
case EOF:
|
|
|
|
return c;
|
|
|
|
case 'n':
|
|
|
|
return '\n';
|
|
|
|
|
|
|
|
case '\\':
|
|
|
|
return '\\';
|
|
|
|
case '"':
|
|
|
|
return '"';
|
|
|
|
default:
|
|
|
|
unget_char(p,c);
|
|
|
|
return '\\';
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We will never reach here */
|
|
|
|
/* return c;*/
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static int skip_chars (struct parser *p, const char * chars)
|
|
|
|
{
|
|
|
|
int c;
|
|
|
|
|
|
|
|
while ( (c = get_char (p)) != EOF) {
|
2018-03-12 11:22:06 +01:00
|
|
|
if (strchr (chars, c))
|
|
|
|
continue;
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
2018-03-30 11:12:50 +02:00
|
|
|
static int skip_to_chars (struct parser *p, const char *chars)
|
2018-03-12 11:22:06 +01:00
|
|
|
{
|
|
|
|
int c;
|
|
|
|
|
2018-03-30 11:12:50 +02:00
|
|
|
while ( (c = get_char (p)) != EOF) {
|
2018-03-12 11:22:06 +01:00
|
|
|
if (strchr (chars, c))
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2018-03-30 11:12:50 +02:00
|
|
|
static int read_key (struct parser *p, char *key, int max_len)
|
2018-03-12 11:22:06 +01:00
|
|
|
{
|
|
|
|
int c,n;
|
|
|
|
|
|
|
|
do {
|
2018-03-30 11:12:50 +02:00
|
|
|
c = skip_chars (p, " \t\n\a\v");
|
2018-03-12 11:22:06 +01:00
|
|
|
if (c == '#') {
|
2018-03-30 11:12:50 +02:00
|
|
|
c = skip_to_chars (p, "\n\a");
|
2018-03-12 11:22:06 +01:00
|
|
|
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} while (c != EOF);
|
2018-03-30 11:12:50 +02:00
|
|
|
|
|
|
|
unget_char(p,c);
|
|
|
|
c=get_char_q(p);
|
2018-03-12 11:22:06 +01:00
|
|
|
|
|
|
|
n=0;
|
|
|
|
while(c!=EOF && n<max_len){
|
2018-03-30 11:12:50 +02:00
|
|
|
if (!p->quote && !isalnum(c) && !strchr("._/-()@#|{}[]\\",c)/*strchr(": \t\n\a",c)*/){
|
|
|
|
unget_char(p,c);
|
2018-03-12 11:22:06 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
key[n]=c;
|
2018-03-30 11:12:50 +02:00
|
|
|
c=get_char_q(p);
|
2018-03-12 11:22:06 +01:00
|
|
|
n++;
|
|
|
|
|
|
|
|
}
|
|
|
|
key[n]=0;
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int skip_to_colon(FILE *f,struct parser * p)
|
|
|
|
{
|
|
|
|
int c;
|
2018-03-30 11:12:50 +02:00
|
|
|
c = skip_chars (p, " \t");
|
2018-03-12 11:22:06 +01:00
|
|
|
if (c!=':'){
|
|
|
|
if (c=='\n'){
|
2018-03-30 11:12:50 +02:00
|
|
|
unget_char(p,c);
|
2018-03-12 11:22:06 +01:00
|
|
|
sprintf(p->error,"Error at line %d, pos %d: Unexpected EOL, collon expected.", p->line, p->pos);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
sprintf(p->error,"Error at line %d, pos %d: Collon expected.", p->line, p->pos);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-03-30 11:12:50 +02:00
|
|
|
static int read_type(struct parser *p, char *type, int max_len)
|
2018-03-12 11:22:06 +01:00
|
|
|
{
|
|
|
|
int c,n;
|
|
|
|
|
2018-03-30 11:12:50 +02:00
|
|
|
if (!skip_to_colon(p->f,p))
|
2018-03-12 11:22:06 +01:00
|
|
|
return -1;
|
|
|
|
|
2018-03-30 11:12:50 +02:00
|
|
|
c = skip_chars (p, " \t");
|
2018-03-12 11:22:06 +01:00
|
|
|
|
|
|
|
if (c==':'){
|
2018-03-30 11:12:50 +02:00
|
|
|
unget_char(p,c);
|
2018-03-25 10:35:53 +02:00
|
|
|
sprintf(type,"%s","");
|
2018-03-12 11:22:06 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isalpha(c)){
|
|
|
|
if (c=='\n'){
|
2018-03-30 11:12:50 +02:00
|
|
|
unget_char(p,c);
|
2018-03-12 11:22:06 +01:00
|
|
|
sprintf(p->error,"Error at line %d, pos %d: Unexpected EOL.", p->line, p->pos);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
sprintf(p->error,"Error at line %d, pos %d: Letter expected.", p->line, p->pos);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
n=0;
|
|
|
|
while(c!=EOF && n<max_len){
|
|
|
|
if (!isalnum(c) && !strchr("_/-.()@#|{}[]",c)/*strchr(": \t\n\a",c)*/){
|
2018-03-30 11:12:50 +02:00
|
|
|
unget_char(p,c);
|
2018-03-12 11:22:06 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
type[n]=c;
|
2018-03-30 11:12:50 +02:00
|
|
|
c=get_char(p);
|
2018-03-12 11:22:06 +01:00
|
|
|
n++;
|
|
|
|
}
|
|
|
|
type[n]=0;
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2018-03-30 11:12:50 +02:00
|
|
|
|
|
|
|
static int read_val(struct parser *p, char *val, int max_len){
|
2018-03-12 11:22:06 +01:00
|
|
|
int c,n,quote;
|
2018-03-30 11:12:50 +02:00
|
|
|
if (!skip_to_colon(p->f,p))
|
2018-03-12 11:22:06 +01:00
|
|
|
return -1;
|
2018-03-30 11:12:50 +02:00
|
|
|
c = skip_chars (p, " \t");
|
2018-03-12 11:22:06 +01:00
|
|
|
if (c=='"'){
|
|
|
|
quote=1;
|
2018-03-30 11:12:50 +02:00
|
|
|
c=get_char(p);
|
2018-03-12 11:22:06 +01:00
|
|
|
}
|
|
|
|
else{
|
|
|
|
quote=0;
|
|
|
|
}
|
|
|
|
n=0;
|
|
|
|
while(c!=EOF && n<max_len){
|
|
|
|
if (quote && c=='"'){
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (c=='\n'){
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (quote){
|
|
|
|
if (c=='\\'){
|
2018-03-30 11:12:50 +02:00
|
|
|
c = get_char(p);
|
2018-03-12 11:22:06 +01:00
|
|
|
switch(c){
|
|
|
|
case 'n':
|
|
|
|
c='\n';
|
|
|
|
break;
|
|
|
|
case '\\':
|
|
|
|
break;
|
|
|
|
case '"':
|
|
|
|
break;
|
|
|
|
default:
|
2018-03-30 11:12:50 +02:00
|
|
|
unget_char(p,c);
|
2018-03-12 11:22:06 +01:00
|
|
|
c='\\';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
val[n++]=c;
|
2018-03-30 11:12:50 +02:00
|
|
|
c=get_char(p);
|
2018-03-12 11:22:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if(!quote && n>0){
|
|
|
|
while(n>0){
|
|
|
|
if (isspace(val[n-1]))
|
|
|
|
n--;
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
val[n]=0;
|
|
|
|
|
|
|
|
return n;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int cw_ktv_read_line (FILE *f, char * key, char * type, char *val)
|
|
|
|
{
|
|
|
|
int n;
|
|
|
|
|
|
|
|
struct parser p;
|
|
|
|
p.line=1;
|
|
|
|
p.pos=0;
|
|
|
|
p.prevpos=0;
|
2018-03-30 11:12:50 +02:00
|
|
|
p.quote=0;
|
|
|
|
p.f=f;
|
2018-03-12 11:22:06 +01:00
|
|
|
|
2018-03-30 11:12:50 +02:00
|
|
|
n = read_key (&p,key,CW_KTV_MAX_KEY_LEN);
|
|
|
|
n = read_type (&p,type,CW_KTV_MAX_KEY_LEN);
|
2018-03-12 11:22:06 +01:00
|
|
|
if (n==-1){
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2018-03-30 11:12:50 +02:00
|
|
|
n = read_val (&p,val,CW_KTV_MAX_KEY_LEN);
|
2018-03-12 11:22:06 +01:00
|
|
|
if (n==-1){
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|