sbase

suckless unix tools
git clone git://git.2f30.org/sbase.git
Log | Files | Refs | README | LICENSE

commit 5b5bb82ec00cb7b7b02ac97550683e2f377e6f13
parent 3de6a7510d37bee553436d0f70c68421f2055be7
Author: sin <sin@2f30.org>
Date:   Fri, 21 Nov 2014 16:20:15 +0000

Factor out readrune and writerune

Diffstat:
Makefile | 4+++-
expand.c | 49++++---------------------------------------------
libutf/readrune.c | 46++++++++++++++++++++++++++++++++++++++++++++++
libutf/writerune.c | 23+++++++++++++++++++++++
utf.h | 6++++--
5 files changed, 80 insertions(+), 48 deletions(-)

diff --git a/Makefile b/Makefile @@ -20,9 +20,11 @@ HDR =\ LIBUTF = libutf.a LIBUTFSRC =\ + libutf/readrune.c\ libutf/rune.c\ libutf/runetype.c\ - libutf/utf.c + libutf/utf.c\ + libutf/writerune.c LIBUTIL = libutil.a LIBUTILSRC =\ diff --git a/expand.c b/expand.c @@ -47,47 +47,6 @@ main(int argc, char *argv[]) return 0; } -int -in(const char *file, FILE *fp, Rune *r) -{ - char buf[UTFmax]; - int c, i; - - c = fgetc(fp); - if (ferror(fp)) - eprintf("%s: read error:", file); - if (feof(fp)) - return 0; - if (c < Runeself) { - *r = (Rune)c; - return 1; - } - buf[0] = c; - for (i = 1; ;) { - c = fgetc(fp); - if (ferror(fp)) - eprintf("%s: read error:", file); - if (feof(fp)) - return 0; - buf[i++] = c; - if (fullrune(buf, i)) - return chartorune(r, buf); - } -} - -static void -out(Rune *r) -{ - char buf[UTFmax]; - int len; - - if ((len = runetochar(buf, r))) { - fwrite(buf, len, 1, stdout); - if (ferror(stdout)) - eprintf("stdout: write error:"); - } -} - static int expand(const char *file, FILE *fp, int tabstop) { @@ -96,7 +55,7 @@ expand(const char *file, FILE *fp, int tabstop) int bol = 1; for (;;) { - if (!in(file, fp, &r)) + if (!readrune(file, fp, &r)) break; switch (r) { @@ -115,18 +74,18 @@ expand(const char *file, FILE *fp, int tabstop) if (col) col--; bol = 0; - out(&r); + writerune(&r); break; case '\n': col = 0; bol = 1; - out(&r); + writerune(&r); break; default: col++; if (r != ' ') bol = 0; - out(&r); + writerune(&r); break; } } diff --git a/libutf/readrune.c b/libutf/readrune.c @@ -0,0 +1,46 @@ +/* See LICENSE file for copyright and license details. */ +#include <errno.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include "../utf.h" + +int +readrune(const char *file, FILE *fp, Rune *r) +{ + char buf[UTFmax]; + int c, i; + + if ((c = fgetc(fp)) == EOF) { + if (ferror(fp)) { + fprintf(stderr, "%s: read error: %s\n", + file, strerror(errno)); + exit(1); + } + return 0; + } + + if (c < Runeself) { + *r = (Rune)c; + return 1; + } + + buf[0] = c; + for (i = 1; ;) { + if ((c = fgetc(fp)) == EOF) { + if (ferror(fp)) { + fprintf(stderr, "%s: read error: %s\n", + file, strerror(errno)); + exit(1); + } + return 0; + } + buf[i++] = c; + if (fullrune(buf, i)) { + chartorune(r, buf); + break; + } + } + return 1; +} diff --git a/libutf/writerune.c b/libutf/writerune.c @@ -0,0 +1,23 @@ +/* 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(Rune *r) +{ + char buf[UTFmax]; + int n; + + if ((n = runetochar(buf, r)) > 0) { + fwrite(buf, n, 1, stdout); + if (ferror(stdout)) { + fprintf(stderr, "stdout: write error: %s\n", + strerror(errno)); + exit(1); + } + } +} diff --git a/utf.h b/utf.h @@ -18,8 +18,7 @@ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER * DEALINGS IN THE SOFTWARE. */ - -#include <stddef.h> +#include <stdio.h> typedef int Rune; @@ -49,3 +48,6 @@ int isspacerune(Rune); int istitlerune(Rune); int isupperrune(Rune); int isdigitrune(Rune); + +int readrune(const char *, FILE *, Rune *); +void writerune(Rune *);