commit 6410f18e2de62d15ad3334bdac10ae8124e42d0d
parent d3254d00ce16359b96c164e7e3a014b1a3f4283c
Author: Pavel Renev <an2qzavok@gmail.com>
Date: Sat, 31 Jul 2021 21:27:18 +0000
refactor mdprint
Diffstat:
M | extra/mdprint.c | | | 106 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++----------------------- |
1 file changed, 76 insertions(+), 30 deletions(-)
diff --git a/extra/mdprint.c b/extra/mdprint.c
@@ -1,47 +1,93 @@
#include <u.h>
#include <libc.h>
#include <bio.h>
+#include <String.h>
-char *
-newobj(void)
-{
- char *buf;
- int fd;
- fd = open("/mnt/richterm/new", OREAD);
- if (fd < 0) sysfatal("%r");
- buf = mallocz(256, 1);
- read(fd, buf, 256);
- close(fd);
- return buf;
-}
+char * newobj(void);
+void parse(char);
+void purge(void);
+
+int newblock = 1;
+String *bbuf;
void
main(int argc, char **argv)
{
int fd;
- long n;
Biobuf *bp;
- char *lp;
+ char c;
+
if (argc > 1) {
if ((fd = open(argv[1], OREAD)) < 0)
sysfatal("can't open %s, %r", argv[1]);
} else fd = 0;
bp = Bfdopen(fd, OREAD);
- while ((lp = Brdstr(bp, '\n', 0)) != nil) {
- char *id;
- char *path;
- int td;
- id = newobj();
- path = smprint("/mnt/richterm/%s/text", id);
- print("%s ", id);
- td = open(path, OWRITE);
- if (td < 0) sysfatal("%r");
- n = write(td, lp, strlen(lp));
- if (n != strlen(lp))
- sysfatal("write failed, %ld/%ld", n, strlen(lp));
- close(td);
- free(lp);
- free(id);
- free(path);
+ bbuf = s_new();
+
+ while ((c = Bgetc(bp)) >= 0) {
+ parse(c);
+ }
+}
+
+void
+parse(char c)
+{
+ int purgeblock;
+
+ purgeblock = 0;
+
+ if (newblock != 0) {
+ newblock = 0;
+ /* ... */
+ } else {
+ /* ... */
}
+
+ bbuf = s_nappend(bbuf, &c, 1);
+
+ if (c == '\n') purgeblock = 1;
+
+ if (purgeblock != 0) {
+ newblock = 1;
+ /* ... */
+ purge();
+ bbuf = s_reset(bbuf);
+ }
+}
+
+void
+purge(void)
+{
+ char *id, *path;
+ int td;
+ usize n, size;
+
+ id = newobj();
+ path = smprint("/mnt/richterm/%s/text", id);
+
+ td = open(path, OWRITE);
+ if (td < 0) sysfatal("%r");
+
+ size = bbuf->end - bbuf->base;
+
+ n = write(td, bbuf->base, size);
+
+ if (n != size)
+ sysfatal("write failed: %r");
+ close(td);
+ free(path);
+
+}
+
+char *
+newobj(void)
+{
+ char *buf;
+ int fd;
+ fd = open("/mnt/richterm/new", OREAD);
+ if (fd < 0) sysfatal("%r");
+ buf = mallocz(256, 1);
+ read(fd, buf, 256);
+ close(fd);
+ return buf;
}