Subversion Repositories spk

Rev

Rev 242 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

#include <spk.h>
#include "Utils/WString.h"

using namespace System;
using namespace Runtime::InteropServices;
using namespace System::Drawing;

#define CONNECTERROR_UNKNOWN    -1
enum {CONNECTERROR_NONE, CONNECTERROR_TIMEOUT, CONNECTERROR_NOFILE, CONNECTERROR_FAILED};

inline Utils::String _S(System::String^ str)
{
        if (!str || !str->Length)
                return "";
        const char* chars = (const char*)(Marshal::StringToHGlobalAnsi(str)).ToPointer();
        std::string s = chars;
        Marshal::FreeHGlobal(IntPtr((void*)chars));
        return s;
}
inline Utils::WString _WS(System::String^ str)
{
        if (!str || !str->Length)
                return L"";
        const wchar_t* chars = (const wchar_t*)(Marshal::StringToHGlobalUni(str)).ToPointer();
        std::wstring s = chars;
        Marshal::FreeHGlobal(IntPtr((void*)chars));
        return s;
}
inline int CheckWebFileExists(String ^url)
{
        int ret = CONNECTERROR_UNKNOWN;
        try 
        {
                // check for the file
                Net::WebRequest ^check = (Net::HttpWebRequest ^)Net::WebRequest::Create(url);
                check->Credentials = Net::CredentialCache::DefaultCredentials;
                Net::WebResponse ^response = (Net::HttpWebResponse ^)check->GetResponse();
                // check the file size
                __int64 fileSize = response->ContentLength;
                if ( fileSize )
                        ret = CONNECTERROR_NONE;

                response->Close();

        }
        catch (System::Net::WebException ^ex)
        {
                if ( ex->Status == System::Net::WebExceptionStatus::ConnectFailure )
                        ret = CONNECTERROR_FAILED;
                else if(ex->Status == System::Net::WebExceptionStatus::ConnectionClosed && !ex->Response)
                        ret = CONNECTERROR_FAILED;
                else
                {
                        switch ( cli::safe_cast<Net::HttpWebResponse ^>(ex->Response)->StatusCode )
                        {
                                case Net::HttpStatusCode::NotFound:
                                        ret = CONNECTERROR_NOFILE;
                                        break;
                                case Net::HttpStatusCode::RequestTimeout:
                                        ret = CONNECTERROR_TIMEOUT;
                                        break;
                                default:
                                        ret = CONNECTERROR_UNKNOWN;
                        }
                }
        }

        return ret;
}

inline String ^ReadDataFromWeb(String ^url)
{
        bool ret = false;
        String ^data;
        try 
        {
                System::Net::WebClient ^Client = gcnew System::Net::WebClient();
                Client->Headers->Add( "user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)" );
                System::IO::Stream ^strm = Client->OpenRead(url);
                System::IO::StreamReader ^sr = gcnew System::IO::StreamReader(strm);
                data = sr->ReadToEnd();

                strm->Close();
                sr->Close();
        }
        catch (System::Net::WebException ^)
        {
                return nullptr;
        }

        if ( data && data->Length )
                return data;
        return nullptr;
}

inline System::String^ _US(const Utils::WString& str)
{
        System::String^ Str = gcnew System::String(str.c_str());
        return Str;
}

inline System::Drawing::Bitmap ^LoadIcon(CBaseFile *p)
{
        Utils::WString file = p->icon()->filePointer();
        if (!file.empty())
        {
                file = file.findReplace(L"/", L"\\").findReplace(L"\\\\", L"\\");
                bool doIcon = false;
                System::String ^sFile = _US(file);
                if ( System::IO::File::Exists(sFile) )
                {
                        Bitmap ^myBitmap = gcnew Bitmap(sFile);
                        return myBitmap;
                }
        }
                        
        return nullptr;
}

inline System::String ^GetProgramVersionString(float version, int beta)
{
        System::String ^str = "V" + _US(Utils::WString::FromFloat(version, 2));
        // beta version
        if ( beta > 0 )
                str += " (Beta " + beta + ")";
        // RC release
        else if ( beta < 0 )
                str += " (RC " + (0 - beta) + ")";
        return str;
}