#include "demo_util.h"
#include <stdlib.h>

namespace demo_util {

    char* TransformUnicodeToUTF8M( const WCHAR* _str )
    {
        char* result = NULL;
        int textlen = 0;
        if (_str)
        {
            textlen = WideCharToMultiByte( CP_UTF8, 0, _str, -1, NULL, 0, NULL, NULL );
            result = new char[textlen+1];
            memset(result, 0, sizeof(char) * ( textlen + 1 ) );
            WideCharToMultiByte( CP_UTF8, 0, _str, -1, result, textlen, NULL, NULL );
        }
        return result;
    }

    WCHAR *TransformUTF8ToUnicode(const char *str){
        int nChar;
        WCHAR *zWideFilename;

        nChar = MultiByteToWideChar(CP_UTF8, 0, str, -1, NULL, 0);
        zWideFilename = new WCHAR[nChar];
        if( zWideFilename==0 ){
            return 0;
        }
        nChar = MultiByteToWideChar(CP_UTF8, 0, str, -1, zWideFilename, nChar);
        if( nChar==0 ){
            free(zWideFilename);
            zWideFilename = 0;
        }
        return zWideFilename;
    }

}
