ref: 6df541775b1cd4eb2d9bd3698d68c5cd6d42736b
parent: e547019e603e4c3bc72117dd653662636a5cae29
author: Sigrid Solveig Haflínudóttir <sigrid@ftrv.se>
date: Sun Nov 10 21:47:58 EST 2024
add io-{line,column} and io-set-{line,column}!
--- a/ios.c
+++ b/ios.c
@@ -706,6 +706,8 @@
s->fpos = -1;
s->fd = -1;
s->ownbuf = 1;
+ s->lineno = 1;
+ s->colno = 0;
}
/* stream object initializers. we do no allocation. */
@@ -821,6 +823,8 @@
ch = s->buf[s->bpos++];
else if(s->_eof || ios_read(s, &ch, 1) < 1)
return IOS_EOF;
+ if(ch == '\n')
+ s->lineno++;
return ch;
}
@@ -880,6 +884,10 @@
break;
}
chartorune(r, buf);
+ if(*r == '\n')
+ s->colno = 0;
+ else
+ s->colno += wcwidth(*r);
return *r == Runeerror ? 0 : 1;
}
--- a/ios.h
+++ b/ios.h
@@ -18,20 +18,20 @@
#define IOS_BUFSIZE 32768
typedef struct {
- bufmode_t bm;
-
- // the state only indicates where the underlying file position is relative
- // to the buffer. reading: at the end. writing: at the beginning.
- // in general, you can do any operation in any state.
- bufstate_t state;
-
uint8_t *buf; // start of buffer
size_t maxsize; // space allocated to buffer
size_t size; // length of valid data in buf, >=ndirty
size_t bpos; // current position in buffer
size_t ndirty; // # bytes at &buf[0] that need to be written
-
+ size_t lineno;
+ size_t colno;
off_t fpos; // cached file pos
+ bufmode_t bm;
+
+ // the state only indicates where the underlying file position is relative
+ // to the buffer. reading: at the end. writing: at the beginning.
+ // in general, you can do any operation in any state.
+ bufstate_t state;
int fd;
--- a/iostream.c
+++ b/iostream.c
@@ -370,6 +370,32 @@
return size_wrap(ios_copyall(dest, src));
}
+BUILTIN("io-line", io_line)
+{
+ argcount(nargs, 1);
+ return size_wrap(toiostream(args[0])->lineno);
+}
+
+BUILTIN("io-set-line!", io_set_line)
+{
+ argcount(nargs, 2);
+ toiostream(args[0])->lineno = toulong(args[1]);
+ return FL(t);
+}
+
+BUILTIN("io-column", io_column)
+{
+ argcount(nargs, 1);
+ return size_wrap(toiostream(args[0])->colno);
+}
+
+BUILTIN("io-set-column!", io_set_column)
+{
+ argcount(nargs, 2);
+ toiostream(args[0])->colno = toulong(args[1]);
+ return FL(t);
+}
+
value_t
stream_to_string(value_t *ps)
{