OSDN Git Service

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