Skip to content

Commit 88afc7c

Browse files
committed
Do not output useless NULs at the end of files.
1 parent 7a7af4e commit 88afc7c

File tree

9 files changed

+81
-17
lines changed

9 files changed

+81
-17
lines changed

libword/aa-word.c

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright (C) 2018 Lars Brinkhoff <lars@nocrew.org>
1+
/* Copyright (C) 2018, 2022 Lars Brinkhoff <lars@nocrew.org>
22
33
This program is free software: you can redistribute it and/or modify
44
it under the terms of the GNU General Public License as published by
@@ -28,6 +28,8 @@
2828

2929
#include "libword.h"
3030

31+
static word_t output = -1;
32+
3133
static inline int
3234
get_byte (FILE *f)
3335
{
@@ -59,12 +61,38 @@ get_aa_word (FILE *f)
5961
static void
6062
write_aa_word (FILE *f, word_t word)
6163
{
62-
fputc ((word >> 29) & 0177, f);
63-
fputc ((word >> 22) & 0177, f);
64-
fputc ((word >> 15) & 0177, f);
65-
fputc ((word >> 8) & 0177, f);
66-
fputc (((word >> 1) & 0177) +
67-
((word << 7) & 0200), f);
64+
if (output != -1)
65+
{
66+
fputc ((output >> 29) & 0177, f);
67+
fputc ((output >> 22) & 0177, f);
68+
fputc ((output >> 15) & 0177, f);
69+
fputc ((output >> 8) & 0177, f);
70+
fputc (((output >> 1) & 0177) +
71+
((output << 7) & 0200), f);
72+
}
73+
74+
output = word;
75+
}
76+
77+
static void
78+
flush_aa_word (FILE *f)
79+
{
80+
int i, c;
81+
if (output == -1)
82+
return;
83+
fputc ((output >> 29) & 0177, f);
84+
for (i = 0; i < 4; i++)
85+
{
86+
output &= 03777777777LL;
87+
if (output == 0)
88+
break;
89+
c = (output >> 22) & 0177;
90+
if (i == 3 && (output & 010000000LL) != 0)
91+
c |= 0200;
92+
fputc (c, f);
93+
output <<= 7;
94+
}
95+
output = -1;
6896
}
6997

7098
struct word_format aa_word_format = {
@@ -73,5 +101,5 @@ struct word_format aa_word_format = {
73101
NULL,
74102
by_five_octets,
75103
write_aa_word,
76-
NULL
104+
flush_aa_word
77105
};

libword/its-word.c

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright (C) 2013 Lars Brinkhoff <lars@nocrew.org>
1+
/* Copyright (C) 2013, 2022 Lars Brinkhoff <lars@nocrew.org>
22
33
This program is free software: you can redistribute it and/or modify
44
it under the terms of the GNU General Public License as published by
@@ -18,6 +18,9 @@
1818
#include "libword.h"
1919

2020
static int leftover, there_is_some_leftover = 0;
21+
static word_t output = -1;
22+
23+
static void write_its_word (FILE *f, word_t word);
2124

2225
#define WORDMASK (0777777777777LL)
2326
#define SIGNBIT (0400000000000LL)
@@ -158,6 +161,7 @@ static void
158161
rewind_its_word (FILE *f)
159162
{
160163
there_is_some_leftover = 0;
164+
output = -1;
161165
rewind (f);
162166
}
163167

@@ -171,7 +175,7 @@ fputc2 (int c1, int c2, FILE *f)
171175
static int previous_octet = -1;
172176

173177
static void
174-
flush_its_word (FILE *f)
178+
end_word (FILE *f)
175179
{
176180
if (previous_octet == 015)
177181
fputc (0356, f);
@@ -183,7 +187,7 @@ flush_its_word (FILE *f)
183187
static void
184188
binary_word (FILE *f, word_t word)
185189
{
186-
flush_its_word (f);
190+
end_word (f);
187191

188192
fputc (((word >> 32) & 017) + 0360, f);
189193
fputc (((word >> 24) & 0377), f);
@@ -193,7 +197,7 @@ binary_word (FILE *f, word_t word)
193197
}
194198

195199
static void
196-
ascii_word (FILE *f, word_t word)
200+
ascii_word (FILE *f, word_t word, int n)
197201
{
198202
char c, octets[5];
199203
int i;
@@ -204,7 +208,7 @@ ascii_word (FILE *f, word_t word)
204208
octets[3] = (word >> 8) & 0177;
205209
octets[4] = (word >> 1) & 0177;
206210

207-
for (i = 0; i < 5; i++)
211+
for (i = 0; i < n; i++)
208212
{
209213
c = octets[i];
210214

@@ -252,13 +256,45 @@ ascii_word (FILE *f, word_t word)
252256
}
253257
}
254258

259+
static int
260+
characters (word_t word)
261+
{
262+
int i, n;
263+
n = 0;
264+
for (i = 0; i < 5; i++)
265+
{
266+
if (word == 0)
267+
break;
268+
word = (word << 7) & 0777777777777LL;
269+
n++;
270+
}
271+
return n;
272+
}
273+
255274
static void
256-
write_its_word (FILE *f, word_t word)
275+
flush_its_word (FILE *f)
257276
{
258-
if (word & 1)
259-
binary_word (f, word);
277+
end_word (f);
278+
if (output == -1)
279+
return;
280+
if (output & 1)
281+
binary_word (f, output);
260282
else
261-
ascii_word (f, word);
283+
ascii_word (f, output, characters (output));
284+
output = -1;
285+
}
286+
287+
static void
288+
write_its_word (FILE *f, word_t word)
289+
{
290+
if (output != -1)
291+
{
292+
if (output & 1)
293+
binary_word (f, output);
294+
else
295+
ascii_word (f, output, 5);
296+
}
297+
output = word;
262298
}
263299

264300
struct word_format its_word_format = {

samples/zeros.scrmbl

-4 Bytes
Binary file not shown.

test/chars.pub.sail.ascii

-1 Bytes
Binary file not shown.

test/linum-1.txt-a

-3 Bytes
Binary file not shown.

test/linum-1.txt-df

-2 Bytes
Binary file not shown.

test/linum-2.txt-af

-3 Bytes
Binary file not shown.

test/linum-2.txt-df

-2 Bytes
Binary file not shown.

test/linum-3.txt-d

-2 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)