-
Notifications
You must be signed in to change notification settings - Fork 20
/
sort.c
163 lines (145 loc) · 3.6 KB
/
sort.c
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/* SC A Spreadsheet Calculator
* Sorting routines
*
* Chuck Martin <[email protected]>
* Originally created: April, 2001
*
* $Revision: 7.16 $
*/
#include <sys/types.h>
#include <string.h>
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <unistd.h>
#include <limits.h>
#include "compat.h"
#include "sc.h"
int compare(const void *row1, const void *row2);
static struct sortcrit {
int direction, type, column;
} *sort;
static int howmany;
void
sortrange(struct ent *left, struct ent *right, char *criteria)
{
int minr, minc, maxr, maxc, r, c;
int *rows, col = 0;
int cp = 0;
struct ent *p;
minr = left->row < right->row ? left->row : right->row;
minc = left->col < right->col ? left->col : right->col;
maxr = left->row > right->row ? left->row : right->row;
maxc = left->col > right->col ? left->col : right->col;
sort = scxmalloc((2 * sizeof(struct sortcrit)));
rows = scxmalloc((maxr - minr + 1) * sizeof(int));
for (r = minr, c = 0; r <= maxr; r++, c++)
rows[c] = r;
if (!criteria) {
sort[0].direction = 1;
sort[0].type = 1;
sort[0].column = minc;
sort[1].direction = 1;
sort[1].type = 0;
sort[1].column = minc;
howmany = 2;
} else
for (howmany = 0; criteria[cp]; howmany++) {
if (howmany > 1)
sort = scxrealloc(sort,
(howmany + 1) * (sizeof(struct sortcrit)));
switch (criteria[cp++]) {
case '+':
sort[howmany].direction = 1;
break;
case '-':
sort[howmany].direction = -1;
break;
default:
error("Invalid sort criteria");
return;
}
switch (criteria[cp++]) {
case '#':
sort[howmany].type = 0;
break;
case '$':
sort[howmany].type = 1;
break;
default:
error("Invalid sort criteria");
return;
}
if (criteria[cp])
col = toupper((int)criteria[cp++]) - 'A';
else {
error("Invalid sort criteria");
return;
}
if (criteria[cp] && criteria[cp] != '+' && criteria[cp] != '-')
col = (col + 1) * 26 + toupper((int)criteria[cp++]) - 'A';
sort[howmany].column = col;
if (col < minc || col > maxc) {
error("Invalid sort criteria");
return;
}
}
qsort(rows, maxr - minr + 1, sizeof(int), compare);
erase_area(minr, minc, maxr, maxc, 1);
sync_ranges();
for (c = 0, p = delbuf[dbidx]; p; p = p->next) {
if (rows[c] != p->row) {
for (c = 0; c <= maxr - minr && rows[c] != p->row; c++) ;
if (c > maxr - minr) {
error("sort error");
return;
}
}
p->row = minr + c;
}
scxfree((char *)sort);
scxfree((char *)rows);
if (criteria) scxfree(criteria);
r = currow;
c = curcol;
currow = minr;
curcol = minc;
pullcells('m');
flush_saved();
currow = r;
curcol = c;
}
int
compare(const void *row1, const void *row2)
{
struct ent *p1;
struct ent *p2;
double diff;
int result = 0;
int i;
for (i = 0; !result && i < howmany; i++) {
p1 = *ATBL(tbl, *((const int *) row1), sort[i].column);
p2 = *ATBL(tbl, *((const int *) row2), sort[i].column);
if (sort[i].type) {
if (p1 && p1->label)
if (p2 && p2->label)
result = strcmp(p1->label, p2->label);
else
result = -1;
else if (p2 && p2->label)
result = 1;
} else
if (p1 && p2 && p1->flags & IS_VALID && p2->flags & IS_VALID) {
diff = (p1->v - p2->v);
result = (diff > 0 ? 1 : diff < 0 ? -1 : 0);
}
else if (p1 && p1->flags & IS_VALID)
result = -1;
else if (p2 && p2->flags & IS_VALID)
result = 1;
result *= sort[i].direction;
}
if (!result)
result = (*((const int *) row1) - *((const int *) row2));
return (result);
}