Skip to content

Instantly share code, notes, and snippets.

@brokendish
Created September 8, 2013 14:33
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brokendish/6485151 to your computer and use it in GitHub Desktop.
Save brokendish/6485151 to your computer and use it in GitHub Desktop.
文字コード変換を行う一括で文字コード変換を行い、変換不可文字があった場合は「@」に置き換える。このモジュールは、1文字ずつの変換ではなく全文を変換に通し、変換エラーがあった場合はエラーポインターの位置に「@」を入れ込む
#include <stdio.h>
#include <iconv.h>
#include <locale.h>
#include <stdlib.h>
#include <string.h>
#include <wchar.h>
/* fgetsの指定バイト数 */
#define READ_SIZE 4096
/* 置き換え文字のサイズ */
#define CONTENT_SIZE 4096
/*ダメ文字置き換え文字*/
#define DAMEMOJI "?"
char outbuf[CONTENT_SIZE];
int convert(char const *src,
char const *dest,
char *text,
char *buf,
size_t bufsize);
void chr_conv(char const *str,char *hairtetu[],int cnt);
/*
---------------------------------------------------------------
文字コード変換を行う
文字コード変換を行う。変換不可文字があった場合は「@」に置き換える
---------------------------------------------------------------
*/
main(void)
{
int ret;
char *moji;
char *moji_to;
/*ロケールの設定*/
char *loc;
//loc = setlocale( LC_ALL, "ja_JP.sjis" );
loc = setlocale( LC_ALL, "" ); /*システムのlocale設定を使用する*/
printf("LOCALE====%s\n",loc);
/*引数の判定*/
int proc = atoi(argv[1]);
//1:utf8-->cp932
//2:cp932-->utf8
char *ifrom="";
char *ito="";
switch(proc){
case 1:
loc = setlocale( LC_ALL, "ja_JP.utf8" ); /*ロケールをUTF8に変更*/
ifrom="UTF-8";
ito="cp932";
break;
case 2:
loc = setlocale( LC_ALL, "ja_JP.sjis" ); /*ロケールをSJISに変更*/
ifrom="cp932";
ito="UTF-8";
break;
default:
break;
}
/* 標準入力からデータを取得(パイプ)-----------------------------------S
*/
char *buf;
char *buf2;
int r=2;
int inMojiSize=0;
buf = (char *)calloc(READ_SIZE,sizeof(char));
buf2 = (char *)calloc(READ_SIZE,sizeof(char));
while (fgets(buf,READ_SIZE,stdin) != NULL){
/* 容量が足りなくなったら再確保 */
buf2 = (char *)realloc(buf2,READ_SIZE * r );
/* 1行又は指定バイト単位で読み込んだデータを蓄積 */
strcat(buf2,buf);
r++;
}
moji = buf2;
/*入力のサイズ取得*/
inMojiSize = strlen(moji);
/* 標準入力からデータを取得(パイプ)-----------------------------------E
*/
int i;
int cnt;
char *remoji[inMojiSize];
char *retstr[inMojiSize];
puts("***************************");
//printf(">>%s\n",moji);
/*UTF8->cp932に変換*/
ret = convert(ifrom, ito,
moji,
outbuf, sizeof(outbuf));
printf(">>%s\n", outbuf);
puts("***************************");
free(buf);
free(buf2);
}
/*
---------------------------------------------------------------
文字コード変換処理。iconvを利用
---------------------------------------------------------------
*/
int convert(char const *src,
char const *dest,
char *intext,
char *outbuf,
size_t bufsize)
{
iconv_t cd;
size_t insrclen, outsrclen;
size_t ret;
int j=0;
cd = iconv_open(dest, src);
if (cd == (iconv_t)-1) {
perror("iconv open");
return 0;
}
insrclen = strlen(intext);
outsrclen = bufsize - 1;
memset(outbuf, '\0', bufsize);
while(0 < insrclen) {
ret = iconv(cd, &intext, &insrclen, &outbuf, &outsrclen);
if (ret == -1) {
printf("intext=%s\n",intext);
/*変換不可文字の場合、「x40=@」に置き換える*/
insrclen--;
intext[1] ='\x40';
intext++;
//printf("AAFFintext=%s\n",intext);
// perror("iconv");
// return 0;
}
}
iconv_close(cd);
return 1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment