#define STRICT
#include <locale.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <wchar.h>
#include "utf16_to_iso2022jp_table.h"
#define DEFAULT_BUFF_LEN 4096
struct _OPTIONS
{
char i[DEFAULT_BUFF_LEN];
char o[DEFAULT_BUFF_LEN];
};
typedef struct _OPTIONS T_OPTIONS;
int bytelen(const unsigned char array[], int max_len);
int getEscape(unsigned char dest[], const unsigned char src[]);
int printUsage();
int Utf16ToIso2022jp(char *dest, size_t dest_size, wchar_t *src
, size_t src_size);
int main(int argc, char *argv[])
{
wchar_t buffer[DEFAULT_BUFF_LEN];
int convertCharCount = 0;
char dest[DEFAULT_BUFF_LEN];
size_t dest_size = 0;
int i = 0;
int iResult = 0;
char key[DEFAULT_BUFF_LEN];
int n = 0;
T_OPTIONS options;
FILE *pInputFile = NULL;
FILE *pOutputFile = NULL;
wchar_t *src = NULL;
size_t src_size = 0;
_wsetlocale(LC_ALL, L"japanese");
if (argc < 5)
{
printUsage();
return 0;
}
else
{
memset(&options, 0, sizeof(options));
memset(key, 0, DEFAULT_BUFF_LEN);
for (i = 1; i < argc; i++)
{
if (*argv[i] == '-')
{
strcpy(key, argv[i]);
}
else
{
if (strcmp(key, "-i") == 0)
{
strcpy(options.i, argv[i]);
}
else if (strcmp(key, "-o") == 0)
{
strcpy(options.o, argv[i]);
}
strcpy(key, "");
}
}
}
if (*(options.i) == '\0' || *(options.o) == '\0')
{
printUsage();
return 0;
}
pInputFile = fopen(options.i, "rb");
if (pInputFile == NULL)
{
fwprintf(stderr, L"ファイルが開けません: %s\n", options.i);
}
pOutputFile = fopen(options.o, "wb");
if (pOutputFile == NULL)
{
fwprintf(stderr, L"ファイルが開けません: %s\n", options.o);
}
if ((pInputFile != NULL) && (pOutputFile != NULL))
{
while (feof(pInputFile) == 0)
{
memset(buffer, 0, sizeof(wchar_t) * DEFAULT_BUFF_LEN);
n = DEFAULT_BUFF_LEN - 1;
if (NULL == fgetws(buffer, n, pInputFile))
{
buffer[0] = L'\0';
}
src = buffer;
if (*src == 0xfeff)
{
src++;
}
memset(dest, 0, sizeof(char) * DEFAULT_BUFF_LEN);
dest_size = DEFAULT_BUFF_LEN - 1;
src_size = wcslen(src);
iResult = Utf16ToIso2022jp(dest, dest_size, src, src_size);
if (iResult == (-1))
{
fwprintf(stderr, L"Utf16ToIso2022jp() Failed.\n");
if (pInputFile != NULL)
{
fclose(pInputFile);
pInputFile = NULL;
}
if (pOutputFile != NULL)
{
fclose(pOutputFile);
pOutputFile = NULL;
}
return 1;
}
convertCharCount += iResult;
fputs(dest, pOutputFile);
}
}
if (pInputFile != NULL)
{
fclose(pInputFile);
pInputFile = NULL;
}
if (pOutputFile != NULL)
{
fclose(pOutputFile);
pOutputFile = NULL;
}
wprintf(L"%d バイト変換しました。\n", convertCharCount);
return 0;
}
int bytelen(const unsigned char array[], int max_len)
{
int i = 0;
if (array == NULL)
{
return 0;
}
for (i = 0; i < max_len; i++)
{
if (array[i] == '\0')
{
break;
}
}
return i;
}
int getEscape(unsigned char dest[], const unsigned char src[])
{
int len = 0;
const unsigned char escapeASCII[] = {0x1b, 0x28, 0x42, 0x00};
if ((dest == NULL) || (src == NULL))
{
return (-1);
}
len = bytelen(src, 5);
if ((len == 0) || (len == 1))
{
strcpy((char*)dest, (char*)escapeASCII);
}
else if (3 <= len)
{
dest[0] = src[0];
dest[1] = src[1];
dest[2] = src[2];
dest[3] = '\0';
}
return 0;
}
int printUsage()
{
wprintf(L"utf16_to_iso2022jp.exe\n");
wprintf(L"UTF-16 から ISO-2022-JP へ文字コードを変換します。\n");
wprintf(L"使用法\n");
wprintf(L"> utf16_to_iso2022jp -i 入力ファイル -o 出力ファイル[Enter]\n");
return 0;
}
int Utf16ToIso2022jp(char *dest, size_t dest_size, wchar_t *src
, size_t src_size)
{
long countNeedsBytes = 0;
long cursor = 0;
const unsigned char dummy_code[] = {0x3f, 0x00, 0x00, 0x00, 0x00};
const unsigned char *iso2022jpCode = NULL;
unsigned long firstChar = 0;
unsigned long firstIndex = 0;
int len = 0;
unsigned char newEscape[4];
unsigned long secondChar = 0;
unsigned long secondIndex = 0;
long sizeBytes = 0;
wchar_t unicode = 0x0000;
unsigned char currentEscape[4];
const unsigned char escapeASCII[] = {0x1b, 0x28, 0x42, 0x00};
if (dest_size == 0)
{
}
else
{
if (dest == NULL)
{
return (-1);
}
if (dest_size < 0)
{
return (-1);
}
}
if (src == NULL)
{
return (-1);
}
if (src_size < 0)
{
return (-1);
}
countNeedsBytes = 0;
memset(currentEscape, 0, sizeof(currentEscape));
strcpy((char*)currentEscape, (char*)escapeASCII);
for (cursor = 0; cursor < src_size; cursor++)
{
unicode = *(src + cursor);
iso2022jpCode = dummy_code;
firstChar = (unicode >> 8) & 0x00ff;
secondChar = (unicode) & 0x00ff;
firstIndex = firstChar & 0x00ff;
if (utf16_to_iso2022jp_index_table[firstIndex].byType == 2)
{
secondIndex = utf16_to_iso2022jp_index_table[firstIndex].dwIndex
+ secondChar;
if (utf16_to_iso2022jp_index_table[secondIndex].byType == 3)
{
iso2022jpCode = utf16_to_iso2022jp_table[
utf16_to_iso2022jp_index_table[secondIndex].dwIndex];
}
}
len = bytelen(iso2022jpCode, 5);
if ((unicode == 0) || (len <= 1))
{
sizeBytes = 1;
}
else
{
sizeBytes = 2;
}
getEscape(newEscape, iso2022jpCode);
if (strcmp((char*)newEscape, (char*)currentEscape) != 0)
{
sizeBytes += 3;
}
if (dest_size && (dest_size < (countNeedsBytes + sizeBytes)))
{
return countNeedsBytes;
}
if (dest_size)
{
if (strcmp((char*)newEscape, (char*)currentEscape) != 0)
{
*(dest) = newEscape[0];
dest++;
*(dest) = newEscape[1];
dest++;
*(dest) = newEscape[2];
dest++;
strcpy((char*)currentEscape, (char*)newEscape);
}
len = bytelen(iso2022jpCode, 5);
if (len == 0)
{
if (unicode == 0)
{
*(dest) = iso2022jpCode[0];
dest++;
}
else
{
*(dest) = dummy_code[0];
dest++;
}
}
else if (len == 1)
{
*(dest) = iso2022jpCode[0];
dest++;
}
else
{
*(dest) = iso2022jpCode[3];
dest++;
*(dest) = iso2022jpCode[4];
dest++;
}
}
countNeedsBytes += sizeBytes;
}
if (strcmp((char*)escapeASCII, (char*)currentEscape) != 0)
{
sizeBytes = 3;
if (dest_size && (dest_size < (countNeedsBytes + sizeBytes)))
{
return countNeedsBytes;
}
if (dest_size)
{
*(dest) = escapeASCII[0];
dest++;
*(dest) = escapeASCII[1];
dest++;
*(dest) = escapeASCII[2];
dest++;
}
countNeedsBytes += sizeBytes;
}
return countNeedsBytes;
}
|