token Class Reference

#include <token.h>

Public Member Functions

 token ()
 ~token ()
void setcerr (ostream *x)
void setstring (char *adr, int len)
char * emsg (const char *s)
void next ()
int nextckend ()
int ckend ()
int cmp (const char *s)
int getstr (char *s, int maxlen)
int getstrnew (char *s[])
int getnumber (int *num)
int getdouble (double *num)
int gethex (char *o, int ol)
int openfile (const char *fname)
void closefile ()
struct token_kwfindkw (struct token_kw *kp)
int nextline ()
int parseline (struct token_kw *kp)

Private Member Functions

 token (token &)

Static Private Member Functions

static int ck_hex (int c)

Private Attributes

int tk_flags
const char * tk_fname
FILE * tk_fp
ostream * tk_cerr
char * tk_strp
char * tk_stre
char * tk_tkp
int tk_tkl
int tk_linecnt
char * tk_orgp
char * tk_orge
char * tk_wstr
char tk_wstrstr [TK_WSTRL]
char tk_line [TK_LINEL]

Static Private Attributes

static const int TK_DEBUG_TOKEN = 0x01
static const int TK_WSTRL = 300
static const int TK_LINEL = 300

Detailed Description

Definition at line 43 of file token.h.


Constructor & Destructor Documentation

token::token ( token  )  [private]
token::token (  ) 

Definition at line 33 of file token.cpp.

00034 {
00035     tk_flags   = 0;
00036     tk_fname   = NULL;
00037     tk_fp      = NULL;
00038     tk_cerr    = NULL;
00039     tk_strp    = NULL;
00040     tk_stre    = NULL;
00041     tk_tkp     = NULL;
00042     tk_tkl     = 0;
00043     tk_linecnt = 0;
00044     tk_orgp    = NULL;
00045     tk_orge    = NULL;
00046     tk_wstr    = tk_wstrstr;
00047     memset( tk_wstrstr, 0, TK_WSTRL );
00048     memset( tk_line,    0, TK_LINEL );
00049 }

token::~token (  ) 

Definition at line 51 of file token.cpp.

00052 {
00053 }


Member Function Documentation

int token::ck_hex ( int  c  )  [static, private]

Definition at line 291 of file token.cpp.

00292 {
00293     if( isdigit( c ) )
00294     {
00295         return(c - '0');
00296     }
00297     c = tolower( c );
00298     if( ( c >= 'a' ) && ( c <= 'f' ) )
00299         return( c - 'a' + 10 );
00300     return -1;
00301 }/* end of ck_hex */

int token::ckend (  ) 

Definition at line 128 of file token.cpp.

00129 {
00130     if( tk_strp == tk_stre )
00131         return 0;
00132     emsg("extra tokens are invalid\n");
00133     return 1;
00134 }    /* end of end */

void token::closefile (  ) 

Definition at line 396 of file token.cpp.

00397 {
00398     fclose( tk_fp );
00399 }

int token::cmp ( const char *  s  ) 

Definition at line 138 of file token.cpp.

00139 {
00140     int len;
00141 
00142     if( tk_tkl == 0 )
00143         return 1;    // no match if null token
00144     len = strlen( s );
00145     if( len != tk_tkl )
00146         return 1;    // no match if not same length
00147     return memcmp( s, tk_tkp, tk_tkl );
00148 }

char * token::emsg ( const char *  s  ) 

Definition at line 68 of file token.cpp.

00069 {
00070     char *e;
00071     e = tk_wstr;
00072     if(tk_fname)
00073         e += sprintf(e, "in file %s ", tk_fname);
00074     if(tk_linecnt)
00075         e += sprintf(e, "in line %d ", tk_linecnt);
00076     if(tk_orgp)
00077         e += sprintf(e, "offset %ld ", ( long ) ( tk_strp-tk_orgp ) );
00078     e += sprintf(e, "%s\n", s);
00079     if(tk_cerr)
00080     {
00081         *tk_cerr << "token emsg: " << tk_wstr << endl;
00082     }
00083     return tk_wstr;
00084 }/* end of emsg */

struct token_kw * token::findkw ( struct token_kw kp  )  [read]

Definition at line 339 of file token.cpp.

00340 {
00341     while( kp->kw_word )
00342     {
00343         if( !cmp( kp->kw_word ) )
00344             return( kp );
00345         kp++;
00346     }
00347     return NULL;
00348 }

int token::getdouble ( double *  num  ) 

Definition at line 208 of file token.cpp.

