-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExtensions.cs
139 lines (122 loc) · 4.53 KB
/
Extensions.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
using System;
using System.Collections.Generic;
using System.Data.SQLite;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
namespace DerpScrapper
{
public static class Extensions
{
public static int ToUnixTimestamp(this DateTime dt)
{
return (int)(dt - new DateTime(1970, 1, 1).ToLocalTime()).TotalSeconds;
}
public static DateTime ToDateTime(this int timestamp)
{
System.DateTime dtDateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0);
dtDateTime = dtDateTime.AddSeconds(timestamp).ToLocalTime();
return dtDateTime;
}
public static SQLiteCommand CreateCommand(this SQLiteConnection connection, string query)
{
var com = connection.CreateCommand();
com.CommandText = query;
return com;
}
public static bool ContainsBoth(this string subject, Tuple<char, char> charCombo)
{
if (subject.Contains(charCombo.Item1.ToString()) && subject.Contains(charCombo.Item2.ToString()))
{
int idx1 = subject.IndexOf(charCombo.Item1);
int idx2 = subject.IndexOf(charCombo.Item2);
if(idx1 < idx2)
return true;
}
return false;
}
public static bool ContainsAll(this IEnumerable<string> subject, IEnumerable<string> parts)
{
foreach (string part in parts)
{
if (!subject.Contains(part))
return false;
}
return true;
}
public static bool ContainsAny(this string subject, IEnumerable<string> checkAgainst)
{
foreach (string part in checkAgainst)
{
if (subject.Contains(part))
return true;
}
return false;
}
public static bool Matches(this string subject, System.Text.RegularExpressions.Regex reg)
{
return reg.IsMatch(subject);
}
public static HtmlAgilityPack.HtmlNode GetNodeWithTypeAndClass(this HtmlAgilityPack.HtmlDocument doc, string type, string classname) {
return doc.DocumentNode.Descendants(type).Where(p => p.HasAttributes && p.Attributes["class"] != null && p.Attributes["class"].Value != null && p.Attributes["class"].Value == classname).FirstOrDefault();
}
public static string Implode(this IEnumerable<object> list, string glue)
{
string s = "";
foreach (var ob in list)
{
s += ob.ToString() + glue;
}
return s.TrimEnd(glue.ToArray());
}
/* Language picker requirements */
public static Dictionary<string, string> Languages()
{
Dictionary<string, string> languages = new Dictionary<string, string>();
languages.Add("nl", "Dutch");
languages.Add("en", "English");
languages.Add("sp", "Spanish");
languages.Add("jp", "Japanese");
languages.Add("de", "German");
return languages;
}
public class ComboboxItem
{
public string Text { get; set; }
public object Value { get; set; }
public override string ToString()
{
return Text;
}
}
/* End of requirements */
/// <summary>
/// Centers the control both horizontially and vertically
/// according to the parent control that contains it.
/// </summary>
/// <param name="control"></param>
public static void Center(this Control control)
{
control.CenterHorizontally();
control.CenterVertically();
}
/// <summary>
/// Centers the control horizontially according
/// to the parent control that contains it.
/// </summary>
public static void CenterHorizontally(this Control control)
{
Rectangle parentRect = control.Parent.ClientRectangle;
control.Left = (parentRect.Width - control.Width) / 2;
}
/// <summary>
/// Centers the control vertically according
/// to the parent control that contains it.
/// </summary>
public static void CenterVertically(this Control control)
{
Rectangle parentRect = control.Parent.ClientRectangle;
control.Top = (parentRect.Height - control.Height) / 2;
}
}
}