基于算法的无状态转换器

每个转换器都应该有前面指定的子例程。 除了对所有转换器来说都相同,并且应该用相同方式进行编码的 instantiate 子例程外,只需要提供子例程头,而无需细节。

下面基于算法的无状态转换器示例是从 IBM-850 代码集转为 ISO8859-1 代码集的样本转换器。

#include <stdlib.h>
#include <iconv.h>
#include "850_88591.h"
/*
 *      Name :  _iconv_exec()
 *      
 *      This contains actual conversion method.  
 */
static size_t   _iconv_exec(_LC_sample_iconv_t *cd, 
                         unsigned char** inbuf, 
                         size_t *inbytesleft,
                         unsigned char** outbuf, 
                         size_t *outbytesleft)
/*
 *      cd               : converter descriptor
 *      inbuf            : input buffer
 *      outbuf           : output buffer
 *      inbytesleft      : number of data(in bytes) in input buffer
 *      outbytesleft     : number of data(in bytes) in output buffer
 */

{
}
 
/*
 *      Name :   _iconv_close()
 *      
 *      Free the allocated converter descriptor
 */
static void      _iconv_close(iconv_t cd)
{
}
 
/*
 *      Name :  init()
 *      
 *      This allocates and initializes the converter descriptor.
 */
static _LC_sample_iconv_t        *init (_LC_core_iconv_t *core_cd, 
                                  char* toname, char* fromname)
{
}
 
/*
 *       Name :  instantiate()
 *       
 *       Core part of a converter descriptor is initialized here.
 */
_LC_core_iconv_t         *instantiate(void)
{
        static _LC_core_iconv_t   cd;
 
        /*
        * * Initialize _LC_MAGIC and _LC_VERSION are 
        ** defined in <lc_core.h>. _LC_ICONV and _LC_core_iconv_t
        ** are defined in <iconv.h>.
         */
        cd.hdr.magic = _LC_MAGIC;
        cd.hdr.version = _LC_VERSION;
        cd.hdr.type_id = _LC_ICONV;
        cd.hdr.size = sizeof (_LC_core_iconv_t);
 
        /*
         *      Set pointers to each method.
         */
        cd.init = init;
        cd.exec = _iconv_exec;
        cd.close = _iconv_close;
 
        /*
         *      Returns the core part
         */
        return &cd;
}