lundi 26 juillet 2010

C++/CLI String^ to std::string and vice-versa

I propose you 2 functions for converting std::string to/from String^ in C++/CLI

    String^ UTF8ToString( const char *s ) {
        int len = strlen(s);
        cli::array^ a = gcnew cli::array(len);
        int i = len;
        while (i-- > 0) {
            a[i] = s[i];
        }
        return System::Text::Encoding::UTF8->GetString(a);
    };

    std::string StringToUTF8( String ^ s) {
        System::Text::UTF8Encoding^ utf8 = gcnew System::Text::UTF8Encoding;
        array^encodedBytes = utf8->GetBytes(s);
        std::string os="";
        for (int i=0;iLength;i++) {
            os+=encodedBytes[i];
        }
        return os;
    }


Useful to work with other libs working in UTF-8 like sqlite...

samedi 24 juillet 2010

install Mono on MACOSX 64 bits

I recently installed mono from sources, no other choice for having it on 64 bits
I didn't have any previous installation of mono.

first you need to install wget and pkg-config. We normally need also gettext but seems it is not compatible with 64 bits, and as we can disable it then I did it without gettext.

wget-1.12
http://ftp.gnu.org/gnu/wget/wget-latest.tar.gz
unzip/untar it (double click on it normally is enough)
>./configure
>make
>sudo make install

pkg-config-0.25
http://pkg-config.freedesktop.org/releases/pkg-config-0.25.tar.gz
unzip/untar it (double click on it normally is enough)
>./configure
>make
>sudo make install

now we get the sources of mono from the SVN repository
As we need 2 folder, we just create a root folder "mono-trunk"

>mkdir mono-trunk
>cd mono-trunk

>svn co http://anonsvn.mono-project.com/source/trunk/mono
>svn co http://anonsvn.mono-project.com/source/trunk/mcs

>cd mono

// -- enable-nls=no is for disabling gettext

>./autogen.sh --prefix=/usr/local/mono --build=x86_64-apple-darwin10 --enable-nls=no
>make get-monolite-latest
>make
>sudo make install

don't forget to update your .profile to add the path to mono/bin
PATH="$PATH:/usr/local/mono/bin"

normally your are done with mono on macosx, please let me know if it worked for you :-)