Scrap writerune(), introducing fputrune()

Interface and function as proposed by cls.
Code is also shorter, everything else analogous to fgetrune().
This commit is contained in:
FRIGN 2015-02-11 20:58:00 +01:00
parent a5ae899a48
commit 7c578bf5b0
9 changed files with 46 additions and 40 deletions

View File

@ -24,7 +24,7 @@ LIBUTFSRC =\
libutf/utf.c\
libutf/chartorunearr.c\
libutf/fgetrune.c\
libutf/writerune.c\
libutf/fputrune.c\
libutf/isalnumrune.c\
libutf/isalpharune.c\
libutf/isblankrune.c\

View File

@ -72,7 +72,7 @@ expand(const char *file, FILE *fp)
col++;
if (r != ' ')
bol = 0;
writerune("<stdout>", stdout, &r);
efputrune(&r, stdout, "<stdout>");
break;
}
}

27
libutf/fputrune.c Normal file
View File

@ -0,0 +1,27 @@
/* See LICENSE file for copyright and license details. */
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "../utf.h"
int
fputrune(const Rune *r, FILE *fp)
{
char buf[UTFmax];
return fwrite(buf, runetochar(buf, r), 1, fp);
}
int
efputrune(const Rune *r, FILE *fp, const char *file)
{
int ret;
if ((ret = fputrune(r, fp)) < 0) {
fprintf(stderr, "fputrune %s: %s\n", file, strerror(errno));
exit(1);
}
return ret;
}

View File

@ -1,23 +0,0 @@
/* See LICENSE file for copyright and license details. */
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "../utf.h"
void
writerune(const char *file, FILE *fp, Rune *r)
{
char buf[UTFmax];
int n;
if ((n = runetochar(buf, r)) > 0) {
fwrite(buf, n, 1, fp);
if (ferror(fp)) {
fprintf(stderr, "%s: write error: %s\n",
file, strerror(errno));
exit(1);
}
}
}

14
paste.c
View File

@ -26,17 +26,17 @@ sequential(struct fdescr *dsc, int fdescrlen, Rune *delim, size_t delimlen)
while (efgetrune(&c, dsc[i].fp, dsc[i].name)) {
if (last == '\n') {
if (delim[d] != '\0')
writerune("<stdout>", stdout, &delim[d]);
efputrune(&delim[d], stdout, "<stdout>");
d = (d + 1) % delimlen;
}
if (c != '\n')
writerune("<stdout>", stdout, &c);
efputrune(&c, stdout, "<stdout>");
last = c;
}
if (last == '\n')
writerune("<stdout>", stdout, &last);
efputrune(&last, stdout, "<stdout>");
}
}
@ -56,22 +56,22 @@ nextline:
for (; efgetrune(&c, dsc[i].fp, dsc[i].name) ;) {
for (m = last + 1; m < i; m++)
writerune("<stdout>", stdout, &(delim[m % delimlen]));
efputrune(&(delim[m % delimlen]), stdout, "<stdout>");
last = i;
if (c == '\n') {
if (i != fdescrlen - 1)
c = d;
writerune("<stdout>", stdout, &c);
efputrune(&c, stdout, "<stdout>");
break;
}
writerune("<stdout>", stdout, &c);
efputrune(&c, stdout, "<stdout>");
}
if (c == 0 && last != -1) {
if (i == fdescrlen - 1)
putchar('\n');
else
writerune("<stdout>", stdout, &d);
efputrune(&d, stdout, "<stdout>");
last++;
}
}

2
tail.c
View File

@ -63,7 +63,7 @@ taketail(FILE *fp, const char *str)
fputs(ring[j], stdout);
free(ring[j]);
} else if (r) {
writerune("<stdout>", stdout, &r[j]);
efputrune(&r[j], stdout, "<stdout>");
}
} while ((j = (j + 1) % num) != i);

2
tr.c
View File

@ -271,6 +271,6 @@ read:
goto read;
write:
lastrune = r;
writerune("<stdout>", stdout, &r);
efputrune(&r, stdout, "<stdout>");
goto read;
}

View File

@ -44,10 +44,10 @@ unexpandspan(size_t last, size_t col)
r = '\t';
for (; last + tablist[i] <= col; last += tablist[i])
writerune("<stdout>", stdout, &r);
efputrune(&r, stdout, "<stdout>");
r = ' ';
for (; last < col; last++)
writerune("<stdout>", stdout, &r);
efputrune(&r, stdout, "<stdout>");
} else {
for (i = 0; i < tablistlen; i++)
if (col < tablist[i])
@ -57,12 +57,12 @@ unexpandspan(size_t last, size_t col)
break;
r = '\t';
for (; j < i; j++) {
writerune("<stdout>", stdout, &r);
efputrune(&r, stdout, "<stdout>");
last = tablist[j];
}
r = ' ';
for (; last < col; last++)
writerune("<stdout>", stdout, &r);
efputrune(&r, stdout, "<stdout>");
}
}
@ -115,7 +115,7 @@ unexpand(const char *file, FILE *fp)
break;
}
if ((r != ' ' && r != '\t') || (!aflag && !bol))
writerune("<stdout>", stdout, &r);
efputrune(&r, stdout, "<stdout>");
}
if (last < col && (bol || aflag))
unexpandspan(last, col);

4
utf.h
View File

@ -61,5 +61,7 @@ Rune toupperrune(Rune);
int fgetrune(Rune *, FILE *);
int efgetrune(Rune *, FILE *, const char *);
void writerune(const char *, FILE *, Rune *);
int fputrune(const Rune *, FILE *);
int efputrune(const Rune *, FILE *, const char *);
int chartorunearr(const char*, Rune **);