home *** CD-ROM | disk | FTP | other *** search
/ Dream 52 / Amiga_Dream_52.iso / Amiga / Jeux / demos / crystalPPC.lha / token.cpp < prev    next >
C/C++ Source or Header  |  1998-02-05  |  3KB  |  112 lines

  1. #include <math.h>
  2. #include <time.h>
  3. #include "system.h"
  4.  
  5. #ifndef DEF_H
  6. #include "def.h"
  7. #endif
  8.  
  9. #ifndef TOKEN_H
  10. #include "token.h"
  11. #endif
  12.  
  13. //---------------------------------------------------------------------------
  14.  
  15. char* spaces = "                                                                                  ";
  16.  
  17. float get_token_float (char** buf)
  18. {
  19.   char* t = get_token (buf);
  20.   if (!((*t >= '0' && *t <= '9') || *t == '-' || *t == '.'))
  21.   {
  22.     printf ("Expected a floating point number! Got '%s' instead!\n", t);
  23.     exit (0);
  24.   }
  25.   float rc;
  26.   sscanf (t, "%f", &rc);
  27.   return rc;
  28. }
  29.  
  30. int get_token_int (char** buf)
  31. {
  32.   char* t = get_token (buf);
  33.   if (!((*t >= '0' && *t <= '9') || *t == '-'))
  34.   {
  35.     printf ("Expected an integer number! Got '%s' instead!\n", t);
  36.     exit (0);
  37.   }
  38.   int rc;
  39.   sscanf (t, "%d", &rc);
  40.   return rc;
  41. }
  42.  
  43. char* get_token (char** buf)
  44. {
  45.   static char token[100];
  46.   char* b = *buf, * t = token;
  47.   while (TRUE)
  48.   {
  49.     while (*b && (*b == ' ' || *b == '\n' || *b == 13 || *b == 10 || *b == '\t')) b++;
  50.     if (*b == ';')
  51.       while (*b && *b != '\n' && *b != 13 && *b != 10) b++;
  52.     else break;
  53.   }
  54.  
  55.   if (*b == '\'')
  56.   {
  57.     b++;
  58.     while (*b && *b != '\'') { *t++ = *b++; }
  59.     *t++ = 0;
  60.     if (*b == '\'') b++;
  61.   }
  62.   else if (*b == '(' || *b == ')' || *b == '=' || *b == ',' || *b == '[' || *b == ']' ||
  63.         *b == '%' || *b == ':' || *b == '{' || *b == '}')
  64.   {
  65.     *t++ = *b++;
  66.     *t++ = 0;
  67.   }
  68.   else if ((*b >= '0' && *b <= '9') || *b == '.' || *b == '-')
  69.   {
  70.     while (*b && ((*b >= '0' && *b <= '9') || *b == '.' || *b == '-')) { *t++ = *b++; }
  71.     *t++ = 0;
  72.   }
  73.   else if ((*b >= 'A' && *b <= 'Z') || (*b >= 'a' && *b <= 'z') || *b == '_')
  74.   {
  75.     while (*b && ((*b >= 'A' && *b <= 'Z') || (*b >= 'a' && *b <= 'z') || *b == '_')) { *t++ = *b++; }
  76.     *t++ = 0;
  77.   }
  78.   else if (*b == 0)
  79.   {
  80.     *buf = b;
  81.     return NULL;
  82.   }
  83.   else printf ("Que (%c%c%c%c%c...)?\n", *b, *(b+1), *(b+2), *(b+3), *(b+4));
  84.  
  85.   *buf = b;
  86.   return token;
  87. }
  88. //vonmir
  89. //#ifdef COMP_WCC
  90.  
  91. //ersatzlos gestrichen
  92. //
  93.  
  94. void skip_token (char** buf, char* tok){skip_token (buf, tok, NULL);}
  95. void skip_token (char** buf, char* tok, char* msg)
  96.  
  97. //vonmir
  98. //#else
  99. //void skip_token (char** buf, char* tok, char* msg = NULL)
  100. //#endif
  101.  
  102. //ersatzlos gestrichen
  103. //
  104. {
  105.   char* t = get_token (buf);
  106.   if (strcmp (t, tok))
  107.     if (msg) printf (msg, tok, t);
  108.     else printf ("Expected '%s' instead of '%s'!\n", tok, t);
  109. }
  110.  
  111. //---------------------------------------------------------------------------
  112.