inconv.h

Language/C 2012. 5. 16. 17:37

1소개 #

  • 홈페이지 : [http]http://www.gnu.org/software/libiconv/
  • 각나라의 코드페이지로 문자열을 변환해주는 라이브러리입니다. 예를 들면, 완성형->조합형, 조합형->UTF-8과 같은 것이죠. :)

2설치 #

  • mingw에 이미 빌드된 인스톨가능한 배포본이 존재합니다. gettext와 상호참조 의존성이 있으므로 같이 설치하는 것이 좋습니다.

3GNU iconv 메뉴얼 번역 #

3.1iconv_open() #

          #include <iconv.h>
          
          iconv_t iconv_open (const char* tocode, const char* fromcode);
          

3.1.1설명 #

iconv_open 함수는 문자 인코딩 fromcode에서 문자 인코딩 tocode 방식으로 문자열을 변환하기위한 변환기 핸들을 생성하는 역할을 합니다. fromcode와 tocode에 지정가능한 값과 두 값의 조합은 시스템마다 다릅니다. libiconv 라이브러리에서는 다음과 같은 인코딩이 지원되며 모두 상호조합이 가능합니다.
한국어EUC-KR, CP949, ISO-2022-KR, JOHAB
유럽ASCII, ISO-8859-{1,2,3,4,5,7,9,10,13,14,15,16}, KOI8-R, KOI8-U, KOI8-RU, CP{1250,1251,1252,1253,1254,1257}, CP{850,866}, Mac{Roman,CentralEurope,Iceland,Croatian,Romania}, Mac{Cyrillic,Ukraine,Greek,Turkish}, Macintosh
셈족 계통ISO-8859-{6,8}, CP{1255,1256}, CP862, Mac{Hebrew,Arabic}
일본어EUC-JP, SHIFT_JIS, CP932, ISO-2022-JP, ISO-2022-JP-2, ISO-2022-JP-1
중국어EUC-CN, HZ, GBK, GB18030, EUC-TW, BIG5, CP950, BIG5-HKSCS, ISO-2022-CN, ISO-2022-CN-EXT
아르메니아ARMSCII-8
그루지아Georgian-Academy, Georgian-PS
타지키스탄KOI8-T
타이어TIS-620, CP874, MacThai
라오스MuleLao-1, CP1133
베트남VISCII, TCVN, CP1258
특정 플렛폼 전용HP-ROMAN8, NEXTSTEP
유니코드UTF-8, UCS-2, UCS-2BE, UCS-2LE, UCS-4, UCS-4BE, UCS-4LE, UTF-16, UTF-16BE, UTF-16LE, UTF-32, UTF-32BE, UTF-32LE, UTF-7, C99, JAVA
uint16_t 또는 uint32_t에 기초한 유니코드(시스템 엔디안과 정렬방식에 의존적임)UCS-2-INTERNAL, UCS-4-INTERNAL
char 또는 wchar_t에 기초한 로케일 의존적(시스템 엔디안과 정렬방식에 의존적임, LC_CTYPE 로케일 facet과 운영체계 설정에 따름)char, wchar_t

--enable-extra-encodings 옵션을 주어 빌드했을 경우 다음과 같은 추가된(잘쓰이지 않는) 인코딩 방식을 사용할 수 있습니다.
유럽CP{437,737,775,852,853,855,857,858,860,861,863,865,869,1125}
셈계통CP864
일본어EUC-JISX0213, Shift_JISX0213, ISO-2022-JP-3
투르크멘 언어TDS565
특정 플렛폼 전용RISCOS-LATIN1

  • 인코딩 명을 ""와 같이 공백으로 지정하면 이는 "char"로 지정한 것과 같이 처리됩니다 : 이는 로케일 의존적인 문자 인코딩 방식을 사용한다는 뜻이 됩니다.
  • tocode에 "//TRANSLIT"을 덧붙여 지정하면 고쳐쓰기 기능이 활성화됩니다. 이는 특정 문자가 결과 문자집합에 존재하지 않을경우 비슷하게 보이는 글자중 하나로 어림잡아 대체한다는 의미입니다.
  • tocode에 "//IGNORE"를 덧붙여 지정하면, 결과 문자집합에 존재하지 않는 문자들은 아무런 경고없이 건너뛰어 처리됩니다.
  • 반환되는 변환기 핸들은 iconv()함수를 사용하여 몇번이고 실행이 가능합니다. 모든 처리가 끝나면 iconv_close()함수로 이 핸들을 닫아주면 됩니다.
  • 변환기 핸들은 변환 상태를 담고 있습니다. iconv_open() 함수를 사용하여 생성된 직후에는, 상태는 초기 상태로 지정됩니다. iconv()함수를 사용하면 핸들의 변환상태를 수정하게 됩니다. (이는 다중 쓰레드 환경에서 동시에 하나의 핸들을 공유해서 사용할 수 없다는 것을 의미합니다) 상태를 다시 초기상태로 되돌리려면, iconv()에 inbuf 매개변수에 NULL을 지정하여 실행하세요.

3.1.2반환값 #

iconv_open() 함수는 변환기 핸들을 반환합니다. 오류가 발생하면 errno에 해당 오류값을 설정하고 (iconv_t)(-1)를 반환합니다.

3.1.3errno #

EINVALfromcode에서 tocode로 변환하는 기능이 현재 구현에서 지원되지 않을 경우

3.2iconv() #

          #include <iconv.h>
          
          size_t iconv (iconv_t cd, const char* * inbuf, size_t * inbytesleft, char* * outbuf, size_t * outbytesleft);
          

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가지 경우에 정지하게 됩니다.
    1. 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.
    2. 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.
    3. 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.
    4. 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내의 변환 상태를 초기 상태로 변경합니다.

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)를 반환합니다.

3.2.3errno #

E2BIG*outbuf에 공간이 부족합니다
EILSEQ부정확한 다중바이트 문자열이 입력값으로 들어왔습니다.
EINVAL완료되지않은 다중바이트문자열이 입력값으로 들어왔습니다.

3.3iconv_close() #

          #include <iconv.h>
          
          int iconv_close (iconv_t cd);
          

3.3.1설명 #

iconv_open()에서 생성된 변환기 핸들 cd를 해제합니다.

3.3.2반환값 #

'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
: