#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifndef FALSE
#define FALSE 0
#endif
#ifndef TRUE
#define TRUE 1
#endif
#define DEFAULT_BUFF_LEN 512
long min(long a, long b)
{
long c;
c = (a < b)?(a):(b);
return c;
}
int Utf8ToUtf16(wchar_t *dest, size_t dest_size, char *src, size_t src_size);
int main(int argc, char *argv[])
{
char instr[DEFAULT_BUFF_LEN];
wchar_t outstr[DEFAULT_BUFF_LEN];
wchar_t bom;
char outbuffbom[4];
FILE *pFile;
long sizeFile;
size_t countRead;
int iResult = 0;
int instr_size = 0;
memset(instr, 0, sizeof(instr));
pFile = fopen("data.txt", "rb");
if (pFile == NULL)
{
perror("ファイルが開けません: data.txt");
return 1;
}
fseek(pFile, 0, SEEK_END);
sizeFile = ftell(pFile);
fseek(pFile, 0, SEEK_SET);
countRead = fread(instr, min(sizeFile, DEFAULT_BUFF_LEN), 1, pFile);
fclose(pFile);
if (countRead == 0)
{
perror("ファイル読み込み失敗: data.txt");
return 1;
}
instr_size = min(sizeFile, DEFAULT_BUFF_LEN) * countRead;
memset(outstr, 0, sizeof(outstr));
iResult = Utf8ToUtf16(outstr, sizeof(outstr), instr, instr_size);
if (iResult == FALSE)
{
perror("Utf8ToUtf16() Failed.\n");
return 1;
}
printf("%d 文字変換しました。\n", iResult);
pFile = fopen("out.txt", "wb");
if (pFile == NULL)
{
perror("ファイルが開けません: out.txt");
return 1;
}
bom = 0xfeff;
memcpy(outbuffbom, &bom, sizeof(wchar_t));
fwrite(outbuffbom, sizeof(wchar_t), 1, pFile);
fwrite(outstr, iResult * 2, 1, pFile);
fclose(pFile);
}
int Utf8ToUtf16(wchar_t *dest, size_t dest_size, char *src, size_t src_size)
{
const int nMaxReadSize = 6;
size_t countNeedsWords;
size_t cursor;
char chBuffer[nMaxReadSize];
size_t nReadDataSize;
int iCh1;
int sizeBytes;
wchar_t wcWork1, wcWork2, wcWork3;
if (dest_size == 0)
{
}
else
{
if (dest == NULL)
{
return FALSE;
}
if (dest_size < 0)
{
return FALSE;
}
}
if (src == NULL)
{
return FALSE;
}
if (src_size < 1)
{
return FALSE;
}
if ((*src == '\xef') && (*(src + 1) == '\xbb') && (*(src + 2) == '\xbf'))
{
src += 3;
}
countNeedsWords = 0;
for (cursor = 0; cursor < src_size;)
{
nReadDataSize = (nMaxReadSize < (src_size - cursor))?(nMaxReadSize):(src_size - cursor);
memcpy(chBuffer, (src + cursor), nReadDataSize);
memset(chBuffer + nReadDataSize, 0, sizeof(chBuffer) - nReadDataSize);
iCh1 = ((int)(*chBuffer)) & 0x00ff;
iCh1 = ~iCh1;
if (iCh1 & 0x0080)
{
sizeBytes = 1;
}
else if (iCh1 & 0x0040)
{
return FALSE;
}
else if (iCh1 & 0x0020)
{
sizeBytes = 2;
}
else if (iCh1 & 0x0010)
{
sizeBytes = 3;
}
else if (iCh1 & 0x0008)
{
sizeBytes = 4;
}
else if (iCh1 & 0x0004)
{
sizeBytes = 5;
}
else if (iCh1 & 0x0002)
{
sizeBytes = 6;
}
else
{
return FALSE;
}
if (dest_size && (dest_size < (countNeedsWords + 1)))
{
return countNeedsWords;
}
if (dest_size) switch (sizeBytes)
{
case 1:
*dest = ((wchar_t)(chBuffer[0])) & (wchar_t)0x00ff;
dest++;
break;
case 2:
wcWork1 = ((wchar_t)(chBuffer[0])) & (wchar_t)0x00ff;
wcWork2 = ((wchar_t)(chBuffer[1])) & (wchar_t)0x00ff;
wcWork1 <<= 6;
wcWork1 &= 0x07c0;
wcWork2 &= 0x003f;
*dest = wcWork1 | wcWork2;
dest++;
break;
case 3:
wcWork1 = ((wchar_t)(chBuffer[0])) & (wchar_t)0x00ff;
wcWork2 = ((wchar_t)(chBuffer[1])) & (wchar_t)0x00ff;
wcWork3 = ((wchar_t)(chBuffer[2])) & (wchar_t)0x00ff;
wcWork1 <<= 12;
wcWork1 &= 0xf000;
wcWork2 <<= 6;
wcWork2 &= 0x0fc0;
wcWork3 &= 0x003f;
*dest = wcWork1 | wcWork2 | wcWork3;
dest++;
break;
case 4:
case 5:
case 6:
default:
*dest = (wchar_t)0xff1f;
dest++;
break;
}
countNeedsWords++;
cursor += sizeBytes;
}
return countNeedsWords;
}
|