/** 3DGPL *************************************************\
* () *
* Interpreting ascii data description, constructing a *
* data structure in memory, atempting to do correct *
* alignment for items in the structs. *
* *
* Defines: *
* D_data Getting a specified export. *
* *
* Internals: *
* DI_char Next char of the source; *
* DI_token Next token of the stream; *
* DI_type Interpreting type; *
* DI_var Interpreting var list; *
* DI_length Length of type in bytes. *
* *
* (c) 1995-98 Sergei Savchenko, (savs@cs.mcgill.ca) *
\**********************************************************/
#include "RayTracing.h" /* HW_error */
#include "data.h" /* self definition */
#include <stdio.h> /* FILE etc. */
#include <string.h> /* cmpstr etc. */
#include <stdlib.h> /* malloc etc. */
#define D_MAX_TOKEN_LENGTH 100 /* token taken from the source */
#define D_NAME_LENGTH 32 /* names gotta be this long */
#define D_BUFFER_LENGTH 256 /* chars being read */
enum D_token_type /* enumerates all tokens */
{
D_EOF, /* end of file */
D_NAME, /* starting from the letter */
D_NUMBER, /* starting from a number */
D_NUMBERF, /* starting from a float number */
D_BASE_INT, /* base types */
D_BASE_FLOAT,
D_BASE_SHORT,
D_BASE_BYTE,
D_BASE_PTR, /* typeless pointer */
D_DEREF, /* dereferencing symbol */
D_OPEN_SQ, /* describes an array */
D_CLOSE_SQ,
D_OPEN_CURL, /* describes a structure */
D_CLOSE_CURL,
D_OPEN_RAW, /* describes a raw data array */
D_CLOSE_RAW,
D_STATE_TYPE, /* statements */
D_STATE_VAR, /* "name" */
D_STATE_EXPORT
};
struct D_token /* describes a token */
{
char d_description[D_MAX_TOKEN_LENGTH]; /* actual text of a token */
enum D_token_type d_type; /* enumeration of this token */
};
struct D_token D_tokens[]= /* what is in the grammer */
{
{"int" ,D_BASE_INT}, /* base types */
{"float" ,D_BASE_FLOAT},
{"short" ,D_BASE_SHORT},
{"byte" ,D_BASE_BYTE},
{"ptr" ,D_BASE_PTR},
{"@" ,D_DEREF}, /* dereferencing symbol */
{"[" ,D_OPEN_SQ}, /* array */
{"]" ,D_CLOSE_SQ},
{"{" ,D_OPEN_CURL}, /* structure */
{"}" ,D_CLOSE_CURL},
{"<" ,D_OPEN_RAW}, /* raw array */
{">" ,D_CLOSE_RAW},
{"type" ,D_STATE_TYPE}, /* statements */
{"var" ,D_STATE_VAR},
{"export",D_STATE_EXPORT},
{"" ,(D_token_type)0} /* tagging the last one */
};
char D_terminators[]=" \n\r@[]{}<>\xff"; /* expression terminators */
char D_filters[]=" \n\r"; /* characters which are ignored */
int D_current; /* char being read */
int D_last; /* how many chars are there */
int D_line; /* number of lines read */
char *D_buffer; /* the chars being read */
FILE *D_file; /* handle to the input */
char D_fname[D_NAME_LENGTH]; /* name of the current file */
struct D_int_alignment_struct { char d_char; int d_int; } D_int_check;
struct D_float_alignment_struct { char d_char; float d_float; } D_float_check;
struct D_short_alignment_struct { char d_char; short d_short; } D_short_check;
struct D_ptr_alignment_struct { char d_char; char *d_ptr; } D_ptr_check;
int D_int_alignment; /* alignment type for int */
int D_float_alignment; /* alignment type for int */
int D_short_alignment; /* alignment type for int */
int D_ptr_alignment; /* gotta be same as int but... */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * *\
* Stream of chars from the script file. *
* *
* RETURNS: Next character from the script stream. *
* -------- *
\* * * * * * * * * * * * * * * * * * * * * * * * * * * * */
char DI_char(void)
{
if(D_current<D_last) /* as long as have something */
{
return(D_buffer[D_current++]); /* current char */
}
else
{
if((D_last=fread(D_buffer,sizeof(char),D_BUFFER_LENGTH,D_file))==0)
return('\xff'); /* end of the source */
D_current=0; /* starting from the first one */
return(D_buffer[D_current++]);
}
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * *\
* Tokenizer. token is defined as a text between *
* a filter ( ignored ) and a terminator. token might *
* be a terminator. *
* *
* RETURNS: Type of the current token. *
* -------- *
* SETS: D_token_text,D_token_number for some kinds of *
* ----- tokens, D_line would have number of \n read. *
\* * * * * * * * * * * * * * * * * * * * * * * * * * * * */
char D_token_text[D_MAX_TOKEN_LENGTH]; /* set after DI_token */
int D_token_number;
float D_token_numberf;
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * */
enum D_token_type DI_token(void)
{
int i,lng=0; /* empty yet */
char c;
for(;;) /* ignore preceding filters */
{
c=DI_char();
if(c=='\xff') /* end of input strem */
{
D_token_text[lng]=0; /* no text associated here */
return(D_EOF); /* end of the source */
}
for(i=0;D_filters[i]!=0;i++)
if(c==D_filters[i]) break; /* yes, filter current symbol */
if(D_filters[i]==0)
{
D_current--; /* process further */
break; /* nope, none of filters */
}
if(D_filters[i]=='\n') D_line++; /* counter of lines read */
}
for(;;) /* copy until the terminator */
{
c=DI_char();
for(i=0;D_terminators[i]!=0;i++)
if(c==D_terminators[i]) break; /* yes, terminate on this one */
if(D_terminators[i]==0) /* not a terminator */
{
D_token_text[lng++]=c; /* char in the token */
}
else /* yes a terminator */
{
D_token_text[lng]=0; /* end of line */
D_current--; /* process a terminator further */
break; /* finished composing a token */
}
}
if(lng==0) /* checking tokens/terminators */
{
for(
评论13