Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 93 lines (84 sloc) 1.514 kb
95ecd63 Fix the bug with input-line overrun from magic \%03o codes
ec429 authored
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include "ctbuf.h"
4
5 void ct_init_char(ctchar **buf, size_t *l, size_t *i)
6 {
7 *l=80;
8 *buf=malloc(*l*sizeof(ctchar));
9 (*buf)[0].d=0;
10 *i=0;
11 }
12
13 void ct_append_char(ctchar **buf, size_t *l, size_t *i, char d)
14 {
15 ct_append_char_c(buf, l, i, (*buf&&*i)?(*buf)[(*i)-1].c:(colour){.fore=7,.back=0,.hi=false,.ul=false}, d);
16 }
17
18 void ct_append_char_c(ctchar **buf, size_t *l, size_t *i, colour c, char d)
19 {
20 if(*buf)
21 {
22 (*buf)[*i]=(ctchar){.c=c, .d=d};
23 (*i)++;
24 }
25 else
26 {
27 ct_init_char(buf, l, i);
28 ct_append_char_c(buf, l, i, c, d);
29 }
30 ctchar *nbuf=*buf;
31 if((*i)>=(*l))
32 {
33 *l=*i*2;
34 nbuf=realloc(*buf, *l*sizeof(ctchar));
35 }
36 if(nbuf)
37 {
38 *buf=nbuf;
39 (*buf)[*i].d=0;
40 }
41 else
42 {
43 free(*buf);
44 ct_init_char(buf, l, i);
45 }
46 }
47
48 void ct_append_str(ctchar **buf, size_t *l, size_t *i, const char *str)
49 {
50 while(str && *str)
51 {
52 ct_append_char(buf, l, i, *str++);
53 }
54 }
55
56 void ct_append_str_c(ctchar **buf, size_t *l, size_t *i, colour c, const char *str)
57 {
58 while(str && *str)
59 {
60 ct_append_char_c(buf, l, i, c, *str++);
61 }
62 }
63
64 void ct_putchar(ctchar a)
65 {
66 setcolour(a.c);
67 putchar(a.d);
68 }
69
70 void ct_puts(const ctchar *buf)
71 {
72 if(!buf) return;
73 size_t i=0;
74 while(buf[i].d)
75 {
76 if(!(i&&eq_colour(buf[i].c, buf[i-1].c)))
77 setcolour(buf[i].c);
78 putchar(buf[i++].d);
79 }
80 }
81
82 void ct_putsn(const ctchar *buf, size_t n)
83 {
84 if(!buf) return;
85 size_t i=0;
86 while((buf[i].d)&&(i<n))
87 {
88 if(!(i&&eq_colour(buf[i].c, buf[i-1].c)))
89 setcolour(buf[i].c);
90 putchar(buf[i++].d);
91 }
92 }
Something went wrong with that request. Please try again.