home *** CD-ROM | disk | FTP | other *** search
/ Dream 45 / Amiga_Dream_45.iso / Amiga / Magazine / Dossier-LaTeX / rtf2latex.lha / latex2rtf / stack.c < prev    next >
C/C++ Source or Header  |  1997-02-28  |  4KB  |  128 lines

  1. /*
  2.  * $Id: stack.c,v 1.1 1994/06/17 11:26:29 ralf Exp $
  3.  * History:
  4.  * $Log: stack.c,v $
  5.  * Revision 1.1  1994/06/17  11:26:29  ralf
  6.  * Initial revision
  7.  *
  8.  */
  9. /***************************************************************************
  10.      name : stack.c
  11.     autor : DORNER Fernando, GRANZER Andreas
  12.   purpose : this is an stack-model which handles the recursions-levels
  13.         occurred by environments, and open and closing-braces
  14.  ******************************************************************************/
  15.  
  16. /********************************* includes **********************************/
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. /******************************************************************************/
  20.  
  21.  
  22. /********************************** extern variables *************************/
  23. extern int BracketLevel;
  24. extern char *progname;
  25. extern char *latexname;
  26. extern long linenumber;
  27. /******************************************************************************/
  28.  
  29. /********************************* defines ***********************************/
  30. #define STACKSIZE 300
  31. /******************************************************************************/
  32.  
  33. /******************************** global variables *****************************/
  34. int stack[STACKSIZE];
  35. int top = 0;
  36. /******************************************************************************/
  37.  
  38. /******************************************************************************/
  39. int Push(int lev, int brack)
  40. /******************************************************************************
  41.   purpose: pushes the parameter lev and brack on the stack
  42. parameter: lev...level
  43.        brack...brackets
  44.  return: top of stack
  45.  ******************************************************************************/
  46. {
  47.   ++top;
  48.   stack[top] = lev;
  49.   ++top;
  50.   stack[top] = brack;
  51.  
  52.   if (top >= STACKSIZE)
  53.   {
  54.     fprintf(stderr,"\n%s: ERROR: too deep nesting -> internal stack-overflow",progname);
  55.     fprintf(stderr,"\nprogram aborted\n");
  56.     exit(-1);
  57.   }
  58.   return top;
  59. }
  60.  
  61. int Pop(int *lev, int *brack)
  62. /******************************************************************************
  63.   purpose: pops the parameter lev and brack from the stack
  64. parameter: lev...level
  65.        brack...brackets
  66.  return: top of stack
  67.  ******************************************************************************/
  68. {
  69.   *brack = stack[top];
  70.   --top;
  71.   *lev = stack[top];
  72.   --top;
  73.   if (top < 0)
  74.   {
  75.     fprintf(stderr,"\n%s: ERROR: error in LaTeX-File: %s at linenumber: %ld\n-> internal stack-overflow",progname,latexname,linenumber);
  76.     fprintf(stderr,"\nprogram aborted\n");
  77.     exit(-1);
  78.   }
  79.   return top;
  80. }
  81.  
  82.  
  83. /* The use of stack */
  84.  
  85. /*
  86. each stack elem consist of 2 integers RecursLevel and BracketLevel. Recurs-
  87. Level is the number of recursive calls of convert function, BracketLevel is
  88. the number of open curly braces. The value on top of stack represents the
  89. current value of the two global variables (RecursLevel and BracketLevel).
  90. Before every command and on an opening curly brace the current settings are
  91. written on the stack. On appearance of a closing curly brace the
  92. corresponding RecursLevel is found by search on the stack. It is the lowest
  93. RecursLevel with the same BracketLevel as now (after subtract of the 1
  94. closing brace found). The initial value RecLev 1, BracketLev 0 remains
  95. always on the stack. The begin document command Pushes 1,1
  96. examples:
  97. { Text {\em Text} Text }
  98. 1      2 3    4      5
  99. 1 Push 12
  100. 2 Push 13
  101. 3 Push 23
  102. 4 Bracket 3->2 Pop 23  Pop 13 Pop 12 Pop 11 -found- Push back 11
  103.   return to level 1
  104. 5 Bracket 2->1
  105.   return to level 1 = current -> no return
  106.  
  107. \mbox{\em Text}
  108. 1    2 3      4
  109. 1 Push 11  RecursLevel+1
  110. 2 Push 22
  111. 3 Push 32
  112. 4 Bracket 2->1 Pop 32 Pop 22 Pop 11 -found-
  113.   return to level 1 from level 3 -> double return from convert
  114.  
  115. The necessary Push before every command increases the stack size. If the
  116. commands don't include a recursiv call the stack is not cleaned up.
  117. After every TranslateCommand-function the stack is cleaned
  118. example
  119. \ldots \LaTeX \today \TeX
  120.  1    2      3      4
  121. 1 Push 11
  122. 2 Push 11
  123. 3 Push 11
  124. 4 Push 11
  125. The cleanup loop pops till the values are not ident and pushes back the last
  126. Therefore 11 is only 1 times on the stack.
  127. */
  128.