JFP 開発ガイド

プログラム例

例 4-2 では、iconv() インタフェースを使用して iconv(1) コマンドのサブセットに相当するフィルタを作成します。これらの API を使用する場合は、iconv.h ヘッダーファイルを取り込む必要があります。一般的な国際化 API と異なり、iconv() では、変換前と変換後の文字集合とエンコーディングに関する情報を変換記述子を通して取得します。したがって、次のプログラム例でも setlocale() は呼び出されていません。


例 4-2 汎用コード変換

sun% cat my_iconv.c

/*
 * Read lines from a stdin and convert the encoding.
 * It is assumed that each line has at most BUFSIZ - 1
 * byte length.
 * Both of source and destination encodings are passed
 * from the command line.
 *
 * Note:        Calling iconv() itself doesn't need to call
 *              setlocale() in advance.
 */

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <unistd.h>
#include <iconv.h>
int
main(int argc, char *argv[])
{
        iconv_t icv_hook;
        char in_buf[BUFSIZ];
        char out_buf[BUFSIZ];
        char *inp;
        char *outp;
        char *from_code;
        char *to_code;
        extern char *optarg;
        extern int optind;
        size_t ret_val;
        size_t in_buf_left;
        size_t out_buf_left;
        int i;
        
        if (argc != 5) {
                fprintf(stderr, "usage: %s -f  -t ¥n", argv[0]);
                exit(-1);
        }
        while ((i = getopt(argc, argv, "f:t:")) != EOF) 
                switch (i) {
                case `f':
                        from_code = optarg;
                        break;
                case `t':
                        to_code = optarg;
                        break;
                default:
                        fprintf(stderr, "usage: %s -f  -t ¥n", argv[0]);
                        exit(-1);
                }
        icv_hook = iconv_open(to_code, from_code);
        if (icv_hook == (iconv_t)-1) {
                perror("iconv_open()");
                exit(-1);
        }
        

        i = 0;
        while(fgets(in_buf, BUFSIZ, stdin) != NULL){
                if (!in_buf[0]) {
                        perror("fgets()");
                        exit(-1);
                }
                i++;
                memset(out_buf, 0, BUFSIZ);
                in_buf_left = strlen(in_buf);
                out_buf_left = BUFSIZ;
                inp = in_buf;
                outp = out_buf;
                errno = 0;
                ret_val = iconv(icv_hook,
                                (const char **)inp, in_buf_left, outp, out_buf_left);
                if (ret_val == (size_t)-1) {
                        if (errno == EILSEQ)
                                perror("EILSEQ");
                        else if (errno == E2BIG)
                                perror("E2BIG");
                        else if (errno == EINVAL)
                                perror("EINVAL");
                        fprintf(stderr, "Line number is %d¥n", i);
                        exit(-1);
                }
                write(STDOUT_FILENO, out_buf, (BUFSIZ - out_buf_left));
        }
        iconv_close(icv_hook);
        return(0);
}
sun% cat file3
新しいシステム*は現在のネットワーク*環境を変えることなく
インターネット*とのシームレス*な接続を可能にします。また
セキュリティ*の問題も新しい認証テクノロジー*を用いることで
アドミニストレータ*の負担を減らしています。
sun% cc -o my_iconv my_iconv.c
sun% cat file3 | ./my_iconv -f eucJP -t PCK | ./my_iconv -f PCK -t eucJP
新しいシステム*は現在のネットワーク*環境を変えることなく
インターネット*とのシームレス*な接続を可能にします。また
セキュリティ*の問題も新しい認証テクノロジー*を用いることで
アドミニストレータ*の負担を減らしています。
                


注意 - 注意 -

* の部分のカタカナは、半角カタカナになります。