Sha256: 9cd0a89419a301708658d71155027e6a4f5b6d73eaa64982307704374a2524b0

Contents?: true

Size: 1.71 KB

Versions: 43

Compression:

Stored size: 1.71 KB

Contents

#pragma once

#include <Gosu/Platform.hpp>

#include <iconv.h>
#include <errno.h>

namespace Gosu
{
    template<typename Out, const char* to, const char* from, typename In>
    Out iconvert(const In& in)
    {
        if (in.empty()) return Out();

        const size_t buffer_len = 128;
        typedef typename In::value_type InElem;
        typedef typename Out::value_type OutElem;

        static iconv_t cd = iconv_open(to, from);
        
        Out result;
        OutElem buffer[buffer_len];
        
        char* inbuf = const_cast<char*>(reinterpret_cast<const char*>(&in[0]));
        size_t inbytesleft = in.size() * sizeof(InElem);
        char* outbuf = reinterpret_cast<char*>(buffer);
        size_t outbytesleft = sizeof buffer;
        
        for (;;) {
            size_t ret = ::iconv(cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
            if (ret == static_cast<size_t>(-1) && errno == EILSEQ) {
                // Skip illegal sequence part, repeat loop.
                // TODO: Or retry w/ different encoding?
                ++inbuf;
                --inbytesleft;
            }
            else if (ret == static_cast<size_t>(-1) && errno == E2BIG) {
                // Append new characters, reset out buffer, then repeat loop.
                result.insert(result.end(), buffer, buffer + buffer_len);
                outbuf = reinterpret_cast<char*>(buffer);
                outbytesleft = sizeof buffer;
            }
            else {
                // Append what's new in the buffer, then LEAVE loop.
                result.insert(result.end(), buffer,
                              buffer + buffer_len - outbytesleft / sizeof(OutElem));
                return result;
            }
        }
    }
}

Version data entries

43 entries across 43 versions & 1 rubygems

Version Path
gosu-1.4.6 src/Iconv.hpp
gosu-1.4.5 src/Iconv.hpp
gosu-1.4.5.pre1 src/Iconv.hpp
gosu-1.4.4 src/Iconv.hpp
gosu-1.4.3 src/Iconv.hpp
gosu-1.4.1 src/Iconv.hpp
gosu-1.4.0 src/Iconv.hpp
gosu-1.3.0 src/Iconv.hpp
gosu-1.2.0 src/Iconv.hpp
gosu-1.1.1.1 src/Iconv.hpp
gosu-1.1.0 src/Iconv.hpp
gosu-1.1.0.pre2 src/Iconv.hpp
gosu-1.1.0.pre1 src/Iconv.hpp
gosu-1.0.0 src/Iconv.hpp
gosu-1.0.0.pre2 src/Iconv.hpp
gosu-1.0.0.pre1 src/Iconv.hpp
gosu-0.15.2 src/Iconv.hpp
gosu-0.15.1 src/Iconv.hpp
gosu-0.15.0 src/Iconv.hpp
gosu-0.14.6.pre1 src/Iconv.hpp