summaryrefslogtreecommitdiff
path: root/utils/hp2ps/Reorder.c
blob: 144b3f99d3681db75fefc15b291c5ff33697d6bf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include "Main.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "Defines.h"
#include "Error.h"
#include "HpFile.h"
#include "Utilities.h"

/* own stuff */
#include "Reorder.h"

static struct order {
    char* ident;
    int order;
} *ordermap = 0;

static int ordermapmax = 0;
static int ordermapindex = 0;


void
OrderFor(char *ident, int order)
{
    if (! ordermap) {
	ordermapmax = (nidents > TWENTY ? nidents : TWENTY) * 2;
	         /* Assume nidents read is indication of the No of
		    idents in the .aux file (*2 for good luck !) */
	ordermap = xmalloc(ordermapmax * sizeof(struct order));
    }

    if (ordermapindex < ordermapmax) {
	ordermap[ ordermapindex ].ident = copystring(ident);
	ordermap[ ordermapindex ].order = order;
	ordermapindex++;
    } else {
	Disaster("order map overflow");
    }
}

/*
 *	Get the order of to be used for "ident" if there is one. 
 *	Otherwise, return 0 which is the minimum ordering value. 
 */

static int
OrderOf(char *ident)
{
    int i;

    for (i = 0; i < ordermapindex; i++) {
	if (strcmp(ordermap[i].ident, ident) == 0) {	/* got it */
	    return(ordermap[i].order);
	}
    }

    return 0; 
}

/*
 *	Reorder on the basis of information from ".aux" file.
 */

void
Reorder(void)
{
    intish i;
    intish j;
    int min;
    struct entry* e;
    int o1, o2;

    for (i = 0; i < nidents-1; i++) {
	min = i; 
	for (j = i+1; j < nidents; j++) {
	    o1 = OrderOf(identtable[  j  ]->name);
	    o2 = OrderOf(identtable[ min ]->name);

	    if (o1 < o2 ) min = j;
	}

        e = identtable[ min ];
	identtable[ min ] = identtable[ i ];
	identtable[ i ] = e;
    } 	
}