add Outputable instance for OccIfaceEq
[ghc-hetmet.git] / utils / hp2ps / AuxFile.c
1 #include "Main.h"
2 #include <ctype.h>
3 #include <stdio.h>
4 #include <string.h>
5 #include "Defines.h"
6 #include "Shade.h"
7 #include "Error.h"
8 #include "HpFile.h"
9 #include "Reorder.h"
10
11 /* own stuff */
12 #include "AuxFile.h"
13
14 static void GetAuxLine PROTO((FILE *)); /* forward */
15 static void GetAuxTok  PROTO((FILE *)); /* forward */
16
17 void
18 GetAuxFile(auxfp)
19   FILE* auxfp;
20 {
21     ch = ' ';
22     endfile = 0;
23     linenum = 1;
24  
25     GetAuxTok(auxfp);
26  
27     while (endfile == 0) {
28         GetAuxLine(auxfp);
29     }
30
31     fclose(auxfp);
32 }
33
34
35
36 /*
37  *      Read the next line from the aux file, check the syntax, and 
38  *      perform the appropriate action.
39  */
40
41 static void
42 GetAuxLine(auxfp)
43   FILE* auxfp;
44 {
45     switch (thetok) {
46     case X_RANGE_TOK:
47         GetAuxTok(auxfp);
48         if (thetok != FLOAT_TOK) {
49             Error("%s, line %d, floating point number must follow X_RANGE", 
50                   auxfile, linenum);
51         }
52         auxxrange = thefloatish;
53         GetAuxTok(auxfp);
54         break;
55     case Y_RANGE_TOK:
56         GetAuxTok(auxfp);
57         if (thetok != FLOAT_TOK) {
58             Error("%s, line %d, floating point number must follow Y_RANGE", 
59                   auxfile, linenum);
60         }
61         auxyrange = thefloatish;
62         GetAuxTok(auxfp);
63         break;
64     case ORDER_TOK:
65         GetAuxTok(auxfp);
66         if (thetok != IDENTIFIER_TOK) {
67             Error("%s, line %d: identifier must follow ORDER",
68                   auxfile, linenum);
69         }
70         GetAuxTok(auxfp);
71         if (thetok != INTEGER_TOK) {
72             Error("%s, line %d: identifier and integer must follow ORDER",
73                   auxfile, linenum);
74         }
75         OrderFor(theident, theinteger);
76         GetAuxTok(auxfp);
77         break;
78     case SHADE_TOK:
79         GetAuxTok(auxfp);
80         if (thetok != IDENTIFIER_TOK) {
81             Error("%s, line %d: identifier must follow SHADE", 
82                   auxfile, linenum);
83         }
84         GetAuxTok(auxfp);
85         if (thetok != FLOAT_TOK) {
86             Error("%s, line %d: identifier and floating point number must follow SHADE",
87                   auxfile, linenum);
88         }
89         ShadeFor(theident, thefloatish);
90         GetAuxTok(auxfp); 
91         break;
92     case EOF_TOK:
93         endfile = 1;
94         break;
95     default:
96         Error("%s, line %d: %s unexpected", auxfile, linenum,
97               TokenToString(thetok));
98         break;
99     }
100 }
101
102
103
104 /*
105  *      Read the next token from the input and assign its value
106  *      to the global variable "thetok". In the case of numbers,
107  *      the corresponding value is also assigned to "thefloatish"; 
108  *      in the case of identifiers it is assigned to "theident".
109  */
110  
111 static void GetAuxTok(auxfp)
112 FILE* auxfp;
113 {
114
115     while (isspace(ch)) {               /* skip whitespace */
116         if (ch == '\n') linenum++;
117         ch = getc(auxfp);
118     } 
119
120     if (ch == EOF) {
121         thetok = EOF_TOK;
122         return;
123     }
124
125     if (isdigit(ch)) {
126         thetok = GetNumber(auxfp);
127         return;
128     } else if (IsIdChar(ch)) {          /* ch can't be a digit here */
129         GetIdent(auxfp);
130         if (!isupper((int)theident[0])) {
131             thetok = IDENTIFIER_TOK;
132         } else if (strcmp(theident, "X_RANGE") == 0) {
133             thetok = X_RANGE_TOK;
134         } else if (strcmp(theident, "Y_RANGE") == 0) {
135             thetok = Y_RANGE_TOK;
136         } else if (strcmp(theident, "ORDER") == 0) {
137             thetok = ORDER_TOK;
138         } else if (strcmp(theident, "SHADE") == 0) {
139             thetok = SHADE_TOK;
140         } else {
141             thetok = IDENTIFIER_TOK;
142         }
143         return;
144     } else {
145         Error("%s, line %d: strange character (%c)", auxfile, linenum, ch);
146     }
147 }
148
149 void
150 PutAuxFile(auxfp)
151   FILE* auxfp;
152 {
153     int i;
154
155     fprintf(auxfp, "X_RANGE %.2f\n", xrange);
156     fprintf(auxfp, "Y_RANGE %.2f\n", yrange);
157
158     for (i = 0; i < nidents; i++) {
159         fprintf(auxfp, "ORDER %s %d\n", identtable[i]->name, i+1);
160     }
161
162     for (i = 0; i < nidents; i++) {
163         fprintf(auxfp, "SHADE %s %.2f\n", identtable[i]->name, 
164                        ShadeOf(identtable[i]->name));
165     }
166
167     fclose(auxfp);
168 }