Factor out readrune and writerune

This commit is contained in:
sin 2014-11-21 16:20:15 +00:00
parent 3de6a7510d
commit 5b5bb82ec0
5 changed files with 80 additions and 48 deletions

View File

@ -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 =\

View File

@ -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;
}
}

46
libutf/readrune.c Normal file
View File

@ -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;
}

23
libutf/writerune.c Normal file
View File

@ -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);
}
}
}

6
utf.h
View File

@ -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 *);