00208                                 {
00209 int len;
00210 char *p;
00211 double x;
00212 bool fraction = false;
00213 double divby = 0;
00214 
00215     x = 0;
00216     len = tk_tkl;
00217     p = tk_tkp;
00218     while(len-- > 0)
00219     {
00220         if( (isdigit(*p)) && (fraction == false) )
00221         {
00222                 x *= 10;
00223                 x += (*p & 0x0f);
00224         }
00225         else if( (isdigit(*p)) && (fraction == true) )
00226         {
00227                 x += (*p & 0x0f)/ divby;
00228                 divby *=  10.0;
00229         }
00230         else if( ( *p == '.' ) || ( *p == ',' ) )
00231         {
00232             fraction = true;
00233             divby = 10.0;
00234         }
00235         else return -1;
00236         p++;
00237     }
00238     *num = x;
00239     return 0;
00240 }

int token::gethex ( char *  o,
int  ol 
)

Definition at line 246 of file token.cpp.

00247 {
00248     int len;
00249     char *t1, *o1;
00250     int n0, n1;
00251 
00252     o1 = o;
00253     t1 = tk_tkp;
00254     len = tk_tkl;
00255     while( len >= 2 )
00256     {
00257         if( (( n0 = ck_hex( *t1 )) < 0 ) || (( n1 = ck_hex(*( t1 + 1 )) ) < 0 ) )
00258         {
00259             emsg("invalid hex char");
00260             return 1;
00261         }
00262         if( o1 > ( o + ol ) )
00263         {
00264             emsg("hex value too int");
00265             return 1;
00266         }
00267         *o1++ = ( n0 << 4 ) | n1;
00268         len -= 2;
00269         t1 += 2;
00270         /* allow "-" between bytes */
00271         if( ( len > 0 ) && ( *t1 == '-' ) )
00272         {
00273             t1++;
00274             len--;
00275         }
00276     }
00277     if( len != 0 )
00278     {
00279         emsg("odd number of nibbles in hex value");
00280         return 1;
00281     }
00282     /* check for exact length */
00283     if( o1 != o + ol )
00284     {
00285         emsg("hex value too short");
00286         return 1;
00287     }
00288     return 0;
00289 }

int token::getnumber ( int *  num  ) 

Definition at line 180 of file token.cpp.

00181 {
00182     int len;
00183     char *p;
00184     int x;
00185 
00186     x = 0;
00187     len = tk_tkl;
00188     p = tk_tkp;
00189     while( len-- > 0 )
00190     {
00191         if( isdigit( *p ) )
00192         {
00193             x *= 10;
00194             x += ( *p & 0x0f );
00195         }
00196         else return -1;
00197         p++;
00198     }
00199     *num = x;
00200     return 0;
00201 }

int token::getstr ( char *  s,
int  maxlen 
)

Definition at line 151 of file token.cpp.

00152 {
00153     if( tk_tkl == 0 ) return 1;
00154     if( tk_tkl > maxlen )
00155     {
00156         emsg("string too int");
00157         return 1;
00158     }
00159     memcpy( s, tk_tkp, tk_tkl );
00160     s[tk_tkl] = '\0';
00161     return 0;
00162 }

int token::getstrnew ( char *  s[]  ) 

Definition at line 166 of file token.cpp.

00167 {
00168     if( tk_tkl == 0 )
00169         return 1;
00170     *s = new char[tk_tkl+1];
00171     memcpy( *s, tk_tkp, tk_tkl );
00172     (*s)[tk_tkl] = '\0';
00173     return 0;
00174 }

void token::next (  ) 

Definition at line 88 of file token.cpp.

00089 {
00090     char *t1, *te, *t2;
00091     char c;
00092     int len;
00093 
00094     t1 = tk_strp;        /* start scan ptr */
00095     te = tk_stre;
00096     /* skip leading white space */
00097     while((t1 < te) && ((c = *t1) == ' ' || c == '\t'))
00098         t1++;
00099     /* scan for end of token */
00100     t2 = t1;
00101     while((t2 < te) && (c = *t2) != ' ' && c != '\t')
00102         t2++;
00103     /* update scan ptr, return token if any */
00104     tk_strp = t2;
00105     tk_tkp = t1;
00106     tk_tkl = t2 - t1;
00107     if(tk_flags & TK_DEBUG_TOKEN)
00108     {
00109         len = TK_WSTRL - 30;    /* room for "token:" etc */
00110         if(tk_tkl < len) len = tk_tkl;
00111         memcpy(tk_wstr, tk_tkp, len);
00112         *(tk_wstr+len) = '\0';
00113         if(tk_cerr)
00114         {
00115             *tk_cerr << "token: >" << tk_wstr << "<" << endl;
00116         }
00117     }
00118 }       /* end of next */

