home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_100 / 181_01 / libes.c < prev    next >
Text File  |  1986-01-09  |  7KB  |  195 lines

  1.  
  2. Reprinted from: Micro/Systems Journal, Volume 1. No. 2. May/June 1985
  3. Article Title: "C Forum - Writing A Translation Program In C"
  4. Author: Alex Soya
  5. -----------------------------------------------------------------
  6. Copy of back issue may be obtained for $4.50 (foreign $6) from:
  7. Subscriptions: $20/yr & $35/2 yrs domestic; published bimonthly
  8. Micro/Systems Journal
  9. Box 1192
  10. Mountainside NJ 07092
  11. -----------------------------------------------------------------
  12. Copyright 1986
  13. Micro/Systems Journal, Box 1192, Mountainside NJ 07092
  14. This software is released into the public domain for
  15.  non-commercial use only.
  16. -----------------------------------------------------------------
  17.  
  18. ▒  #includσ <stdio.h>
  19. 2  #include <ctype.h>
  20.  
  21. 4  #define TRUE        1
  22. 5  #define FALSE        0
  23.  
  24. 7  #define NL        '\n'
  25. 8  #define CR        '\r'
  26. 9  #define SOFT_HYPHEN    0x1f    /* inserted to break words at eol */
  27.  
  28. 11  #define TABSIZE    8
  29.  
  30. 13  #define CONTROL(x)    ((x)-'a')
  31. 14  #define BOLD    (CONTROL('b'))
  32. 15  #define SUPERSCRIPT    (CONTROL('t'))
  33. 16  #define SUBSCRIPT    (CONTROL('v'))
  34. 17  #define UNDERSCORE    (CONTROL('s'))
  35.  
  36. 19  #define BOLD_BEGIN        "<<<"
  37. 20  #define BOLD_END        ">>>"
  38. 21  #define UNDERSCORE_BEGIN    "<_"
  39. 22  #define UNDERSCORE_END    "_>"
  40. 23  #define SUBSCRIPT_BEGIN    "<<"
  41. 24  #define SUBSCRIPT_END    ">>"
  42. 25  #define SUPERSCRIPT_BEGIN    "<^"
  43. 26  #define SUPERSCRIPT_END    "^>"
  44.  
  45. 28  typedef int boolean;
  46.  
  47. 30  int column;        /* column to output next character */
  48. 31  int c;              /* last character read */
  49.  
  50. 33  boolean superscript = FALSE;/* if superscripting */
  51. 34  boolean subscript = FALSE;    /* if subscripting */
  52. 35  boolean bold = FALSE;    /* if emboldening */
  53. 36  boolean underscore = FALSE;    /* if underscoring */
  54. 37  boolean msb_was_set;    /* if char most significant bit set */
  55. 38  boolean soft_space = FALSE;    /* if between words and have seen */
  56. 39                  /* soft space, but no other spaces */
  57. 40  boolean soft_hyphen = FALSE;/* if in the middle of a word that */
  58. 41                  /* was hyphenated by ws */
  59. 42  boolean dotcmd = FALSE;    /* if in the middle of a dot cmd */
  60. 43  boolean wrap_soon = FALSE;    /* if should wrap as soon as we get */
  61. 44                  /* to the end of the current word */
  62.  
  63. 46  int spacerun = 0;        /* number of spaces seen in a row */
  64.  
  65. 48  main() {
  66. 49      while (TRUE) {
  67. 50          /* look at single characters */
  68. 51          msb_was_set = FALSE;
  69. 52          if (EOF == (c = getchar())) break;
  70. 53          if (!isascii(c)) {    /* is most sig. bit set? */
  71. 54              c = toascii(c);    /* turn of most sig. bit */è55              msb_was_set = TRUE;
  72. 56          }
  73. 57          if (c == CR) continue; /* always followed by an NL */
  74. 58          if (c == NL) {
  75. 59              if (soft_hyphen) {
  76. 60                  wrap_soon = TRUE;
  77. 61              } else newline();
  78. 62          } else if (dotcmd) {
  79. 63              /* throw away chars if we are in a dot command */
  80. 64              continue;
  81. 65          } else if (c == ' ') {
  82. 66              /* ignore blanks with msb set - they are soft */
  83. 67              if (!msb_was_set) {    /* a real space */
  84. 68                  soft_space = FALSE;
  85. 69                  spacerun++;
  86. 70                  column++;
  87. 71                  if (wrap_soon) { /* wrap now! */
  88. 72                      newline();
  89. 73                      wrap_soon = FALSE;
  90. 74                  }
  91. 75              } else soft_space = TRUE;
  92. 76          } else if (c == SOFT_HYPHEN) {
  93. 77              /* ignore hyphens with msb set - they are soft */
  94. 78              soft_hyphen = TRUE;
  95. 79          } else if (c == '.' && column == 0) {
  96. 80              /* text processing directive */
  97. 81              dotcmd = TRUE;
  98. 82          } else if (iswscntrl(c)) {
  99. 83              /* ignore wordstar print control characters */
  100. 84              /* e.g. ^S (underscore), ^B (bold) */
  101. 85              wscntrl(c);
  102. 86          } else if (iscntrl(c)) {
  103. 87              /* unknown control character - ignore */
  104. 88              continue;
  105. 89          } else { /* normal character */
  106. 90              /* if we encountered a soft space, stick in at */
  107. 91              /* least one space */
  108. 92              if (soft_space) {
  109. 93                  spacerun = 1;
  110. 94                  column++;
  111. 95                  soft_space = FALSE;
  112. 96              }
  113. 97              if (spacerun) { /* beginning of word */
  114. 98                  /* calculate tabs and blanks to lay down */
  115. 99          space_out(column-spacerun,column);
  116. 100                  spacerun = 0;
  117. 101              }
  118. 102              putchar(c);
  119. 103              column++;
  120. 104          }
  121. 105      }
  122. 106  }
  123.  
  124. 108  /* print least number of spaces & tabs to move from "oldpos" */
  125. 109  /* to "newpos" */è110  space_out(oldpos,curpos)
  126. 111  int oldpos;    /* old position */
  127. 112  int newpos;    /* new position */
  128. 113  {
  129. 114      int spaces, tabs;   /* number of spaces & tabs to print */
  130. 115      int i;
  131.  
  132. 117      if (oldpos >= newpos) return;    /* no space in between */
  133.  
  134. 119      /* first calculate tabs */
  135. 120      tabs = newpos/TABSIZE - oldpos/TABSIZE;
  136.  
  137. 122      /* now calulate spaces */
  138. 123      /* if old & new at same tab stop, its just difference */
  139. 124      if (tabs == 0) spaces = newpos - oldpos;
  140. 125      /* if not, then its remainder from nearest tab stop */
  141. 126      else spaces = newpos % TABSIZE;
  142.  
  143. 128      for (i=0;i<tabs;i++) putchar('\t');
  144. 129      for (i=0;i<spaces;i++) putchar(' ');
  145. 130  }
  146.  
  147. 132  /* true if wordstar control character */
  148. 133  boolean
  149. 134  iswscntrl(c)
  150. 135  int c;
  151. 136  {
  152. 137      if ((c == BOLD) ||
  153. 138         (c == UNDERSCORE) ||
  154. 139         (c == SUPERSCRIPT) ||
  155. 140         (c == SUBSCRIPT)) return(TRUE);
  156. 141      else return(FALSE);
  157. 142  }
  158.  
  159. 144  newline()
  160. 145  {
  161. 146      if (!dotcmd) putchar('\n');
  162. 147      column = 0;
  163. 148      spacerun = 0;
  164. 149      dotcmd = FALSE;
  165. 150  }
  166.  
  167. 152  wscntrl(c)
  168. 153  char c;
  169. 154  {
  170. 155      switch (c) {    /* print control character */
  171. 156      case BOLD:
  172. 157          if (bold) printf(BOLD_END);
  173. 158          else printf(BOLD_BEGIN);
  174. 159          bold = !bold;
  175. 160          break;
  176. 161      case UNDERSCORE:
  177. 162          if (underscore) printf(UNDERSCORE_END);
  178. 163          else printf(UNDERSCORE_BEGIN);
  179. 164          underscore = !underscore;è165          break;
  180. 166      case SUBSCRIPT:
  181. 167          if (subscript) printf(SUBSCRIPT_END);
  182. 168          else printf(SUBSCRIPT_BEGIN);
  183. 169          subscript = !subscript;
  184. 170          break;
  185. 171      case SUPERSCRIPT:
  186. 172          if (superscript) printf(SUPERSCRIPT_END);
  187. 173          else printf(SUPERSCRIPT_BEGIN);
  188. 174          superscript = !superscript;
  189. 175          break;
  190. 176      default:
  191. 177          /* unknown - ignore for now */
  192. 178          break;
  193. 179      }
  194. 180  }
  195.