inconv.h
Language/C 2012. 5. 16. 17:371소개 #
- 홈페이지 :
http://www.gnu.org/software/libiconv/
- 각나라의 코드페이지로 문자열을 변환해주는 라이브러리입니다. 예를 들면, 완성형->조합형, 조합형->UTF-8과 같은 것이죠.
[edit]
3.1iconv_open() #
#include <iconv.h> iconv_t iconv_open (const char* tocode, const char* fromcode);
[edit]
3.1.1설명 #
iconv_open 함수는 문자 인코딩 fromcode에서 문자 인코딩 tocode 방식으로 문자열을 변환하기위한 변환기 핸들을 생성하는 역할을 합니다. fromcode와 tocode에 지정가능한 값과 두 값의 조합은 시스템마다 다릅니다. libiconv 라이브러리에서는 다음과 같은 인코딩이 지원되며 모두 상호조합이 가능합니다.
--enable-extra-encodings 옵션을 주어 빌드했을 경우 다음과 같은 추가된(잘쓰이지 않는) 인코딩 방식을 사용할 수 있습니다.
--enable-extra-encodings 옵션을 주어 빌드했을 경우 다음과 같은 추가된(잘쓰이지 않는) 인코딩 방식을 사용할 수 있습니다.
- 인코딩 명을 ""와 같이 공백으로 지정하면 이는 "char"로 지정한 것과 같이 처리됩니다 : 이는 로케일 의존적인 문자 인코딩 방식을 사용한다는 뜻이 됩니다.
- tocode에 "//TRANSLIT"을 덧붙여 지정하면 고쳐쓰기 기능이 활성화됩니다. 이는 특정 문자가 결과 문자집합에 존재하지 않을경우 비슷하게 보이는 글자중 하나로 어림잡아 대체한다는 의미입니다.
- tocode에 "//IGNORE"를 덧붙여 지정하면, 결과 문자집합에 존재하지 않는 문자들은 아무런 경고없이 건너뛰어 처리됩니다.
- 반환되는 변환기 핸들은 iconv()함수를 사용하여 몇번이고 실행이 가능합니다. 모든 처리가 끝나면 iconv_close()함수로 이 핸들을 닫아주면 됩니다.
- 변환기 핸들은 변환 상태를 담고 있습니다. iconv_open() 함수를 사용하여 생성된 직후에는, 상태는 초기 상태로 지정됩니다. iconv()함수를 사용하면 핸들의 변환상태를 수정하게 됩니다. (이는 다중 쓰레드 환경에서 동시에 하나의 핸들을 공유해서 사용할 수 없다는 것을 의미합니다) 상태를 다시 초기상태로 되돌리려면, iconv()에 inbuf 매개변수에 NULL을 지정하여 실행하세요.
[edit]
3.2iconv() #
#include <iconv.h> size_t iconv (iconv_t cd, const char* * inbuf, size_t * inbytesleft, char* * outbuf, size_t * outbytesleft);
[edit]
3.2.1설명 #
- 매개변수 cd는 iconv_open()함수를 사용하여 생성된 변환기 핸들이어야만 합니다.
- 기본적인 사용법은 inbuf 및 *inbuf 모두 NULL이 아닐 경우입니다. In this case, the iconv function converts the multibyte sequence starting at *inbuf to a multibyte sequence starting at *outbuf. At most *inbytesleft bytes, starting at *inbuf, will be read. At most *outbytesleft bytes, starting at *outbuf, will be written.
- iconv() 함수는 한번에 하나의 다중바이트 문자 하나를 변환합니다. 그리고 각각의 문자변환때마다 변환된 입력문자 바이트수만큼 *inbuf를 증가시키고 *inbytesleft를 감소시키게 되며, 변환된 출력문자 바이트수만큼 *outbuf을 증가시키고 *outbytesleft를 감소시킵니다. 그런다음 cd 변환기 핸들내의 변환 상태를 갱신합니다. 변환은 다음과 같은 4가지 경우에 정지하게 됩니다.
- An invalid multibyte sequence is encountered in the input. In this case it sets errno to EILSEQ and returns (size_t)(-1). *inbuf is left pointing to the beginning of the invalid multibyte sequence.
- The input byte sequence has been entirely converted, i.e. *inbytesleft has gone down to 0. In this case iconv returns the number of non-reversible conversions performed during this call.
- An incomplete multibyte sequence is encountered in the input, and the input byte sequence terminates after it. In this case it sets errno to EINVAL and returns (size_t)(-1). *inbuf is left pointing to the beginning of the incomplete multibyte sequence.
- The output buffer has no more room for the next converted character. In this case it sets errno to E2BIG and returns (size_t)(-1).
- A different case is when inbuf is NULL or *inbuf is NULL, but outbuf is not NULL and *outbuf is not NULL. In this case, the iconv function attempts to set cd's conversion state to the initial state and store a corresponding shift sequence at *outbuf. At most *outbytesleft bytes, starting at *outbuf, will be written. If the output buffer has no more room for this reset sequence, it sets errno to E2BIG and returns (size_t)(-1). Otherwise it increments *outbuf and decrements *outbytesleft by the number of bytes written.
- inbuf가 NULL 또는 *inbuf가 NULL이며, outbuf가 NULL 또는 *outbuf가 NULL인 경우에는, iconv() 함수는 변환기 핸들 cd내의 변환 상태를 초기 상태로 변경합니다.
[edit]
3.2.2반환값 #
- iconv() function returns the number of characters converted in a non-reversible way during this call; reversible conversions are not counted.
- 오류가 발생하면 errno에 오류코드를 넣고 (iconv_t)(-1)를 반환합니다.
[edit]
3.3.2반환값 #
- 성공적으로 실행이 끝나면 iconv_close() 함수는 0을 반환합니다.
- 오류가 발생하면 errno를 설정하고 -1을 반환합니다.
[출처] iconv - 각나라의 코드페이지로 문자열을 변환해주는 라이브러리|작성자 코바코바
'Language > C' 카테고리의 다른 글
typedef & struct (1) (0) | 2012.05.23 |
---|---|
inconv source (iconv_open, iconv, iconv_close) (0) | 2012.05.16 |
read() 파일 읽기 (0) | 2012.05.11 |
execl 다른 프로그램 실행 (0) | 2012.04.26 |
Why use select() instead of sleep()? (0) | 2012.04.26 |