home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / i / iritsm3s.zip / cagd_lib / cagd_tkn.c < prev    next >
C/C++ Source or Header  |  1991-10-27  |  7KB  |  188 lines

  1. /******************************************************************************
  2. * Bzr-Read.c - Bezier curves handling routines - read from file.          *
  3. *******************************************************************************
  4. * Written by Gershon Elber, Mar. 90.                          *
  5. ******************************************************************************/
  6.  
  7. #ifdef __MSDOS__
  8. #include <stdlib.h>
  9. #endif /* __MSDOS__ */
  10.  
  11. #include <ctype.h>
  12. #include <stdio.h>
  13. #include <string.h>
  14. #include "cagd_loc.h"
  15.  
  16. #define  UNGET_STACK_SIZE    3
  17.  
  18. int _CagdGlblLineCount = 1;         /* Used to locate errors in input file. */
  19.  
  20. static int GlblTknStackSize = 0;          /* Used by parser, to unget token. */
  21. static char GlblStringToken[UNGET_STACK_SIZE][LINE_LEN];/* Save unget tokens.*/
  22.  
  23. static void GetStringToken(FILE *f, char *StringToken);
  24.  
  25. /*****************************************************************************
  26. * Routine to read from input file f the    following [ATTR ...] [ATTR ...].     *
  27. * Note the '[' was allready read.                         *
  28. * Current supported attributes: None.                         *
  29. * Returns NULL if O.k., otherwise string describing the error.             *
  30. *****************************************************************************/
  31. char *_CagdGetCurveAttributes(FILE *f)
  32. {
  33.     TokenNumType i;
  34.     char StringToken[LINE_LEN];
  35.  
  36.     do {
  37.     switch (_CagdGetToken(f, StringToken)) {
  38.         default:
  39.         while ((i = _CagdGetToken(f, StringToken)) !=
  40.                             TOKEN_CLOSE_PAREN &&
  41.                i != TOKEN_EOF);
  42.         if (i == TOKEN_EOF)
  43.             return "EOF detected in middle of attribute.";
  44.         break;
  45.     }
  46.     }
  47.     while (_CagdGetToken(f, StringToken) == TOKEN_OPEN_PAREN);
  48.  
  49.     _CagdUnGetToken(StringToken);
  50.  
  51.     return NULL;
  52. }
  53.  
  54. /*****************************************************************************
  55. * Routine to read from input file f the    following [ATTR ...] [ATTR ...].     *
  56. * Note the '[' was allready read.                         *
  57. * Current supported attributes: None.                         *
  58. * Returns NULL if O.k., otherwise string describing the error.             *
  59. *****************************************************************************/
  60. char *_CagdGetSurfaceAttributes(FILE *f)
  61. {
  62.     TokenNumType i;
  63.     char StringToken[LINE_LEN];
  64.  
  65.     do {
  66.     switch (_CagdGetToken(f, StringToken)) {
  67.         default:
  68.         while ((i = _CagdGetToken(f, StringToken)) !=
  69.                             TOKEN_CLOSE_PAREN &&
  70.                i != TOKEN_EOF);
  71.         if (i == TOKEN_EOF)
  72.             return "EOF detected in middle of attribute.";
  73.         break;
  74.     }
  75.     }
  76.     while (_CagdGetToken(f, StringToken) == TOKEN_OPEN_PAREN);
  77.  
  78.     _CagdUnGetToken(StringToken);
  79.  
  80.     return NULL;
  81. }
  82.  
  83. /******************************************************************************
  84. *   Routine to unget one token (on stack of UNGET_STACK_SIZE levels!)          *
  85. ******************************************************************************/
  86. void _CagdUnGetToken(char *StringToken)
  87. {
  88.     if (GlblTknStackSize >= UNGET_STACK_SIZE)
  89.     FATAL_ERROR(CAGD_ERR_PARSER_STACK_OV);
  90.  
  91.     strcpy(GlblStringToken[GlblTknStackSize++], StringToken);
  92. }
  93.  
  94. /******************************************************************************
  95. *   Routine to get the next token out of the input file    f.              *
  96. * Returns the next token found,    as StringToken.                      *
  97. * Note:    StringToken must be allocated before calling this routine!          *
  98. ******************************************************************************/
  99. static void GetStringToken(FILE *f, char *StringToken)
  100. {
  101.     int    Len;
  102.     char c, *LocalStringToken;
  103.  
  104.     if (GlblTknStackSize) {               /* Get first the unget token. */
  105.     strcpy(StringToken, GlblStringToken[--GlblTknStackSize]);
  106.     return;
  107.     }
  108.  
  109.     /* skip white spaces and comments: */
  110.     while (!feof(f) && ((c = getc(f)) == ' ' || c == '\t' || c == '\n') ||
  111.                                      c == '#') {
  112.         /* Skip a comment if encounter one. */
  113.     if (c == '#') while (!feof(f) && c != '\n') c = getc(f);
  114.     if (c == '\n') _CagdGlblLineCount++;         /* Count the lines. */
  115.     }
  116.  
  117.     LocalStringToken = StringToken;
  118.     if (c == '[')              /* Its a token by itself so return it. */
  119.     *LocalStringToken++ = c;          /* Copy the token into string. */
  120.     else {
  121.     if (!feof(f))
  122.         do *LocalStringToken++ = c;       /* Copy the token into string. */
  123.         while ((!feof(f)) &&
  124.            ((c = getc(f)) != ' ') && (c != '\t') && (c != '\n'));
  125.     if (c == '\n') ungetc(c, f);     /* Save it to be counted next time. */
  126.     }
  127.     *LocalStringToken =    0;                     /* Put eos. */
  128.  
  129.     /* The following handles the spacial case were we have XXXX] - we must   */
  130.     /* split it    into two token XXXX and    ], UnGetToken(']') and return XXXX:  */
  131.     if ((StringToken[Len = strlen(StringToken)-1] == ']') && (Len > 0))    {
  132.     /* Return CloseParen */
  133.     _CagdUnGetToken(&StringToken[Len]);             /* Save next token. */
  134.     StringToken[Len] = 0;            /* Set end of string on "]". */
  135.     }
  136. }
  137.  
  138. /******************************************************************************
  139. *   Routine to get the next token out of the input file    f as token number.    *
  140. * Returns the next token number    found, with numeric result in NumericToken    *
  141. * if TokenType is TOKEN_NUMBER.                              *
  142. * Note:    StringToken must be allocated before calling this routine!          *
  143. ******************************************************************************/
  144. TokenNumType _CagdGetToken(FILE *f, char *StringToken)
  145. {
  146.     GetStringToken(f, StringToken);
  147.  
  148.     if (feof(f))                 return TOKEN_EOF;
  149.  
  150.     if (!strcmp(StringToken, "["))         return TOKEN_OPEN_PAREN;
  151.     if (!strcmp(StringToken, "]"))         return TOKEN_CLOSE_PAREN;
  152.  
  153.     if (!strcmp(StringToken, "BEZIER"))      return TOKEN_BEZIER;
  154.     if (!strcmp(StringToken, "BSPLINE"))     return TOKEN_BSPLINE;
  155.     if (!strcmp(StringToken, "POWER"))       return TOKEN_POWER;
  156.  
  157.     if (!strcmp(StringToken, "CURVE"))         return TOKEN_CURVE;
  158.     if (!strcmp(StringToken, "SURFACE"))     return TOKEN_SURFACE;
  159.  
  160.     if (!strcmp(StringToken, "PTYPE"))       return TOKEN_PTYPE;
  161.     if (!strcmp(StringToken, "NUMPTS"))         return TOKEN_NUM_PTS;
  162.     if (!strcmp(StringToken, "ORDER"))         return TOKEN_ORDER;
  163.  
  164.     if (!strcmp(StringToken, "KV"))         return TOKEN_KV;
  165.  
  166.     return TOKEN_OTHER;                  /* Must be number or name. */
  167. }
  168.  
  169. /*****************************************************************************
  170. * Convert a real number into a string.                         *
  171. *****************************************************************************/
  172. char *_CagdReal2Str(double R)
  173. {
  174.     static char Buffer[LINE_LEN];
  175.     int j;
  176.  
  177.     if (ABS(R) < EPSILON) R = 0.0;
  178.  
  179.     sprintf(Buffer, "%-8.6lg", R);
  180.  
  181.     for (j = strlen(Buffer) - 1; Buffer[j] == ' ' && j > 0; j--);
  182.     if (strchr(Buffer, '.') != NULL)
  183.     for (; Buffer[j] == '0' && j > 0; j--);
  184.     Buffer[j+1] = 0;
  185.  
  186.     return Buffer;
  187. }
  188.