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 / GSUTIL.C < prev    next >
C/C++ Source or Header  |  1992-09-08  |  5KB  |  157 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. /* gsutil.c */
  21. /* Utilities for Ghostscript library */
  22. #include "std.h"
  23. #include "string_.h"
  24. #include "memory_.h"
  25. #include "gsprops.h"
  26. #include "gsutil.h"            /* for prototype checking */
  27.  
  28. /* Export the size of a gs_prop_item for clients. */
  29. const uint gs_prop_item_sizeof = sizeof(gs_prop_item);
  30.  
  31. /* ------ String utilities ------ */
  32.  
  33. /* Compare two strings, returning -1 if the first is less, */
  34. /* 0 if they are equal, and 1 if first is greater. */
  35. /* We can't use memcmp, because we always use unsigned characters. */
  36. int
  37. bytes_compare(const byte *s1, uint len1, const byte *s2, uint len2)
  38. {    register uint len = len1;
  39.     if ( len2 < len ) len = len2;
  40.        {    register const byte *p1 = s1;
  41.         register const byte *p2 = s2;
  42.         while ( len-- )
  43.             if ( *p1++ != *p2++ )
  44.                 return (p1[-1] < p2[-1] ? -1 : 1);
  45.        }
  46.     /* Now check for differing lengths */
  47.     return (len1 == len2 ? 0 : len1 < len2 ? -1 : 1);
  48. }
  49.  
  50. /* Test whether a string matches a pattern with wildcards. */
  51. /* '*' = any substring, '?' = any character, '\' quotes next character. */
  52. int
  53. string_match(const byte *str, uint len, const byte *pstr, uint plen,
  54.   int ignore_case)
  55. {    const byte *pback = 0;
  56.     const byte *p = pstr, *pend = pstr + plen;
  57.     const byte *sp = str, *spend = str + len;
  58.     uint matched = 0;
  59.     while ( p < pend )
  60.        {    byte ch = *p;
  61.         switch ( ch )
  62.            {
  63.         case '*':
  64.             pback = ++p, matched = 0;
  65.             continue;
  66.         case '?':
  67.             if ( sp == spend ) return 0;    /* str too short */
  68.             p++, sp++, matched++;
  69.             continue;
  70.         case '\\':
  71.             if ( ++p == pend ) return 1;    /* bad pattern */
  72.             ch = *p;
  73.            }
  74.         if ( sp == spend ) return 0;    /* str too short */
  75.         if ( *sp == ch || ignore_case && (*sp ^ ch) == 0x20 &&
  76.              (ch &= ~0x20) >= 0x41 && ch <= 0x5a
  77.            )
  78.             p++, sp++, matched++;
  79.         else if ( pback == 0 )
  80.             return 0;    /* no * to back up to */
  81.         else
  82.            {    sp += 1 - matched;
  83.             p = pback;
  84.             matched = 0;
  85.            }
  86.        }
  87.     return 1;
  88. }
  89.  
  90. /* Compute a hash for a string */
  91. uint
  92. string_hash(const byte *ptr, uint len)
  93. {    register const byte *p = ptr;
  94.     register uint hash = 0;
  95.     register uint n = len;
  96.     while ( n-- ) hash = hash * 19 + *p++;
  97.     return hash;
  98. }
  99.  
  100. /* ------ Property list utilities ------ */
  101.  
  102. /* Extract known properties from a list.  See gsutil.h for more details. */
  103. int
  104. props_extract(gs_prop_item *plist, int count,
  105.   const gs_prop_item *template, int tcount,
  106.   gs_prop_item **pknown, int finished)
  107. {    const gs_prop_item *pti;
  108.     gs_prop_item **ppi;
  109.     gs_prop_item *pli;
  110.     int i, j;
  111.     for ( pti = template, ppi = pknown, j = 0; j < tcount; pti++, ppi++, j++ )
  112.        {    const char *tstr = pti->pname;
  113.         int tlen = pti->name_size;
  114.         if ( tstr == 0 ) continue;    /* no name */
  115.         if ( tlen < 0 ) tlen = strlen(tstr);
  116.         *ppi = 0;
  117.         for ( pli = plist, i = 0; i < count; pli++, i++ )
  118.           if ( pli->status == pv_set )
  119.            {    const char *lstr = pli->pname;
  120.             int llen = pli->name_size;
  121.             if ( lstr == 0 ) continue;
  122.             if ( llen < 0 ) llen = strlen(lstr);
  123.             if ( llen == tlen && !memcmp(tstr, lstr, llen) )
  124.                {    /* Names match, check types. */
  125.                 if ( pli->type != pti->type )
  126.                  { /* Check for int [array] -> float [array] */
  127.                    if ( pli->type == prt_int &&
  128.                     pti->type == prt_float
  129.                       )
  130.                      pli->type = prt_float,
  131.                      pli->value.f = pli->value.i;
  132.                    else if ( pli->type == prt_int_array &&
  133.                          pti->type == prt_float_array
  134.                        )
  135.                     { int i;
  136.                       gs_prop_item *vp = pli->value.a.p.v;
  137.                       pli->type = prt_float_array;
  138.                       for ( i = pli->value.a.size; --i >= 0; vp++ )
  139.                     vp->type = prt_float,
  140.                     vp->value.f = vp->value.i;
  141.                     }
  142.                    else
  143.                     { pli->status = pv_typecheck;
  144.                       break;
  145.                     }
  146.                  }
  147.                 *ppi = pli, pli->status = pv_OK;
  148.                 break;
  149.                }
  150.            }
  151.        }
  152.     if ( finished )
  153.       for ( pli = plist, i = 0; i < count; pli++, i++ )
  154.         if ( pli->status != pv_OK ) pli->status = pv_unknown;
  155.     return 0;
  156. }
  157.