Speed up and simplify reading of standard input

This commit is contained in:
Jacob 2023-07-19 01:06:38 +02:00
parent 9daedf9554
commit b812702115

View file

@ -1,65 +1,58 @@
/* See LICENSE file for copyright and license details. */ /* See LICENSE file for copyright and license details. */
void readstdin(void) { void readstdin(void) {
char buf[sizeof tx.text], *p; char *line = NULL;
size_t i, itemsiz = 0; size_t i, junk, size = 0;
unsigned int tmpmax = 0; ssize_t len;
#if USEIMAGE
int oneitem = 0;
#endif
if (passwd) { if (passwd) { // -P: No items should be displayed
sp.inputw = lines = 0; sp.inputw = lines = 0;
return; return;
} }
if (listfile) { if (listfile) { // -lf: List file is used so no need to read stdin
readfile(); readfile();
return; return;
} }
int o = 0; /* read each line from stdin and add it to the item list */
for (i = 0; (len = getline(&line, &junk, stdin)) != -1; i++, line = NULL) {
// read each line from stdin and add it to the item list if (i + 1 >= size / sizeof *items)
for (i = 0; fgets(buf, sizeof buf, stdin); i++) { if (!(items = realloc(items, (size += BUFSIZ))))
if (i + 1 >= itemsiz) { die("cannot realloc %zu bytes:", size);
itemsiz += 256; if (line[len - 1] == '\n')
if (!(items = realloc(items, itemsiz * sizeof(*items)))) line[len - 1] = '\0';
die("spmenu: cannot realloc %zu bytes:", itemsiz * sizeof(*items));
}
if ((p = strchr(buf, '\n')))
*p = '\0';
if (!(items[i].text = strdup(buf)))
die("spmenu: cannot strdup %u bytes:", strlen(buf) + 1);
items[i].hp = arrayhas(hpitems, hplength, items[i].text);
draw_font_getexts(draw->font, buf, strlen(buf), &tmpmax, NULL, True);
if (tmpmax > sp.inputw) {
sp.inputw = tmpmax;
}
items[i].text = line;
items[i].index = i; items[i].index = i;
items[i].hp = arrayhas(hpitems, hplength, items[i].text);
if (parsemarkup(i)) { if (parsemarkup(i)) {
o = 1;
}
#if !USEIMAGE
if (o) {
;
}
#endif
}
#if USEIMAGE #if USEIMAGE
if (!o) img.longestedge = img.imagegaps = 0; oneitem = 1;
#endif #endif
}
// clean }
if (items) { free(line);
items[i].text = NULL;
if (items) {
items[i].text = NULL;
#if USEIMAGE #if USEIMAGE
items[i].image = NULL; items[i].image = NULL;
#endif #endif
} }
lines = MIN(lines, i); #if USEIMAGE
if (oneitem) {
img.longestedge = img.imagegaps = 0;
}
#endif
lines = MIN(lines, i);
} }
void readfile(void) { void readfile(void) {
@ -200,5 +193,7 @@ int parsemarkup(int index) {
items[index].text += strlen("img://")+strlen(data)+1; items[index].text += strlen("img://")+strlen(data)+1;
} }
} }
return 0;
#endif #endif
} }