Blame |
Last modification |
View Log
| RSS feed
/*
*
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define MAXTOKENSIZE 128
static char string
[MAXTOKENSIZE
];
static char buffer
[MAXTOKENSIZE
*8];
static FILE
*fin
;
int line
;
static int flags
;
static char *lasttoken
,*token
;
int init_tokenizer
(char *filename
,int fl
)
{
fin
=fopen(filename
,"rt");
if (fin
==NULL
) return -1;
line
=0;
buffer
[0]='\0';
strtok(buffer
," \n");
flags
=fl
;
lasttoken
=NULL
;
token
=NULL
;
return 0;
}
void ungettoken
(void)
{
if (flags
&0x02) {fprintf(stderr
,"<<UNGET>>"); fflush(stderr
); }
if (lasttoken
!=NULL
) abort();
lasttoken
=token
;
}
char *gettoken
(void)
{
char *s
;
if (lasttoken
!=NULL
) {
token
=lasttoken
;
lasttoken
=NULL
;
if (flags
&0x02) {fprintf(stderr
,"<%s>",token
); fflush(stderr
); }
return token
;
}
s
=strtok(NULL
," \n");
if (s
==NULL
) {
for (;;) {
s
=fgets(buffer
,sizeof(buffer
),fin
);
line
++;
if (flags
&0x1) {fprintf(stderr
,"[%i]",line
); fflush(stderr
); }
if (s
==NULL
) {
if (flags
&0x03) {fprintf(stderr
,"\n"); fflush(stderr
); }
return NULL
;
}
s
=strtok(buffer
," \n");
if (s
!=NULL
) {
if (*s
=='#') continue;
break;
}
}
}
if (*s
=='\"') {
/* Well, the strings are returned with a \" on first character */
char *p
;
strcpy(string
,s
);
p
=strchr(string
+1,'\"');
if (p
!=NULL
) {
*p
='\0';
if (flags
&0x02) {fprintf(stderr
,"<%s>",string
); fflush(stderr
); }
return token
=string
;
}
s
=strtok(NULL
,"\"");
if (s
==NULL
) {
if (flags
&0x03) {fprintf(stderr
,"\n"); fflush(stderr
); }
return NULL
;
}
strcat(string
," ");
strcat(string
,s
);
p
=strchr(string
+1,'\"');
if (p
!=NULL
) *p
='\0';
if (flags
&0x02) {fprintf(stderr
,"<%s>",string
); fflush(stderr
); }
return token
=string
;
}
if (flags
&0x02) {fprintf(stderr
,"<%s>",s
); fflush(stderr
); }
return token
=s
;
}
int done_tokenizer
(void)
{
return fclose(fin
);
}
#if 0
int main
(int argc
, char *argv
[])
{
char *s
;
init_tokenizer
(argv
[1]);
while ((s
=gettoken
())!=NULL
) {
printf("<%s>\n",s
);
}
done_tokenizer
();
}
#endif