6
6
using System . Reflection ;
7
7
using System . Runtime . InteropServices ;
8
8
using System . Text ;
9
+ using System . Xml . Linq ;
9
10
using SimpleJSON ;
10
11
11
12
namespace CLibDatabase
@@ -14,7 +15,7 @@ public class DllEntry
14
15
{
15
16
private static string loadedDatabase = "" ;
16
17
17
- private static readonly string databaseFolder =
18
+ private static string databaseFolder =
18
19
Path . Combine ( Environment . GetFolderPath ( Environment . SpecialFolder . ApplicationData ) , "CLibDatabase" ) ;
19
20
20
21
private static Dictionary < string , string > database = new Dictionary < string , string > ( ) ;
@@ -65,6 +66,14 @@ private static string GetVersion()
65
66
66
67
return "0.0.0.0" ;
67
68
}
69
+ [ DllExport ( "SetExportPath" ) ]
70
+ public static string SetExportPath ( string path )
71
+ {
72
+ databaseFolder = path ;
73
+ if ( ! Directory . Exists ( databaseFolder ) )
74
+ Directory . CreateDirectory ( databaseFolder ) ;
75
+ return "true" ;
76
+ }
68
77
69
78
[ DllExport ( "KeyExists" ) ]
70
79
public static string KeyExists ( string key )
@@ -97,10 +106,10 @@ public static string Load(string filename)
97
106
{
98
107
if ( loadedDatabase == filename )
99
108
return "true" ;
100
-
109
+
101
110
using ( FileStream fs = File . OpenRead ( Path . Combine ( databaseFolder , filename + ".clibdata" ) ) )
102
111
{
103
- GZipStream cmp = new GZipStream ( fs , CompressionLevel . Optimal ) ;
112
+ GZipStream cmp = new GZipStream ( fs , CompressionMode . Decompress ) ;
104
113
using ( BinaryReader reader = new BinaryReader ( cmp ) )
105
114
{
106
115
int count = reader . ReadInt32 ( ) ;
@@ -121,11 +130,10 @@ public static string Load(string filename)
121
130
[ DllExport ( "Save" ) ]
122
131
public static string Save ( string filename )
123
132
{
124
- using ( FileStream fs = File . OpenWrite ( Path . Combine (
125
- Environment . GetFolderPath ( Environment . SpecialFolder . ApplicationData ) , "CLibDatabase" ,
126
- filename + ".clibdata" ) ) )
133
+ string path = Path . Combine ( databaseFolder , filename + ".clibdata" ) ;
134
+ using ( FileStream fs = File . OpenWrite ( path ) )
127
135
{
128
- GZipStream dcmp = new GZipStream ( fs , CompressionMode . Decompress ) ;
136
+ GZipStream dcmp = new GZipStream ( fs , CompressionLevel . Optimal ) ;
129
137
130
138
using ( BinaryWriter writer = new BinaryWriter ( dcmp ) )
131
139
{
@@ -136,14 +144,13 @@ public static string Save(string filename)
136
144
writer . Write ( pair . Value ) ;
137
145
}
138
146
}
139
-
140
- return "true" ;
147
+ return $ "File Exported to { path } ";
141
148
}
142
149
}
143
150
144
151
private static JSONNode ConvertToJson ( )
145
152
{
146
- JSONNode json = JSON . Parse ( "{}" ) ;
153
+ JSONNode json = new JSONObject ( ) ;
147
154
foreach ( KeyValuePair < string , string > item in database ) json . Add ( item . Key , item . Value ) ;
148
155
return json ;
149
156
}
@@ -154,6 +161,23 @@ private static void ConvertToDictionary(JSONNode json)
154
161
foreach ( KeyValuePair < string , JSONNode > item in json . Linq ) database . Add ( item . Key , item . Value . Value ) ;
155
162
}
156
163
164
+ private static void ConvertToDictionary ( XContainer xml )
165
+ {
166
+ database . Clear ( ) ;
167
+ foreach ( XElement item in xml . Elements ( ) )
168
+ {
169
+ database . Add ( item . Name . LocalName , item . Value ) ;
170
+ }
171
+ }
172
+ private static XDocument ConvertToXML ( )
173
+ {
174
+ XDocument xml = new XDocument ( ) ;
175
+ foreach ( KeyValuePair < string , string > item in database )
176
+ {
177
+ xml . Add ( item . Key , new JSONString ( item . Value ) ) ;
178
+ }
179
+ return xml ;
180
+ }
157
181
#region Import/Export
158
182
159
183
[ DllExport ( "ExportJson" ) ]
@@ -163,26 +187,30 @@ public static string ExportJson(string filename)
163
187
StringBuilder exportStringBuilder = new StringBuilder ( ) ;
164
188
json . WriteToStringBuilder ( exportStringBuilder , 0 , 4 , JSONTextMode . Indent ) ;
165
189
File . WriteAllText (
166
- Path . Combine ( Environment . GetFolderPath ( Environment . SpecialFolder . ApplicationData ) , "CLibDatabase" ,
167
- filename + ".json" ) , exportStringBuilder . ToString ( ) ) ;
190
+ Path . Combine ( databaseFolder , filename + ".json" ) , exportStringBuilder . ToString ( ) ) ;
168
191
return "true" ;
169
192
}
170
193
171
194
[ DllExport ( "ExportJsonBinary" ) ]
172
195
public static string ExportJsonBinary ( string filename )
173
196
{
174
197
JSONNode json = ConvertToJson ( ) ;
175
- json . SaveToCompressedFile ( Path . Combine ( Environment . GetFolderPath ( Environment . SpecialFolder . ApplicationData ) ,
176
- "CLibDatabase" , filename + ".bson" ) ) ;
198
+ json . SaveToCompressedFile ( Path . Combine ( databaseFolder , filename + ".bson" ) ) ;
199
+ return "true" ;
200
+ }
201
+
202
+ [ DllExport ( "ExportXml" ) ]
203
+ public static string ExportXml ( string filePath )
204
+ {
205
+ XDocument xml = ConvertToXML ( ) ;
206
+ xml . Save ( filePath , SaveOptions . OmitDuplicateNamespaces ) ;
177
207
return "true" ;
178
208
}
179
209
180
210
[ DllExport ( "ImportJson" ) ]
181
211
public static string ImportJson ( string filename )
182
212
{
183
- string jsonStr = File . ReadAllText ( Path . Combine (
184
- Environment . GetFolderPath ( Environment . SpecialFolder . ApplicationData ) , "CLibDatabase" ,
185
- filename + ".json" ) ) ;
213
+ string jsonStr = File . ReadAllText ( Path . Combine ( databaseFolder , filename + ".json" ) ) ;
186
214
JSONNode json = JSON . Parse ( jsonStr ) ;
187
215
ConvertToDictionary ( json ) ;
188
216
return "true" ;
@@ -191,18 +219,23 @@ public static string ImportJson(string filename)
191
219
[ DllExport ( "ImportJsonBinary" ) ]
192
220
public static string ImportJsonBinary ( string filename )
193
221
{
194
- JSONNode json = JSONNode . LoadFromCompressedFile ( Path . Combine (
195
- Environment . GetFolderPath ( Environment . SpecialFolder . ApplicationData ) , "CLibDatabase" ,
196
- filename + ".bson" ) ) ;
222
+ JSONNode json = JSONNode . LoadFromCompressedFile ( Path . Combine ( databaseFolder , filename + ".bson" ) ) ;
197
223
ConvertToDictionary ( json ) ;
198
224
return "true" ;
199
225
}
200
226
227
+ public static string ImportXml ( string filePath )
228
+ {
229
+ XDocument xml = XDocument . Load ( filePath ) ;
230
+ ConvertToDictionary ( xml ) ;
231
+ return "true" ;
232
+ }
233
+
201
234
#endregion Import/Export
202
235
203
236
~ DllEntry ( )
204
237
{
205
238
Save ( loadedDatabase ) ;
206
239
}
207
240
}
208
- }
241
+ }
0 commit comments