Subversion Repositories spk

Rev

Rev 1 | Blame | Compare with Previous | Last modification | View Log | RSS feed

#include <spk.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 CyString CyStringFromSystemString( System::String ^str)
{
        if ( !str || !str->Length )
                return NullString;
        const char* chars = (const char*)(Marshal::StringToHGlobalAnsi(str)).ToPointer();
        std::string s = chars;
//      CyString *newStr = new CyString(chars);
        Marshal::FreeHGlobal(IntPtr((void*)chars));
//      return *newStr;
        return CyString(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
                {
                        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 ^SystemStringFromCyString ( CyString str )
{
        System::String ^Str = gcnew System::String(str.c_str());
        return Str;
}

inline System::Drawing::Bitmap ^LoadIcon(CBaseFile *p)
{
        CyString file = p->GetIcon()->GetFilePointer();
        if ( !file.Empty() )
        {
                file = file.FindReplace("/", "\\").FindReplace("\\\\", "\\");
                bool doIcon = false;
                System::String ^sFile = SystemStringFromCyString(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" + SystemStringFromCyString(CyString::CreateFromFloat(version, 2));
        // beta version
        if ( beta > 0 )
                str += " (Beta " + beta + ")";
        // RC release
        else if ( beta < 0 )
                str += " (RC " + (0 - beta) + ")";
        return str;
}