Last Update: Jan.10,2001

C Source Library

「LSI C-86 Ver.3.30c 試食版」、「Borland C++ 5.5」、「Microsoft Visual C++ 6.0」でコンパイル出来ます。

■ 半角カナを全角カナにする

/*
**  han2zen.c : 半角カナを全角カナにする
**
**  Copyright (c) 2000 AOK. <soft@aokura.com>
**  
**      2000.12.11 - 初版
**
**  使い方:HAN2ZEN 入力ファイル名
**
**  (拡張子を .new に置き換えて出力します)
**
*/

#include <stdio.h>
#include <string.h>

#ifndef LSI_C   /* Borland C++ 5.5 */
    #include <mbctype.h>
    #include <mbstring.h>
    #define CT_ANK          _MBC_SINGLE
    #define CT_KJ1          _MBC_LEAD
    #define CT_KJ2          _MBC_TRAIL
    #define CT_ILGL         _MBC_ILLEGAL
    #define chkctype(x, y)  _mbbtype(x, y)
    #define iskana(x)       _ismbbkana(x)
#else           /* LSI C-86 */
    #include <jctype.h>
#endif

static unsigned int z[] = {
    0x8142/*。*/, 0x8175/*「*/, 0x8176/*」*/, 0x8141/*、*/, 0x8145/*・*/,
    0x8392/*ヲ*/, 0x8340/*ァ*/, 0x8342/*ィ*/, 0x8344/*ゥ*/, 0x8346/*ェ*/,
    0x8348/*ォ*/, 0x8383/*ャ*/, 0x8385/*ュ*/, 0x8387/*ョ*/, 0x8362/*ッ*/,
    0x815b/*ー*/, 0x8341/*ア*/, 0x8343/*イ*/, 0x8345/*ウ*/, 0x8347/*エ*/,
    0x8349/*オ*/, 0x834a/*カ*/, 0x834c/*キ*/, 0x834e/*ク*/, 0x8350/*ケ*/,
    0x8352/*コ*/, 0x8354/*サ*/, 0x8356/*シ*/, 0x8358/*ス*/, 0x835a/*セ*/,
    0x835c/*ソ*/, 0x835e/*タ*/, 0x8360/*チ*/, 0x8363/*ツ*/, 0x8365/*テ*/,
    0x8367/*ト*/, 0x8369/*ナ*/, 0x836a/*ニ*/, 0x836b/*ヌ*/, 0x836c/*ネ*/,
    0x836d/*ノ*/, 0x836e/*ハ*/, 0x8371/*ヒ*/, 0x8374/*フ*/, 0x8377/*ヘ*/,
    0x837a/*ホ*/, 0x837d/*マ*/, 0x837e/*ミ*/, 0x8380/*ム*/, 0x8381/*メ*/,
    0x8382/*モ*/, 0x8384/*ヤ*/, 0x8386/*ユ*/, 0x8388/*ヨ*/, 0x8389/*ラ*/,
    0x838a/*リ*/, 0x838b/*ル*/, 0x838c/*レ*/, 0x838d/*ロ*/, 0x838f/*ワ*/,
    0x8393/*ン*/, 0x814a/*゛*/, 0x814b/*゜*/,
};

int main(int argc, char **argv)
{
    FILE *fin, *fout;
    char filename[FILENAME_MAX+1];
    unsigned int code;
    int c, mode = CT_ANK;
    unsigned char ch, next;

    if (argc < 2)
    {
        puts("パラメータが足りません.\n使い方:HAN2ZEN filename");
        return 1;
    }
    strcpy(filename, *(++argv));
    strcpy(strrchr(filename, '.'), ".new");
    if ((fin = fopen(*argv, "r")) == NULL)
    {
        fprintf(stderr, "エラー:%sを開けません.\n", *argv);
        return 1;
    }
    if ((fout = fopen(filename, "w")) == NULL)
    {
        fprintf(stderr, "エラー:%sを開けません.\n", filename);
        fclose(fin);
        return 1;
    }
    
    while ((c = getc(fin)) != EOF)
    {
        ch = (unsigned char)c;
        mode = chkctype(ch, mode);
        if (mode == CT_ANK && iskana(ch))
        {
            code = z[ch - 0xa1];
            c = getc(fin);
            next = (unsigned char)c;
            if (next == 0xde /* 濁点 */ &&
                (ch == 0xb3 || (ch >= 0xb6 && ch <=0xc4) || (ch >= 0xca && ch <= 0xce)))
            {
                if (ch == 0xb3)     /* ウ */
                    code = 0x8394;  /* ヴ */
                else
                    code += 1;
            }
            else if (next == 0xdf /* 半濁点 */ &&
                (ch >= 0xca && ch <= 0xce))
            {
                code += 2;
            }
            else
            {
                ungetc(next, fin);  /* 1文字戻す */
            }
                    
            putc((char)(code>>8), fout);
            putc((char)code, fout);
            if (c == EOF) break;
        }
        else
        {
            putc(ch, fout);
        }   
    }

    fclose(fin);
    fclose(fout);
    puts("変換に成功しました.");
    
    return 0;
}