| 1 |
700 |
jeremybenn |
/*
|
| 2 |
|
|
Redistribution and use in source and binary forms, with or without
|
| 3 |
|
|
modification, are permitted provided that the following conditions are met:
|
| 4 |
|
|
|
| 5 |
|
|
* Redistributions of source code must retain the above copyright
|
| 6 |
|
|
notice, this list of conditions and the following disclaimer.
|
| 7 |
|
|
|
| 8 |
|
|
* Redistributions in binary form must reproduce the above copyright
|
| 9 |
|
|
notice, this list of conditions and the following disclaimer in the
|
| 10 |
|
|
documentation and/or other materials provided with the distribution.
|
| 11 |
|
|
|
| 12 |
|
|
* Neither the name of "The Computer Language Benchmarks Game" nor the
|
| 13 |
|
|
name of "The Computer Language Shootout Benchmarks" nor the names of
|
| 14 |
|
|
its contributors may be used to endorse or promote products derived
|
| 15 |
|
|
from this software without specific prior written permission.
|
| 16 |
|
|
|
| 17 |
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
| 18 |
|
|
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
| 19 |
|
|
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
| 20 |
|
|
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
| 21 |
|
|
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
| 22 |
|
|
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
| 23 |
|
|
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
| 24 |
|
|
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
| 25 |
|
|
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
| 26 |
|
|
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
| 27 |
|
|
POSSIBILITY OF SUCH DAMAGE.
|
| 28 |
|
|
*/
|
| 29 |
|
|
|
| 30 |
|
|
/*
|
| 31 |
|
|
* The Computer Language Benchmarks Game
|
| 32 |
|
|
* http://shootout.alioth.debian.org
|
| 33 |
|
|
*
|
| 34 |
|
|
* contributed by Bob W
|
| 35 |
|
|
*/
|
| 36 |
|
|
|
| 37 |
|
|
#include <stdio.h>
|
| 38 |
|
|
#include <stdlib.h>
|
| 39 |
|
|
|
| 40 |
|
|
#define JBFSIZE 82 // line input buffer size
|
| 41 |
|
|
#define QBFSIZE 5200 // output buffer initial size
|
| 42 |
|
|
#define Z16 "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
|
| 43 |
|
|
#define V32 "\0TVGH\0\0CD\0\0M\0KN\0\0\0YSA\0BW\0R\0\0\0\0\0\0"
|
| 44 |
|
|
#define VALL Z16 Z16 Z16 Z16 V32 V32 Z16 Z16 Z16 Z16 Z16 Z16 Z16 Z16
|
| 45 |
|
|
|
| 46 |
|
|
int errex(char *s, int n) { // error message+value, return 1
|
| 47 |
|
|
fprintf(stderr,"\n*** Error: %s [%d]!\n", s, n);
|
| 48 |
|
|
return 1;
|
| 49 |
|
|
}
|
| 50 |
|
|
|
| 51 |
|
|
int main () { // ***** main *****
|
| 52 |
|
|
char *pj, *pq, *pr; // buffer pointers: inp,out,/out
|
| 53 |
|
|
char *jjj = malloc(JBFSIZE); // allocate input line buffer
|
| 54 |
|
|
char *qqq = malloc(QBFSIZE); // output buffer (dyn. size)
|
| 55 |
|
|
char *pqstop = qqq+QBFSIZE; // end-of-buffer pointer
|
| 56 |
|
|
char xtab[256] = VALL; // char conversion table
|
| 57 |
|
|
|
| 58 |
|
|
if (!jjj || !qqq)
|
| 59 |
|
|
return errex("Buffer allocation", !jjj + !qqq);
|
| 60 |
|
|
pj = fgets(jjj,JBFSIZE,stdin); // fetch 1st line
|
| 61 |
|
|
if (!pj)
|
| 62 |
|
|
return errex("No input data",0);
|
| 63 |
|
|
if (*jjj != '>')
|
| 64 |
|
|
return errex("1st char not '>'", 0);
|
| 65 |
|
|
|
| 66 |
|
|
while (pj) { // MAIN LOOP: process data
|
| 67 |
|
|
fputs(jjj, stdout); // output ID line
|
| 68 |
|
|
|
| 69 |
|
|
for (pq=qqq+1, pr=pqstop; ; pq++) { // LOOP: fill output buffer
|
| 70 |
|
|
pj = fgets(jjj, JBFSIZE, stdin); // get line from stdin
|
| 71 |
|
|
if (!pj || (*jjj=='>')) break; // EOF or new ID line
|
| 72 |
|
|
if (pr <= (pq+61)) { // need to resize buffer
|
| 73 |
|
|
char *newstop = pqstop + 12777888;
|
| 74 |
|
|
char *newptr = realloc(qqq, newstop-qqq);
|
| 75 |
|
|
if (!newptr)
|
| 76 |
|
|
return errex("Out of memory", 0);
|
| 77 |
|
|
if (newptr != qqq) { // new base: adj. pointers
|
| 78 |
|
|
size_t x = newptr-qqq; // offset for pointer update
|
| 79 |
|
|
pq+=x; pr+=x; qqq+=x;
|
| 80 |
|
|
newstop+=x; pqstop+=x;
|
| 81 |
|
|
}
|
| 82 |
|
|
pr = __builtin_memmove(newstop-(pqstop-pr), pr, pqstop-pr);
|
| 83 |
|
|
pqstop = newstop; // buffer resize complete
|
| 84 |
|
|
}
|
| 85 |
|
|
while (*pj) { // LOOP: conv. & revert line
|
| 86 |
|
|
char c = xtab[(unsigned char)(*pj++)];
|
| 87 |
|
|
if (c) // conversion valid
|
| 88 |
|
|
*(--pr) = c;
|
| 89 |
|
|
}
|
| 90 |
|
|
}
|
| 91 |
|
|
|
| 92 |
|
|
for (pq = qqq; pr<pqstop; ) { // LOOP: format output
|
| 93 |
|
|
size_t x = (pqstop-pr)<60 ? pqstop-pr : 60;
|
| 94 |
|
|
__builtin_memmove(pq,pr,x); // move line to free space
|
| 95 |
|
|
pr+=x; pq+=x; *(pq++) = 0xA; // adjust pointers, add LF
|
| 96 |
|
|
}
|
| 97 |
|
|
fwrite(qqq, 1, pq-qqq, stdout); // output converted data
|
| 98 |
|
|
}
|
| 99 |
|
|
return 0;
|
| 100 |
|
|
}
|