Sha256: ed9a7c4d50f4fbf9f9b8de2679cf1566e2c0a04a06452a1e939667406fbb20e0

Contents?: true

Size: 1.88 KB

Versions: 37

Compression:

Stored size: 1.88 KB

Contents

#ifndef GOSIMPL_ICONV_HPP
#define GOSIMPL_ICONV_HPP

#include <Gosu/Platform.hpp>

#ifdef __APPLE__
#include </usr/include/iconv.h> // We want Apple's iconv
#else
#include <iconv.h>
#endif
//#endif
#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 bufferLen = 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[bufferLen];
        
        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 + bufferLen);
                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 + bufferLen - outbytesleft / sizeof(OutElem));
                return result;
            }
        }        
    }
}


#endif

Version data entries

37 entries across 37 versions & 1 rubygems

Version Path
gosu-0.7.48 GosuImpl/Iconv.hpp
gosu-0.7.47.1 GosuImpl/Iconv.hpp
gosu-0.7.46 GosuImpl/Iconv.hpp
gosu-0.7.45 GosuImpl/Iconv.hpp
gosu-0.7.44 GosuImpl/Iconv.hpp
gosu-0.7.43 GosuImpl/Iconv.hpp
gosu-0.7.41 GosuImpl/Iconv.hpp
gosu-0.7.40 GosuImpl/Iconv.hpp
gosu-0.7.39 GosuImpl/Iconv.hpp
gosu-0.7.38 GosuImpl/Iconv.hpp
gosu-0.7.37 GosuImpl/Iconv.hpp
gosu-0.7.36.2 GosuImpl/Iconv.hpp
gosu-0.7.35 GosuImpl/Iconv.hpp
gosu-0.7.33 GosuImpl/Iconv.hpp
gosu-0.7.32 GosuImpl/Iconv.hpp
gosu-0.7.31 GosuImpl/Iconv.hpp
gosu-0.7.30 GosuImpl/Iconv.hpp
gosu-0.7.29 GosuImpl/Iconv.hpp
gosu-0.7.28 GosuImpl/Iconv.hpp
gosu-0.7.27.1 GosuImpl/Iconv.hpp