commit 348937f2b6f30ada60b6487a3829df541ae87a7a
parent 5ada192bf07e7a2e7ec5bbfe06068edda22a1fe9
Author: glenda <glenda@9front.local>
Date: Wed, 16 Feb 2022 00:20:34 +0000
refactoring
Diffstat:
M | richterm.c | | | 165 | +++++++++++++++++++++++++++++++++++++++++++++++++++++-------------------------- |
M | richterm.h | | | 20 | ++++++++++++-------- |
2 files changed, 123 insertions(+), 62 deletions(-)
diff --git a/richterm.c b/richterm.c
@@ -985,12 +985,29 @@ minobj(Object *o1, Object *o2)
/* **** New Code Beyond This Point **** */
Array *elems;
+Array *richdata;
+
+char *sampledata =
+ ".alpha\n"
+ "s\n"
+ ".beta\n"
+ "s\n"
+ "Lhttp://nsmpr.xyz\n"
+ ".gamma\n"
+ "L\n"
+ "s\n"
+ "Lhttp://9front.org\n"
+ "F/lib/font/bit/terminus/unicode.12.font\n"
+ ".delta\n"
+ "L\n"
+ "F\n"
+ "n\n"
+ ".Каким-то макаром попали к татарам амбары да бары пропали задаром\n";
char *
elemparse(Elem *e, char *str, long n)
{
int type;
- int count;
char *sp, *ep;
type = *str;
@@ -998,79 +1015,119 @@ elemparse(Elem *e, char *str, long n)
if (n <= 1) return nil;
ep = memchr(sp, '\n', n - 1);
if (ep == nil) return nil;
- count = ep - sp;
e->type = type;
e->str = sp;
- e->count = count;
+ e->count = ep - sp;
return ep + 1;
}
-char *sampledata =
- ".alpha\n"
- "s\n"
- ".beta\n"
- "s\n"
- "Lhttp://nsmpr.xyz\n"
- ".gamma\n"
- "L\n"
- "s\n"
- "Lhttp://9front.org\n"
- "F/lib/font/bit/terminus/unicode.12.font\n"
- ".delta\n"
- "L\n"
- "F\n"
- "n\n"
- ".Каким-то макаром попали к татарам амбары да бары пропали задаром\n";
-
void
-generatesampleelems(void)
+parsedata(Array *data, Array *elems)
{
- Elem *e, *eold;
- char *dp, *link, *fstr;
- Font *sfont;
- long count;
+ Elem *e;
+ char *dp;
+
+ qlock(data->l);
+
+ dp = data->p;
- elems = arraycreate(sizeof(Elem *), 128, nil);
- e = nil;
- link = nil;
- sfont = font;
- dp = sampledata;
- count = strlen(sampledata);
while (dp != nil) {
- eold = e;
e = mallocz(sizeof(Elem), 1);
- dp = elemparse(e, dp, sampledata + count - dp);
+ dp = elemparse(e, dp, data->p + data->count - dp);
if (dp == nil) break;
+ arraygrow(elems, 1, &e);
+ }
+
+ qunlock(data->l);
+}
+
+char *
+elemstr(Elem *e)
+{
+ /* TODO: maybe use static char buffer instead of mallocing */
+ char *str;
+
+ if (e->count == 0) return nil;
+
+ str = malloc(e->count + 1);
+ memcpy(str, e->str, e->count);
+ str[e->count] = '\0';
+
+ return str;
+}
+
+void
+elemslink(Array *elems)
+{
+ Elem *e, *eold;
+ long i;
+ for (i = 0; i < elems->count; i++) {
+ eold = e;
+ arrayget(elems, i, &e);
+
e->prev = eold;
if (eold != nil) eold->next = e;
+ }
+}
+
+void
+elemsupdatecache(Array *elems)
+{
+ Elem *e;
+ char *link, *str;
+ Font *fnt;
+ long i;
+
+ link = nil;
+ fnt = font;
- switch (e->type) {
+ for (i = 0; i < elems->count; i++) {
+ str = elemstr(e);
+
+ switch(e->type) {
case E_LINK:
- /* TODO: maybe use char Array instead of blindly reallocing link */
- if (e->count > 0) {
- link = realloc(link, e->count + 1);
- memcpy(link, e->str, e->count);
- link[e->count] = '\0';
- } else link = realloc(link, 0);
+ if (link != nil) free(link);
+ if (str != nil) link = strdup(str);
+ else link = nil;
break;
case E_FONT:
- /* TODO: maybe use char Array instead of blindly reallocing fstr */
- if (e->count > 0) {
- fstr = mallocz(e->count + 1, 1);
- memcpy(fstr, e->str, e->count);
- sfont = getfont(fonts, fstr);
- free(fstr);
- } else sfont = font;
+ if (str != nil) fnt = getfont(fonts, str);
+ else fnt = font;
+ break;
+ case E_IMAGE:
+ /* load image
+ * e->image = image
+ */
break;
}
+
+ if (str != nil) free(str);
+
if (link != nil) e->link = strdup(link);
- e->font = sfont;
- arraygrow(elems, 1, &e);
+ e->font = fnt;
}
- if (link != nil) free(link);
+
+ if (link != nil) free(link);
+}
+
+void
+generatesampleelems(void)
+{
+ long count;
+
+ elems = arraycreate(sizeof(Elem *), 128, nil);
+
+ count = strlen(sampledata);
+
+ richdata = arraycreate(sizeof(char *), count, nil);
+ arraygrow(richdata, count, (void *)sampledata);
+
+ parsedata(richdata, elems);
+ elemslink(elems);
+ elemsupdatecache(elems);
}
void
@@ -1117,8 +1174,8 @@ drawelem(Elem *e)
drawp = dtable[e->type];
if (drawp == nil) {
- fprint(2, "drawelem: unknown elem type: %uhhx", e->type);
- // e->type = E_NOOP;
+ fprint(2, "drawelem: unknown elem type: 0x%uhhx '%c'\n", e->type, e->type);
+ e->type = E_NOOP;
drawp = drawnoop;
}
@@ -1181,8 +1238,8 @@ drawtext(Elem *e)
Point
drawnl(Elem *e)
{
- if (e->nlpos.y < e->pos.y + e->font->height)
- e->nlpos.y = e->pos.y + e->font->height;
+// if (e->nlpos.y < e->pos.y + e->font->height)
+// e->nlpos.y = e->pos.y + e->font->height;
/*
* if (selected) Ibg = Isel;
diff --git a/richterm.h b/richterm.h
@@ -106,6 +106,7 @@ enum {
};
typedef struct Token Token;
+typedef struct Elem Elem;
struct Token {
int type;
@@ -113,24 +114,20 @@ struct Token {
long count;
};
-typedef struct Elem Elem;
-
struct Elem {
Token;
- Point pos;
- Point nlpos;
-
Elem *next;
Elem *prev;
+
char *link;
Image *image;
Font *font;
-};
-extern Array *elems;
+ Point pos;
+ Point nlpos;
+};
-void generatesampleelems(void);
void drawelems(void);
Point drawelem(Elem *);
Point drawtext(Elem *);
@@ -138,3 +135,10 @@ Point drawnl(Elem *);
Point drawspace(Elem *);
Point drawnoop(Elem *);
char * elemparse(Elem *, char *, long);
+char * elemstr(Elem *);
+void elemslink(Array *);
+void elemsupdatecache(Array *);
+void generatesampleelems(void);
+void parsedata(Array *, Array *);
+
+extern Array *elems;