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

  1. /* Copyright (C) 1989, 1990, 1991, 1992 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. /* zstring.c */
  20. /* String operators */
  21. #include "memory_.h"
  22. #include "ghost.h"
  23. #include "errors.h"
  24. #include "gsutil.h"
  25. #include "ialloc.h"
  26. #include "iname.h"
  27. #include "ivmspace.h"            /* for ialloc_local_attr */
  28. #include "oper.h"
  29. #include "store.h"
  30.  
  31. /* The generic operators (copy, get, put, getinterval, putinterval, */
  32. /* length, and forall) are implemented in zgeneric.c. */
  33.  
  34. /* <int> string <string> */
  35. int
  36. zstring(register os_ptr op)
  37. {    byte *sbody;
  38.     uint size;
  39.     check_int_leu(*op, max_string_size);
  40.     size = op->value.intval;
  41.     sbody = ialloc_string(size, "string");
  42.     if ( sbody == 0 )
  43.         return_error(e_VMerror);
  44.     make_string(op, a_all | ilocal_attr, size, sbody);
  45.     memset(sbody, 0, size);
  46.     return 0;
  47. }
  48.  
  49. /* <string> <pattern> anchorsearch <post> <match> -true- */
  50. /* <string> <pattern> anchorsearch <string> -false- */
  51. int
  52. zanchorsearch(register os_ptr op)
  53. {    os_ptr op1 = op - 1;
  54.     uint size = r_size(op);
  55.     check_read_type(*op1, t_string);
  56.     check_read_type(*op, t_string);
  57.     if ( size <= r_size(op1) && !memcmp(op1->value.bytes, op->value.bytes, size) )
  58.     {    os_ptr op0 = op;
  59.         push(1);
  60.         *op0 = *op1;
  61.         r_set_size(op0, size);
  62.         op1->value.bytes += size;
  63.         r_dec_size(op1, size);
  64.         make_true(op);
  65.     }
  66.     else
  67.         make_false(op);
  68.     return 0;
  69. }
  70.  
  71. /* <string> <pattern> search <post> <match> <pre> -true- */
  72. /* <string> <pattern> search <string> -false- */
  73. int
  74. zsearch(register os_ptr op)
  75. {    os_ptr op1 = op - 1;
  76.     uint size = r_size(op);
  77.     uint count;
  78.     byte *ptr;
  79.     check_read_type(*op1, t_string);
  80.     check_read_type(*op, t_string);
  81.     if ( size > r_size(op1) )        /* can't match */
  82.        {    make_false(op);
  83.         return 0;
  84.        }
  85.     count = r_size(op1) - size;
  86.     ptr = op1->value.bytes;
  87.     do
  88.        {    if ( !memcmp(ptr, op->value.bytes, size) )
  89.            {    op->tas.type_attrs = op1->tas.type_attrs;
  90.             op->value.bytes = ptr;
  91.             r_set_size(op, size);
  92.             push(2);
  93.             op[-1] = *op1;
  94.             r_set_size(op - 1, ptr - op[-1].value.bytes);
  95.             op1->value.bytes = ptr + size;
  96.             r_set_size(op1, count);
  97.             make_true(op);
  98.             return 0;
  99.            }
  100.         ptr++;
  101.        }
  102.     while ( count-- );
  103.     /* No match */
  104.     make_false(op);
  105.     return 0;
  106. }
  107.  
  108. /* <obj> <pattern> .stringmatch <bool> */
  109. int
  110. zstringmatch(register os_ptr op)
  111. {    os_ptr op1 = op - 1;
  112.     bool result;
  113.     check_read_type(*op, t_string);
  114.     switch ( r_type(op1) )
  115.        {
  116.     case t_string:
  117.         check_read(*op1);
  118.         goto cmp;
  119.     case t_name:
  120.         name_string_ref(op1, op1);        /* can't fail */
  121. cmp:        result = string_match(op1->value.const_bytes, r_size(op1),
  122.                       op->value.const_bytes, r_size(op),
  123.                       NULL);
  124.         break;
  125.     default:
  126.         result = (r_size(op) == 1 && *op->value.bytes == '*');
  127.        }
  128.     make_bool(op1, result);
  129.     pop(1);
  130.     return 0;
  131. }
  132.  
  133. /* ------ Initialization procedure ------ */
  134.  
  135. op_def zstring_op_defs[] = {
  136.     {"2anchorsearch", zanchorsearch},
  137.     {"2search", zsearch},
  138.     {"1string", zstring},
  139.     {"2.stringmatch", zstringmatch},
  140.     op_def_end(0)
  141. };
  142.