home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1996 February / PCWK0296.iso / sharewar / dos / program / gs300sr1 / gs300sr1.exe / ISCANNUM.C < prev    next >
Encoding:
C/C++ Source or Header  |  1994-07-27  |  8.8 KB  |  361 lines

  1. /* Copyright (C) 1994 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of Aladdin Ghostscript.
  4.   
  5.   Aladdin Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author
  6.   or distributor accepts any responsibility for the consequences of using it,
  7.   or for whether it serves any particular purpose or works at all, unless he
  8.   or she says so in writing.  Refer to the Aladdin Ghostscript Free Public
  9.   License (the "License") for full details.
  10.   
  11.   Every copy of Aladdin Ghostscript must include a copy of the License,
  12.   normally in a plain ASCII text file named PUBLIC.  The License grants you
  13.   the right to copy, modify and redistribute Aladdin Ghostscript, but only
  14.   under certain conditions described in the License.  Among other things, the
  15.   License requires that the copyright notice and this notice be preserved on
  16.   all copies.
  17. */
  18.  
  19. /* iscannum.c */
  20. /* Number scanner for Ghostscript interpreter */
  21. #include "ghost.h"
  22. #include "errors.h"
  23. #include "scommon.h"
  24. #include "iscannum.h"            /* defines interface */
  25. #include "scanchar.h"
  26. #include "store.h"
  27.  
  28. #define is_digit(d, c)\
  29.   ((d = decoder[c]) < 10)
  30.  
  31. #define scan_sign(sign, ptr)\
  32.   switch ( *ptr ) {\
  33.     case '-': sign = -1; ptr++; break;\
  34.     case '+': sign = 1; ptr++; break;\
  35.     default: sign = 0;\
  36.   }
  37.  
  38. /* Scan a number for cvi or cvr. */
  39. /* The first argument is a t_string.  This is just like scan_number, */
  40. /* but allows leading or trailing whitespace. */
  41. int
  42. scan_number_only(const ref *psref, ref *pnref)
  43. {    const byte *str = psref->value.const_bytes;
  44.     const byte *end = str + r_size(psref);
  45.     int sign, code;
  46.     const byte *newstr;
  47.     if ( !r_has_attr(psref, a_read) )
  48.         return_error(e_invalidaccess);
  49.     while ( str < end && scan_char_decoder[*str] == ctype_space )
  50.         str++;
  51.     while ( str < end && scan_char_decoder[end[-1]] == ctype_space )
  52.         end--;
  53.     if ( str == end )
  54.         return_error(e_syntaxerror);
  55.     scan_sign(sign, str);
  56.     code = scan_number(str, end, sign, pnref, &newstr);
  57.     if ( code <= 0 )
  58.         return code;
  59.     return_error(e_syntaxerror);
  60. }
  61.  
  62. /* Note that the number scanning procedures use a byte ** and a byte * */
  63. /* rather than a stream.  (It makes quite a difference in performance.) */
  64. #define ngetc(cvar, sp, exit)\
  65.   if ( sp >= end ) { exit; } else cvar = *sp++
  66.  
  67. /* Scan a number.  If the number consumes the entire string, return 0; */
  68. /* if not, set *psp to the first character beyond the number and return 1. */
  69. int
  70. scan_number(register const byte *sp, const byte *end, int sign,
  71.   ref *pref, const byte **psp)
  72. {    /* Powers of 10 up to 6 can be represented accurately as */
  73.     /* a single-precision float. */
  74. #define num_powers_10 6
  75.     static const float powers_10[num_powers_10+1] =
  76.     {    1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6    };
  77.     static const double neg_powers_10[num_powers_10+1] =
  78.     {    1e0, 1e-1, 1e-2, 1e-3, 1e-4, 1e-5, 1e-6    };
  79.     int ival;
  80.     long lval;
  81.     double dval;
  82.     int exp10;
  83.     int code = 0;
  84.     register int c, d;
  85.     register const byte _ds *decoder = scan_char_decoder;
  86.     ngetc(c, sp, return_error(e_syntaxerror));
  87. #define would_overflow(val, d, maxv)\
  88.   (val >= maxv / 10 && (val > maxv / 10 || d > (int)(maxv % 10)))
  89.     if ( !is_digit(d, c) )
  90.     {    if ( c != '.' )
  91.             return_error(e_syntaxerror);
  92.         /* Might be a number starting with '.'. */
  93.         ngetc(c, sp, return_error(e_syntaxerror));
  94.         if ( !is_digit(d, c) )
  95.             return_error(e_syntaxerror);
  96.         ival = 0;
  97.         goto i2r;
  98.     }
  99.  
  100.     /* Accumulate an integer in ival. */
  101.     /* Do up to 4 digits without a loop, */
  102.     /* since we know this can't overflow and since */
  103.     /* most numbers have 4 (integer) digits or fewer. */
  104.     ival = d;
  105.     if ( end - sp >= 3 )        /* just check once */
  106.     {    if ( !is_digit(d, (c = *sp)) )
  107.         {    sp++;
  108.             goto ind;
  109.         }
  110.         ival = ival * 10 + d;
  111.         if ( !is_digit(d, (c = sp[1])) )
  112.         {    sp += 2;
  113.             goto ind;
  114.         }
  115.         ival = ival * 10 + d;
  116.         sp += 3;
  117.         if ( !is_digit(d, (c = sp[-1])) )
  118.             goto ind;
  119.         ival = ival * 10 + d;
  120.     }
  121.     for ( ; ; ival = ival * 10 + d )
  122.     {    ngetc(c, sp, goto iret);
  123.         if ( !is_digit(d, c) )
  124.             break;
  125.         if ( would_overflow(ival, d, max_int) )
  126.             goto i2l;
  127.     }
  128. ind:    switch ( c )
  129.     {
  130.     case '.':
  131.         ngetc(c, sp, c = EOFC);
  132.         goto i2r;
  133.     default:
  134.         *psp = sp;
  135.         code = 1;
  136.         break;
  137.     case 'e': case 'E':
  138.         if ( sign < 0 )
  139.             ival = -ival;
  140.         dval = ival;
  141.         exp10 = 0;
  142.         goto fe;
  143.     case '#':
  144.     {    ulong uval = 0, lmax;
  145. #define radix (uint)ival
  146.         if ( sign || radix < min_radix || radix > max_radix )
  147.             return_error(e_syntaxerror);
  148.         /* Avoid multiplies for power-of-2 radix. */
  149.         if ( !(radix & (radix - 1)) )
  150.         {    int shift;
  151.             switch ( radix )
  152.             {
  153. #define set_shift(n)\
  154.   shift = n; lmax = max_ulong >> n
  155.             case 2: set_shift(1); break;
  156.             case 4: set_shift(2); break;
  157.             case 8: set_shift(3); break;
  158.             case 16: set_shift(4); break;
  159.             case 32: set_shift(5); break;
  160. #undef set_shift
  161.             }
  162.             for ( ; ; uval = (uval << shift) + d )
  163.             {    ngetc(c, sp, break);
  164.                 d = decoder[c];
  165.                 if ( d >= radix )
  166.                 {    *psp = sp;
  167.                     code = 1;
  168.                     break;
  169.                 }
  170.                 if ( uval > lmax )
  171.                     return_error(e_limitcheck);
  172.             }
  173.         }
  174.         else
  175.         {    int lrem = max_ulong % radix;
  176.             lmax = max_ulong / radix;
  177.             for ( ; ; uval = uval * radix + d )
  178.             {    ngetc(c, sp, break);
  179.                 d = decoder[c];
  180.                 if ( d >= radix )
  181.                 {    *psp = sp;
  182.                     code = 1;
  183.                     break;
  184.                 }
  185.                 if ( uval >= lmax &&
  186.                      (uval > lmax || d > lrem)
  187.                    )
  188.                     return_error(e_limitcheck);
  189.             }
  190.         }
  191. #undef radix
  192.         make_int_new(pref, uval);
  193.         return code;
  194.     }
  195.     }
  196. iret:    make_int_new(pref, (sign < 0 ? -ival : ival));
  197.     return code;
  198.  
  199.     /* Accumulate a long in lval. */
  200. i2l:    for ( lval = ival; ; )
  201.     {    lval = lval * 10 + d;
  202.         ngetc(c, sp, goto lret);
  203.         if ( !is_digit(d, c) )
  204.             break;
  205.         if ( would_overflow(lval, d, max_long) )
  206.         {    /* Make a special check for entering the smallest */
  207.             /* (most negative) integer. */
  208.             if ( lval == max_long / 10 &&
  209.                  d == (int)(max_long % 10) + 1 && sign < 0
  210.                )
  211.             {    ngetc(c, sp, c = EOFC);
  212.                 dval = -(double)min_long;
  213.                 if ( c == 'e' || c == 'E' || c == '.' )
  214.                 {    exp10 = 0;
  215.                     goto fs;
  216.                 }
  217.                 else if ( !is_digit(d, c) )
  218.                 {    lval = min_long;
  219.                     break;
  220.                 }
  221.             }
  222.             else
  223.                 dval = lval;
  224.             goto l2d;
  225.         }
  226.     }
  227.     switch ( c )
  228.     {
  229.     case '.':
  230.         ngetc(c, sp, c = EOFC);
  231.         exp10 = 0;
  232.         goto l2r;
  233.     default:
  234.         *psp = sp;
  235.         code = 1;
  236.         break;
  237.     case 'e': case 'E':
  238.         exp10 = 0;
  239.         goto le;
  240.     case '#':
  241.         return_error(e_syntaxerror);
  242.     }
  243. lret:    make_int_new(pref, (sign < 0 ? -lval : lval));
  244.     return code;
  245.  
  246.     /* Accumulate a double in dval. */
  247. l2d:    exp10 = 0;
  248.     for ( ; ; )
  249.     {    dval = dval * 10 + d;
  250.         ngetc(c, sp, c = EOFC);
  251.         if ( !is_digit(d, c) )
  252.             goto fs;
  253.     }
  254.  
  255.     /* We saw a '.' while accumulating an integer in ival. */
  256. i2r:    exp10 = 0;
  257.     while ( is_digit(d, c) )
  258.     {    if ( would_overflow(ival, d, max_int) )
  259.         {    lval = ival;
  260.             goto l2r;
  261.         }
  262.         ival = ival * 10 + d;
  263.         exp10--;
  264.         ngetc(c, sp, c = EOFC);
  265.     }
  266.     if ( sign < 0 )
  267.         ival = -ival;
  268.     /* Take a shortcut for the common case */
  269.     if ( !(c == 'e' || c == 'E' || exp10 < -num_powers_10) )
  270.     {    /* Check for trailing garbage */
  271.         if ( c != EOFC )
  272.             *psp = sp, code = 1;
  273.         make_real_new(pref, ival * neg_powers_10[-exp10]);
  274.         return code;
  275.     }
  276.     dval = ival;
  277.     goto fe;
  278.  
  279.     /* We saw a '.' while accumulating a long in lval. */
  280. l2r:    while ( is_digit(d, c) )
  281.     {    if ( would_overflow(lval, d, max_long) )
  282.         {    dval = lval;
  283.             goto fd;
  284.         }
  285.         lval = lval * 10 + d;
  286.         exp10--;
  287.         ngetc(c, sp, c = EOFC);
  288.     }
  289. le:    if ( sign < 0 )
  290.         lval = -lval;
  291.     dval = lval;
  292.     goto fe;
  293.  
  294.     /* Now we are accumulating a double in dval. */
  295. fd:    while ( is_digit(d, c) )
  296.     {    dval = dval * 10 + d;
  297.         exp10--;
  298.         ngetc(c, sp, c = EOFC);
  299.     }
  300. fs:    if ( sign < 0 )
  301.         dval = -dval;
  302. fe:    /* Now dval contains the value, negated if necessary. */
  303.     switch ( c )
  304.     {
  305.     case 'e': case 'E':
  306.     {    /* Check for a following exponent. */
  307.         int esign = 0;
  308.         int iexp;
  309.         ngetc(c, sp, return_error(e_syntaxerror));
  310.         switch ( c )
  311.         {
  312.         case '-':
  313.             esign = 1;
  314.         case '+':
  315.             ngetc(c, sp, return_error(e_syntaxerror));
  316.         }
  317.         /* Scan the exponent.  We limit it arbitrarily to 999. */
  318.         if ( !is_digit(d, c) )
  319.             return_error(e_syntaxerror);
  320.         iexp = d;
  321.         for ( ; ; iexp = iexp * 10 + d )
  322.         {    ngetc(c, sp, break);
  323.             if ( !is_digit(d, c) )
  324.             {    *psp = sp;
  325.                 code = 1;
  326.                 break;
  327.             }
  328.             if ( iexp > 99 )
  329.                 return_error(e_limitcheck);
  330.         }
  331.         if ( esign )
  332.             exp10 -= iexp;
  333.         else
  334.             exp10 += iexp;
  335.         break;
  336.     }
  337.     default:
  338.         *psp = sp;
  339.         code = 1;
  340.     case EOFC:
  341.         ;
  342.     }
  343.     /* Compute dval * 10^exp10. */
  344.     if ( exp10 > 0 )
  345.     {    while ( exp10 > num_powers_10 )
  346.             dval *= powers_10[num_powers_10],
  347.             exp10 -= num_powers_10;
  348.         if ( exp10 > 0 )
  349.             dval *= powers_10[exp10];
  350.     }
  351.     else if ( exp10 < 0 )
  352.     {    while ( exp10 < -num_powers_10 )
  353.             dval /= powers_10[num_powers_10],
  354.             exp10 += num_powers_10;
  355.         if ( exp10 < 0 )
  356.             dval /= powers_10[-exp10];
  357.     }
  358.     make_real_new(pref, dval);
  359.     return code;
  360. }
  361.