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 / ZSTACK.C < prev    next >
C/C++ Source or Header  |  1992-06-13  |  5KB  |  192 lines

  1. /* Copyright (C) 1989, 1991 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. /* zstack.c */
  21. /* Operand stack operators for Ghostscript */
  22. #include "memory_.h"
  23. #include "ghost.h"
  24. #include "errors.h"
  25. #include "oper.h"
  26. #include "store.h"
  27.  
  28. /* pop */
  29. int
  30. zpop(register os_ptr op)
  31. {    check_op(1);
  32.     pop(1);
  33.     return 0;
  34. }
  35.  
  36. /* exch */
  37. int
  38. zexch(register os_ptr op)
  39. {    ref next;
  40.     check_op(2);
  41.     ref_assign(&next, op - 1);
  42.     ref_assign(op - 1, op);
  43.     ref_assign(op, &next);
  44.     return 0;
  45. }
  46.  
  47. /* dup */
  48. int
  49. zdup(register os_ptr op)
  50. {    check_op(1);
  51.     push(1);
  52.     ref_assign(op, op - 1);
  53.     return 0;
  54. }
  55.  
  56. /* index */
  57. int
  58. zindex(register os_ptr op)
  59. {    os_ptr opn;
  60.     check_type(*op, t_integer);
  61.     if ( (ulong)op->value.intval >= op - osbot ) return e_rangecheck;
  62.     opn = op + ~(int)op->value.intval;
  63.     ref_assign(op, opn);
  64.     return 0;
  65. }
  66.  
  67. /* roll */
  68. int
  69. zroll(register os_ptr op)
  70. {    os_ptr op1 = op - 1;
  71.     int count, mod;
  72.     check_type(*op1, t_integer);
  73.     check_type(*op, t_integer);
  74.     if ( (ulong)op1->value.intval > op1 - osbot )
  75.       return e_rangecheck;
  76.     count = op1->value.intval;
  77.     if ( count == 0 ) { pop(2); return 0; }    /* no action */
  78.     mod = op->value.intval % count;
  79.     pop(2);                /* we're OK now */
  80.     op -= 2;
  81.     if ( mod < 0 ) mod += count;    /* can't assume % means mod! */
  82.     /* The elegant approach, requiring no extra space, would be to */
  83.     /* rotate the elements in chains separated by mod elements. */
  84.     /* Instead, we simply check to make sure there is enough space */
  85.     /* above op to do the roll in two block moves. */
  86.     /* Unfortunately, we can't count on memcpy doing the right thing */
  87.     /* in *either* direction. */
  88.     { register os_ptr from, to;
  89.       register int n;
  90.       if ( mod == 1 )    /* common special case */
  91.         { for ( from = op, n = count; n--; from-- )
  92.         from[1] = *from;
  93.           from[1] = op[1];
  94.         }
  95.       else if ( mod <= count >> 1)
  96.         { /* Move everything up, then top elements down. */
  97.           if ( mod >= ostop - op )
  98.         return e_stackoverflow;
  99.           for ( to = op + mod, from = op, n = count; n--; to--, from-- )
  100.         *to = *from;
  101.           memcpy((char *)(from + 1), (char *)(op + 1), mod * sizeof(ref));
  102.         }
  103.       else
  104.         { /* Move bottom elements up, then everything down. */
  105.           os_ptr base = op - count + 1;
  106.           mod = count - mod;
  107.           if ( mod == 1 )    /* common special case */
  108.         { op[1] = *base;
  109.           for ( to = base, n = count; n--; to++ )
  110.             *to = to[1];
  111.         }
  112.           else
  113.         { if ( mod >= ostop - op )
  114.             return e_stackoverflow;
  115.           memcpy((char *)(op + 1), (char *)base, mod * sizeof(ref));
  116.           for ( to = base, from = base + mod, n = count; n--; to++, from++ )
  117.             *to = *from;
  118.         }
  119.         }
  120.     }
  121.     return 0;
  122. }
  123.  
  124. /* clear */
  125. /* The function name is changed, because the IRIS library has */
  126. /* a function called zclear. */
  127. int
  128. zclear_stack(os_ptr op)
  129. {    osp = osbot - 1;
  130.     return 0;
  131. }
  132.  
  133. /* count */
  134. int
  135. zcount(register os_ptr op)
  136. {    push(1);
  137.     make_int(op, op - osbot);
  138.     return 0;
  139. }
  140.  
  141. /* mark */
  142. int
  143. zmark(register os_ptr op)
  144. {    push(1);
  145.     make_t(op, t_mark);
  146.     return 0;
  147. }
  148.  
  149. /* cleartomark */
  150. int
  151. zcleartomark(register os_ptr op)
  152. {    while ( op >= osbot )
  153.        {    if ( r_has_type(op, t_mark) )
  154.            {    osp = op - 1;
  155.             return 0;
  156.            }
  157.         op--;
  158.        }
  159.     return e_unmatchedmark;
  160. }
  161.  
  162. /* counttomark */
  163. int
  164. zcounttomark(os_ptr op)
  165. {    register os_ptr mp = op;
  166.     while ( mp >= osbot )
  167.        {    if ( r_has_type(mp, t_mark) )
  168.            {    push(1);
  169.             make_int(op, op - mp - 1);
  170.             return 0;
  171.            }
  172.         mp--;
  173.        }
  174.     return e_unmatchedmark;
  175. }
  176.  
  177. /* ------ Initialization procedure ------ */
  178.  
  179. op_def zstack_op_defs[] = {
  180.     {"0clear", zclear_stack},
  181.     {"0cleartomark", zcleartomark},
  182.     {"0count", zcount},
  183.     {"0counttomark", zcounttomark},
  184.     {"1dup", zdup},
  185.     {"2exch", zexch},
  186.     {"2index", zindex},
  187.     {"0mark", zmark},
  188.     {"1pop", zpop},
  189.     {"2roll", zroll},
  190.     op_def_end(0)
  191. };
  192.