multipage_hp2ps
[ghc.git] / utils / hp2ps / Reorder.c
1 #include "Main.h"
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include "Defines.h"
6 #include "Error.h"
7 #include "HpFile.h"
8 #include "Utilities.h"
9
10 /* own stuff */
11 #include "Reorder.h"
12
13 static struct order {
14 char* ident;
15 int order;
16 } *ordermap = 0;
17
18 static int ordermapmax = 0;
19 static int ordermapindex = 0;
20
21
22 void
23 OrderFor(ident, order)
24 char* ident;
25 int order;
26 {
27 if (! ordermap) {
28 ordermapmax = (nidents > TWENTY ? nidents : TWENTY) * 2;
29 /* Assume nidents read is indication of the No of
30 idents in the .aux file (*2 for good luck !) */
31 ordermap = xmalloc(ordermapmax * sizeof(struct order));
32 }
33
34 if (ordermapindex < ordermapmax) {
35 ordermap[ ordermapindex ].ident = copystring(ident);
36 ordermap[ ordermapindex ].order = order;
37 ordermapindex++;
38 } else {
39 Disaster("order map overflow");
40 }
41 }
42
43 /*
44 * Get the order of to be used for "ident" if there is one.
45 * Otherwise, return 0 which is the minimum ordering value.
46 */
47
48 int
49 OrderOf(ident)
50 char* ident;
51 {
52 int i;
53
54 for (i = 0; i < ordermapindex; i++) {
55 if (strcmp(ordermap[i].ident, ident) == 0) { /* got it */
56 return(ordermap[i].order);
57 }
58 }
59
60 return 0;
61 }
62
63 /*
64 * Reorder on the basis of information from ".aux" file.
65 */
66
67 void
68 Reorder()
69 {
70 intish i;
71 intish j;
72 int min;
73 struct entry* e;
74 int o1, o2;
75
76 for (i = 0; i < nidents-1; i++) {
77 min = i;
78 for (j = i+1; j < nidents; j++) {
79 o1 = OrderOf(identtable[ j ]->name);
80 o2 = OrderOf(identtable[ min ]->name);
81
82 if (o1 < o2 ) min = j;
83 }
84
85 e = identtable[ min ];
86 identtable[ min ] = identtable[ i ];
87 identtable[ i ] = e;
88 }
89 }