tokenizer.c

changeset 35
5a71d53d0228
parent 21
6d4282804e73
child 36
63cd573ffbcf
--- a/tokenizer.c	Sun Apr 01 01:43:46 2001 +0200
+++ b/tokenizer.c	Sat Jan 19 19:14:36 2002 +0100
@@ -112,6 +112,12 @@
 	if(tokz->ungetc!=-1){
 		c=tokz->ungetc;
 		tokz->ungetc=-1;
+	}else if (tokz->flags&TOKZ_READ_FROM_BUFFER) {
+		assert(tokz->buffer.data!=NULL);
+		if (tokz->buffer.pos==tokz->buffer.len)
+			c=EOF;
+		else
+			c=tokz->buffer.data[tokz->buffer.pos++];
 	}else{
 		c=getc(tokz->file);
 	}
@@ -578,6 +584,7 @@
 {
 	int c, c2, e;
 	
+	if (!(tokz->flags&TOKZ_READ_FROM_BUFFER))
 	assert(tokz->file!=NULL);
 	
 	tok_free(tok);
@@ -804,6 +811,9 @@
 	tokz->nest_lvl=0;
 	tokz->filestack_n=0;
 	tokz->filestack=NULL;
+	tokz->buffer.data=0;
+	tokz->buffer.len=0;
+	tokz->buffer.pos=0;
 	
 	return tokz;
 }
@@ -838,6 +848,27 @@
 	return tokz;
 }
 
+Tokenizer *tokz_prepare_buffer(char *buffer, int len)
+{
+	Tokenizer *tokz;
+	char old=0;
+
+	tokz=tokz_create();
+	if (len>0) {
+		old=buffer[len-1];
+		buffer[len-1]='\0';
+	}
+
+	tokz->flags|=TOKZ_READ_FROM_BUFFER;
+	tokz->buffer.data=scopy(buffer);
+	tokz->buffer.len=len>0 ? len : strlen(tokz->buffer.data);
+	tokz->buffer.pos=0;
+
+	if (old>0)
+		buffer[len-1]=old;
+
+	return tokz;
+}
 
 /*
  * File close

mercurial