X-Git-Url: http://git.indexdata.com/?p=yaz-moved-to-github.git;a=blobdiff_plain;f=src%2Fsiconv.c;h=1cd81ed659a29042b1549329c4a1eafe96cf38c7;hp=b04072931c0fd57cc3e61b2e3859954164d8bcd1;hb=60b5f5ba6f34ef79b037eb8af1e2554d9842bb10;hpb=6e2be464f5440ef47e6c6228ad91062d961b75b2 diff --git a/src/siconv.c b/src/siconv.c index b040729..1cd81ed 100644 --- a/src/siconv.c +++ b/src/siconv.c @@ -2,7 +2,7 @@ * Copyright (C) 1995-2007, Index Data ApS * See the file LICENSE for details. * - * $Id: siconv.c,v 1.32 2007-01-03 08:42:15 adam Exp $ + * $Id: siconv.c,v 1.33 2007-01-18 14:45:05 adam Exp $ */ /** * \file siconv.c @@ -85,6 +85,8 @@ struct yaz_iconv_struct { size_t (*write_handle)(yaz_iconv_t cd, unsigned long x, char **outbuf, size_t *outbytesleft, int last); + size_t (*flush_handle)(yaz_iconv_t cd, + char **outbuf, size_t *outbytesleft); int marc8_esc_mode; int comb_offset; @@ -880,6 +882,23 @@ static size_t yaz_write_marc8_2(yaz_iconv_t cd, unsigned long x, return 0; } +static size_t yaz_flush_marc8(yaz_iconv_t cd, + char **outbuf, size_t *outbytesleft) +{ + if (strcmp(cd->write_marc8_page_chr, "\033(B")) + { + if (*outbytesleft < 3) + { + cd->my_errno = YAZ_ICONV_E2BIG; + return (size_t) (-1); + } + memcpy(*outbuf, "\033(B", 3); + (*outbuf) += 3; + *outbytesleft -= 3; + } + return 0; +} + static size_t yaz_write_marc8(yaz_iconv_t cd, unsigned long x, char **outbuf, size_t *outbytesleft, int last) @@ -951,6 +970,7 @@ yaz_iconv_t yaz_iconv_open (const char *tocode, const char *fromcode) cd->write_handle = 0; cd->read_handle = 0; cd->init_handle = 0; + cd->flush_handle = 0; cd->my_errno = YAZ_ICONV_UNKNOWN; /* a useful hack: if fromcode has leading @, @@ -988,9 +1008,15 @@ yaz_iconv_t yaz_iconv_open (const char *tocode, const char *fromcode) else if (!yaz_matchstr(tocode, "UCS4LE")) cd->write_handle = yaz_write_UCS4LE; else if (!yaz_matchstr(tocode, "MARC8")) + { cd->write_handle = yaz_write_marc8; + cd->flush_handle = yaz_flush_marc8; + } else if (!yaz_matchstr(tocode, "MARC8s")) + { cd->write_handle = yaz_write_marc8; + cd->flush_handle = yaz_flush_marc8; + } #if HAVE_WCHAR_H else if (!yaz_matchstr(tocode, "WCHAR_T")) cd->write_handle = yaz_write_wchar_t; @@ -1021,7 +1047,7 @@ yaz_iconv_t yaz_iconv_open (const char *tocode, const char *fromcode) size_t yaz_iconv(yaz_iconv_t cd, char **inbuf, size_t *inbytesleft, char **outbuf, size_t *outbytesleft) { - char *inbuf0; + char *inbuf0 = 0; size_t r = 0; #if HAVE_ICONV_H @@ -1049,17 +1075,30 @@ size_t yaz_iconv(yaz_iconv_t cd, char **inbuf, size_t *inbytesleft, return r; } #endif - if (inbuf == 0 || *inbuf == 0) + + if (inbuf) + inbuf0 = *inbuf; + + if (cd->init_flag) { - cd->init_flag = 1; cd->my_errno = YAZ_ICONV_UNKNOWN; - return 0; + cd->marc8_esc_mode = 'B'; + + cd->comb_offset = cd->comb_size = 0; + cd->compose_char = 0; + + cd->write_marc8_comb_no = 0; + cd->write_marc8_second_half_char = 0; + cd->write_marc8_last = 0; + cd->write_marc8_page_chr = "\033(B"; + + cd->unget_x = 0; + cd->no_read_x = 0; } - inbuf0 = *inbuf; if (cd->init_flag) { - if (cd->init_handle) + if (cd->init_handle && inbuf && *inbuf) { size_t no_read = 0; size_t r = (cd->init_handle)(cd, (unsigned char *) *inbuf, @@ -1074,32 +1113,26 @@ size_t yaz_iconv(yaz_iconv_t cd, char **inbuf, size_t *inbytesleft, *inbytesleft -= no_read; *inbuf += no_read; } - cd->marc8_esc_mode = 'B'; - - cd->comb_offset = cd->comb_size = 0; - cd->compose_char = 0; - - cd->write_marc8_comb_no = 0; - cd->write_marc8_second_half_char = 0; - cd->write_marc8_last = 0; - cd->write_marc8_page_chr = "\033(B"; - - cd->init_flag = 0; - cd->unget_x = 0; - cd->no_read_x = 0; } + cd->init_flag = 0; + while (1) { unsigned long x; size_t no_read; - if (*inbytesleft == 0) + if (cd->unget_x) { - r = *inbuf - inbuf0; - break; + x = cd->unget_x; + no_read = cd->no_read_x; } - if (!cd->unget_x) + else if (inbuf && *inbuf) { + if (*inbytesleft == 0) + { + r = *inbuf - inbuf0; + break; + } x = (cd->read_handle)(cd, (unsigned char *) *inbuf, *inbytesleft, &no_read); if (no_read == 0) @@ -1110,8 +1143,12 @@ size_t yaz_iconv(yaz_iconv_t cd, char **inbuf, size_t *inbytesleft, } else { - x = cd->unget_x; - no_read = cd->no_read_x; + r = 0; + if (cd->flush_handle && outbuf && *outbuf) + r = (*cd->flush_handle)(cd, outbuf, outbytesleft); + if (r == 0) + cd->init_flag = 1; + break; } if (x) {