int token::nextckend (  ) 

Definition at line 121 of file token.cpp.

00122 {
00123     next();
00124     return(ckend());
00125 }    /* end of nextend */

int token::nextline (  ) 

Definition at line 306 of file token.cpp.

00307 {
00308     int len;
00309 
00310     while( 1 )
00311     {
00312         tk_tkl = 0;        // no token yet
00313         if( fgets( tk_line, TK_LINEL, tk_fp) == NULL )
00314         {
00315             return 1;
00316         }
00317         tk_linecnt++;
00318         len = strlen( tk_line );
00319         if( tk_line[len-1] == '\n' )    // remove trailing \n if any
00320         {
00321             len--;
00322             tk_line[len] = '\0';
00323         }
00324         if( tk_cerr )
00325         {
00326             *tk_cerr << ">" << tk_line << endl;
00327         }
00328         if( tk_line[0] == '#' )         // comment?
00329             continue;
00330         setstring( tk_line, len );
00331         return 0;
00332     }
00333     return 1;
00334 }

int token::openfile ( const char *  fname  ) 

Definition at line 377 of file token.cpp.

00378 {
00379     tk_fname = fname;
00380     tk_fp = fopen( tk_fname, "r" );
00381     if(tk_fp == NULL && tk_cerr )
00382     {
00383         *tk_cerr << "Could not open file " << tk_fname << endl;
00384         return 1;
00385     }
00386     else if( tk_fp == NULL )
00387     {
00388         /* tk_cerr was not set */
00389         RioErr << "Could not open file " << fname << endl; 
00390         return 1;
00391     }
00392     return 0;
00393 }

int token::parseline ( struct token_kw kp  ) 

Definition at line 353 of file token.cpp.

00354 {
00355     struct token_kw *kp;
00356 
00357     do {
00358         if( nextline() )
00359             return END_TOKEN;
00360         next();
00361     } while( tk_tkl == 0 );// loop if empty line
00362 
00363     kp = findkw(keys);
00364     if( kp != NULL )
00365     {
00366         next();// to next token
00367         return kp->kw_value;
00368     }
00369     // have invalid kw here
00370     emsg( "invalid keyword" );
00371     return ERROR_TOKEN;
00372 }

void token::setcerr ( ostream *  x  ) 

Definition at line 55 of file token.cpp.

00056 {
00057     tk_cerr = x;
00058 }

void token::setstring ( char *  adr,
int  len 
)

Definition at line 60 of file token.cpp.

00061 {
00062     tk_strp = s;
00063     tk_stre = s+len;
00064     tk_orgp = tk_strp;
00065     tk_orge = tk_stre;
00066 }


Field Documentation

ostream* token::tk_cerr [private]

Definition at line 52 of file token.h.

const int token::TK_DEBUG_TOKEN = 0x01 [static, private]

Definition at line 49 of file token.h.

int token::tk_flags [private]

Definition at line 48 of file token.h.

const char* token::tk_fname [private]

Definition at line 50 of file token.h.

FILE* token::tk_fp [private]

Definition at line 51 of file token.h.

char token::tk_line[TK_LINEL] [private]

Definition at line 64 of file token.h.

int token::tk_linecnt [private]

Definition at line 57 of file token.h.

const int token::TK_LINEL = 300 [static, private]

Definition at line 63 of file token.h.

char* token::tk_orge [private]

Definition at line 59 of file token.h.

char* token::tk_orgp [private]

Definition at line 58 of file token.h.

char* token::tk_stre [private]

Definition at line 54 of file token.h.

char* token::tk_strp [private]

Definition at line 53 of file token.h.

int token::tk_tkl [private]

Definition at line 56 of file token.h.

char* token::tk_tkp [private]

Definition at line 55 of file token.h.

char* token::tk_wstr [private]

Definition at line 61 of file token.h.

const int token::TK_WSTRL = 300 [static, private]

Definition at line 60 of file token.h.

char token::tk_wstrstr[TK_WSTRL] [private]

Definition at line 62 of file token.h.


The documentation for this class was generated from the following files:
Generated on Wed Jul 4 16:03:38 2012 for RIO by  doxygen 1.6.3