home *** CD-ROM | disk | FTP | other *** search
/ APDL Public Domain 1 / APDL_PD1A.iso / printing / ghostscrip / source / _gs / c / zstack < prev    next >
Encoding:
Text File  |  1991-10-25  |  3.8 KB  |  168 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 "ghost.h"
  23. #include "errors.h"
  24. #include "oper.h"
  25. #include "store.h"
  26.  
  27. /* pop */
  28. int
  29. zpop(register os_ptr op)
  30. {    check_op(1);
  31.     pop(1);
  32.     return 0;
  33. }
  34.  
  35. /* exch */
  36. int
  37. zexch(register os_ptr op)
  38. {    ref next;
  39.     check_op(2);
  40.     ref_assign(&next, op - 1);
  41.     ref_assign(op - 1, op);
  42.     ref_assign(op, &next);
  43.     return 0;
  44. }
  45.  
  46. /* dup */
  47. int
  48. zdup(register os_ptr op)
  49. {    check_op(1);
  50.     push(1);
  51.     ref_assign(op, op - 1);
  52.     return 0;
  53. }
  54.  
  55. /* index */
  56. int
  57. zindex(register os_ptr op)
  58. {    os_ptr opn;
  59.     check_type(*op, t_integer);
  60.     if ( (ulong)op->value.intval >= op - osbot ) return e_rangecheck;
  61.     opn = op + ~(int)op->value.intval;
  62.     ref_assign(op, opn);
  63.     return 0;
  64. }
  65.  
  66. /* roll */
  67. int
  68. zroll(register os_ptr op)
  69. {    os_ptr op1 = op - 1;
  70.     int count, mod;
  71.     int istart, n;
  72.     os_ptr base;
  73.     check_type(*op1, t_integer);
  74.     check_type(*op, t_integer);
  75.     if ( (ulong)op1->value.intval > op1 - osbot )
  76.         return e_rangecheck;
  77.     count = op1->value.intval;
  78.     if ( count == 0 ) { pop(2); return 0; }    /* no action */
  79.     mod = op->value.intval % count;
  80.     pop(2);                /* we're OK now */
  81.     op -= 2;
  82.     if ( mod < 0 )            /* can't assume % means mod! */
  83.         mod += count;
  84.     else if ( mod == 0 ) return 0;    /* no action */
  85.     /* Rotate the elements in chains separated by mod elements. */
  86.     /* The number of chains is gcd(count,mod): rather than compute */
  87.     /* this, we simply proceed until we have moved count elements. */
  88.     /* Since the loop interprets mod backwards from the way PostScript */
  89.     /* specifies it, we have to complement mod first. */
  90.     mod = count - mod;
  91.     istart = 0;            /* first element of chain */
  92.     n = count;            /* # of elements left to move */
  93.     base = op - count + 1;
  94.     while ( n )
  95.        {    ref save;
  96.         int i = istart;
  97.         int inext;
  98.         save = base[istart];    /* no auto init for structures! */
  99.         while ( n--, (inext = (i + mod) % count) != istart )
  100.            {    base[i] = base[inext];
  101.             i = inext;
  102.            }
  103.         base[i] = save;
  104.         istart++;
  105.        }
  106.     return 0;
  107. }
  108.  
  109. /* clear */
  110. /* The function name is changed, because the IRIS library has */
  111. /* a function called zclear. */
  112. int
  113. zclear_stack(os_ptr op)
  114. {    osp = osbot - 1;
  115.     return 0;
  116. }
  117.  
  118. /* count */
  119. int
  120. zcount(register os_ptr op)
  121. {    push(1);
  122.     make_int(op, op - osbot);
  123.     return 0;
  124. }
  125.  
  126. /* cleartomark */
  127. int
  128. zcleartomark(register os_ptr op)
  129. {    while ( op >= osbot )
  130.        {    if ( r_has_type(op, t_mark) )
  131.            {    osp = op - 1;
  132.             return 0;
  133.            }
  134.         op--;
  135.        }
  136.     return e_unmatchedmark;
  137. }
  138.  
  139. /* counttomark */
  140. int
  141. zcounttomark(os_ptr op)
  142. {    register os_ptr mp = op;
  143.     while ( mp >= osbot )
  144.        {    if ( r_has_type(mp, t_mark) )
  145.            {    push(1);
  146.             make_int(op, op - mp - 1);
  147.             return 0;
  148.            }
  149.         mp--;
  150.        }
  151.     return e_unmatchedmark;
  152. }
  153.  
  154. /* ------ Initialization procedure ------ */
  155.  
  156. op_def zstack_op_defs[] = {
  157.     {"0clear", zclear_stack},
  158.     {"0cleartomark", zcleartomark},
  159.     {"0count", zcount},
  160.     {"0counttomark", zcounttomark},
  161.     {"1dup", zdup},
  162.     {"2exch", zexch},
  163.     {"2index", zindex},
  164.     {"1pop", zpop},
  165.     {"2roll", zroll},
  166.     op_def_end(0)
  167. };
  168.