diff options
author | IIMarckus <iimarckus@gmail.com> | 2015-01-08 03:14:58 -0700 |
---|---|---|
committer | IIMarckus <iimarckus@gmail.com> | 2015-01-08 03:14:58 -0700 |
commit | f5248045ac938a141ace86d60ab908f03d6dd063 (patch) | |
tree | cc3e2aebd011ae002395e580cf89651e0b08a810 /redtools/redrle.c | |
parent | fa0761ea70308099bf3a61a88ecf8359cc594d44 (diff) |
redrle: reduce code duplication.
Diffstat (limited to 'redtools/redrle.c')
-rw-r--r-- | redtools/redrle.c | 61 |
1 files changed, 25 insertions, 36 deletions
diff --git a/redtools/redrle.c b/redtools/redrle.c index 580c3e4..b8ec529 100644 --- a/redtools/redrle.c +++ b/redtools/redrle.c @@ -32,6 +32,15 @@ usage() exit(1); } +void +squash(int threshold, FILE *f, int lastbyte, int *count, int *xpos) +{ + fputc(lastbyte << 4 | threshold, f); + *xpos += threshold; + *xpos %= 20; + *count -= threshold; +} + int main(int argc, char *argv[]) { @@ -101,30 +110,20 @@ main(int argc, char *argv[]) if (feof(infile)) { while (count > 0xF) { if (rows && 20 - xpos <= 0xF) { - count -= (20 - xpos); - fputc(lastbyte << 4 | - (20 - xpos), outfile); - xpos = 0; + squash(20 - xpos, outfile, + lastbyte, &count, &xpos); continue; } - count -= 0xF; - fputc(lastbyte << 4 | 0xF, outfile); - xpos += 0xF; - if (xpos == 20) - xpos = 0; + squash(0xF, outfile, lastbyte, &count, + &xpos); } if (count != 0) { if (rows && 20 - xpos < count) { - count -= (20 - xpos); - fputc(lastbyte << 4 | - (20 - xpos), outfile); - xpos = 0; + squash(20 - xpos, outfile, + lastbyte, &count, &xpos); } - fputc(lastbyte << 4 | count, outfile); - xpos += count; - if (xpos == 20) - xpos = 0; - count = 0; + squash(count, outfile, lastbyte, + &count, &xpos); } break; } @@ -140,30 +139,20 @@ main(int argc, char *argv[]) else { while (count > 0xF) { if (rows && 20 - xpos <= 0xF) { - count -= (20 - xpos); - fputc(lastbyte << 4 | - (20 - xpos), outfile); - xpos = 0; + squash(20 - xpos, outfile, + lastbyte, &count, &xpos); continue; } - count -= 0xF; - fputc(lastbyte << 4 | 0xF, outfile); - xpos += 0xF; - if (xpos == 20) - xpos = 0; + squash(0xF, outfile, lastbyte, &count, + &xpos); } if (count != 0) { if (rows && 20 - xpos < count) { - count -= (20 - xpos); - fputc(lastbyte << 4 | - (20 - xpos), outfile); - xpos = 0; + squash(20 - xpos, outfile, + lastbyte, &count, &xpos); } - fputc(lastbyte << 4 | count, outfile); - xpos += count; - if (xpos == 20) - xpos = 0; - count = 0; + squash(count, outfile, lastbyte, + &count, &xpos); } lastbyte = byte; |