Subversion Repositories spk

Rev

Rev 191 | 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 CyString CyStringFromSystemString( System::String ^str)
30
{
31
	if ( !str || !str->Length )
32
		return NullString;
33
	const char* chars = (const char*)(Marshal::StringToHGlobalAnsi(str)).ToPointer();
34
	std::string s = chars;
35
//	CyString *newStr = new CyString(chars);
36
	Marshal::FreeHGlobal(IntPtr((void*)chars));
37
//	return *newStr;
38
	return CyString(s);
39
}
40
 
41
inline int CheckWebFileExists(String ^url)
42
{
43
	int ret = CONNECTERROR_UNKNOWN;
44
	try 
45
	{
46
		// check for the file
47
		Net::WebRequest ^check = (Net::HttpWebRequest ^)Net::WebRequest::Create(url);
48
		check->Credentials = Net::CredentialCache::DefaultCredentials;
49
		Net::WebResponse ^response = (Net::HttpWebResponse ^)check->GetResponse();
50
		// check the file size
51
		__int64 fileSize = response->ContentLength;
52
		if ( fileSize )
53
			ret = CONNECTERROR_NONE;
54
 
55
		response->Close();
56
 
57
	}
58
	catch (System::Net::WebException ^ex)
59
	{
60
		if ( ex->Status == System::Net::WebExceptionStatus::ConnectFailure )
61
			ret = CONNECTERROR_FAILED;
121 cycrow 62
		else if(ex->Status == System::Net::WebExceptionStatus::ConnectionClosed && !ex->Response)
63
			ret = CONNECTERROR_FAILED;
1 cycrow 64
		else
65
		{
66
			switch ( cli::safe_cast<Net::HttpWebResponse ^>(ex->Response)->StatusCode )
67
			{
68
				case Net::HttpStatusCode::NotFound:
69
					ret = CONNECTERROR_NOFILE;
70
					break;
71
				case Net::HttpStatusCode::RequestTimeout:
72
					ret = CONNECTERROR_TIMEOUT;
73
					break;
74
				default:
75
					ret = CONNECTERROR_UNKNOWN;
76
			}
77
		}
78
	}
79
 
80
	return ret;
81
}
82
 
83
inline String ^ReadDataFromWeb(String ^url)
84
{
85
	bool ret = false;
86
	String ^data;
87
	try 
88
	{
89
		System::Net::WebClient ^Client = gcnew System::Net::WebClient();
90
		Client->Headers->Add( "user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)" );
91
		System::IO::Stream ^strm = Client->OpenRead(url);
92
		System::IO::StreamReader ^sr = gcnew System::IO::StreamReader(strm);
93
		data = sr->ReadToEnd();
94
 
95
		strm->Close();
96
		sr->Close();
97
	}
98
	catch (System::Net::WebException ^)
99
	{
100
		return nullptr;
101
	}
102
 
103
	if ( data && data->Length )
104
		return data;
105
	return nullptr;
106
}
107
 
108
inline System::String ^SystemStringFromCyString ( CyString str )
109
{
110
	System::String ^Str = gcnew System::String(str.c_str());
111
	return Str;
112
}
187 cycrow 113
 
114
inline System::String^ _US(const Utils::WString& str)
39 cycrow 115
{
187 cycrow 116
	System::String^ Str = gcnew System::String(str.c_str());
39 cycrow 117
	return Str;
118
}
1 cycrow 119
 
120
inline System::Drawing::Bitmap ^LoadIcon(CBaseFile *p)
121
{
197 cycrow 122
	Utils::WString file = p->icon()->filePointer();
129 cycrow 123
	if (!file.empty())
1 cycrow 124
	{
197 cycrow 125
		file = file.findReplace(L"/", L"\\").findReplace(L"\\\\", L"\\");
1 cycrow 126
		bool doIcon = false;
129 cycrow 127
		System::String ^sFile = _US(file);
1 cycrow 128
		if ( System::IO::File::Exists(sFile) )
129
		{
130
			Bitmap ^myBitmap = gcnew Bitmap(sFile);
131
			return myBitmap;
132
		}
133
	}
134
 
135
	return nullptr;
136
}
137
 
138
inline System::String ^GetProgramVersionString(float version, int beta)
139
{
197 cycrow 140
	System::String ^str = "V" + _US(Utils::WString::FromFloat(version, 2));
1 cycrow 141
	// beta version
142
	if ( beta > 0 )
143
		str += " (Beta " + beta + ")";
144
	// RC release
145
	else if ( beta < 0 )
146
		str += " (RC " + (0 - beta) + ")";
147
	return str;
148
}
149