Mercurial > ~dholland > hg > tradcpp > index.cgi
comparison output.c @ 21:e3fab8f1b52c
strip comments.
author | David A. Holland |
---|---|
date | Mon, 20 Dec 2010 04:15:02 -0500 |
parents | 40748b097655 |
children | cef2dc916269 |
comparison
equal
deleted
inserted
replaced
20:40748b097655 | 21:e3fab8f1b52c |
---|---|
2 #include <fcntl.h> | 2 #include <fcntl.h> |
3 #include <err.h> | 3 #include <err.h> |
4 | 4 |
5 #include "utils.h" | 5 #include "utils.h" |
6 #include "mode.h" | 6 #include "mode.h" |
7 #include "place.h" | |
7 #include "output.h" | 8 #include "output.h" |
8 | 9 |
9 static int outputfd = -1; | 10 static int outputfd = -1; |
11 static bool incomment = false; | |
10 | 12 |
11 static | 13 static |
12 void | 14 void |
13 output_open(void) | 15 output_open(void) |
14 { | 16 { |
17 warn("%s", mode.output_file); | 19 warn("%s", mode.output_file); |
18 die(); | 20 die(); |
19 } | 21 } |
20 } | 22 } |
21 | 23 |
24 static | |
22 void | 25 void |
23 output(const char *buf, size_t len) | 26 dowrite(const char *buf, size_t len) |
24 { | 27 { |
25 size_t done; | 28 size_t done; |
26 ssize_t result; | 29 ssize_t result; |
27 static unsigned write_errors = 0; | 30 static unsigned write_errors = 0; |
28 | 31 |
49 done += (size_t)result; | 52 done += (size_t)result; |
50 } | 53 } |
51 } | 54 } |
52 | 55 |
53 void | 56 void |
57 output(const struct place *p, const char *buf, size_t len) | |
58 { | |
59 size_t pos, start; | |
60 struct place p2; | |
61 | |
62 start = 0; | |
63 for (pos = 0; pos < len - 1; pos++) { | |
64 if (buf[pos] == '/' && buf[pos+1] == '*') { | |
65 if (incomment && warns.nestcomment) { | |
66 p2 = *p; | |
67 p2.column += pos; | |
68 complain(p, "Warning: %c%c within comment", | |
69 '/', '*'); | |
70 if (mode.werror) { | |
71 complain_failed(); | |
72 } | |
73 } else if (!incomment) { | |
74 if (pos > start) { | |
75 dowrite(buf + start, pos - start); | |
76 } | |
77 start = pos; | |
78 pos += 2; | |
79 incomment = true; | |
80 /* cancel out the loop's pos++ */ | |
81 pos--; | |
82 continue; | |
83 } | |
84 } else if (buf[pos] == '*' && buf[pos+1] == '/') { | |
85 if (incomment) { | |
86 pos += 2; | |
87 if (mode.output_retain_comments) { | |
88 dowrite(buf + start, pos - start); | |
89 } | |
90 start = pos; | |
91 pos += 2; | |
92 incomment = false; | |
93 /* cancel out the loop's pos++ */ | |
94 pos--; | |
95 continue; | |
96 } | |
97 } | |
98 } | |
99 } | |
100 | |
101 void | |
54 output_eof(void) | 102 output_eof(void) |
55 { | 103 { |
56 if (outputfd >= 0) { | 104 if (outputfd >= 0) { |
57 close(outputfd); | 105 close(outputfd); |
58 } | 106 } |