OSDN Git Service

Change the character code of windows into UTF-16 completely.
[ffftp/ffftp.git] / mbtoutf8 / mbtoutf8.cpp
index 502cdf1..34a99c5 100644 (file)
-// 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
-//
-
-#include "stdafx.h"
-
-
-int _tmain(int argc, _TCHAR* argv[])
-{
-       FILE* fpIn;
-       FILE* fpOut;
-       char InBuffer[16384];
-       char OutBuffer[65536];
-       int InPosition;
-       int OutPosition;
-       bool bEscape;
-       bool bString;
-       bool bEncoded;
-       int Count;
-       int UTF8Count;
-       wchar_t UTF16Buffer[4];
-       char UTF8Buffer[8];
-       int i;
-       _tsetlocale(LC_ALL, _T(""));
-       if(argc != 3)
-       {
-               _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"));
-               _tprintf(_T("\83R\83}\83\93\83h\83\89\83C\83\93\n"));
-               _tprintf(_T("mbtoutf8 [in] [out]\n"));
-               _tprintf(_T("[in]    \8c³\82Ì\83\\81[\83X\83t\83@\83C\83\8b\82Ì\83t\83@\83C\83\8b\96¼\n"));
-               _tprintf(_T("[out]   \95Û\91\90æ\82Ì\83t\83@\83C\83\8b\96¼\n"));
-               return 0;
-       }
-       fpIn = _tfopen(argv[1], _T("rt"));
-       if(!fpIn)
-       {
-               _tprintf(_T("\83t\83@\83C\83\8b\"%s\"\82ª\8aJ\82¯\82Ü\82¹\82ñ\81B\n"), argv[1]);
-               return 0;
-       }
-       fpOut = _tfopen(argv[2], _T("wt"));
-       if(!fpIn)
-       {
-               _tprintf(_T("\83t\83@\83C\83\8b\"%s\"\82ª\8dì\90¬\82Å\82«\82Ü\82¹\82ñ\81B\n"), argv[2]);
-               return 0;
-       }
-       while(fgets(InBuffer, sizeof(InBuffer) / sizeof(char), fpIn))
-       {
-               InPosition = 0;
-               OutPosition = 0;
-               bEscape = false;
-               bString = false;
-               bEncoded = false;
-               while(InBuffer[InPosition])
-               {
-                       Count = max(mblen(&InBuffer[InPosition], 4), 1);
-                       if(Count == 1)
-                       {
-                               switch(InBuffer[InPosition])
-                               {
-                               case '\\':
-                                       bEscape = !bEscape;
-                                       bEncoded = false;
-                                       strncpy(&OutBuffer[OutPosition], &InBuffer[InPosition], Count);
-                                       InPosition += Count;
-                                       OutPosition += Count;
-                                       break;
-                               case '\"':
-                                       bEscape = false;
-                                       bString = !bString;
-                                       bEncoded = false;
-                                       strncpy(&OutBuffer[OutPosition], &InBuffer[InPosition], Count);
-                                       InPosition += Count;
-                                       OutPosition += Count;
-                                       break;
-                               case '0':
-                               case '1':
-                               case '2':
-                               case '3':
-                               case '4':
-                               case '5':
-                               case '6':
-                               case '7':
-                               case '8':
-                               case '9':
-                               case 'A':
-                               case 'B':
-                               case 'C':
-                               case 'D':
-                               case 'E':
-                               case 'F':
-                               case 'a':
-                               case 'b':
-                               case 'c':
-                               case 'd':
-                               case 'e':
-                               case 'f':
-                                       bEscape = false;
-                                       if(bEncoded)
-                                       {
-                                               for(i = 0; i < Count; i++)
-                                               {
-                                                       sprintf(&OutBuffer[OutPosition], "\\x%02X", (unsigned char)InBuffer[InPosition]);
-                                                       InPosition++;
-                                                       OutPosition += 4;
-                                               }
-                                       }
-                                       else
-                                       {
-                                               strncpy(&OutBuffer[OutPosition], &InBuffer[InPosition], Count);
-                                               InPosition += Count;
-                                               OutPosition += Count;
-                                               break;
-                                       }
-                                       break;
-                               default:
-                                       bEscape = false;
-                                       bEncoded = false;
-                                       strncpy(&OutBuffer[OutPosition], &InBuffer[InPosition], Count);
-                                       InPosition += Count;
-                                       OutPosition += Count;
-                                       break;
-                               }
-                       }
-                       else
-                       {
-                               if(bString)
-                               {
-                                       bEscape = false;
-                                       bEncoded = true;
-                                       UTF8Count = MultiByteToWideChar(CP_ACP, 0, &InBuffer[InPosition], Count, UTF16Buffer, sizeof(UTF16Buffer) / sizeof(wchar_t));
-                                       UTF8Count = WideCharToMultiByte(CP_UTF8, 0, UTF16Buffer, UTF8Count, UTF8Buffer, sizeof(UTF8Buffer) / sizeof(char), NULL, NULL);
-                                       InPosition += Count;
-                                       for(i = 0; i < UTF8Count; i++)
-                                       {
-                                               sprintf(&OutBuffer[OutPosition], "\\x%02X", (unsigned char)UTF8Buffer[i]);
-                                               OutPosition += 4;
-                                       }
-                               }
-                               else
-                               {
-                                       bEscape = false;
-                                       bEncoded = false;
-                                       strncpy(&OutBuffer[OutPosition], &InBuffer[InPosition], Count);
-                                       InPosition += Count;
-                                       OutPosition += Count;
-                               }
-                       }
-               }
-               OutBuffer[OutPosition] = '\0';
-               fputs(OutBuffer, fpOut);
-       }
-       fclose(fpIn);
-       fclose(fpOut);
-       return 0;
-}
-
+// 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
+//\r
+\r
+#include "stdafx.h"\r
+\r
+\r
+int _tmain(int argc, _TCHAR* argv[])\r
+{\r
+       FILE* fpIn;\r
+       FILE* fpOut;\r
+       char InBuffer[16384];\r
+       char OutBuffer[65536];\r
+       int InPosition;\r
+       int OutPosition;\r
+       bool bEscape;\r
+       bool bString;\r
+       bool bEncoded;\r
+       int Count;\r
+       int UTF8Count;\r
+       wchar_t UTF16Buffer[4];\r
+       char UTF8Buffer[8];\r
+       int i;\r
+       _tsetlocale(LC_ALL, _T(""));\r
+       if(argc != 3)\r
+       {\r
+               _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
+               _tprintf(_T("\83R\83}\83\93\83h\83\89\83C\83\93\n"));\r
+               _tprintf(_T("mbtoutf8 [in] [out]\n"));\r
+               _tprintf(_T("[in]    \8c³\82Ì\83\\81[\83X\83t\83@\83C\83\8b\82Ì\83t\83@\83C\83\8b\96¼\n"));\r
+               _tprintf(_T("[out]   \95Û\91\90æ\82Ì\83t\83@\83C\83\8b\96¼\n"));\r
+               return 0;\r
+       }\r
+       fpIn = _tfopen(argv[1], _T("rt"));\r
+       if(!fpIn)\r
+       {\r
+               _tprintf(_T("\83t\83@\83C\83\8b\"%s\"\82ª\8aJ\82¯\82Ü\82¹\82ñ\81B\n"), argv[1]);\r
+               return 0;\r
+       }\r
+       fpOut = _tfopen(argv[2], _T("wt"));\r
+       if(!fpIn)\r
+       {\r
+               _tprintf(_T("\83t\83@\83C\83\8b\"%s\"\82ª\8dì\90¬\82Å\82«\82Ü\82¹\82ñ\81B\n"), argv[2]);\r
+               return 0;\r
+       }\r
+       while(fgets(InBuffer, sizeof(InBuffer) / sizeof(char), fpIn))\r
+       {\r
+               InPosition = 0;\r
+               OutPosition = 0;\r
+               bEscape = false;\r
+               bString = false;\r
+               bEncoded = false;\r
+               while(InBuffer[InPosition])\r
+               {\r
+                       Count = max(mblen(&InBuffer[InPosition], 4), 1);\r
+                       if(Count == 1)\r
+                       {\r
+                               switch(InBuffer[InPosition])\r
+                               {\r
+                               case '\\':\r
+                                       bEscape = !bEscape;\r
+                                       bEncoded = false;\r
+                                       strncpy(&OutBuffer[OutPosition], &InBuffer[InPosition], Count);\r
+                                       InPosition += Count;\r
+                                       OutPosition += Count;\r
+                                       break;\r
+                               case '\"':\r
+                                       bEscape = false;\r
+                                       bString = !bString;\r
+                                       bEncoded = false;\r
+                                       strncpy(&OutBuffer[OutPosition], &InBuffer[InPosition], Count);\r
+                                       InPosition += Count;\r
+                                       OutPosition += Count;\r
+                                       break;\r
+                               case '0':\r
+                               case '1':\r
+                               case '2':\r
+                               case '3':\r
+                               case '4':\r
+                               case '5':\r
+                               case '6':\r
+                               case '7':\r
+                               case '8':\r
+                               case '9':\r
+                               case 'A':\r
+                               case 'B':\r
+                               case 'C':\r
+                               case 'D':\r
+                               case 'E':\r
+                               case 'F':\r
+                               case 'a':\r
+                               case 'b':\r
+                               case 'c':\r
+                               case 'd':\r
+                               case 'e':\r
+                               case 'f':\r
+                                       bEscape = false;\r
+                                       if(bEncoded)\r
+                                       {\r
+                                               for(i = 0; i < Count; i++)\r
+                                               {\r
+                                                       sprintf(&OutBuffer[OutPosition], "\\x%02X", (unsigned char)InBuffer[InPosition]);\r
+                                                       InPosition++;\r
+                                                       OutPosition += 4;\r
+                                               }\r
+                                       }\r
+                                       else\r
+                                       {\r
+                                               strncpy(&OutBuffer[OutPosition], &InBuffer[InPosition], Count);\r
+                                               InPosition += Count;\r
+                                               OutPosition += Count;\r
+                                               break;\r
+                                       }\r
+                                       break;\r
+                               default:\r
+                                       bEscape = false;\r
+                                       bEncoded = false;\r
+                                       strncpy(&OutBuffer[OutPosition], &InBuffer[InPosition], Count);\r
+                                       InPosition += Count;\r
+                                       OutPosition += Count;\r
+                                       break;\r
+                               }\r
+                       }\r
+                       else\r
+                       {\r
+                               if(bString)\r
+                               {\r
+                                       bEscape = false;\r
+                                       bEncoded = true;\r
+                                       UTF8Count = MultiByteToWideChar(CP_ACP, 0, &InBuffer[InPosition], Count, UTF16Buffer, sizeof(UTF16Buffer) / sizeof(wchar_t));\r
+                                       UTF8Count = WideCharToMultiByte(CP_UTF8, 0, UTF16Buffer, UTF8Count, UTF8Buffer, sizeof(UTF8Buffer) / sizeof(char), NULL, NULL);\r
+                                       InPosition += Count;\r
+                                       for(i = 0; i < UTF8Count; i++)\r
+                                       {\r
+                                               sprintf(&OutBuffer[OutPosition], "\\x%02X", (unsigned char)UTF8Buffer[i]);\r
+                                               OutPosition += 4;\r
+                                       }\r
+                               }\r
+                               else\r
+                               {\r
+                                       bEscape = false;\r
+                                       bEncoded = false;\r
+                                       strncpy(&OutBuffer[OutPosition], &InBuffer[InPosition], Count);\r
+                                       InPosition += Count;\r
+                                       OutPosition += Count;\r
+                               }\r
+                       }\r
+               }\r
+               OutBuffer[OutPosition] = '\0';\r
+               fputs(OutBuffer, fpOut);\r
+       }\r
+       fclose(fpIn);\r
+       fclose(fpOut);\r
+       return 0;\r
+}\r
+\r