-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreverse.impala
105 lines (91 loc) · 3.53 KB
/
reverse.impala
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
/* The Computer Language Benchmarks Game
* http://benchmarksgame.alioth.debian.org/
*
* contributed by Bob W
*/
extern "C" {
fn printn(&[u8], int) -> ();
fn print(&[u8]) -> ();
fn println(&[u8]) -> ();
fn read(&[u8], int) -> int;
fn impala_realloc(~[u8], int) -> ~[u8];
fn impala_memmove(&u8, &u8, int) -> ();
}
static nullptr = 0 as ~[u8];
static JBFSIZE = 82; // line input buffer size
static QBFSIZE = 5200; // output buffer initial size
fn errex(s: &[u8]) -> () { // error message+value, return 1
println("*** Error: ");
println(s);
println("\n");
}
fn take_address(&[u8]) -> () {}
fn main(n: int) -> () { // ***** main *****
let mut jjj: [u8*82]; // allocate input line buffer
let mut qqq = ~[QBFSIZE: u8]; // output buffer (dyn. size)
let mut pqstop = QBFSIZE; // size of qqq
let mut xtab = "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
"\0TVGH\0\0CD\0\0M\0KN\0\0\0YSA\0BW\0R\0\0\0\0\0\0"
"\0TVGH\0\0CD\0\0M\0KN\0\0\0YSA\0BW\0R\0\0\0\0\0\0"
"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"; // char conversion table
take_address(&xtab);
let mut pj = read(&jjj, JBFSIZE) == 1; // fetch 1st line
if (!pj) {
errex("No input data");
return()
}
if (jjj(0) != '>') {
errex("1st char not '>'");
return()
}
while pj { // MAIN LOOP: process data
print(&jjj); // output ID line
let mut pr = pqstop;
let mut pq = 1;
while true { // LOOP: fill output buffer
pj = read(&jjj, JBFSIZE) == 1; // get line from stdin
if !pj || (jjj(0) == '>') {
break() // EOF or new ID line
}
if (pr <= (pq+61)) { // resize buffer
let newstop = pqstop + 12777888;
qqq = impala_realloc(qqq, newstop);
if (qqq == nullptr) {
errex("Out of memory");
return()
}
let newpr = newstop-(pqstop-pr);
impala_memmove(&qqq(newpr), &qqq(pr), pqstop-pr);
pr = newpr;
pqstop = newstop; // buffer resize complete
}
let mut idx = 0;
while (jjj(idx) != 0u8) { // LOOP: conv. & revert line
let c = xtab(jjj(idx++));
if (c != 0u8) { // conversion valid
qqq(--pr) = c;
}
}
pq++;
}
pq = 0;
while pr < pqstop {
let x = if (pqstop-pr) < 60 { pqstop-pr } else { 60 };
impala_memmove(&qqq(pq),&qqq(pr),x); // move line to free space
pr+=x;
pq+=x;
qqq(pq++) = 0xA_u8; // adjust pointers, add LF
}
printn(qqq, pq);
}
}