forked from worthingtonse/CloudCoinCore4
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathImporter.cs
More file actions
360 lines (311 loc) · 14.5 KB
/
Importer.cs
File metadata and controls
360 lines (311 loc) · 14.5 KB
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace Foundation
{
class Importer
{
/* INSTANCE VARAIBLES */
FileUtils fileUtils;
/* CONSTRUCTOR */
public Importer(FileUtils fileUtils)
{
this.fileUtils = fileUtils;
}//Constructor
/* PUBLIC METHODS */
public bool importAll()
{
var ext = new List<string> { ".jpg", ".stack", ".jpeg" };
var fnamesRaw = Directory.GetFiles(this.fileUtils.importFolder, "*.*", SearchOption.TopDirectoryOnly).Where(s => ext.Contains(Path.GetExtension(s)));
string[] fnames = new string[fnamesRaw.Count()];
for (int i = 0; i < fnamesRaw.Count(); i++){
fnames[i] = Path.GetFileName( fnamesRaw.ElementAt(i) );
};
//String[] fnames = new DirectoryInfo(this.fileUtils.importFolder).GetFiles().Select(o => o.Name).ToArray();//Get a list of all in the folder except the directory "imported"
if (fnames.Length == 0)// Console.Out.WriteLine("There were no CloudCoins to import. Please place our CloudCoin .jpg and .stack files in your imports" + " folder at " + this.fileUtils.importFolder );
{
return false;
}
else
{
// Console.ForegroundColor = ConsoleColor.Green;
// Console.Out.WriteLine("Importing the following files: ");
// Console.ForegroundColor = ConsoleColor.White;
for (int i = 0; i < fnames.Length; i++)// Loop through each file.
{
Console.Out.WriteLine(fnames[i]);
this.importOneFile(fnames[i]);
} // end for each file name
return true;
}//end if no files
}// End Import All
/* PRIVATE METHODS */
/* IMPORT ONE FILE. COULD BE A JPEG, STACK or CHEST */
private bool importOneFile(String fname)
{
String extension = "";
int indx = fname.LastIndexOf('.');//Get file extension
if (indx > 0)
{
extension = fname.Substring(indx + 1);
}
extension = extension.ToLower();
if (extension == "jpeg" || extension == "jpg")//Run if file is a jpeg
{
if (!this.importJPEG(fname))
{
if (!File.Exists(this.fileUtils.trashFolder + fname))
{
File.Move(this.fileUtils.importFolder + fname, this.fileUtils.trashFolder + fname);
Console.Out.WriteLine( StringHolder.importer_1 + fname );// "File moved to trash: " + fname);
}
else
{
File.Delete(this.fileUtils.importedFolder + fname);
File.Move(this.fileUtils.importFolder + fname, this.fileUtils.importedFolder + fname);
}
return false;//"Failed to load JPEG file");
}//end if import fails
}//end if jpeg
/*
else if (extension == "chest" || extension == ".chest")//Run if file is a jpeg
{
if (!this.importChest(fname))
{
if (!File.Exists(this.fileUtils.trashFolder + fname))
{
File.Move(this.fileUtils.importFolder + fname, this.fileUtils.trashFolder + fname);
Console.Out.WriteLine("File moved to trash: " + fname);
}
return false;//"Failed to load JPEG file");
}//end if import fails
}//end if jpeg
*/
else if (!this.importStack(fname))// run if file is a stack
{
if (! File.Exists(this.fileUtils.trashFolder + fname)) {
File.Move(this.fileUtils.importFolder + fname, this.fileUtils.trashFolder + fname);
Console.Out.WriteLine( StringHolder.importer_1);// "File moved to trash: " + fname);
}
return false;//"Failed to load .stack file");
}
if (!File.Exists(this.fileUtils.importedFolder + fname))
{
File.Move(this.fileUtils.importFolder + fname, this.fileUtils.importedFolder + fname);
}
else {
File.Delete(this.fileUtils.importedFolder + fname);
File.Move(this.fileUtils.importFolder + fname, this.fileUtils.importedFolder + fname);
}
//End if the file is there
return true;
}//End importOneFile
/* IMPORT ONE JPEG */
private bool importJPEG(String fileName)//Move one jpeg to suspect folder.
{
bool isSuccessful = false;
// Console.Out.WriteLine("Trying to load: " + this.fileUtils.importFolder + fileName );
try
{
// Console.Out.WriteLine("Loading coin: " + fileUtils.importFolder + fileName);
//CloudCoin tempCoin = this.fileUtils.loadOneCloudCoinFromJPEGFile( fileUtils.importFolder + fileName );
/*Begin import from jpeg*/
/* GET the first 455 bytes of he jpeg where the coin is located */
String wholeString = "";
byte[] jpegHeader = new byte[455];
// Console.Out.WriteLine("Load file path " + fileUtils.importFolder + fileName);
FileStream fileStream = new FileStream( fileUtils.importFolder + fileName, FileMode.Open, FileAccess.Read);
try
{
int count; // actual number of bytes read
int sum = 0; // total number of bytes read
// read until Read method returns 0 (end of the stream has been reached)
while ((count = fileStream.Read(jpegHeader, sum, 455 - sum)) > 0)
sum += count; // sum is a buffer offset for next reading
}
finally
{
fileStream.Close();
}
wholeString = bytesToHexString(jpegHeader);
CloudCoin tempCoin = this.parseJpeg(wholeString);
// Console.Out.WriteLine("From FileUtils returnCC.fileName " + tempCoin.fileName);
/*end import from jpeg file */
// Console.Out.WriteLine("Loaded coin filename: " + tempCoin.fileName);
this.fileUtils.writeTo(this.fileUtils.suspectFolder, tempCoin);
return true;
}
catch (FileNotFoundException ex)
{
Console.Out.WriteLine("File not found: " + fileName + ex);
}
catch (IOException ioex)
{
Console.Out.WriteLine("IO Exception:" + fileName + ioex);
}// end try catch
return isSuccessful;
}
/* IMPORT ONE STACK FILE */
private bool importStack(String fileName)
{
bool isSuccessful = false;
// System.out.println("Trying to load: " + importFolder + fileName );
try
{
String incomeJson = fileUtils.importJSON( this.fileUtils.importFolder + fileName );//Load file as JSON .stack or .chest
CloudCoin[] tempCoin = null;
if (!seemsValidJSON(incomeJson))
{
tempCoin = this.fileUtils.loadManyCloudCoinFromJsonFile(this.fileUtils.importFolder + fileName, incomeJson);
}
if (tempCoin != null)
{
for (int i = 0; i < tempCoin.Length; i++)
{
this.fileUtils.writeTo(this.fileUtils.suspectFolder, tempCoin[i]);
}//end for each temp Coin
return true;
}//end if lenth not null.
else {
Console.ForegroundColor = ConsoleColor.Red;
Console.Out.WriteLine(StringHolder.importer_importstack1 );// "The following file does not appear to be valid JSON. It will be moved to the Trash Folder: ");
Console.Out.WriteLine(fileName);
Console.Out.WriteLine( StringHolder.importer_importstack2);// "Paste the text into http://jsonlint.com/ to check for validity.");
Console.ForegroundColor = ConsoleColor.White;
return false;//CloudCoin was null so move to trash
}
}
catch (FileNotFoundException ex)
{
Console.Out.WriteLine("File not found: " + fileName + ex);
}
catch (IOException ioex)
{
Console.Out.WriteLine("IO Exception:" + fileName + ioex);
}
// end try catch
return isSuccessful;
}//import stack
public bool seemsValidJSON(string json)
{
/*This does some simple tests to see if the JSON is valid. It is not precise.*/
if (json.Count(f => f == '{') != json.Count(f => f == '}'))
{
Console.ForegroundColor = ConsoleColor.Red;
Console.Out.WriteLine( StringHolder.importer_seemsValidJSON_1 );// "The stack file did not have a matching number of { }. There were " + json.Count(f => f == '{') + " {, and " + json.Count(f => f == '}') + " }");
Console.ForegroundColor = ConsoleColor.White;
return false;
}//Check if number of currly brackets open are the same as closed
if (json.Count(f => f == '[') != json.Count(f => f == ']'))
{
Console.ForegroundColor = ConsoleColor.Red;
Console.Out.WriteLine( StringHolder.importer_seemsValidJSON_2);//"The stack file did not have a matching number of []. There were " + json.Count(f => f == '[') + " [, and " + json.Count(f => f == ']') + " ]");
Console.ForegroundColor = ConsoleColor.White;
return false;
}//Check if number of brackets open are the same as closed
if (IsOdd(json.Count(f => f == '\"')))
{
Console.ForegroundColor = ConsoleColor.Red;
Console.Out.WriteLine( StringHolder.importer_seemsValidJSON_3);//"The stack file did not have a matching number of double quotations");
Console.ForegroundColor = ConsoleColor.White;
return false;
}//Check if number of
if (IsNotFive(json.Count(f => f == ':') - 1))
{
Console.ForegroundColor = ConsoleColor.Red;
Console.Out.WriteLine( StringHolder.importer_seemsValidJSON_4);//"The stack file did not have a the right number of full colons :");
Console.ForegroundColor = ConsoleColor.White;
return false;
}//Check if number of
return true;
}//end seems valid
public static bool IsOdd(int value)
{
return value % 2 != 0;
}
public static bool IsNotFive(int value)
{
return value % 5 != 0;
}
/*
private bool importChest(String fileName)
{
bool isSuccessful = false;
// System.out.println("Trying to load: " + importFolder + fileName );
try
{
CloudCoin[] tempCoin = fileUtils.loadManyCloudCoinFromJsonFile(this.fileUtils.importFolder + fileName,);
for (int i = 0; i < tempCoin.Length; i++)
{
this.fileUtils.writeTo(this.fileUtils.bankFolder, tempCoin[i]);
}//end for each temp Coin
return true;
}
catch (FileNotFoundException ex)
{
Console.Out.WriteLine("File not found: " + fileName + ex);
}
catch (IOException ioex)
{
Console.Out.WriteLine("IO Exception:" + fileName + ioex);
}
// end try catch
return isSuccessful;
}//import stack
*/
public string bytesToHexString(byte[] data)
{
if (data == null)
{
throw new ArgumentNullException("data");
}
int length = data.Length;
char[] hex = new char[length * 2];
int num1 = 0;
for (int index = 0; index < length * 2; index += 2)
{
byte num2 = data[num1++];
hex[index] = GetHexValue(num2 / 0x10);
hex[index + 1] = GetHexValue(num2 % 0x10);
}
return new string(hex);
}//End NewConverted
private char GetHexValue(int i)
{
if (i < 10)
{
return (char)(i + 0x30);
}
return (char)((i - 10) + 0x41);
}//end GetHexValue
private CloudCoin parseJpeg(String wholeString)
{
CloudCoin cc = new CloudCoin();
int startAn = 40;
for (int i = 0; i < 25; i++)
{
cc.ans[i] = wholeString.Substring(startAn, 32);
// Console.Out.WriteLine(i +": " + cc.ans[i]);
startAn += 32;
}
// end for
cc.aoid = null;
// wholeString.substring( 840, 895 );
cc.hp = 25;
// Integer.parseInt(wholeString.substring( 896, 896 ), 16);
cc.ed = wholeString.Substring(898, 4);
cc.nn = Convert.ToInt32(wholeString.Substring(902, 2), 16);
cc.sn = Convert.ToInt32(wholeString.Substring(904, 6), 16);
for (int i = 0; i < 25; i++)
{
cc.pans[i] = cc.generatePan();
cc.setPastStatus("undetected", i);
}
cc.fileName = cc.getDenomination() + ".CloudCoin." + cc.nn + "." + cc.sn + ".";
//Console.Out.WriteLine("parseJpeg cc.fileName " + cc.fileName);
// end for each pan
return cc;
}// end parse Jpeg
}//end class
}//end namespace