-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathCSnippetter.cs
155 lines (119 loc) · 3.74 KB
/
CSnippetter.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*
This file is part of TorqueDev.
TorqueDev is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by the
Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
TorqueDev is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with TorqueDev. If not, see <http://www.gnu.org/licenses>
EXCEPTIONS TO THE GPL: TorqueDev links in a number of third party libraries,
which are exempt from the license. If you want to write closed-source
third party modules that you are going to link into TorqueDev, you may do so
without restriction. I acknowledge that this technically allows for
one to bypass the open source provisions of the GPL, but the real goal
is to keep the core of TorqueDev free and open. The rest is up to you.
*/
using System;
using System.Collections;
using System.IO;
using System.Runtime.Serialization;
using System.Xml.Serialization;
namespace TSDev
{
[Serializable]
public class CSnippetter
{
public CSnippetter() {
// Blank constructor
}
public static string ParseKeywords(ArrayList kw) {
string final = "";
foreach(string keyword in kw) {
final += keyword + " ";
}
return final.Trim();
}
public static void Save(CSnippetter snippet, string filename) {
XmlSerializer xml = new XmlSerializer(typeof(CSnippetter));
FileStream file = new FileStream(filename, FileMode.Create, FileAccess.Write, FileShare.None);
xml.Serialize(file, snippet);
file.Close();
}
public static CSnippetter Load(string filename) {
if (!File.Exists(filename))
return new CSnippetter();
XmlSerializer xml = new XmlSerializer(typeof(CSnippetter));
FileStream file = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.None);
CSnippetter newsnip = null;
try {
newsnip = (CSnippetter)xml.Deserialize(file);
} catch {
newsnip = new CSnippetter();
}
file.Close();
return newsnip;
}
[Serializable]
public class CodeEntry {
public CodeEntry(string title, string descr, string contents, string category) {
this.CodeContents = contents;
this.CodeDescr = descr;
this.CodeTitle = title;
if (category == "")
category = "Code";
this.CodeCategory = category;
}
public CodeEntry() {}
public string CodeContents = "";
public string CodeTitle = "";
public string CodeDescr = "";
public string CodeCategory = "";
public ArrayList CodeKeywords = new ArrayList();
public string CodeURL = "";
public long CodeExpires = 0;
}
[Serializable]
public class CodeEntryCollection : CollectionBase {
public CodeEntryCollection () {}
public virtual void Add(CodeEntry entry) {
this.List.Add(entry);
}
public void Add(object entry) {
this.List.Add(entry);
}
public virtual void Remove(CodeEntry entry) {
this.List.Remove(entry);
}
public CodeEntryCollection Search(string[] keywords) {
CodeEntryCollection ret = new CodeEntryCollection();
foreach(CodeEntry entry in this.List) {
foreach(string keyword in keywords) {
if (entry.CodeKeywords.Contains(keyword.ToLower())) {
ret.Add(entry);
goto breakloop;
}
}
breakloop:
// Do nothing
if (true) {}
}
return ret;
}
public int Count {
get {
return this.List.Count;
}
}
public CodeEntry this[int Index] {
get {
return (CodeEntry)this.List[Index];
}
}
}
public CodeEntryCollection CodeSnippets = new CodeEntryCollection();
}
}