Browse Source

utf8 fputc

vampirefrog 1 year ago
parent
commit
8e076e2763
3 changed files with 15 additions and 8 deletions
  1. 1
    8
      sjis2utf8.c
  2. 11
    0
      utf8.c
  3. 3
    0
      utf8.h

+ 1
- 8
sjis2utf8.c View File

@@ -1,13 +1,6 @@
1 1
 #include "sjis.h"
2 2
 #include "utf8.h"
3 3
 
4
-void put_utf8(FILE *f, int c) {
5
-	char buf[4];
6
-	int chars = utf8_encode(c, buf);
7
-	if(chars) {
8
-		fwrite(buf, chars, 1, f);
9
-	}
10
-}
11 4
 
12 5
 int main(int argc, char **argv) {
13 6
 	FILE *in = stdin, *out = stdout;
@@ -20,7 +13,7 @@ int main(int argc, char **argv) {
20 13
 		if(SJIS_FIRST_CHAR(b)) {
21 14
 			last_byte = b;
22 15
 		} else {
23
-			put_utf8(out, sjis_char_to_unicode((last_byte << 8) | b));
16
+			utf8_fputc(sjis_char_to_unicode((last_byte << 8) | b), out);
24 17
 			last_byte = 0;
25 18
 		}
26 19
 	}

+ 11
- 0
utf8.c View File

@@ -25,3 +25,14 @@ int utf8_encode(int c, char *buf) {
25 25
 	buf[0] = c;
26 26
 	return 1;
27 27
 }
28
+
29
+int utf8_fputc(int c, FILE *f) {
30
+	char buf[4];
31
+	int chars = utf8_encode(c, buf);
32
+	if(chars) {
33
+		if(fwrite(buf, chars, 1, f) < 1)
34
+			return EOF;
35
+		return c;
36
+	}
37
+	return EOF;
38
+}

+ 3
- 0
utf8.h View File

@@ -1,5 +1,8 @@
1 1
 #pragma once
2 2
 
3
+#include <stdio.h>
4
+
3 5
 int utf8_encode(int c, char *buf);
4 6
 int utf8_strlen(char *buf);
5 7
 int utf8_strnlen(char *buf, int n);
8
+int utf8_fputc(int c, FILE *f);

Loading…
Cancel
Save