3 // Insert one stack into another before position in old stack.
4 // (Does not copy contents of strings, just shuffles index array contents.)
5 void linestack_addstack(struct linestack **lls, struct linestack *throw,
8 struct linestack *catch = *lls;
11 if (pos > catch->len) error_exit("linestack_addstack past end.");
13 // Make a hole, allocating more space if necessary.
14 if (catch->len+throw->len >= catch->max) {
15 // New size rounded up to next multiple of 64, allocate and copy start.
16 catch->max = ((catch->len+throw->len)|63)+1;
17 *lls = xmalloc(sizeof(struct linestack)+catch->max*sizeof(struct ptr_len));
18 memcpy(*lls, catch, sizeof(struct linestack)+pos*sizeof(struct ptr_len));
21 // Copy end (into new allocation if necessary)
22 if (pos != catch->len)
23 memmove((*lls)->idx+pos+throw->len, catch->idx+pos,
24 (catch->len-pos)*sizeof(struct ptr_len));
26 // Cleanup if we had to realloc.
32 memcpy(catch->idx+pos, throw->idx, throw->len*sizeof(struct ptr_len));
33 catch->len += throw->len;
36 void linestack_insert(struct linestack **lls, long pos, char *line, long len)
38 // alloca() was in 32V and Turbo C for DOS, but isn't in posix or c99.
39 // I'm not thrashing the heap for this, but this should work even if
40 // a broken compiler adds gratuitous padding.
46 ls.ls.len = ls.ls.max = 1;
47 ls.ls.idx[0].ptr = line;
48 ls.ls.idx[0].len = len;
49 linestack_addstack(lls, &ls.ls, pos);
52 void linestack_append(struct linestack **lls, char *line)
54 linestack_insert(lls, (*lls)->len, line, strlen(line));
57 struct linestack *linestack_load(char *name)
59 FILE *fp = fopen(name, "r");
64 ls = xzalloc(sizeof(struct linestack));
70 if ((len = getline(&line, (void *)&len, fp))<1) break;
71 if (line[len-1]=='\n') len--;
72 linestack_insert(&ls, ls->len, line, len);
79 // Show width many columns, negative means from right edge.
80 // If out=0 just measure
81 // if escout, send it unprintable chars, returns columns output or -1 for
82 // standard escape: ^X if <32, <XX> if invliad UTF8, U+XXXX if UTF8 !iswprint()
83 // Returns width in columns, moves *str to end of data consumed.
84 int crunch_str(char **str, int width, FILE *out, char *escmore,
85 int (*escout)(FILE *out, int cols, int wc))
87 int columns = 0, col, bytes;
90 for (end = start = *str; *end; columns += col, end += bytes) {
93 if ((bytes = mbrtowc(&wc, end, MB_CUR_MAX, 0))>0 && (col = wcwidth(wc))>=0)
95 if (!escmore || wc>255 || !strchr(escmore, wc)) {
96 if (width-columns<col) break;
97 if (out) fwrite(end, bytes, 1, out);
109 col = escout(out, col, wc);
116 int crunch_escape(FILE *out, int cols, int wc)
121 if (wc<' ') rc = sprintf(buf, "^%c", '@'+wc);
122 else if (wc<256) rc = sprintf(buf, "<%02X>", wc);
123 else rc = sprintf(buf, "U+%04X", wc);
125 if (rc > cols) buf[rc = cols] = 0;
126 if (out) fputs(buf, out);
131 int crunch_rev_escape(FILE *out, int cols, int wc)
136 rc = crunch_escape(out, cols, wc);
142 // Write width chars at start of string to strdout with standard escapes
143 // Returns length in columns so caller can pad it out with spaces.
144 int draw_str(char *start, int width)
146 return crunch_str(&start, width, stdout, 0, crunch_rev_escape);
149 // Return utf8 columns
150 int utf8len(char *str)
152 return crunch_str(&str, INT_MAX, 0, 0, crunch_rev_escape);
155 // Return bytes used by (up to) this many columns
156 int utf8skip(char *str, int width)
160 crunch_str(&s, width, 0, 0, crunch_rev_escape);
165 // Print utf8 to stdout with standard escapes, trimmed to width and padded
166 // out to padto. If padto<0 left justify. Returns columns printed
167 int draw_trim_esc(char *str, int padto, int width, char *escmore,
168 int (*escout)(FILE *out, int cols, int wc))
170 int apad = abs(padto), len = utf8len(str);
172 if (padto<0 && len>width) str += utf8skip(str, len-width);
173 if (len>width) len = width;
175 // Left pad if right justified
176 if (padto>0 && apad>len) printf("%*s", apad-len, "");
177 crunch_str(&str, len, stdout, 0, crunch_rev_escape);
178 if (padto<0 && apad>len) printf("%*s", apad-len, "");
180 return (apad > len) ? apad : len;
183 // draw_trim_esc() with default escape
184 int draw_trim(char *str, int padto, int width)
186 return draw_trim_esc(str, padto, width, 0, 0);