3 // A linestack is an array of struct ptr_len.
5 // Insert one stack into another before position in old stack.
6 // (Does not copy contents of strings, just shuffles index array contents.)
7 void linestack_addstack(struct linestack **lls, struct linestack *throw,
10 struct linestack *catch = *lls;
13 if (pos > catch->len) error_exit("linestack_addstack past end.");
15 // Make a hole, allocating more space if necessary.
16 if (catch->len+throw->len >= catch->max) {
17 // New size rounded up to next multiple of 64, allocate and copy start.
18 catch->max = ((catch->len+throw->len)|63)+1;
19 *lls = xmalloc(sizeof(struct linestack)+catch->max*sizeof(struct ptr_len));
20 memcpy(*lls, catch, sizeof(struct linestack)+pos*sizeof(struct ptr_len));
23 // Copy end (into new allocation if necessary)
24 if (pos != catch->len)
25 memmove((*lls)->idx+pos+throw->len, catch->idx+pos,
26 (catch->len-pos)*sizeof(struct ptr_len));
28 // Cleanup if we had to realloc.
34 // Copy new chunk we made space for
35 memcpy(catch->idx+pos, throw->idx, throw->len*sizeof(struct ptr_len));
36 catch->len += throw->len;
39 // Insert one line/len into a linestack at pos
40 void linestack_insert(struct linestack **lls, long pos, char *line, long len)
42 // alloca() was in 32V and Turbo C for DOS, but isn't in posix or c99.
43 // This allocates enough memory for the linestack to have one ptr_len.
44 // (Even if a compiler adds gratuitous padidng that just makes it bigger.)
50 ls.ls.len = ls.ls.max = 1;
51 ls.ls.idx[0].ptr = line;
52 ls.ls.idx[0].len = len;
53 linestack_addstack(lls, &ls.ls, pos);
56 void linestack_append(struct linestack **lls, char *line)
58 linestack_insert(lls, (*lls)->len, line, strlen(line));
61 struct linestack *linestack_load(char *name)
63 FILE *fp = fopen(name, "r");
68 ls = xzalloc(sizeof(struct linestack));
74 if ((len = getline(&line, (void *)&len, fp))<1) break;
75 if (line[len-1]=='\n') len--;
76 linestack_insert(&ls, ls->len, line, len);
83 // Show width many columns, negative means from right edge, out=0 just measure
84 // if escout, send it unprintable chars, otherwise pass through raw data.
85 // Returns width in columns, moves *str to end of data consumed.
86 int crunch_str(char **str, int width, FILE *out, char *escmore,
87 int (*escout)(FILE *out, int cols, int wc))
89 int columns = 0, col, bytes;
92 for (end = start = *str; *end; columns += col, end += bytes) {
95 if ((bytes = mbrtowc(&wc, end, MB_CUR_MAX, 0))>0 && (col = wcwidth(wc))>=0)
97 if (!escmore || wc>255 || !strchr(escmore, wc)) {
98 if (width-columns<col) break;
99 if (out) fwrite(end, bytes, 1, out);
111 if (escout) col = escout(out, col, wc);
112 else if (out) fwrite(end, bytes, 1, out);
120 // standard escapes: ^X if <32, <XX> if invliad UTF8, U+XXXX if UTF8 !iswprint()
121 int crunch_escape(FILE *out, int cols, int wc)
126 if (wc<' ') rc = sprintf(buf, "^%c", '@'+wc);
127 else if (wc<256) rc = sprintf(buf, "<%02X>", wc);
128 else rc = sprintf(buf, "U+%04X", wc);
130 if (rc > cols) buf[rc = cols] = 0;
131 if (out) fputs(buf, out);
136 // Display "standard" escapes in reverse video.
137 int crunch_rev_escape(FILE *out, int cols, int wc)
142 rc = crunch_escape(out, cols, wc);
148 // Write width chars at start of string to strdout with standard escapes
149 // Returns length in columns so caller can pad it out with spaces.
150 int draw_str(char *start, int width)
152 return crunch_str(&start, width, stdout, 0, crunch_rev_escape);
155 // Return utf8 columns
156 int utf8len(char *str)
158 return crunch_str(&str, INT_MAX, 0, 0, crunch_rev_escape);
161 // Return bytes used by (up to) this many columns
162 int utf8skip(char *str, int width)
166 crunch_str(&s, width, 0, 0, crunch_rev_escape);
171 // Print utf8 to stdout with standard escapes, trimmed to width and padded
172 // out to padto. If padto<0 left justify. Returns columns printed
173 int draw_trim_esc(char *str, int padto, int width, char *escmore,
174 int (*escout)(FILE *out, int cols, int wc))
176 int apad = abs(padto), len = utf8len(str);
178 if (padto>=0 && len>width) str += utf8skip(str, len-width);
179 if (len>width) len = width;
181 // Left pad if right justified
182 if (padto>0 && apad>len) printf("%*s", apad-len, "");
183 crunch_str(&str, len, stdout, 0, crunch_rev_escape);
184 if (padto<0 && apad>len) printf("%*s", apad-len, "");
186 return (apad > len) ? apad : len;
189 // draw_trim_esc() with default escape
190 int draw_trim(char *str, int padto, int width)
192 return draw_trim_esc(str, padto, width, 0, 0);