Subversion Repositories spk

Rev

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

Rev Author Line No. Line
1 cycrow 1
#include <spk.h>
187 cycrow 2
#include "Utils/WString.h"
1 cycrow 3
 
4
using namespace System;
5
using namespace Runtime::InteropServices;
6
using namespace System::Drawing;
7
 
8
#define CONNECTERROR_UNKNOWN	-1
9
enum {CONNECTERROR_NONE, CONNECTERROR_TIMEOUT, CONNECTERROR_NOFILE, CONNECTERROR_FAILED};
10
 
187 cycrow 11
inline Utils::String _S(System::String^ str)
39 cycrow 12
{
187 cycrow 13
	if (!str || !str->Length)
39 cycrow 14
		return "";
15
	const char* chars = (const char*)(Marshal::StringToHGlobalAnsi(str)).ToPointer();
16
	std::string s = chars;
17
	Marshal::FreeHGlobal(IntPtr((void*)chars));
18
	return s;
19
}
187 cycrow 20
inline Utils::WString _WS(System::String^ str)
21
{
22
	if (!str || !str->Length)
23
		return L"";
24
	const wchar_t* chars = (const wchar_t*)(Marshal::StringToHGlobalUni(str)).ToPointer();
25
	std::wstring s = chars;
26
	Marshal::FreeHGlobal(IntPtr((void*)chars));
27
	return s;
28
}
1 cycrow 29
inline int CheckWebFileExists(String ^url)
30
{
31
	int ret = CONNECTERROR_UNKNOWN;
32
	try 
33
	{
34
		// check for the file
35
		Net::WebRequest ^check = (Net::HttpWebRequest ^)Net::WebRequest::Create(url);
36
		check->Credentials = Net::CredentialCache::DefaultCredentials;
37
		Net::WebResponse ^response = (Net::HttpWebResponse ^)check->GetResponse();
38
		// check the file size
39
		__int64 fileSize = response->ContentLength;
40
		if ( fileSize )
41
			ret = CONNECTERROR_NONE;
42
 
43
		response->Close();
44
 
45
	}
46
	catch (System::Net::WebException ^ex)
47
	{
48
		if ( ex->Status == System::Net::WebExceptionStatus::ConnectFailure )
49
			ret = CONNECTERROR_FAILED;
121 cycrow 50
		else if(ex->Status == System::Net::WebExceptionStatus::ConnectionClosed && !ex->Response)
51
			ret = CONNECTERROR_FAILED;
1 cycrow 52
		else
53
		{
54
			switch ( cli::safe_cast<Net::HttpWebResponse ^>(ex->Response)->StatusCode )
55
			{
56
				case Net::HttpStatusCode::NotFound:
57
					ret = CONNECTERROR_NOFILE;
58
					break;
59
				case Net::HttpStatusCode::RequestTimeout:
60
					ret = CONNECTERROR_TIMEOUT;
61
					break;
62
				default:
63
					ret = CONNECTERROR_UNKNOWN;
64
			}
65
		}
66
	}
67
 
68
	return ret;
69
}
70
 
71
inline String ^ReadDataFromWeb(String ^url)
72
{
73
	bool ret = false;
74
	String ^data;
75
	try 
76
	{
77
		System::Net::WebClient ^Client = gcnew System::Net::WebClient();
78
		Client->Headers->Add( "user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)" );
79
		System::IO::Stream ^strm = Client->OpenRead(url);
80
		System::IO::StreamReader ^sr = gcnew System::IO::StreamReader(strm);
81
		data = sr->ReadToEnd();
82
 
83
		strm->Close();
84
		sr->Close();
85
	}
86
	catch (System::Net::WebException ^)
87
	{
88
		return nullptr;
89
	}
90
 
91
	if ( data && data->Length )
92
		return data;
93
	return nullptr;
94
}
95
 
187 cycrow 96
inline System::String^ _US(const Utils::WString& str)
39 cycrow 97
{
187 cycrow 98
	System::String^ Str = gcnew System::String(str.c_str());
39 cycrow 99
	return Str;
100
}
1 cycrow 101
 
102
inline System::Drawing::Bitmap ^LoadIcon(CBaseFile *p)
103
{
197 cycrow 104
	Utils::WString file = p->icon()->filePointer();
129 cycrow 105
	if (!file.empty())
1 cycrow 106
	{
197 cycrow 107
		file = file.findReplace(L"/", L"\\").findReplace(L"\\\\", L"\\");
1 cycrow 108
		bool doIcon = false;
129 cycrow 109
		System::String ^sFile = _US(file);
1 cycrow 110
		if ( System::IO::File::Exists(sFile) )
111
		{
112
			Bitmap ^myBitmap = gcnew Bitmap(sFile);
113
			return myBitmap;
114
		}
115
	}
116
 
117
	return nullptr;
118
}
119
 
120
inline System::String ^GetProgramVersionString(float version, int beta)
121
{
197 cycrow 122
	System::String ^str = "V" + _US(Utils::WString::FromFloat(version, 2));
1 cycrow 123
	// beta version
124
	if ( beta > 0 )
125
		str += " (Beta " + beta + ")";
126
	// RC release
127
	else if ( beta < 0 )
128
		str += " (RC " + (0 - beta) + ")";
129
	return str;
130
}
131