-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathHelper.cs
69 lines (56 loc) · 2.02 KB
/
Helper.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
using System;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using QtCore;
namespace QtSharp.Tests
{
public static class Helper
{
public static bool Contains(this string source, string toCheck, StringComparison comp)
{
return source.IndexOf(toCheck, comp) >= 0;
}
public static string RandomString(int size)
{
var random = new Random((int)DateTime.Now.Ticks);
var builder = new StringBuilder();
char ch;
for (var i = 0; i < size; i++)
{
ch = (char)random.Next('0', 'z');
builder.Append(ch);
}
return builder.ToString();
}
public static string[] GenerateQtArgv(string[] args)
{
var argv = new string[args.Length + 1];
var a = Assembly.GetEntryAssembly() ?? Assembly.GetExecutingAssembly();
var attrs = a.GetCustomAttributes(typeof(AssemblyTitleAttribute), false);
if (attrs.Length > 0)
{
argv[0] = ((AssemblyTitleAttribute)attrs[0]).Title;
}
else
{
var info = new QFileInfo(a.Location);
argv[0] = info.BaseName;
}
args.CopyTo(argv, 1);
return argv;
}
public unsafe static QCoreApplication CreateQCoreApplicationInstance(string[] args)
{
var argv = GenerateQtArgv(args);
var argc = argv.Count();
var arguments = argv.Aggregate("", (current, arg) => current + (arg + "\0"));
var stringPointer = (char*)Marshal.StringToHGlobalAuto(arguments).ToPointer();
var app = new QCoreApplication(ref argc, null);
//var app = new QCoreApplication(&argc, &stringPointer);
Marshal.FreeHGlobal(new IntPtr(stringPointer));
return app;
}
}
}