home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-387-Vol-3of3.iso / g / gs252src.zip / GS252 / IDPARAM.C < prev    next >
C/C++ Source or Header  |  1992-06-21  |  5KB  |  171 lines

  1. /* Copyright (C) 1992 Aladdin Enterprises.  All rights reserved.
  2.    Distributed by Free Software Foundation, Inc.
  3.  
  4. This file is part of Ghostscript.
  5.  
  6. Ghostscript is distributed in the hope that it will be useful, but
  7. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  8. to anyone for the consequences of using it or for whether it serves any
  9. particular purpose or works at all, unless he says so in writing.  Refer
  10. to the Ghostscript General Public License for full details.
  11.  
  12. Everyone is granted permission to copy, modify and redistribute
  13. Ghostscript, but only under the conditions described in the Ghostscript
  14. General Public License.  A copy of this license is supposed to have been
  15. given to you along with Ghostscript so you can know your rights and
  16. responsibilities.  It should be in a file named COPYING.  Among other
  17. things, the copyright notice and this notice must be preserved on all
  18. copies.  */
  19.  
  20. /* idparam.c */
  21. /* Utilities for getting parameters out of dictionaries. */
  22. #include "memory_.h"
  23. #include "ghost.h"
  24. #include "dict.h"
  25. #include "gsmatrix.h"        /* for dict_matrix_param */
  26. #include "dparam.h"        /* interface definition */
  27. #include "errors.h"
  28. #include "iutil.h"        /* for num_params */
  29.  
  30. /* Get a Boolean parameter from a dictionary. */
  31. /* Return 0 if found, 1 if defaulted, <0 if wrong type. */
  32. int
  33. dict_bool_param(const ref *pdict, const ref *pname, int defaultval, int *pvalue)
  34. {    ref *pdval;
  35.     if ( dict_find(pdict, pname, &pdval) <= 0 )
  36.        {    *pvalue = defaultval;
  37.         return 1;
  38.        }
  39.     if ( !r_has_type(pdval, t_boolean) ) return e_typecheck;
  40.     *pvalue = pdval->value.index;
  41.     return 0;
  42. }        
  43.  
  44. /* Get an integer parameter from a dictionary. */
  45. /* Return 0 if found, 1 if defaulted, <0 if missing or out of range. */
  46. /* Note that the default value may be out of range, in which case */
  47. /* a missing value will return e_rangecheck rather than 1. */
  48. int
  49. dict_int_param(const ref *pdict, const ref *pname, int minval, int maxval,
  50.   int defaultval, int *pvalue)
  51. {    int code;
  52.     ref *pdval;
  53.     long ival;
  54.     if ( dict_find(pdict, pname, &pdval) <= 0 )
  55.        {    ival = defaultval;
  56.         code = 1;
  57.        }
  58.     else
  59.        {    switch ( r_type(pdval) )
  60.         {
  61.         case t_integer:
  62.             ival = pdval->value.intval;
  63.             break;
  64.         case t_real:
  65.             /* Allow an integral real, because Fontographer */
  66.             /* (which violates the Adobe specs in other ways */
  67.             /* as well) sometimes generates output that */
  68.             /* needs this. */
  69.             if ( pdval->value.realval < minval || pdval->value.realval > maxval )
  70.                 return e_rangecheck;
  71.             ival = (long)pdval->value.realval;
  72.             if ( ival != pdval->value.realval )
  73.                 return e_rangecheck;
  74.             break;
  75.         default:
  76.             return e_typecheck;
  77.         }
  78.         code = 0;
  79.        }
  80.     if ( ival < minval || ival > maxval )
  81.         return e_rangecheck;
  82.     *pvalue = (int)ival;
  83.     return code;
  84. }        
  85.  
  86. /* Get a float parameter from a dictionary. */
  87. /* Return 0 if found, 1 if defaulted, <0 if wrong type. */
  88. int
  89. dict_float_param(const ref *pdict, const ref *pname, floatp defaultval, float *pvalue)
  90. {    ref *pdval;
  91.     if ( dict_find(pdict, pname, &pdval) <= 0 )
  92.        {    *pvalue = defaultval;
  93.         return 1;
  94.        }
  95.     switch ( r_type(pdval) )
  96.        {
  97.     case t_integer: *pvalue = pdval->value.intval; return 0;
  98.     case t_real: *pvalue = pdval->value.realval; return 0;
  99.        }
  100.     return e_typecheck;
  101. }        
  102.  
  103. /* Get an integer array from a dictionary. */
  104. /* Return the element count if OK, 0 if missing, <0 if invalid. */
  105. int
  106. dict_int_array_param(const ref *pdict, const ref *pname, uint maxlen, int *ivec)
  107. {    ref *pdval;
  108.     const ref *pa;
  109.     int *pi = ivec;
  110.     uint size;
  111.     int i;
  112.     if ( dict_find(pdict, pname, &pdval) <= 0 ) return 0;
  113.     if ( !r_has_type(pdval, t_array) ) return e_typecheck;
  114.     size = r_size(pdval);
  115.     if ( size > maxlen ) return e_limitcheck;
  116.     pa = pdval->value.const_refs;
  117.     for ( i = 0; i < size; i++, pa++, pi++ )
  118.        {    /* See dict_int_param above for why we allow reals here. */
  119.         switch ( r_type(pa) )
  120.         {
  121.         case t_integer:
  122.             if ( pa->value.intval != (int)pa->value.intval )
  123.                 return e_rangecheck;
  124.             *pi = (int)pa->value.intval;
  125.             break;
  126.         case t_real:
  127.             /* We don't have max_int and min_int.... */
  128.             if ( pa->value.realval < -(int)(max_uint>>1)-1 ||
  129.                  pa->value.realval > max_uint>>1 ||
  130.                  pa->value.realval != (int)pa->value.realval
  131.                )
  132.                 return e_rangecheck;
  133.             *pi = (int)pa->value.realval;
  134.             break;
  135.         default:
  136.             return e_typecheck;
  137.         }
  138.        }
  139.     return size;
  140. }        
  141.  
  142. /* Get a float array from a dictionary. */
  143. /* Return the element count if OK, <0 if invalid. */
  144. /* If the parameter is missing, then if defaultvec is NULL, return 0; */
  145. /* if defaultvec is not NULL, copy it into fvec (maxlen elements) */
  146. /* and return maxlen. */
  147. int
  148. dict_float_array_param(const ref *pdict, const ref *pname, uint maxlen, float *fvec, float *defaultvec)
  149. {    ref *pdval;
  150.     uint size;
  151.     int code;
  152.     if ( dict_find(pdict, pname, &pdval) <= 0 )
  153.     {    if ( defaultvec == NULL ) return 0;
  154.         memcpy(fvec, defaultvec, maxlen * sizeof(float));
  155.         return maxlen;
  156.     }
  157.     if ( !r_has_type(pdval, t_array) ) return e_typecheck;
  158.     size = r_size(pdval);
  159.     if ( size > maxlen ) return e_limitcheck;
  160.     code = num_params(pdval->value.refs + size - 1, size, fvec);
  161.     return (code >= 0 ? size : code);
  162. }        
  163.  
  164. /* Get a matrix from a dictionary. */
  165. int
  166. dict_matrix_param(const ref *pdict, const ref *pname, gs_matrix *pmat)
  167. {    ref *pdval;
  168.     if ( dict_find(pdict, pname, &pdval) <= 0 ) return e_typecheck;
  169.     return read_matrix(pdval, pmat);
  170. }
  171.