Browse Source

Use wchar_t in ttpre

June 2 months ago
parent
commit
f6b6b79ea8
Signed by: Curtis McEnroe <june@causal.agency> GPG Key ID: CEA2F97ADCFCD77C
1 changed files with 18 additions and 15 deletions
  1. 18
    15
      bin/ttpre.c

+ 18
- 15
bin/ttpre.c View File

@@ -14,41 +14,44 @@
14 14
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15 15
  */
16 16
 
17
+#include <locale.h>
17 18
 #include <stdio.h>
18 19
 #include <stdlib.h>
19 20
 #include <string.h>
21
+#include <wchar.h>
20 22
 
21
-static void put(const char *tag, char ch) {
23
+static void put(const char *tag, wchar_t ch) {
22 24
 	if (tag) printf("<%s>", tag);
23 25
 	switch (ch) {
24
-		break; case '&': printf("&amp;");
25
-		break; case '<': printf("&lt;");
26
-		break; case '>': printf("&gt;");
27
-		break; default:  printf("%c", ch);
26
+		break; case L'&': printf("&amp;");
27
+		break; case L'<': printf("&lt;");
28
+		break; case L'>': printf("&gt;");
29
+		break; default:   printf("%lc", ch);
28 30
 	}
29 31
 	if (tag) printf("</%s>", tag);
30 32
 }
31 33
 
32
-static void push(char ch) {
33
-	static char q[3];
34
-	if (q[1] == '\b' && q[0] == '_') {
34
+static void push(wchar_t ch) {
35
+	static wchar_t q[3];
36
+	if (q[1] == L'\b' && q[0] == L'_') {
35 37
 		put("i", q[2]);
36
-		memset(q, 0, 3);
37
-	} else if (q[1] == '\b' && q[0] == q[2]) {
38
+		memset(q, 0, sizeof(q));
39
+	} else if (q[1] == L'\b' && q[0] == q[2]) {
38 40
 		put("b", q[2]);
39
-		memset(q, 0, 3);
41
+		memset(q, 0, sizeof(q));
40 42
 	} else if (q[0]) {
41 43
 		put(NULL, q[0]);
42 44
 	}
43
-	memmove(q, &q[1], 2);
45
+	memmove(q, &q[1], sizeof(q) - sizeof(wchar_t));
44 46
 	q[2] = ch;
45 47
 }
46 48
 
47 49
 int main() {
50
+	setlocale(LC_CTYPE, "");
48 51
 	printf("<pre>");
49
-	char ch;
50
-	while (EOF != (ch = getchar_unlocked())) push(ch);
51
-	push('\0'); push('\0'); push('\0');
52
+	wchar_t ch;
53
+	while (WEOF != (ch = getwchar())) push(ch);
54
+	push(0); push(0); push(0);
52 55
 	printf("</pre>\n");
53 56
 	return EXIT_SUCCESS;
54 57
 }

Loading…
Cancel
Save