Mercurial > ~dholland > hg > tradcpp > index.cgi
comparison output.c @ 27:01c3a2088ab4
fix some more bugs
author | David A. Holland |
---|---|
date | Mon, 20 Dec 2010 05:01:18 -0500 |
parents | daa801fe719e |
children | 76c114899f63 |
comparison
equal
deleted
inserted
replaced
26:ac45dcc57ff9 | 27:01c3a2088ab4 |
---|---|
1 #include <string.h> | |
1 #include <unistd.h> | 2 #include <unistd.h> |
2 #include <fcntl.h> | 3 #include <fcntl.h> |
3 #include <err.h> | 4 #include <err.h> |
4 | 5 |
5 #include "utils.h" | 6 #include "utils.h" |
7 #include "place.h" | 8 #include "place.h" |
8 #include "output.h" | 9 #include "output.h" |
9 | 10 |
10 static int outputfd = -1; | 11 static int outputfd = -1; |
11 static bool incomment = false; | 12 static bool incomment = false; |
13 static char *linebuf; | |
14 static size_t linebufpos, linebufmax; | |
15 static struct place linebufplace; | |
12 | 16 |
13 static | 17 static |
14 void | 18 void |
15 output_open(void) | 19 output_open(void) |
16 { | 20 { |
40 | 44 |
41 if (outputfd < 0) { | 45 if (outputfd < 0) { |
42 output_open(); | 46 output_open(); |
43 } | 47 } |
44 | 48 |
45 /* XXX this will often come by ones and twos, should buffer */ | |
46 | |
47 done = 0; | 49 done = 0; |
48 while (done < len) { | 50 while (done < len) { |
49 result = write(outputfd, buf+done, len-done); | 51 result = write(outputfd, buf+done, len-done); |
50 if (result == -1) { | 52 if (result == -1) { |
51 warn("%s: write", mode.output_file); | 53 warn("%s: write", mode.output_file); |
60 } | 62 } |
61 done += (size_t)result; | 63 done += (size_t)result; |
62 } | 64 } |
63 } | 65 } |
64 | 66 |
67 | |
68 static | |
65 void | 69 void |
66 output(const struct place *p, const char *buf, size_t len) | 70 filter_output(const struct place *p, const char *buf, size_t len) |
67 { | 71 { |
68 size_t pos, start; | 72 size_t pos, start; |
69 struct place p2; | 73 struct place p2; |
70 | 74 |
71 start = 0; | 75 start = 0; |
95 pos += 2; | 99 pos += 2; |
96 if (mode.output_retain_comments) { | 100 if (mode.output_retain_comments) { |
97 dowrite(buf + start, pos - start); | 101 dowrite(buf + start, pos - start); |
98 } | 102 } |
99 start = pos; | 103 start = pos; |
100 pos += 2; | |
101 incomment = false; | 104 incomment = false; |
102 /* cancel out the loop's pos++ */ | 105 /* cancel out the loop's pos++ */ |
103 pos--; | 106 pos--; |
104 continue; | 107 continue; |
105 } | 108 } |
113 } | 116 } |
114 } | 117 } |
115 } | 118 } |
116 | 119 |
117 void | 120 void |
121 output(const struct place *p, const char *buf, size_t len) | |
122 { | |
123 if (linebufpos + len > linebufmax) { | |
124 if (linebufmax == 0) { | |
125 linebufmax = 64; | |
126 } | |
127 while (linebufpos + len > linebufmax) { | |
128 linebufmax *= 2; | |
129 } | |
130 linebuf = dorealloc(linebuf, linebufmax); | |
131 } | |
132 if (linebufpos == 0) { | |
133 linebufplace = *p; | |
134 } | |
135 memcpy(linebuf + linebufpos, buf, len); | |
136 linebufpos += len; | |
137 | |
138 if (len == 1 && buf[0] == '\n') { | |
139 filter_output(&linebufplace, linebuf, linebufpos); | |
140 linebufpos = 0; | |
141 } | |
142 } | |
143 | |
144 void | |
118 output_eof(void) | 145 output_eof(void) |
119 { | 146 { |
120 if (mode.output_file != NULL && outputfd >= 0) { | 147 if (mode.output_file != NULL && outputfd >= 0) { |
121 close(outputfd); | 148 close(outputfd); |
122 } | 149 } |