home *** CD-ROM | disk | FTP | other *** search
/ ARM Club 1 / ARM_CLUB_CD.iso / contents / apps / graphs / progs / gnuplot34 / !GnuPlot_Docs_c_doc2imp < prev    next >
Encoding:
Text File  |  1993-07-14  |  7.0 KB  |  288 lines

  1. /*
  2.  * doc2imp.c  -- program to convert Gnuplot .DOC format to impression
  3.  * textstory (Document Description) format.
  4.  *
  5.  * usage:  doc2imp file.doc textstory
  6.  *    or:  doc2imp < file.doc > textstory
  7.  *
  8.  * Bugs fixed & Output improved : 24/6/93 by Paul Field
  9.  * Namely :
  10.  *   Added 'VERBOSE' so that doc2imp with file indirection works OK.
  11.  *   No more spurious spaces at the beginning of paragraphs.
  12.  *   { is now encoded properly so that bits of text don't disappear.
  13.  *   ` characters are converted to ' which Impression can use for 'smart quotes'.
  14.  *   Tables now line up properly (but unfortunately don't use TABs).
  15.  *   Examples and syntax are now in a 'verbatim' style.
  16.  * N.B. The definition of 'basestyle' output appears to be ignored by
  17.  * Impression (V2.17).
  18.  *
  19.  * Bugs fixed & improvment: 08 Jul 1993 Erik van de Pol
  20.  * Namely:
  21.  *   Paul got the command line parsing wrong :- misplaced else (hi Paul!).
  22.  *   Improved chapter numbering.
  23.  */
  24.  
  25. #include <stdio.h>
  26. #include <ctype.h>
  27.  
  28. #define MAX_LINE_LEN    256
  29. #define BOOL int
  30. #define TRUE 1
  31. #define FALSE 0
  32. #define VERBOSE TRUE
  33.  
  34. void ddf_newstyle(FILE * outfile, const char *name, const char *fontname,
  35.               unsigned size, int spaceabove);
  36. void process_line(char *line, FILE * f);
  37. void convert(FILE * a, FILE * f);
  38. void section(char *line, FILE * f);
  39. void verbatimline(char *line, FILE * f);
  40. void outputline(char *line, FILE * f);
  41. void outputchar(char c, FILE * f);
  42. void troffline(char *line, FILE * f);
  43.  
  44.  
  45. static int chapter[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  46.  
  47.  
  48. int main(int argc, char *argv[])
  49. {
  50.     FILE *infile;
  51.     FILE *outfile;
  52.  
  53.     infile = stdin;
  54.     outfile = stdout;
  55.     if (argc > 3 || argc < 2) {
  56.     fprintf(stderr, "Usage: %s infile outfile\n", argv[0]);
  57.     return 1;
  58.     }
  59.     if (argc >= 2) {
  60.     if ((infile = fopen(argv[1], "r")) == NULL) {
  61.         fprintf(stderr, "%s: Can't open %s for reading\n", argv[0], argv[1]);
  62.         return 1;
  63.     }
  64.     if (argc == 3) {
  65.         if ((outfile = fopen(argv[2], "w")) == NULL) {
  66.         fprintf(stderr, "%s: Can't open %s for writing\n", argv[0], argv[2]);
  67.         return 1;
  68.         }
  69.     }
  70. #if VERBOSE
  71.     else {
  72.         fprintf(stderr, "%s: Can't use stdout for output - recompile without VERBOSE\n", argv[0]);
  73.         return 1;
  74.     }
  75. #endif
  76.     }
  77.  
  78.     /* init impression */
  79.     fprintf(outfile, "{define style \"BaseStyle\"; font Trinity.Medium; fontsize 12pt; spaceabove 10pt; spacebelow 0pt; leftmargin 1.8pt; rightmargin 0pt; returnmargin 1.8pt; condframethrow 0pt; rulewidth 0pt; ruleoffset 0pt; fontaspect 100%%; fontcolour rgb=(0.000,0.000,0.000); linecolour rgb=(0.000,0.000,0.000); rulecolour rgb=(0.000,0.000,0.000); justify left; underline 0; strikeout off; script off; hyphenate off; leader \"\"; locktolinespace off; tabs 72pt,144pt,216pt,288pt,360pt,432pt,504pt,576pt,648pt}");
  80.  
  81.     ddf_newstyle(outfile,    "Item1",    "Homerton.Bold",    32, 10);
  82.     ddf_newstyle(outfile,    "Item2",    "Homerton.Bold",    18, 10);
  83.     ddf_newstyle(outfile,    "Item3",    "Homerton.Medium",    18, 10);
  84.     ddf_newstyle(outfile,    "Item4",    "Homerton.Medium",    14, 10);
  85.     ddf_newstyle(outfile,    "Item5",    "Homerton.Medium",    12, 10);
  86.     ddf_newstyle(outfile,    "Table",    "Homerton.Medium",    10, 0);
  87.     ddf_newstyle(outfile,    "Table header",    "Homerton.Bold",    10, 0);
  88.     ddf_newstyle(outfile,    "Verbatim",    "Corpus.Medium",    10, 0);
  89.     convert(infile, outfile);
  90.     return 0;
  91. }
  92.  
  93.  
  94. void ddf_newstyle(FILE * outfile, const char *name, const char *fontname,
  95.               unsigned size, int spaceabove)
  96. {
  97.     fprintf(outfile, "{define style \"%s\"; font %s; fontsize %dpt; spaceabove %dpt}",
  98.         name, fontname, size, spaceabove);
  99. }
  100.  
  101.  
  102. void convert(FILE * a, FILE * b)
  103. {
  104.     static char line[MAX_LINE_LEN];
  105.  
  106.     while (fgets(line, MAX_LINE_LEN, a)) {
  107.     process_line(line, b);
  108.     }
  109. }
  110.  
  111.  
  112. void tidy(char *line)
  113. {
  114.     int pos;
  115.  
  116.     for (pos = 0; line[pos] != NULL; pos++) {
  117.     if (line[pos] < ' ') {
  118.         line[pos] = ' ';
  119.     }
  120.     }
  121.     for (pos--; line[pos] == ' ' && pos > 0; pos--) {
  122.     line[pos] = NULL;
  123.     }
  124. }
  125.  
  126.  
  127. void process_line(char *line, FILE * f)
  128. {
  129.     static int line_count = 0;
  130.     static BOOL table_mode = 0;
  131.     static BOOL plain_text = 0;
  132.  
  133.     line_count++;
  134.     tidy(line);
  135.  
  136.     /*
  137.      * I think this checks for an 'end of paragraph', I have no idea what 'plain_text' stands for but it
  138.      * seems to signal when some text is output (so you don't output more than 1 newline in a row).
  139.      */
  140.     if (plain_text && (line[0] != ' ' || line[1] == ' ' || line[1] == NULL)) {
  141.     plain_text = FALSE;
  142.     fputc('\n', f);
  143.     }
  144.     /* Deal with the control character */
  145.     switch (line[0]) {
  146.     case '?':
  147.     break;            /* ignore */
  148.  
  149.     case '@':            /* start/end table */
  150.     {
  151.         if (!table_mode) {
  152.         table_mode = TRUE;
  153.         fprintf(f, "{\"Table\" on}");
  154.         } else {
  155.         table_mode = FALSE;
  156.         fprintf(f, "{\"Table\" off}\n");
  157.         }
  158.         break;
  159.     }
  160.  
  161.     case '#':
  162.     break;            /* latex table entry - ignore */
  163.     case '%':
  164.     if (line[1] != NULL) {
  165.         troffline(line + 1, f);
  166.     }
  167.     break;            /* troff table entry - convert this to something useful */
  168.     case '\n':            /* empty text line */
  169.     case ' ':            /* normal text line */
  170.     {
  171.         if (table_mode) {
  172.         break;        /* inore this - we already use the troff table entry */
  173.         } else {
  174.         if (line[1] != NULL) {
  175.             if (line[1] != ' ') {
  176.             plain_text = TRUE;
  177.             outputline(line + 1, f);
  178.             } else {
  179.             verbatimline(line + 1, f);
  180.             }
  181.         }
  182.         }
  183.         break;
  184.     }
  185.  
  186.     default:
  187.     {
  188.         if (isdigit(line[0])) {    /* start of section */
  189.         if (!table_mode) {    /* ignore while in table */
  190.             section(line, f);
  191.         }
  192.         } else {
  193.         fprintf(stderr, "unknown control code '%c' in column 1, line %d\n",
  194.             line[0], line_count);
  195.         break;
  196.         }
  197.     }
  198.     }
  199. }
  200.  
  201.  
  202. /* process a line with a digit control char */
  203. /* starts a new [sub]section */
  204.  
  205. void section(char *line, FILE * f)
  206. {
  207.     int sh_i;
  208.     int lvl;
  209.  
  210.     (void) sscanf(line, "%d", &sh_i);
  211.  
  212. #if VERBOSE
  213.     printf("lv=%d  ", sh_i);
  214. #endif
  215.  
  216.     chapter[sh_i] += 1;
  217.     chapter[sh_i + 1] = 0;
  218.  
  219.     /* NOTE: section level 1 is used only for the document title */
  220.  
  221.     fprintf(f, "{\"Item%d\" on}", sh_i);
  222.     fprintf(f, "%d", chapter[2]);
  223. #if VERBOSE
  224.     printf("%d", chapter[2]);
  225. #endif
  226.     for (lvl = 3; chapter[lvl] > 0; lvl++) {
  227.     fprintf(f, ".%d", chapter[lvl]);
  228. #if VERBOSE
  229.     printf(".%d", chapter[lvl]);
  230. #endif
  231.     }
  232. #if VERBOSE
  233.     printf(" %s\n", line + 2);
  234. #endif
  235.     fprintf(f, " %s", line + 2);
  236.     fprintf(f, "{\"Item%d\" off}\n", sh_i);
  237. }
  238.  
  239.  
  240. void verbatimline(char *line, FILE * f)
  241. {
  242.     fputs("{\"Verbatim\" on}", f);
  243.     outputline(line, f);
  244.     fputc('\n', f);
  245.     fputs("{\"Verbatim\" off}", f);
  246. }
  247.  
  248.  
  249. void outputline(char *line, FILE * f)
  250. {
  251.     for (; *line != NULL; line++) {
  252.     outputchar(*line, f);
  253.     }
  254.     fputc(' ', f);
  255. }
  256.  
  257.  
  258. void troffline(char *line, FILE * f)
  259. {
  260.     for (; *line != NULL; line++) {
  261.     if (*line == '@') {
  262.         fputs("{tab}", f);
  263.     } else {
  264.          outputchar(*line, f);
  265.     }
  266.     }
  267.     fputc('\n', f);
  268. }
  269.  
  270.  
  271. /* Translates various characters as they are output :
  272.  * '{' starts a DDF command so must be translated to "{\123}"
  273.  * Changing ` to ' allows Impression to use 'smart quotes' if you configure it
  274.  */
  275.  
  276. void outputchar(char c, FILE * f)
  277. {
  278.     switch (c) {
  279.     case '{':fputs("{\\123}", f);
  280.     break;
  281.     case '`':
  282.     fputc('\'', f);
  283.     break;
  284.     default:
  285.     fputc(c, f);
  286.     }
  287. }
  288.