OSDN Git Service

Add UTF-8 to UTF-16 API bridge.
[ffftp/ffftp.git] / mbtoutf8 / mbtoutf8.cpp
1 // mbtoutf8.cpp : \83R\83\93\83\\81[\83\8b \83A\83v\83\8a\83P\81[\83V\83\87\83\93\82Ì\83G\83\93\83g\83\8a \83|\83C\83\93\83g\82ð\92è\8b`\82µ\82Ü\82·\81B
2 //
3
4 #include "stdafx.h"
5
6
7 int _tmain(int argc, _TCHAR* argv[])
8 {
9         FILE* fpIn;
10         FILE* fpOut;
11         char InBuffer[16384];
12         char OutBuffer[65536];
13         int InPosition;
14         int OutPosition;
15         bool bEscape;
16         bool bString;
17         bool bEncoded;
18         int Count;
19         int UTF8Count;
20         wchar_t UTF16Buffer[4];
21         char UTF8Buffer[8];
22         int i;
23         _tsetlocale(LC_ALL, _T(""));
24         if(argc != 3)
25         {
26                 _tprintf(_T("\83}\83\8b\83`\83o\83C\83g\95\8e\9a\81i\83R\81[\83h\83y\81[\83W932\82Ü\82½\82ÍShift JIS\81j\82Å\8f\91\82©\82ê\82½C\8c¾\8cê\83\\81[\83X\83t\83@\83C\83\8b\93à\82Ì\95\8e\9a\97ñ\83\8a\83e\83\89\83\8b\82ðUTF-8\82É\83G\83\93\83R\81[\83h\82µ\82Ü\82·\81B\n"));
27                 _tprintf(_T("\83R\83}\83\93\83h\83\89\83C\83\93\n"));
28                 _tprintf(_T("mbtoutf8 [in] [out]\n"));
29                 _tprintf(_T("[in]    \8c³\82Ì\83\\81[\83X\83t\83@\83C\83\8b\82Ì\83t\83@\83C\83\8b\96¼\n"));
30                 _tprintf(_T("[out]   \95Û\91\90æ\82Ì\83t\83@\83C\83\8b\96¼\n"));
31                 return 0;
32         }
33         fpIn = _tfopen(argv[1], _T("rt"));
34         if(!fpIn)
35         {
36                 _tprintf(_T("\83t\83@\83C\83\8b\"%s\"\82ª\8aJ\82¯\82Ü\82¹\82ñ\81B\n"), argv[1]);
37                 return 0;
38         }
39         fpOut = _tfopen(argv[2], _T("wt"));
40         if(!fpIn)
41         {
42                 _tprintf(_T("\83t\83@\83C\83\8b\"%s\"\82ª\8dì\90¬\82Å\82«\82Ü\82¹\82ñ\81B\n"), argv[2]);
43                 return 0;
44         }
45         while(fgets(InBuffer, sizeof(InBuffer) / sizeof(char), fpIn))
46         {
47                 InPosition = 0;
48                 OutPosition = 0;
49                 bEscape = false;
50                 bString = false;
51                 bEncoded = false;
52                 while(InBuffer[InPosition])
53                 {
54                         Count = max(mblen(&InBuffer[InPosition], 4), 1);
55                         if(Count == 1)
56                         {
57                                 switch(InBuffer[InPosition])
58                                 {
59                                 case '\\':
60                                         bEscape = !bEscape;
61                                         bEncoded = false;
62                                         strncpy(&OutBuffer[OutPosition], &InBuffer[InPosition], Count);
63                                         InPosition += Count;
64                                         OutPosition += Count;
65                                         break;
66                                 case '\"':
67                                         bEscape = false;
68                                         bString = !bString;
69                                         bEncoded = false;
70                                         strncpy(&OutBuffer[OutPosition], &InBuffer[InPosition], Count);
71                                         InPosition += Count;
72                                         OutPosition += Count;
73                                         break;
74                                 case '0':
75                                 case '1':
76                                 case '2':
77                                 case '3':
78                                 case '4':
79                                 case '5':
80                                 case '6':
81                                 case '7':
82                                 case '8':
83                                 case '9':
84                                 case 'A':
85                                 case 'B':
86                                 case 'C':
87                                 case 'D':
88                                 case 'E':
89                                 case 'F':
90                                 case 'a':
91                                 case 'b':
92                                 case 'c':
93                                 case 'd':
94                                 case 'e':
95                                 case 'f':
96                                         bEscape = false;
97                                         if(bEncoded)
98                                         {
99                                                 for(i = 0; i < Count; i++)
100                                                 {
101                                                         sprintf(&OutBuffer[OutPosition], "\\x%02X", (unsigned char)InBuffer[InPosition]);
102                                                         InPosition++;
103                                                         OutPosition += 4;
104                                                 }
105                                         }
106                                         else
107                                         {
108                                                 strncpy(&OutBuffer[OutPosition], &InBuffer[InPosition], Count);
109                                                 InPosition += Count;
110                                                 OutPosition += Count;
111                                                 break;
112                                         }
113                                         break;
114                                 default:
115                                         bEscape = false;
116                                         bEncoded = false;
117                                         strncpy(&OutBuffer[OutPosition], &InBuffer[InPosition], Count);
118                                         InPosition += Count;
119                                         OutPosition += Count;
120                                         break;
121                                 }
122                         }
123                         else
124                         {
125                                 if(bString)
126                                 {
127                                         bEscape = false;
128                                         bEncoded = true;
129                                         UTF8Count = MultiByteToWideChar(CP_ACP, 0, &InBuffer[InPosition], Count, UTF16Buffer, sizeof(UTF16Buffer) / sizeof(wchar_t));
130                                         UTF8Count = WideCharToMultiByte(CP_UTF8, 0, UTF16Buffer, UTF8Count, UTF8Buffer, sizeof(UTF8Buffer) / sizeof(char), NULL, NULL);
131                                         InPosition += Count;
132                                         for(i = 0; i < UTF8Count; i++)
133                                         {
134                                                 sprintf(&OutBuffer[OutPosition], "\\x%02X", (unsigned char)UTF8Buffer[i]);
135                                                 OutPosition += 4;
136                                         }
137                                 }
138                                 else
139                                 {
140                                         bEscape = false;
141                                         bEncoded = false;
142                                         strncpy(&OutBuffer[OutPosition], &InBuffer[InPosition], Count);
143                                         InPosition += Count;
144                                         OutPosition += Count;
145                                 }
146                         }
147                 }
148                 OutBuffer[OutPosition] = '\0';
149                 fputs(OutBuffer, fpOut);
150         }
151         fclose(fpIn);
152         fclose(fpOut);
153         return 0;
154 }
155